代码之家  ›  专栏  ›  技术社区  ›  Loren Cahlander

如何将d3.jsv5模块导入到polymer 3元素中?

  •  1
  • Loren Cahlander  · 技术社区  · 7 年前

    我以前使用D3V3创建过Polymer1.x自定义元素。我想把它们更新到聚合物3和D3V5。

    这是一个基本聚合物3元素,我想把d3 v5包括在其中:

    import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
    
    /**
     * `foo-bar`
     * 
     *
     * @customElement
     * @polymer
     * @demo demo/index.html
     */
    class FooBar extends PolymerElement {
      static get template() {
        return html`
          <style>
            :host {
              display: block;
            }
          </style>
          <h2>Hello [[prop1]]!</h2>
        `;
      }
      static get properties() {
        return {
          prop1: {
            type: String,
            value: 'foo-bar',
          },
        };
      }
      constructor() {
        super();
      }
    
      ready() {
        super.ready();
        console.log('foo-bar is ready!');
      }
    
    }
    
    window.customElements.define('foo-bar', FooBar);
    

    npm install d3

    我在想我需要在未来做点什么 构造函数() 就绪() 在聚合物元件内的功能,以利用d3库。

    import 'd3/dist/d3.js';
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   User 28    7 年前

    d3 船上有ES模块。所以你可以 import 随你的便。

    import { select, scaleOrdinal } from 'd3'
    // or
    import * as d3 from 'd3'
    

    那你可以用 d3级

    例子:

    索引.html

    <foo-bar></foo-bar>
    <script type='module' src='app.js'></script>
    

    import { PolymerElement, html } from '@polymer/polymer/polymer-element.js'
    import * as d3 from 'd3'
    
    class FooBar extends PolymerElement {
      static get template () {
        return html`
          <style>
            .links line {
              stroke: #999;
              stroke-opacity: 0.6;
            }
    
            .nodes circle {
              stroke: #fff;
              stroke-width: 1.5px;
            }
          </style>
    
          <svg width='960' height='600'></svg>
        `
      }
    
      ready () {
        super.ready()
        this.initGraph()
      }
    
      initGraph () {
        let svg = d3.select(this.shadowRoot.querySelector('svg'))
        let width = +svg.attr('width')
        let height = +svg.attr('height')
    
        let color = d3.scaleOrdinal(d3.schemeCategory10)
    
        let simulation = d3.forceSimulation()
          .force('link', d3.forceLink().id(d => d.id))
          .force('charge', d3.forceManyBody())
          .force('center', d3.forceCenter(width / 2, height / 2))
    
        d3.json('miserables.json').then(graph => {
          let link = svg.append('g')
            .attr('class', 'links')
            .selectAll('line')
            .data(graph.links)
            .enter().append('line')
            .attr('stroke-width', d => Math.sqrt(d.value))
    
          let node = svg.append('g')
            .attr('class', 'nodes')
            .selectAll('circle')
            .data(graph.nodes)
            .enter().append('circle')
            .attr('r', 5)
            .attr('fill', d => color(d.group))
            .call(d3.drag()
              .on('start', dragstarted)
              .on('drag', dragged)
              .on('end', dragended))
    
          node.append('title').text(d => d.id)
    
          simulation.nodes(graph.nodes)
            .on('tick', () => {
              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)
              node
                .attr('cx', d => d.x)
                .attr('cy', d => d.y)
            })
    
          simulation.force('link').links(graph.links)
        })
    
        function dragstarted (d) {
          if (!d3.event.active) simulation.alphaTarget(0.3).restart()
          d.fx = d.x
          d.fy = d.y
        }
    
        function dragged (d) {
          d.fx = d3.event.x
          d.fy = d3.event.y
        }
    
        function dragended (d) {
          if (!d3.event.active) simulation.alphaTarget(0)
          d.fx = null
          d.fy = null
        }
      }
    }
    
    customElements.define('foo-bar', FooBar)
    

    注:聚合物使用普通选择器(例如。 d3.select )无法通过。

    在这个例子中,我修改了 Force-Directed Graph .

    推荐文章