【发布时间】:2025-12-18 12:00:02
【问题描述】:
我正在尝试在 angular2 组件中构建 D3 图表。当我单击链接创建 D3 图表时,它会创建一个新实例。请注意创建多个 SVG 标签副本的 HTML。任何想法为什么会发生以及如何避免它? 每次我点击链接创建 D3 图表时,它应该清除/清空现有实例并创建一个新的图表组件。
从父组件创建新实例的代码,
import { Component } from '@angular/core';
import { BubbleChart } from '../Charts/BubbleChart';
@Component({
template: `
<div id="divBubbleChart">
<bubble-chart></bubble-chart>
</div>
`,
directives: [BubbleChart]
})
export class CacheVisualization {
constructor() {
console.log("CacheVisualization component being called");
}
}
子 d3 组件
import { Component, ViewEncapsulation } from '@angular/core';
import { HTTP_PROVIDERS, Http } from '@angular/http';
import { Configuration } from '../Configuration/Configuration';
declare var d3: any;
@Component({
selector: 'bubble-chart',
styleUrls: ['css/BubbleChart.css'],
providers: [Configuration, HTTP_PROVIDERS],
template: ``,
encapsulation: ViewEncapsulation.None
})
export class BubbleChart {
public resultData: any;
public chartData: any;
margin = 5;
diameter = 660;
constructor(private _Configuration: Configuration) {
console.log("In constructor of BubbleChartComponent");
this.DrawBubbleChart();
}
private DrawBubbleChart(): void {
console.log("Inside DrawBubbleChart in BubbleChartComponent");
//console.log(this.resultData);
var color = d3.scale.linear()
.domain([-1, 5])
.range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
.interpolate(d3.interpolateHcl);
var pack = d3.layout.pack()
.padding(2)
.size([this.diameter - this.margin, this.diameter - this.margin])
.value(function (d) { return d.size; })
var svg = d3.select("body").append("svg")
.attr("width", this.diameter)
.attr("height", this.diameter)
.append("g")
.attr("transform", "translate(" + this.diameter / 2 + "," + this.diameter / 2 + ")");
var chart = d3.json(this._Configuration.BLUESKYDATACACHEAPI_GETEXTRACTORQUEUESLATEST, (error, root) => {
if (error) throw error;
var focus = root,
nodes = pack.nodes(root),
view;
var circle = svg.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", (d) => { return d.children ? color(d.depth) : null; })
.on("click", (d) => { if (focus !== d) zoom.call(this, d), d3.event.stopPropagation(); });
var text = svg.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"; })
.text(function (d) { return d.name; });
var node = svg.selectAll("circle,text");
d3.select("body")
.style("background", "white")
//.style("vertical-align", "top")
//.style("background", color(-1))
.on("click", () => { zoom.call(this, root); });
zoomTo.call(this, [root.x, root.y, root.r * 2 + this.margin]);
function zoom(d) {
var focus0 = focus; focus = d;
var transition = d3.transition()
.duration(d3.event.altKey ? 7500 : 750)
.tween("zoom", (d) => {
var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + this.margin]);
return (t) => { zoomTo.call(this, i(t)); };
});
transition.selectAll("text")
.filter(function (d) { return d.parent === focus || this.style.display === "inline"; })
.style("fill-opacity", function (d) { return d.parent === focus ? 1 : 0; })
.each("start", function (d) { if (d.parent === focus) this.style.display = "inline"; })
.each("end", function (d) { if (d.parent !== focus) this.style.display = "none"; });
}
function zoomTo(v) {
var k = this.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; });
}//end zoomTo
});//end chart
}//end DrawBubbleChart
}
【问题讨论】:
-
这与创建多个 Angular2 组件无关。创建的内容在 Angular2 之外,可能是由外部 D3 JS 库(可能由 Angular2 组件包装)引起的。我猜你需要在创建新图表之前删除生成的图表,但是因为这不是由 Angular2 引起的,所以它可能无法使用 Angular2 方法修复。
-
好的,这里如何删除生成的图表?有任何想法吗?还是我需要在创建新实例之前找到 DOM 元素并清除它?
-
我会推荐在创建 svg 时,即 **** var svg = d3.select("body").append("svg") **** 给它一个你想要的任何东西的 id想要,例如,'mySVG'。像 ****.attr('id', 'mySVG')**** 然后在它之前删除 '#mySVG'。所以 d3.select('#mySVG').remove()。然后这将解决重复问题。确保在明显附加任何内容之前给它一个 id
-
@thatOneGuy,我尝试了您建议的方式,但它包含父/选择器 html 标记的 ID 属性,而不是此组件,即此处的 svg。请参阅附加的快照。以下是我添加的代码.. d3.select("bubble-chart") .attr('id', 'mySVG')
-
我知道您找到了解决方案,但在您的问题中,您选择的是“body”而不是“bubble-chart”,所以显然它不会起作用。
标签: d3.js angular angular2-directives