在d3中,如何从SVG行中获取插值行数据



我显示了一个带有D3的折线图,大致包含以下代码(给定比例函数xy和浮点数组data):

 var line = d3.svg.line()
         .interpolate("basis")
         .x(function (d, i) { return x(i); })
         .y(function (d) { return y(d); });
 d3.select('.line').attr('d', line(data));

现在我想知道在给定的水平像素位置上的线的垂直高度。data阵列的数据点比像素少,并且显示的线是插值的,因此仅从data阵列推断给定像素处的线的高度不是直接的。

有什么提示吗?

这个解决方案比公认的答案效率高得多。它的执行时间是对数的(而公认的答案具有线性复杂性)。

var findYatXbyBisection = function(x, path, error){
  var length_end = path.getTotalLength()
    , length_start = 0
    , point = path.getPointAtLength((length_end + length_start) / 2) // get the middle point
    , bisection_iterations_max = 50
    , bisection_iterations = 0
  error = error || 0.01
  while (x < point.x - error || x > point.x + error) {
    // get the middle point
    point = path.getPointAtLength((length_end + length_start) / 2)
    if (x < point.x) {
      length_end = (length_start + length_end)/2
    } else {
      length_start = (length_start + length_end)/2
    }
    // Increase iteration
    if(bisection_iterations_max < ++ bisection_iterations)
      break;
  }
  return point.y
}

根据评论于2012年9月19日编辑 非常感谢nrabinowitz

您需要对getPointAtLength返回的数据进行某种搜索。(请参见https://developer.mozilla.org/en-US/docs/DOM/SVGPathElement.)

// Line
var line = d3.svg.line()
     .interpolate("basis")
     .x(function (d) { return i; })
     .y(function(d, i) { return 100*Math.sin(i) + 100; });
// Append the path to the DOM
d3.select("svg#chart") //or whatever your SVG container is
     .append("svg:path")
     .attr("d", line([0,10,20,30,40,50,60,70,80,90,100]))
     .attr("id", "myline");
// Get the coordinates
function findYatX(x, linePath) {
     function getXY(len) {
          var point = linePath.getPointAtLength(len);
          return [point.x, point.y];
     }
     var curlen = 0;
     while (getXY(curlen)[0] < x) { curlen += 0.01; }
     return getXY(curlen);
}
console.log(findYatX(5, document.getElementById("myline")));

对我来说,这将返回[5.00403881072998140.6229248046875]。

这个搜索函数findYatX远没有效率(在O(n)时间内运行),但说明了这一点。

我尝试过实现findYatXdivision(正如bumbu很好地建议的那样),但我无法让它按原样工作。

我没有将长度修改为length_end和length_start的函数,而是将长度减少了50%(如果x<point.x)或增加了50%(当x>point.x),但始终相对于零的起始长度。我还结合了revXscale/revYscale,将像素转换为d3.scale函数设置的x/y值。

function findYatX(x,path,error){
    var length = apath.getTotalLength()
        , point = path.getPointAtLength(length)
        , bisection_iterations_max=50
        , bisection_iterations = 0
    error = error || 0.1
    while (x < revXscale(point.x) -error || x> revXscale(point.x + error) {
        point = path.getPointAtlength(length)
        if (x < revXscale(point.x)) {
             length = length/2
        } else {
             length = 3/2*length
        }
        if (bisection_iterations_max < ++ bisection_iterations) {
              break;
        }
    }
return revYscale(point.y)
}

最新更新