【问题标题】:why d3.path().moveTo() function is undefined?为什么 d3.path().moveTo() 函数未定义?
【发布时间】:2021-09-23 12:26:42
【问题描述】:

我的生成路径的函数:

const getPath = (source, target) => {
    // console.log('path', path.moveTo(source.x, source.y));
    const path = d3.path()
      .moveTo(source.x, source.y)
      .lineTo(target.x, source.y)
      .lineTo(target.x, target.y)
      .toString();
    // console.log('path', path);
    return path;
  }

d3 导入语句:

import * as d3 from "d3";

源和目标结构:

{
  x: Number,
  y: Number
}

为什么 moveTo 未定义 我究竟做错了什么? 顺便说一句,我正在反应。

【问题讨论】:

    标签: javascript reactjs d3.js


    【解决方案1】:

    moveTo 不返回任何内容,因此其返回值为undefined。它不返回路径,因此您不能在此处进行方法链接。你可以在source code 中看到这个moveTo

    moveTo: function(x, y) {
      this._ += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y);
    },
    

    你可以调整你的代码,它应该可以工作:

    const getPath = (source, target) => {
        const path = d3.path();
    
        path.moveTo(source.x, source.y);
        path.lineTo(target.x, source.y);
        path.lineTo(target.x, target.y);
    
        return path.toString();
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-13
      • 2020-03-28
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多