我的数据每5分钟更新一次,现在我只是在componentDidUpdate(){this.drawChart());}上重新渲染,并使用以下命令删除以前的数据:
componentDidUpdate() {
if (this.props.metricsState.metricsSecondOrder.length > 0 && this.props.metricsState.metricsSecondOrder[0].value) {
this.drawChart();
}
}
drawChart() {
var svg = d3.select("svg.circle-pack"),
margin = 20,
diameter = 490;
svg.select("g").remove();
var g = svg.append("g").attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
var color = d3.scaleLinear()
.domain([-1, 5])
.range(["hsl(152,100%,100%)", "hsl(228,80%,20%)"])
.interpolate(d3.interpolateHcl);
var pack = d3.pack()
.size([diameter - margin, diameter - margin])
.padding(2);
let root = convertMetricDataToD3(this.props.metricsState.metricsSecondOrder)
root = d3.hierarchy(root)
.sum(function (d) { return d.size; })
.sort(function (a, b) { return b.value - a.value; });
var focus = root,
nodes = pack(root).descendants(),
view;
var circle = g.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("class", function (d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
.style("fill", function (d) { return d.children ? color(d.depth) : d.data.fill; })
.on("click", function (d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); });
var text = g.selectAll("text")
.data(nodes)
.enter().append("text")
.attr("class", "label")
.style("fill-opacity", function (d) { return d.parent === root ? 1 : 0; })
.style("display", function (d) { return d.parent === root ? "inline" : "none"; })
.html(function (d) { return "<tspan x='0' dy='1em'>" + d.data.displayName + "</tspan>" + "<tspan x='0' dy='1.1em' font-size='1.4em' font-weight='bold'>" + d.data.value + "</tspan>" });
var node = g.selectAll("circle,text");
svg
.style("background", color(-1))
.on("click", function () { zoom(root); });
zoomTo([root.x, root.y, root.r * 2 + margin]);
function zoom(d) {
var focus0 = focus; focus = d;
console.dir(d);
var transition = d3.transition()
.duration(d3.event.altKey ? 7500 : 750)
.tween("zoom", function (d) {
var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
return function (t) { zoomTo(i(t)); };
});
transition.selectAll(".circle-pack text")
.filter(function (d) { return d.parent === focus || this.style.display === "inline"; })
.style("fill-opacity", function (d) { return d.parent === focus ? 1 : 0; })
.on("start", function (d) { if (d.parent === focus) this.style.display = "inline"; })
.on("end", function (d) { if (d.parent !== focus) this.style.display = "none"; });
}
function zoomTo(v) {
var k = diameter / v[2]; view = v;
node.attr("transform", function (d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
circle.attr("r", function (d) { return d.r * k; });
}
}
render() {
return (
<div className={'graph-box box-container'}>
<div>
<BoxHeader title={'Time In State'} showAlarm={false} />
<div>
<svg className="circle-pack" width="525" height="500"></svg>
</div>
</div>
</div>
)
}
}
如果我放大了Circle Pack,只是重置,所以我只想更新更改而无需重置缩放,但是我不知道最佳方法或最佳方法是什么?在道具中保持zoom(d)或创建一个函数以使用联接更新d3,但是我不知道该怎么做。任何例子都很棒。我的圈子包基于此https://mbostock.github.io/d3/talk/20111116/pack-hierarchy.html