gpt4 book ai didi

angular - d3 中的动画折线图

转载 作者:行者123 更新时间:2023-12-04 10:51:07 24 4
gpt4 key购买 nike

我找到了 example d3 中的动画系列。

我试图在我的 TS 中做同样的动画代码。

但它没有成功。

// Start Animation on Click
d3.select("#start").on("click", function() {
var path = svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);

// Variable to Hold Total Length
var totalLength = path.node().getTotalLength();

// Set Properties of Dash Array and Dash Offset and initiate Transition
path
.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.transition() // Call Transition Method
.duration(4000) // Set Duration timing (ms)
.ease(d3.easeLinear) // Set Easing option
.attr("stroke-dashoffset", 0); // Set final value of dash-offset for transition
});

// Reset Animation
d3.select("#reset").on("click", function() {
d3.select(".line").remove();
});

如何将上述代码嵌入到我的 Angular 代码中。
数据正在从 API 获取。虽然图表工作得很好,但我只需要根据引用添加动画。
private drawPath(): void {
let city = this.g
.selectAll(".city")
.data(this.TEMPERATURES)
.enter()
.append("g")
.attr("fill", "transparent")
.attr("class", "city")
.style("stroke", "transparent");

city
.append("path")
.attr("class", "line")
.attr("d", d => this.line(d.values))
.style("stroke", function(d) {
return d.color;
});

let totalLength = city.length;
console.log(totalLength, "Length total");

city
.append("text")
.datum(function(d) {
return { id: d.id, value: d.values[d.values.length - 1] };
})
.attr(
"transform",
d =>
"translate(" +
this.x(d.value.date) +
"," +
this.y(d.value.temperature) +
")"
)
.attr("x", 3)
.attr("dy", "0.35em")
.transition()
.style("font", "10px sans-serif")
.text(function(d) {
return d.id;
});
}

结果根据@SmokeyShakers
enter image description here

最佳答案

您的 totalLength 计算不正确。从您的示例工作:

let totalLength = city.select('.line').node().getTotalLength();

city.select('.line')
.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.transition() // Call Transition Method
.duration(4000) // Set Duration timing (ms)
.ease(d3.easeLinear) // Set Easing option
.attr("stroke-dashoffset", 0);

关于angular - d3 中的动画折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59467526/

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