Customize tooltip in FLOT graph

空扰寡人 提交于 2019-12-08 09:31:33

问题


I am using FLOT to display graphs.

To display tootip i am using https://github.com/krzysu/flot.tooltip.

Now I want to customize content of tooltip so I am using callback to set content of tooltip. Code snippet:

tooltip: true,
  tooltipOpts: {
    content: function(label, xval, yval, flotItem){
      var xAxis = plot.getXAxes();
      return xval;
    },
    defaultTheme: false
} 

But its giving me error

caught TypeError: Object function (label, xval, yval, flotItem){
            var xAxis = plot.getXAxes();
            return xval;
           } has no method 'replace' 

Could any one help me ?

Thanks in advance.


回答1:


I'm not using the flot tooltip. There is easier way to show tooltip and customize it. Check out this fiddler:

http://jsfiddle.net/Margo/yKG7X/5/

$("#placeholder").bind("plothover", function (event, pos, item) { if (item) {
$("#tooltip").remove();

                var x = item.datapoint[0],
                    y = item.datapoint[1];
                showTooltip(item.pageX, item.pageY, x + " / " + y );
                }
});
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).fadeOut(6000);
}

Hope it helps :)




回答2:


The documentation states that:

If you require even more control over how the tooltip is generated you can pass a callback function(label, xval, yval, flotItem) that must return a string with the format described.

xval is the numeric x axis value where the tooltip is located. It is not a string. The replace method it is failing on is the standard string.replace:

 > "".replace
 function replace() { [native code] }


来源:https://stackoverflow.com/questions/22640112/customize-tooltip-in-flot-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!