2009-05-24 44 views
2

如果我将函数绑定到flot的“plotselected”事件,有没有办法获得选定区域的起始点和结束点的主要系列索引?查找在flot中选择的总数

我看到用“plothover”可以使用“item”变量,但不清楚它是否适用于选择。另外,每次调用函数时,我都不想遍历整个系列。我的目标是得到这样的:

$("#placeholder").bind("plotselected", function (itemx1, itemx2) { 
      var x1 = itemx1.plot.pos //The index for this plot point in series"; 
      var x2 = itemx2.plot.pos //The index for this plot point in series"; 
      var sum = 0; 
      for (var i = x1; i < x2; i++) { 
       sum += d[i][0]; 
       } 
      $("#total_selected").text(sum); 
      }); 

如果我能得到的是,我还可以输出(我的数据)是这样的:

  "You earned X points over Y days, Z hours, F minutes. Good Job!" 

看起来这应该是简单的,但海军报是真的扔我一个循环。

谢谢!

+0

您的意思是否这样写: VAR X2 = itemx2.plot.pos //该指数在这个系列情节点“; – dplante 2009-06-05 22:17:03

+0

是好眼力,我已经修复它,我想的总和!。我希望flot有一些基本的黎曼总和类型的动作,我可以在任何两点得到一个更准确的总和。不应该很难,因为我的线不曲线,但这就是我得到的停止在代数2. – Anthony 2009-06-05 23:24:48

回答

5

flot api documentation:“plotselected”事件函数需要两个参数“事件”和“范围”。范围对象包含选择的x和y坐标。

$('#placeholder').bind('plotselected', function (event, ranges) { 
    var x1 = ranges.xaxis.from; 
    var x2 = ranges.xaxis.to; 
    var y1 = ranges.yaxis.from; 
    var y2 = ranges.yaxis.to;  
    var sum = 0; 

    /* The values returned by the coordinates are floats. 
    You may need to tweak this to get the correct results.*/ 
    for (var i = x1; i < x2; i++) { 
     sum += d[i][0]; 
    } 
    $("#total_selected").text(sum); 
});