How to use D3 in Node.js properly?

前端 未结 3 1109
别跟我提以往
别跟我提以往 2020-12-02 10:06

I\'ve been trying to invoke D3 within Node.js. I tried firstly to import d3.v2.js from D3\'s website with the script tag, but then read this thread:

I want to run d3

3条回答
  •  悲哀的现实
    2020-12-02 10:32

    You need to install jsdom using yarn or npm. Node.js does not support dom by default, thus the need to use jsdom for creating dom elements. Use d3 for all other manipulations except fetch operations. i.e d3.xml,d3.tsv

    import jsdom from 'jsdom';
    import * as d3 from 'd3';
    const { JSDOM } = jsdom;
    JSDOM.fromURL(
        'your resource url',
      ).then((dom) => {
        const doc = dom.window.document;
        const states = d3
          .select(doc)
          .select('path')
          .attr(':fme:ID');
        console.log(states);
      });
    

    creating dom from file

    JSDOM.fromURL(
        'your resource url',
      ).then((dom) => {
        const doc = dom.window.document;
    }
    

    dom from html string

    const dom = new JSDOM(
      `

    Hello

    `, { includeNodeLocations: true } );

提交回复
热议问题