admin管理员组文章数量:1424936
I have to draw multiple lines and would like to plot a tooltip on the data point when mouse hovering. However, some might share the same (x, y). This results in overlapping points. They are plotted on top of each other.
It seems the "plothover" event only returns the data on the very top which is drawn at the last. As a result, the tooltip is only for that point. I want to draw multiple tooltips when multiple data points are hovered.
Has anyone encountered this issue before?
Thanks!
I have to draw multiple lines and would like to plot a tooltip on the data point when mouse hovering. However, some might share the same (x, y). This results in overlapping points. They are plotted on top of each other.
It seems the "plothover" event only returns the data on the very top which is drawn at the last. As a result, the tooltip is only for that point. I want to draw multiple tooltips when multiple data points are hovered.
Has anyone encountered this issue before?
Thanks!
Share Improve this question asked Dec 17, 2013 at 0:56 alextcalextc 3,52916 gold badges71 silver badges119 bronze badges1 Answer
Reset to default 9Yes, looking at the source, flot
will only find the top most point on the plothover
. The simplest approach is just to do the work yourself. Here's a generic search for the plothover
bind:
if (item) {
var content = item.series.label;
var someData = plot.getData();
for (var i = 0; i < someData.length; i++){
if (someData[i].label == item.series.label){
continue; // skip item's series...
}
for (var j=0; j < someData[i].data.length; j++){
if (someData[i].data[j][0] == item.datapoint[0] &&
someData[i].data[j][1] == item.datapoint[1]){
content += ' ' + someData[i].label;
}
}
}
showTooltip(item.pageX, item.pageY, content);
}
Fiddle example here.
Full Code:
$(function () {
var plot = $.plot($("#placeholder"),[
{ data: [[0,0],[1,1],[2,2]], label: "Line One"},
{ data: [[0,2],[1,1],[2,0]], label: "Line Two"},
{ data: [[0,0],[0.5,1],[1,1],[2,3]], label: "Line Three"}
], {
series: {
lines: { show: true },
points: { show: true }
},
grid: { hoverable: true, clickable: true }
});
function showTooltip(x, y, contents) {
$('#tooltip').html(contents);
$('#tooltip').css({
top: y + 5,
left: x + 5,
display: 'block'});
}
$("#placeholder").bind("plothover", function (event, pos, item) {
if (item) {
var content = item.series.label;
var someData = plot.getData();
for (var i = 0; i < someData.length; i++){
if (someData[i].label == item.series.label){
continue;
}
for (var j=0; j < someData[i].data.length; j++){
if (someData[i].data[j][0] == item.datapoint[0] &&
someData[i].data[j][1] == item.datapoint[1]){
content += ' ' + someData[i].label;
}
}
}
showTooltip(item.pageX, item.pageY, content);
}
else
{
$("#tooltip").css('display','none');
}
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.flotcharts/javascript/jquery.flot.min.js"></script>
<br/><br/>
<div id="placeholder" style="width:400px;height:300px;"></div>
<div id="tooltip" style="position: absolute;
display: none;
border: 1px solid #fdd;
padding: 2px;
background-color: #fee;
opacity: 0.80"></div>
本文标签: jqueryOverlapping data lines with Flot Javascript plotting libraryStack Overflow
版权声明:本文标题:jquery - Overlapping data lines with Flot Javascript plotting library - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745413441a2657572.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论