【发布时间】:2020-11-24 16:22:20
【问题描述】:
如何使用新数据更新 d3 图表: 我正在实现 ngOnchanges 三个功能:
this.removeSvg(this.data);
this.createSvg();
this.drawBars(this.data);
在哪里 createSvg() :
private createSvg(): void {
this.svg = d3.select("figure#histogram")
.append("svg")
.attr("width", '100%')
.attr("height", '100%')
.append("g")
.attr("transform", "translate(" + this.margin + "," + this.margin + ")");
}
和 removeSvg() :
this.svg = d3.select("figure#histogram")
.exit().remove()
d3.select("#histogram").select("svg").remove();
和drawBars(数据:任何[]):
private drawBars(data: any[]): void {
// Create the X-axis band scale
const x = d3.scaleBand()
.range([this.axisMin, 340])
.domain(data.map(d => d.xAxis))
.padding(0);
// Create the Y-axis band scale
const y = d3.scaleLinear()
.domain([0, this.axisMax])
.range([this.height, 0])
.nice();
// Create and fill the bars
this.svg.selectAll("bars")
.data(data)
.enter()
.append("rect")
.attr("x", d => x(d.xAxis))
.attr("y", d => y(d.yAxis))
.attr("width", x.bandwidth())
.attr("height", (d) => this.height - y(d.yAxis))
.attr("fill", "rgb(13, 185, 240)");
}
但它不会刷新图表,即使确定数据正在刷新(因此对于第一个图表,我们继续将所有其他图表视为相同的数据) 有什么帮助吗?也许删除或退出在这里是错误的!
【问题讨论】:
标签: javascript angular typescript d3.js charts