interacting-axes.html 3 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 16 17 18 19 20 21 22
    <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>

    <p>With multiple axes, you sometimes need to interact with them. A
    simple way to do this is to draw the plot, deduce the axis
    placements and insert a couple of divs on top to catch events.
    Try clicking an axis.</p>

    <p id="click"></p>

23
<script type="text/javascript">
24 25 26 27 28 29 30 31 32 33 34
$(function () {
    function generate(start, end, fn) {
        var res = [];
        for (var i = 0; i <= 100; ++i) {
            var x = start + i / 100 * (end - start);
            res.push([x, fn(x)]);
        }
        return res;
    }

    var data = [
35 36 37 38
        { data: generate(0, 10, function (x) { return Math.sqrt(x);}), xaxis: 1, yaxis:1 },
        { data: generate(0, 10, function (x) { return Math.sin(x);}), xaxis: 1, yaxis:2 },
        { data: generate(0, 10, function (x) { return Math.cos(x);}), xaxis: 1, yaxis:3 },
        { data: generate(2, 10, function (x) { return Math.tan(x);}), xaxis: 2, yaxis: 4 }
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    ];

    var plot = $.plot($("#placeholder"),
                      data,
                      {
                          xaxes: [
                              { position: 'bottom' },
                              { position: 'top'}
                          ],
                          yaxes: [
                              { position: 'left' },
                              { position: 'left' },
                              { position: 'right' },
                              { position: 'left' }
                          ]
                      });

    // now for each axis, create a div
57 58 59 60
    $.each(plot.getAxes(), function (i, axis) {
        if (!axis.show)
            return;
        
61
        var box = axis.box;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        
        $('<div class="axisTarget" style="position:absolute;left:' + box.left + 'px;top:' + box.top + 'px;width:' + box.width +  'px;height:' + box.height + 'px"></div>')
            .data('axis.direction', axis.direction)
            .data('axis.n', axis.n)
            .css({ backgroundColor: "#f00", opacity: 0, cursor: "pointer" })
            .appendTo(plot.getPlaceholder())
            .hover(
                function () { $(this).css({ opacity: 0.10 }) },
                function () { $(this).css({ opacity: 0 }) }
            )
            .click(function () {
                $("#click").text("You clicked the " + axis.direction + axis.n + "axis!")
            });
    });
});
</script>
 </body>
</html>