我有以下代码使用 Flot 显示图表。我想让鼠标悬停以显示带有天数/配额值的工具提示
<?php
include("connect.php");
$FundName=$_POST["FundName"];
$mes=$_POST["mes"];
$cnpj=$_POST["cnpj"];
?>
<?php
$query = "SELECT Dia, Quota FROM CDVM WHERE Competence='$mes' AND FundName='$FundName' AND 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>
我还从文件头部的 flot 网站调用 jquery。我只是在将整个代码复制到这里时遇到问题。再次感谢您的帮助!