interacting.html 3.33 KB
Newer Older
1 2 3 4 5
<!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>
6
    <link href="layout.css" rel="stylesheet" type="text/css">
7
    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../excanvas.min.js"></script><![endif]-->
8 9 10 11 12 13 14 15
    <script language="javascript" type="text/javascript" src="../jquery.js"></script>
    <script language="javascript" type="text/javascript" src="../jquery.flot.js"></script>
 </head>
    <body>
    <h1>Flot Examples</h1>

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

16 17
    <p>One of the goals of Flot is to support user interactions. Try
    pointing and clicking on the points.</p>
18

19 20 21 22 23
    <p>
      <label><input id="enablePosition" type="checkbox">Show mouse position</label>
      <span id="hoverdata"></span>
      <span id="clickdata"></span>
    </p>
24

25 26 27
    <p>A tooltip is easy to build with a bit of jQuery code and the
    data returned from the plot.</p>

28
    <p><label><input id="enableTooltip" type="checkbox">Enable tooltip</label></p>
29

30
<script type="text/javascript">
31
$(function () {
32 33 34 35 36 37
    var sin = [], cos = [];
    for (var i = 0; i < 14; i += 0.5) {
        sin.push([i, Math.sin(i)]);
        cos.push([i, Math.cos(i)]);
    }

38
    var plot = $.plot($("#placeholder"),
39 40 41 42 43 44 45
           [ { data: sin, label: "sin(x)"}, { data: cos, label: "cos(x)" } ], {
               series: {
                   lines: { show: true },
                   points: { show: true }
               },
               grid: { hoverable: true, clickable: true },
               yaxis: { min: -1.2, max: 1.2 }
46
             });
47

48 49 50 51 52 53
    function showTooltip(x, y, contents) {
        $('<div id="tooltip">' + contents + '</div>').css( {
            position: 'absolute',
            display: 'none',
            top: y + 5,
            left: x + 5,
olau@iola.dk's avatar
olau@iola.dk committed
54
            border: '1px solid #fdd',
55 56 57 58 59 60 61
            padding: '2px',
            'background-color': '#fee',
            opacity: 0.80
        }).appendTo("body").fadeIn(200);
    }

    var previousPoint = null;
62
    $("#placeholder").bind("plothover", function (event, pos, item) {
63 64 65 66
        if ($("#enablePosition:checked").length > 0) {
            var str = "(" + pos.x.toFixed(2) + ", " + pos.y.toFixed(2) + ")";
            $("#hoverdata").text(str);
        }
67

68 69
        if ($("#enableTooltip:checked").length > 0) {
            if (item) {
70 71
                if (previousPoint != item.dataIndex) {
                    previousPoint = item.dataIndex;
72 73 74 75 76 77 78 79 80 81
                    
                    $("#tooltip").remove();
                    var x = item.datapoint[0].toFixed(2),
                        y = item.datapoint[1].toFixed(2);
                    
                    showTooltip(item.pageX, item.pageY,
                                item.series.label + " of " + x + " = " + y);
                }
            }
            else {
82
                $("#tooltip").remove();
83
                previousPoint = null;            
84 85
            }
        }
86
    });
87

88 89
    $("#placeholder").bind("plotclick", function (event, pos, item) {
        if (item) {
90
            $("#clickdata").text(" - click point " + item.dataIndex + " in " + item.series.label);
91
            plot.highlight(item.series, item.datapoint);
92
        }
93 94 95 96 97 98
    });
});
</script>

 </body>
</html>