javascript - d3 v4 : How to make a path being curved when only start and end point are known?

标签 javascript d3.js svg

我想在我成功应用的水平 d3 条形图上使用注释。当前,注释通过 <line> 连接。使用条形的结束坐标(又名因为它是水平条形图它的 width )和注释的 getBBox() 到相应的条形图坐标。但是,我希望这条线是弯曲的而不是直线。我知道我需要使用 <path>为此,但是当只知道起点和终点时,如何将曲线应用于路径?

仅供引用:我不想对坐标进行硬编码,因为条形图是动画的并且会不断更改条形的 width .

如何在只知道起点和终点坐标的情况下使路径弯曲?

最佳答案

我使用的一个选项是带有 d3 的自定义曲线 (d3-shape)。这允许代码同时使用 Canvas 和 SVG。它还允许将任意数量的点与规定的模式链接在一起。

custom curves 的文档有点用处,但看一个例子可能更有用:

var curve = function(context) {
  var custom = d3.curveLinear(context);
  custom._context = context;
  custom.point = function(x,y) {
    x = +x, y = +y;
    switch (this._point) {
      case 0: this._point = 1; 
        this._line ? this._context.lineTo(x, y) : this._context.moveTo(x, y);
        this.x0 = x; this.y0 = y;        
        break;
      case 1: this._point = 2;
      default: 
        var x1 = this.x0 * 0.5 + x * 0.5;
        var y1 = this.y0 * 0.5 + y * 0.5;
        var m = 1/(y1 - y)/(x1 - x);
        var r = -100; // offset of mid point.
        var k = r / Math.sqrt(1 + (m*m) );
        if (m == Infinity) {
          y1 += r;
        }
        else {
          y1 += k;
          x1 += m*k;
        }     
        this._context.quadraticCurveTo(x1,y1,x,y); 
        this.x0 = x; this.y0 = y;        
        break;
    }
  }
  return custom;
}

这里对于第一个点我们简单地记录下这个点,对于后续的点我们绘制一条从当前点([x,y])到前一个点([x0,y0])的二次曲线。在上面的示例中,[x1,y1] 是控制点 - 它偏移连接 [x,y] 和 [x0,y0] 的线的垂直量:

var curve = function(context) {
  var custom = d3.curveLinear(context);
  custom._context = context;
  custom.point = function(x,y) {
    x = +x, y = +y;
    switch (this._point) {
      case 0: this._point = 1; 
        this._line ? this._context.lineTo(x, y) : this._context.moveTo(x, y);
        this.x0 = x; this.y0 = y;        
        break;
      case 1: this._point = 2;
      default: 
        var x1 = this.x0 * 0.5 + x * 0.5;
        var y1 = this.y0 * 0.5 + y * 0.5;
        var m = 1/(y1 - y)/(x1 - x);
        var r = -50; // offset of mid point.
        var k = r / Math.sqrt(1 + (m*m) );
        if (m == Infinity || m == -Infinity) {
          y1 += r;
        }
        else {
          y1 += k;
          x1 += m*k;
        }     
        this._context.quadraticCurveTo(x1,y1,x,y); 
        this.x0 = x; this.y0 = y;        
        break;
    }
  }
  return custom;
}

// Basic horizontal bar graph:
var svg = d3.select("body").append("svg")
  .attr("width",500)
  .attr("height", 400);

var data = [5,6,7];

var x = d3.scaleLinear()
  .domain([0,10])
  .range([0,320]);
  
var line = d3.line()
  .curve(curve)
  .x(function(d) { return d[0]; })
  .y(function(d) { return d[1]; })  
  
var g = svg.selectAll("g")
 .data(data)
 .enter()
 .append("g")
 .attr("transform",function(d,i) {
   return "translate("+[40,i*90+30]+")"
 });
 
g.append("rect")
 .attr("width",x)
 .attr("height", 40)

g.append("path")
  .attr("d", function(d,i) {
     return line([[0,-3],[x(d),-3]])
  })
path {
  stroke-width: 1px;
  stroke: black;
  fill:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

我会创建一个 Canvas 示例,但是从线条的 Angular 来看原理完全相同,唯一的区别是我们将使用 d3.line().context(context).curve(...

关于javascript - d3 v4 : How to make a path being curved when only start and end point are known?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56784097/

相关文章:

javascript新对象构造函数替代正确吗?

javascript - 是否可以将 csv 数据加载到 nvd3 库?

javascript - D3.js 条形图动画不正确并在动画后留下伪像

javascript - d3.js 中每一行的多个列值的总和

javascript - 使用 RaphaelJS 确定图像何时加载到 svg 中

javascript - 如何验证 SVG img 在 Firefox 中正确加载(在 webdriver 测试中)

Javascript SVG 交互问题

Javascript 和 HTML - 从 SPAN 和选择中获取值(value)

javascript - OpenLayers map 未在 CordovaActivity 内加载

javascript - React 预渲染条件