找到在flot中选择的总和

Ant*_*ony 2 jquery user-interface graph flot

如果我将一个函数绑定到flot的"plotselected"事件,有没有办法获得所选区域的起点和终点的主系列索引?

我看到使用"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);
          });
Run Code Online (Sandbox Code Playgroud)

如果我能得到它,我也可以输出(我的数据)类似于:

         "You earned X points over Y days, Z hours, F minutes. Good Job!"
Run Code Online (Sandbox Code Playgroud)

看起来这应该很简单,但是flot真的让我失去了一个循环.

谢谢!

Jos*_*lio 5

flot api文档中:"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);
});
Run Code Online (Sandbox Code Playgroud)