[英]D3 Typescript SVG append not working
I've been able to get numerous different objects to populate correctly on the DOM using D3 and Angular. 我已经能够使用D3和Angular获取许多不同的对象,以正确地在DOM上填充。 Angular 4.4.6 D3 4.13.0 types/d3 4.12.0 My problem is the SVG will populate correctly the first time but will not update either through a button or through interval duration. Angular 4.4.6 D3 4.13.0类型/ d3 4.12.0我的问题是SVG会在第一次正确填充,但是不会通过按钮或间隔持续时间进行更新。 I've tried running this outside of angular using NgZone and that did not work either. 我试过使用NgZone在angular之外运行它,但也没有用。 I have attached my code same running in Stackblitz and have also linked the "working" copy that is in JavaScript. 我已经将我的代码附加在Stackblitz中运行,并且还链接了JavaScript中的“工作”副本。 Here is the link in Stackblitz https://stackblitz.com/edit/angular-df5mk8 这是Stackblitz中的链接https://stackblitz.com/edit/angular-df5mk8
update() {
this.cdr.detectChanges();
var svg = d3.select(this.root.nativeElement)
.append('svg')
.attr('height', 600)
.attr('width', 600)
.style('background-color', 'orange');
var a = [1, 2, 3, 4, 5];
var rects = svg.selectAll("rect").data(a);
rects.enter().append("rect")
.attr("x", function (d, i) { return i * 20; })
.attr("width", "15px")
.attr("height", function (d) { return d * 5; })
.attr("data-currentVal", function (d) { return d });
var updateData = function (newData: any) {
rects.data(newData).transition().duration(2000)
.attr("height", function (d: any) { return d * 5; })
.attr("data-currentVal", function (d: any) { return d })
.style("fill", function (d) {
if (d3.select(this).attr("data-currentVal") != d) {
return "red";
}
else { return "black"; }
});
console.log("updateData" + 1);
}
a[1] = 10;
a[4] = 50;
var enough = false;
var repeatFunction = window.setInterval(function () {
//console.log("repeat");
updateData(a);
a[3] = 20;
a[1] = 15;
if (enough) { window.clearInterval(repeatFunction); }
else { enough = true; }
}, 3000);
};
You are not binding the new data to your rect
in updateData
您没有将新数据绑定到updateData
的rect
https://bost.ocks.org/mike/selection/#data https://bost.ocks.org/mike/selection/#data
Perhaps surprisingly, data is not a property of the selection, but a property of its elements. 也许令人惊讶的是,数据不是选择的属性,而是其元素的属性。 This means that when you bind data to a selection, the data is stored in the DOM rather than in the selection: data is assigned to the data property of each element. 这意味着当您将数据绑定到选择项时,数据将存储在DOM中而不是存储在选择项中:数据被分配给每个元素的data属性。 If an element lacks this property, the associated datum is undefined. 如果元素缺少此属性,则关联的基准是不确定的。 Data is therefore persistent while selections can be considered transient: you can reselect elements from the DOM and they will retain whatever data was previously bound to them. 因此,数据是持久的,而选择可以被认为是短暂的:您可以从DOM中重新选择元素,它们将保留先前绑定到它们的任何数据。
The correct method is : 正确的方法是:
var updateData = function (newData: any) {
svg.selectAll("rect").data(newData).transition().duration(2000)
.attr("height", function (d: any) { return d * 5; })
.attr("data-currentVal", function (d: any) { return d })
.style("fill", function (d) {
if (d3.select(this).attr("data-currentVal") != d) {
return "red";
}
else { return "black"; }
});
console.log("updateData" + 1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.