gpt4 book ai didi

javascript - 如何在 Flot 图中显示带有数据点数据的弹出窗口?

转载 作者:行者123 更新时间:2023-11-29 17:33:06 25 4
gpt4 key购买 nike

我得到了 Flot 创建的图表。我想要完成的是当用户将鼠标移到它上面时获取某种信息 - 最好是在某种 javascript 弹出窗口中显示数据(从 x 和 y 轴)。

这可能是个微不足道的问题,但我想不通...

现在我的 javascript 看起来像这样:

<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>

所以最好是在悬停点时获得 x: 1251756000000 y: 122.68 (x: 1251756000000, y: any)。或者甚至按照 timeformat (2009/11/14) 中定义的格式设置 x 值。

最佳答案

This example显示如何启用工具提示(如果单击启用工具提示复选框)。这是使用您的示例代码的起点:

<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>

关于javascript - 如何在 Flot 图中显示带有数据点数据的弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1502633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com