6

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

提前致谢 :)

4

3 回答 3

6

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

$(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 函数中输入一个数字。然后使用图形填充将其放置在您想要的位置。

希望这对将来的某人有所帮助:)

于 2011-06-14T10:55:12.417 回答
2

浮动 API

各种东西都塞在一个轴对象中,例如,您可以使用 getAxes().xaxis.ticks 找出 xaxis 的刻度。另外两个有用的属性是 p2c 和 c2p,用于从数据点空间转换到画布绘图空间并返回的函数。两者都返回与绘图偏移量偏移的值。

结合plot.offset()(网格区域相对于文档的偏移量),您应该拥有计算其余部分所需的所有工具。plot.pointOffset()也很有用。它返回一个点相对于包含 div 的位置。

于 2010-08-02T20:09:42.500 回答
0

理论上获取容器内的 x,y 坐标如下:

$(function () {
        $('#container').mousemove(function (e) {
            $('.xPos').text(e.clientX - $('#container').offset().left);
            $('.yPos').text(e.clientY - $('#container').offset().top);
        });
    });
于 2010-06-03T10:25:02.447 回答