【问题标题】:How to call function inside d3.json()如何在 d3.json() 中调用函数
【发布时间】:2020-07-07 19:51:18
【问题描述】:

我正在尝试从 .json 文件加载我的数据以创建树形图。一切正常,除了当我在 d3.json() 中调用辅助函数时出现错误

Cannot read property 'zoomin' of undefined

我的代码是这样的

d3.json("../static/warehouses.json").then(function(data){
//do stuff here for treemap
      this.zoomin() //function to zoom in on a clicked treemap node
    })

如果可能的话,我如何在 d3.json 函数中包含辅助函数?如果不是,我该如何解决这个错误?

【问题讨论】:

    标签: javascript json d3.js


    【解决方案1】:

    问题在于this 上下文。 您可以将this 存储在某个变量中并使用它调用 zoomin。

    let self = this;
    d3.json("../static/warehouses.json").then(function(data){
    //do stuff here for treemap
          self.zoomin() //function to zoom in on a clicked treemap node
    })
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多