gpt4 book ai didi

javascript - 尝试导入错误 : 'behavior' is not exported from 'd3'

转载 作者:行者123 更新时间:2023-11-30 14:17:56 25 4
gpt4 key购买 nike

我正在尝试使用 d3 和 react.js 制作桑基图,我正在使用此示例作为引用 https://bl.ocks.org/emeeks/9673c96a682fe3948379 .我对 react 还很陌生(2 天),我收到了这个错误--

./src/components/SankeyComponent.js

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

桑基组件.js这是我创建 SankeyComponent 的代码。一旦我清除了一个错误,就会出现其他导入和导出错误。

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;

最佳答案

看起来 d3.behavior.drag 在 v4 中被重命名为 d3.draghttps://github.com/d3/d3/blob/master/CHANGES.md#dragging-d3-drag

由于您使用的是 d3 v4,您可以更新代码或降级到 v3

关于javascript - 尝试导入错误 : 'behavior' is not exported from 'd3' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53250700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com