简体   繁体   English

尝试导入错误:未从“ d3”导出“行为”

[英]Attempted import error: 'behavior' is not exported from 'd3'

I am trying to make a sankey diagram using d3 and react.js and I am using this example as a reference https://bl.ocks.org/emeeks/9673c96a682fe3948379 . 我正在尝试使用d3和react.js制作sankey图,并且正在使用此示例作为参考https://bl.ocks.org/emeeks/9673c96a682fe3948379 I am fairly new to react(2 days) and I am getting this error-- 我刚开始反应(2天),却收到这个错误-

./src/components/SankeyComponent.js

Attempted import error: 'behavior' is not exported from 'd3' (imported as 'd3'). 尝试导入错误:未从“ d3”(导入为“ d3”)导出“行为”。

SankeyComponent.js This is my code for creating SankeyComponent. SankeyComponent.js这是我创建SankeyComponent的代码。 As soon as I clear one error other import and export erros are coming up. 一旦我清除了一个错误,就会出现其他进出口错误。

import React, { Component } from 'react';
import ReactFauxDOM from 'react-faux-dom';
import * as d3 from 'd3';
import sankey from 'd3-plugins-sankey';
import _ from 'lodash';
import { NODES } from '../shared/nodes';
import { LINKS } from '../shared/links';

class Sankey extends Component {

     constructor(props) {
        super(props);

        this.state = {
            nodes: NODES,
            links: LINKS
        };
    }
    render() {

            // Set units, margin, sizes
            var margin = {top: 1, right: 1, bottom: 6, left: 1},
            width = 960 - margin.left - margin.right,
            height = 500 - margin.top - margin.bottom;

            var formatNumber = d3.format(",.0f"),
                format = function(d) { return formatNumber(d) + " TWh"; },
                color = d3.scale.category20();

            // Set the sankey diagram properties

            var sankey = d3.sankey()
                            .nodeWidth(15)
                            .nodePadding(10)
                            .size([width, height]);

            var path = sankey.link();

            //var freqCounter = 1;

            var graph = {
                  nodes: _.cloneDeep(this.state.nodes),
                  links: _.cloneDeep(this.state.links)
                };

            sankey.nodes(graph.nodes)
                  .links(graph.links)
                  .layout(32);

            // Initialize and append the svg canvas to faux-DOM

            var svgNode = ReactFauxDOM.createElement('svg');

            var svg = d3.select(svgNode)
                        .attr("width", width + margin.left + margin.right)
                        .attr("height", height + margin.top + margin.bottom)
                        .append("g")
                        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

            // Initialize and append the svg canvas to faux-DOM
            var link = svg.append("g").selectAll(".link")
                          .data(graph.links)
                          .enter().append("path")
                          .attr("class", "link")
                          .attr("d", path)
                          .style("stroke-width", function(d) { return Math.max(1, d.dy); })
                          .sort(function(a, b) { return b.dy - a.dy; });

            link.append("title")
                .text(function(d) { return d.source.name + " → " + d.target.name + "\n" + format(d.value); });

            // Add nodes

            var node = svg.append("g").selectAll(".node")
                          .data(graph.nodes)
                          .enter().append("g")
                              .attr("class", "node")
                              .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
                          .call(d3.behavior.drag()
                              .origin(function(d) { return d; })
                              .on("dragstart", function() { this.parentNode.appendChild(this); })
                              .on("drag", dragmove));

            node.append("rect")
                  .attr("height", function(d) { return d.dy; })
                  .attr("width", sankey.nodeWidth())
                  .style("fill", function(d) { return d.color = color(d.name.replace(/ .*/, "")); })
                  .style("stroke", "none")
            .append("title")
                   .text(function(d) { return d.name + "\n" + format(d.value); });

            node.append("text")
                  .attr("x", -6)
                  .attr("y", function(d) { return d.dy / 2; })
                  .attr("dy", ".35em")
                  .attr("text-anchor", "end")
                  .attr("transform", null)
                  .text(function(d) { return d.name; })
            .filter(function(d) { return d.x < width / 2; })
                  .attr("x", 6 + sankey.nodeWidth())
                  .attr("text-anchor", "start");

            var linkExtent = d3.extent(graph.links, function (d) {return d.value});
            var frequencyScale = d3.scale.linear().domain(linkExtent).range([0.05,1]);
            //var particleSize = d3.scale.linear().domain(linkExtent).range([1,5]);

            // the function for moving the nodes
            function dragmove(d) {
                d3.select(this).attr("transform", "translate(" + d.x + "," + (d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))) + ")");
                sankey.relayout();
                link.attr("d", path);
            }

            graph.links.forEach(function (link) {
                link.freq = frequencyScale(link.value);
                link.particleSize = 2.5;
                link.particleColor = d3.scale.linear().domain([0,1])
                .range([link.source.color, link.target.color]);
             })



            return (
        <svg>
          {svg.node().toReact()}
          </svg>
    );

    }
}

export default Sankey;

Looks like d3.behavior.drag was renamed to d3.drag in v4. 貌似d3.behavior.drag更名为d3.drag在V4。 https://github.com/d3/d3/blob/master/CHANGES.md#dragging-d3-drag https://github.com/d3/d3/blob/master/CHANGES.md#dragging-d3-drag

Since you're using d3 v4, you can either update your code or downgrade to v3 由于您使用的是d3 v4,因此可以更新代码或降级到v3

暂无
暂无

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

相关问题 无法在React.js中导入d3队列? 尝试导入错误:“队列”未从“d3”导出(导入为“d3”) - Can't import d3-queue in React.js? Attempted import error: 'queue' is not exported from 'd3' (imported as 'd3') 尝试导入错误:'未从 - Attempted import error:' is not exported from 尝试导入错误:“ApiRequests”未从“./ApiRequests”导出 - Attempted import error: 'ApiRequests' is not exported from './ApiRequests' 尝试导入错误:“Typography”未从“antd”导出 - Attempted import error: 'Typography' is not exported from 'antd' 尝试导入错误:“必需”未从“是的”导出 - Attempted import error : 'required' is not exported from 'yup' 尝试导入错误:“App”未从“./App”导出 - Attempted import error: 'App' is not exported from './App' 尝试导入错误:“getMoviesList”未从“./actions”导出; 反应错误 redux - Attempted import error: 'getMoviesList' is not exported from './actions'; error in react redux 反应错误“尝试导入错误:'TodoItem'未从'./Todoitem'导出。” - error in react " Attempted import error: 'TodoItem' is not exported from './Todoitem'. " 尝试导入错误:“app”未从“firebase/app”导出(导入为“firebase”) - Attempted import error: 'app' is not exported from 'firebase/app' (imported as 'firebase') 尝试导入错误:“Navigate”未从“react-router-dom”导出 - Attempted import error: 'Navigate' is not exported from 'react-router-dom'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM