gpt4 book ai didi

javascript - float 条形图的工具提示

转载 作者:行者123 更新时间:2023-12-01 07:38:53 24 4
gpt4 key购买 nike

我正在使用 Flot API 来构建条形图。我在绘制条形图方面很成功。但是我没有得到这些图表的工具提示。我已经尝试了很多,但我没有做到这一点。
我的代码是:

    $(function () {
var a1 = [
[0, 100],
[1, 200],
[2,300],
[3,400],
[4,500]

];
var a2 = [
[0, 90],
[1, 150],
[2,250],
[3,380],
[4,450]
];

//var ticks=[[0,"Overall"],[1,"SEA"],[2,"INDIA"],[3,"NEA"],[4,"PZ"]];
var data = [
{
label: "Pre Transformation",
data: a1
},
{
label: "Post Transformation",
data: a2
}



];

$.plot($("#placeholder2"), data, {
series: {
bars: {
show: true,
barWidth: 0.13,
order: 1
}
},
xaxis: {
ticks: [[0,"Overall"],[1,"SEA"],[2,"INDIA"],[3,"NEA"],[4,"PZ"]]
//tickLength: 0
},
grid: {
      hoverable: true,
clickable:true
  
     //mouseActiveRadius: 30  //specifies how far the mouse can activate an item
},


valueLabels: {
show: true
}

});

});

var previousPoint = null, previousLabel = null;

function showTooltip(x, y, color, contents) {
$('<div id="tooltip">' + contents + '</div>').css({
position: 'absolute',
display: 'none',
top: y - 40,
left: x - 120,
border: '2px solid ' + color,
padding: '3px',
'font-size': '9px',
'border-radius': '5px',
'background-color': '#fff',
'font-family': 'Verdana, Arial, Helvetica, Tahoma, sans-serif',
opacity: 0.9
}).appendTo("body").fadeIn(200);
}

$.fn.UseTooltip = function () {
$(this).bind("plothover", function (event, pos, item) {
if (item) {
if ((previousLabel != item.series.label) || (previousPoint != item.dataIndex)) {
previousPoint = item.dataIndex;
previousLabel = item.series.label;
$("#tooltip").remove();

var x = item.datapoint[0];
var y = item.datapoint[1];

var color = item.series.color;

//console.log(item.series.xaxis.ticks[x].label);

showTooltip(item.pageX,
item.pageY,
color,
"<strong>" + item.series.label + "</strong><br>" + item.series.xaxis.ticks[x].label + " : <strong>" + y + "</strong> °C");
}
} else {
$("#tooltip").remove();
previousPoint = null;
}
});
};

请在这方面帮助我。

最佳答案

不确定你想达到什么目的

$.fn.UseTooltip = function () {
$(this).bind("plothover", function (event, pos, item) {

但是尝试用这个替换:
$("#placeholder2").on("plothover", function (event, pos, item) {
if (item) {
// and removing trailling } in the end

Demo

关于javascript - float 条形图的工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21309955/

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