【问题标题】:d3 - Where to catch missing node loopin error?d3 - 在哪里捕获丢失的节点循环错误?
【发布时间】:2020-05-09 18:38:46
【问题描述】:

我正在为我的应用程序使用 d3,我想知道在哪里可以在我的应用程序中设置 try/catch 方法。 有时我设置没有匹配节点的链接,我知道这是一个错误,但我想捕捉这个错误。

目前我收到此错误

ERROR TypeError: Cannot create property 'vx' on string 'sadd-123-c3c-sdc .... 

而且它循环了很多时间(800+)。 我在哪里可以设置 try/catch 来处理这种情况?

到目前为止,我在这里尝试过

try {
    this.simulation = d3.forceSimulation()
}
catch(err){
  console.log('print the error', error)
}

也在这里

  this.simulation
  .nodes(<any>this.data.nodes)
  .on('tick', () => { this.setNodePostion(); });

setNodePostion() {
try {

  this.node
    .attr('cx', (d) => {
      return d.x;
    }).attr('cy', (d) => {
      return dd.y;
    })
    .attr('transform', (d) => 'translate(' + d.x + ',' + d.y + ')');

  this.link
    .attr('x1', (d) => d.source.x)
    .attr('y1', (d) => d.source.y)
    .attr('x2', (d) => d.target.x)
    .attr('y2', (d) => d.target.y);
} catch (error) {
  console.log('third', error);
}



}

知道我可以将这个 try/catch 放在哪里吗? 谢了

我正在使用 d3 版本 5 和 Angular

【问题讨论】:

    标签: javascript angular d3.js


    【解决方案1】:

    我找到了应该将 try-catch 放在哪里,但解决方案是 catch 动作。

    在模拟动作之前放置 try/catch 并在 catch 中停止模拟

    try{
        this.simulation
          .nodes(<any>this.data.nodes)
          .on('tick', () => { this.setNodePostion(); });
    
    this.simulation.force('center', d3.forceCenter(this.width / 2, this.height / 2));
            .force<any>('link')
            .links(this.data.links);
    
       } catch (err){
        this.simulation.stop();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 2015-05-31
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      相关资源
      最近更新 更多