php - Flot tooltip on hover -
i have following code showing graph using flot. want mouse hover show tooltip values of days/quota
<?php include("connect.php"); $fundname=$_post["fundname"]; $mes=$_post["mes"]; $cnpj=$_post["cnpj"]; ?> <?php $query = "select dia, quota cdvm competence='$mes' , fundname='$fundname' , quota > 0"; $result = mysql_query($query); ?> <?php $points = ""; while($row = mysql_fetch_assoc($result)) { $quota = str_replace(',', '.', $row['quota']); $points .= "[{$row['dia']}, {$quota}], "; } $points = rtrim($points, ", "); ?> <div id="placeholder" style="width:500px;height:200px"></div> <script type="text/javascript"> $(function() { $.plot("#placeholder", [[ <?php echo $points ?> ], { series: { lines: { show: true }, points: { show: true } }, grid: { hoverable: true, clickable: true } }); $("#placeholder").bind("plothover", function (event, pos, item) { if (item) { if (previouspoint != item.dataindex) { previouspoint = item.dataindex; $("#tooltip").remove(); var x = item.datapoint[0].tofixed(2), y = item.datapoint[1].tofixed(2); showtooltip(item.pagex, item.pagey, "dia=" + x + ", quota=" + y); } } 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>
i alo have call jquery flot website in head of file. have problems copying whole code on here. again help!
you need create element tooltip (a span or div), , bind plothover it. used example on website "interacting data points" on own website.
the below code uses showtooltip function create tooltip div, triggered call .bind("plothover"). enables hovering trigger tooltip , populate relevant data item.
here's code modified complete working version. make sure database string in there before "while($row = mysql_fetch_assoc($result))"
<html> <head> <script language="javascript" type="text/javascript" src="http://www.flotcharts.org/flot/jquery.js"></script> <script language="javascript" type="text/javascript" src="http://www.flotcharts.org/flot/jquery.flot.js"></script> </head> <body> <?php $points = ""; while($row = mysql_fetch_assoc($result)) { $quota = str_replace(',', '.', $row['quota']); $points .= "[{$row['dia']}, {$quota}], "; } $points = rtrim($points, ", "); ?> <div id="placeholder" style="width:500px;height:200px"></div> <script type="text/javascript"> $(function() { $.plot("#placeholder", [[ <?php echo $points ?> ]], { series: { lines: { show: true }, points: { show: true } }, grid: { hoverable: true, clickable: true } }); $("#placeholder").bind("plothover", function (event, pos, item) { if (item) { if (previouspoint != item.dataindex) { previouspoint = item.dataindex; $("#tooltip").remove(); var x = item.datapoint[0].tofixed(2), y = item.datapoint[1].tofixed(2); showtooltip(item.pagex, item.pagey, "dia=" + x + ", quota=" + y); } } 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> </body> </html>
Comments
Post a Comment