【发布时间】:2017-06-14 06:05:01
【问题描述】:
我定义了以下进入/更新/退出阶段。
// this.x = my x time scale
// this.y = my y scale
// this.c = a color scale with 2 colors (red,blue)
// this.chart = D3.select() element
let series = D3.stack().keys(['point', 'topPoint'])(<any[]>this.barData);
this.chart
.append('g')
.selectAll('g')
.data(series)
.enter().append('g')
.attr('class', (d) => {return d.key + ' layer';})
.attr('fill', (d) => {return this.c(d.key);})
.selectAll('.bar')
.data((d) => {return d;})
.enter()
.append('rect')
.attr('class', 'bar');
// Update Phase
this.chart.selectAll('.bar').transition()
.attr('x', (d) => {return this.x(this._parseTime(d.data.date));})
.attr('y', (d) => {return this.y(d[1]); })
.attr('height', (d) => {return this.y(d[0]) - this.y(d[1]);})
.attr('width', 15);
// Exit phase
this.chart.selectAll('.point.layer').selectAll('.bar').exit().remove();
this.chart.selectAll('.topPoint.layer').selectAll('.bar').exit().remove();
当数据发生变化时,会绘制新的条形图,但会在旧条形图上绘制。
【问题讨论】:
-
与问题无关,但使用箭头函数时不需要
return。此外,单个参数不需要括号。 -
你能提供小提琴吗?
-
@GerardoFurtado 最初它不是单个参数或单个参数。我去掉了很多代码来简化问题。
标签: javascript angularjs d3.js