cuốn sách gpt4 ai đã làm

javascript - d3 : update dataset not updating the DOM

In lại Tác giả: Walker 123 更新时间:2023-11-28 19:08:25 31 4
mua khóa gpt4 Nike

我想使用 D3 使用以下数据创建一个列表:

var dataSet = [
{ label: 'a', value: 10},
{ label: 'b', value: 20},
{ label: 'c', value: 30},
{ label: 'd', value: 40}
];

var circle = svg.selectAll('circle')
.data(dataSet)
.đi vào()
.append('circle')
.attr({
r:function(d){ return d.value },
cx:function(d, i){ return i * 100 + 50 },
cy:50,
fill: 'red'
});

这有效。一段时间后,我更改了数据

dataSet[0].value = 40;
dataSet[1].value = 30;
dataSet[2].value = 20;
dataSet[3].value = 10;

我想再次绘制列表:

setTimeout(function () {
var circle = svg.selectAll('circle')
.data(dataSet, function (d) {
return d.label;
})
.sort(function (a,b){ return d3.ascending(a.value, b.value);})
.đi vào()
.append('circle')
.attr({
r:function(d){ return d.value },
cx:function(d, i){ return i * 100 + 50 },
cy:50,
fill: 'red'
});
},1000);

THỬ NGHIỆM

但是,这个列表并没有真正更新。有什么建议如何解决这个问题吗?

câu trả lời hay nhất

问题是您只处理输入选择,更新时该选择将为空 - 您需要处理更新选择:

svg.selectAll('circle')
.data(dataSet, function (d) {
return d.label;
})
.sort(function (a,b){ return d3.ascending(a.value, b.value);})
.transition()
.attr({
r:function(d){ return d.value },
cx:function(d, i){ return i * 100 + 50 },
cy:50,
fill: 'red'
});

更新了 fiddle đây 。有关不同选择的更多信息,请查看 this tutorial .

关于javascript - d3 : update dataset not updating the DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206515/

31 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress