简体   繁体   English

如何在node.js中正确导入d3库?

[英]How to import d3 library properly in node.js?

I am working with d3 in order to visualize a force directed graph. 我正在使用d3 ,以便可视化力导向图。

I started my project using a cdn file. 我使用CDN文件开始了我的项目。 But I found one problem, if I use this code: 但是,如果使用以下代码,我发现了一个问题:

https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js

..functions like d3.forceSimulation() are not available, at least it throws an error. ..d3.forceSimulation d3.forceSimulation()类的功能不可用,至少会引发错误。 So I changed the url to this one: 因此,我将网址更改为以下网址:

https://d3js.org/d3.v4.min.js https://d3js.org/d3.v4.min.js

...according to this example Force-Directed Graph ...根据此示例力导向图

But new problem came out: Uncaught TypeError: Cannot read property 'append' of null , that means, svg . 但是出现了新问题: Uncaught TypeError: Cannot read property 'append' of null ,即svg

I tried to install d3 using npm . 我尝试使用npm安装d3 So I imported d3 : 所以我导入了d3

import * as d3 from 'd3'

However, I cannot get how to append a svg to div 但是,我不知道如何将svg附加到div

This is my code when error occurs: 这是发生错误时的代码:

graphic/index.js 图形/ index.js

// import modules
import * as d3 from 'd3'

// export function
const force_graph = (w, h) => {
  let svg = d3.select('#Gforce')
    .append('svg')
    .attr({
      'width': w,
      'height': h,
      'cursor': 'default'
    })

  let circle = svg.append('circle')
    .attr({
      'cx': 325,
      'cy': 270,
      'r': 10,
      'fill': '#7A6464',
      'class': 'dot'
    })
}

module.exports = force_graph

index.js index.js

const graph = require('./graphic')
let w = document.getElementById('Gforce').offsetWidth
let h = document.getElementById('Gforce').offsetHeight
graph(w, h)

How can I import properly d3 library. 如何正确导入d3库。 I tried those options, but I cannot get any results. 我尝试了这些选项,但没有任何结果。 What am I doing wrong? 我究竟做错了什么?

EDIT 1 编辑1

I removed all d3 scripts from html . 我从html删除了所有d3脚本。

I added this in my viz file: 我将此添加到我的viz文件中:

const d3 = require('d3')
const d3SelectionMulti = require('d3-selection-multi')

I also tried: 我也尝试过:

import * as d3 from 'd3'
import 'd3-selection-multi'

I used .attrs instead .attr according to docs: d3-selection-multi v1.0.1 我根据文档使用了.attrs而不是.attrd3-selection-multi v1.0.1

This is the error it throws after those changes: Uncaught TypeError: d3.select(...).append(...).attrs is not a function 这是这些更改后引发的错误: Uncaught TypeError: d3.select(...).append(...).attrs is not a function

D3.js v4 made breaking api changes. D3.js v4对API进行了重大更改。 You're in version purgatory. 您正在使用炼狱版。 You need to let go of v3 and embrace v4. 您需要放开v3并拥抱v4。

functions like d3.forceSimulation() are not available d3.forceSimulation()之类的功能不可用

Because you were trying to use a v4 function that doesn't exist in v3. 因为您试图使用v3中不存在的v4函数。

But new problem came out: Uncaught TypeError: Cannot read property 'append' of null, that means, svg. 但是出现了新问题:Uncaught TypeError:无法读取null的属性“ append”,这意味着svg。

Most likely it's because you're trying to use attr with an object, which v4 doesn't support out of the box. 很有可能是因为您要尝试将attr与某个对象一起使用,而v4不支持该功能。

You need https://github.com/d3/d3-selection-multi 您需要https://github.com/d3/d3-selection-multi

Your import statement is correct. 您的导入声明是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM