欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Flot example

程序员文章站 2022-03-17 11:17:02
...

Flot is an open source javascript charting tool. I need to plot an interest rate curve in a web page, so the x-axis is date.

 

Here is an example modified from the file interacting.html in the flot example package

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Flot Examples</title>
    <link href="layout.css" rel="stylesheet" type="text/css"></link>
	<script language="javascript" type="text/javascript" src="flotjs/excanvas.min.js"></script>
    <script language="javascript" type="text/javascript" src="flotjs/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="flotjs/jquery.flot.js"></script>
 </head>
    <body>
    <h1>Flot Examples</h1>

    <div id="placeholder" style="width:600px;height:300px"></div>   

    <p id="hoverdata">Mouse hovers at
    (<span id="x">0</span>, <span id="y">0</span>). <span id="clickdata"></span></p>

<script id="source" language="javascript" type="text/javascript">
$(function () {
    var sin = [], cos = [];
    for (var i = 0; i < 50; i += 1) {
	    var date1 = new Date(); 
		date1 = date1.setDate(i+1);
        sin.push([date1, Math.sin(i)]);
    }

    var plot = $.plot($("#placeholder"),
           [ { data: sin, label: "sin(x)"} ], {
               series: {
                   lines: { show: true },
                   points: { show: true }
               },
               grid: { hoverable: true, clickable: true },
			   xaxis: { mode: 'time', timeformat: "%m/%d", ticks: 10},
               yaxis: { min: -1.2, max: 1.2 }
             });

    function showTooltip(x, y, contents) {
        $('<div id="tooltip">' + contents + '</div>').css( {
            position: 'absolute',
            display: 'none',
            top: y + 5,
            left: x + 5,
            border: '1px solid #fdd',
            padding: '2px',
            'background-color': '#fee',
            opacity: 0.80
        }).appendTo("body").fadeIn(200);
    }

    var previousPoint = null;
    $("#placeholder").bind("plothover", function (event, pos, item) {
        $("#x").text(pos.x.toFixed(2));
        $("#y").text(pos.y.toFixed(2));
        
		if (item) {
			if (previousPoint != item.datapoint) {
				previousPoint = item.datapoint;
				
				$("#tooltip").remove();
				var x = item.datapoint[0].toFixed(2),
					y = item.datapoint[1].toFixed(2);
				
				var ddd = new Date();
				ddd.setTime(x);
				var mmm = ddd.getMonth() + 1;
				var mdd = ddd.getDate() ;
				showTooltip(item.pageX, item.pageY,
							"(" + mmm + "/" + mdd + ", " + y + ")");
			}
		}
		else {
			$("#tooltip").remove();
			previousPoint = null;            
		}
        
    });

    $("#placeholder").bind("plotclick", function (event, pos, item) {
        if (item) {
            $("#clickdata").text("You clicked point " + item.dataIndex + " in " + item.series.label + ".");
            plot.highlight(item.series, item.datapoint);
        }
    });
});
</script>

 </body>
</html>

 

The result is like this:

 

Flot example
            
    
    博客分类: web JavaScriptjQueryCSSHTMLWeb 

The tooltip values are useful for users.

  • Flot example
            
    
    博客分类: web JavaScriptjQueryCSSHTMLWeb 
  • 大小: 48.8 KB