views:

95

answers:

1

I got the Flot-created graph. What I wanted to acomplish is to get some kind of information when user moves the mouse over it - best would be to show the data (from x and y axis) in some kind of javascript popup.

It's probably trivial question, but I can't figure it out...

Right now my javascript looks like this:

<script  id="source" language="javascript" type="text/javascript">
$(function () {
    var data = [[1251756000000, 122.68],[1251842400000, 122.68],[1251928800000, 125.13],[1252015200000, 112.62],[1252101600000, 122.76]]
    $.plot($("#graph_placeholder"), [ data ], { 
        xaxis: { mode: "time", minTickSize: [1, "day"], timeformat : "%y/%m/%d", },
        lines: { show: true },
        points: { show: false },
    } );
});
</script>

So best would be to get the x: 1251756000000 y: 122.68 when hovering the point (x: 1251756000000, y: any). Or even have the x value formatted as defined in the timeformat (2009/11/14).

+2  A: 

This example shows how to enable a tooltip (if you click the Enable Tooltip checkbox). Here's a starting point using your example code:

<script  id="source" language="javascript" type="text/javascript">
$(function () {
var data = [[1251756000000, 122.68],[1251842400000, 122.68],[1251928800000, 125.13],[1252015200000, 112.62],[1252101600000, 122.76]]
$.plot($("#graph_placeholder"), [ data ], {
    xaxis: { mode: "time", minTickSize: [1, "day"], timeformat : "%y/%m/%d", },
    lines: { show: true },
    points: { show: true },
    grid: { hoverable: true },
} );
});

var previousPoint = null;
$("#graph_placeholder").bind("plothover", function (event, pos, item) {
if (item) {
 if (previousPoint != item.datapoint) {
  previousPoint = item.datapoint;
  $("#tooltip").remove();
  showTooltip(item.pageX, item.pageY, '(' + item.datapoint[0] + ', ' + item.datapoint[1]+')');
 }
} else {
 $("#tooltip").remove();
 previousPoint = null;
}
});

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);
}
</script>
Derek Kurth