gpt4 book ai didi

javascript - d3 v5 : How to transition only when a specific attribute changed?

转载 作者:行者123 更新时间:2023-11-30 13:57:42 25 4
gpt4 key购买 nike

我有一张 map ,每个国家/地区的中心都有一个圆圈。在 general update pattern 之后,我应用了一个每 3 秒更新一次圆圈的动画。迈克博斯托克。然而,我的问题是我想对留在图表中的旧元素应用转换(即不属于 exit() 的一部分),但仅适用于那些在特定方面有变化的元素属性(因为这个属性定义了圆的颜色)。我想我应该将旧值作为一个属性存储在圆圈中,然后将这个属性值与更新数据中新分配的值进行比较。

但是,当我使用普通的 js if 子句时,它只为我提供了第一个元素,这意味着当这个圆圈的值发生变化时,所有其他圆圈也会进行转换。

我如何检查此模式中的旧值和新值?

这是一个示例代码:

//DATA JOIN                 
var countryCircles = circleCont.selectAll(".dataCircle")
.data(filData, function(d){ return d.id})

//EXIT OLD CIRCLES THAT ARE NOT INCLUDED IN NEW DATASET
countryCircles.exit()
.transition()
.ease(d3.easeLinear)
.duration(500)
.attr("r",0)
.remove()

//CHECK IF OLD CIRCLES THAT ARE INCLUDED CHANGED THE CRITICAL ATTRIBUTE VALUE (main)
if (countryCircles.attr('main') != countryCircles.data()[0].main) {

countryCircles
.attr("main", function(d) {return d.main})
.attr("id", function(d) {return "circle" + d.id})
.attr("class","dataCircle")
.transition()
.ease(d3.easeLinear)
.duration(500)
.ease(d3.easeLinear)
.attr("r",0)
.transition()
.duration(500)
.ease(d3.easeLinear)
.attr("r",10)
.style("fill", function(d) {
if (d.main === "nodata") {
return "grey"
} else {
return color_data.find(function (y) {
return y.main === d.main;
}).color;
}
})

} else {
countryCircles
.attr("main", function(d) {return d.main})
.attr("id", function(d) {return "circle" + d.id})
.attr("class","dataCircle")
}

//CREATE CIRCLES THAT ARRE NEW IN THE UPDATED DATASET
var newCircle = countryCircles.enter()
.append("circle")
.attr("class","dataCircle")
.attr("cx", getCX)
.attr("cy", getCY)
.attr("id", function(d) {return "circle" + d.id})
.attr("main", function(d) {return d.main})
.style("cursor","crosshair")
.attr("r", 0)
.transition()
.delay(500)
.duration(500)
.ease(d3.easeLinear)
.attr("r",10)
.style("fill", function(d) {
if (d.main === "nodata") {
return "grey"
} else {
return color_data.find(function (y) {
return y.main === d.main;
}).color;
}
})

最佳答案

好吧,解决方案有点直截了当(我是盲人......)。

我没有对单个元素使用 if 语句,而是使用 each() 函数并在其中检查更改。

...
//CHECK IF OLD CIRCLES THAT ARE INCLUDED CHANGED THE CRITICAL ATTRIBUTE VALUE (main)
countryCircles.each(function(d) {
if (d3.select(this).attr('main') != d3.select(this).data()[0].main) {
...

关于javascript - d3 v5 : How to transition only when a specific attribute changed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56905954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com