javascript - jQuery/弗洛特 : How do you get the coordinates of a datapoint?

标签 javascript jquery flot

我目前正在查看 http://people.iola.dk/olau/flot/examples/interacting.html 中的示例但我不知道如何获取数据点的坐标。我不会点击 Plot,所以我无法使用事件 plotclick。现在我的问题是:是否有另一种无需单击即可获取数据点的 x 和 y 坐标的方法?我将使用 jQuery slider 突出显示图表上的不同点,并希望在数据点旁边有一个工具提示。

提前致谢:)

最佳答案

这有点晚了,但我在绘制图表后运行了这个函数,作为一种将标签放在折线图中绘制的数据点下方的方法。

$(document).ready(function(){
$(function(){
      var data =  [
            {data: [[1, 5], [2, 10], [3, 15], [4, 15], [5, 10], [6, 5]], color: '#3a8df6'},
            {data: [[1, 52], [2, 42], [3, 32], [4, 32], [5, 42], [6, 52]], color: '#ff0000'}
            ];
      var options = {
              lines: {show: true},
              yaxis: {tickDecimals: 0, min: 0, max: 100, autoscaleMargin: null}
             };
     var graph = $.plot($('#graph'), data, options);
     var points = graph.getData();
     var graphx = $('#graph').offset().left;
     graphx = graphx + 30; // replace with offset of canvas on graph
     var graphy = $('#graph').offset().top;
     graphy = graphy + 10; // how low you want the label to hang underneath the point
     for(var k = 0; k < points.length; k++){
          for(var m = 0; m < points[k].data.length; m++){
            showTooltip(graphx + points[k].xaxis.p2c(points[k].data[m][0]), graphy + points[k].yaxis.p2c(points[k].data[m][1]),points[k].data[m][1])
            }
      }
 });
 });
 function showTooltip(x,y,contents){
      $('<div id="tooltip">' +  contents + '</div>').css( {
            position: 'absolute',
            display: 'none',
            top: y,
            left: x
      }).appendTo("body").fadeIn(200);
 } 

这超出了我的想象,但基本上,这个函数遍历所有数据点并在轴中使用 p2c 函数,然后将其添加到图形本身的偏移量(带有一些填充)。然后它只使用正常的工具提示覆盖。

此外,如果在条形图上使用它,您可以设置工具提示的宽度,给它一个中心对齐的文本,然后如果您希望所有标签都排成一行,那么只需在 yaxis p2c 中放置一个数字功能。然后使用图形填充将其放置在您想要的位置。

希望这对以后的人有帮助:)

关于javascript - jQuery/弗洛特 : How do you get the coordinates of a datapoint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2964798/

相关文章:

jquery - 使用 chrome 开发人员工具 Xpath 找到正确的子 div id 以使用 Jquery 的点击功能定位

javascript - Jquery Flot "plothover"事件不工作

javascript - float 绘图悬停和变换

javascript - 我们可以在其他域上托管 manifest.Json 吗?

jquery - 内部 div 可垂直拖动,应允许父 div 可水平拖动

javascript - 我如何使用 sencha 选项卡上的按钮来放置覆盖面板

javascript - jQuery 触发器仅在第一次发生

javascript - 拖放重叠点

javascript - 将事件处理程序分配给元素集合

javascript - 通过 Javascript 检测图像的 mime 类型