gpt4 book ai didi

javascript - 为什么我的折线图过渡看起来滞后?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:28 26 4
gpt4 key购买 nike

我的代码怎么了?为什么折线图动画不能流畅连续?

我使用的是最新的 d3.js v5。动画看起来很滞后真的很奇怪,我似乎无法弄清楚如何让它平滑过渡。

这是我的代码:

<html>
<head>
<title>Test</title>
<script src="https://d3js.org/d3.v5.js"></script>
<style>
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}
</style>
</head>
<body>

<p>
<b>Size:</b> 300x30 &nbsp;&nbsp;<b>Interpolation:</b> basis &nbsp;&nbsp;<b>Animation:</b> true &nbsp;&nbsp;<b>Transition:</b>
1000ms &nbsp;&nbsp;<b>Update Frequency:</b> 1000ms
<div id="graph1" class="aGraph" style="width:300px; height:30px;"></div>
</p>
<script>

function displayGraphExample(id, width, height, updateDelay, transitionDelay) {
var graph = d3.select(id)
.append("svg:svg")
.attr("width", "100%")
.attr("height", "100%");
var data = [3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 9];
var x = d3.scaleLinear().domain([0, d3.max(data) * 5]).range([-5, width]);
var y = d3.scaleLinear().domain([0, 10]).range([0, height]);
var line = d3.line()
.x(function (d, i) {
return x(i);
})
.y(function (d) {
return y(d);
})
.curve(d3.curveBasis);
graph.append("svg:path").attr("d", line(data));
function redrawWithAnimation() {
graph.selectAll("path")
.data([data])
.attr("transform", "translate(" + x(1) + ")")
.attr("d", line)
.transition()
.ease(d3.easeLinear)
.duration(transitionDelay)
.attr("transform", "translate(" + x(0) + ")");
}
d3.interval(function() {
var v = data.shift();
data.push(v);
redrawWithAnimation();
}, updateDelay);
}

displayGraphExample("#graph1", 300, 30, 1000, 1000);
</script>

</body>
</html>

最佳答案

这里的问题是您为 updateDelaytransitionDelay 设置了完全相同的值。因此,您在前一个过渡结束之前调用一个过渡,这实际上取消了当前过渡。

简单而幼稚的解决方案是稍微增加updateDelay。例如,多 50 毫秒:

<html>
<head>
<title>Test</title>
<script src="https://d3js.org/d3.v5.js"></script>
<style>
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}
</style>
</head>
<body>

<p>
<b>Size:</b> 300x30 &nbsp;&nbsp;<b>Interpolation:</b> basis &nbsp;&nbsp;<b>Animation:</b> true &nbsp;&nbsp;<b>Transition:</b>
1000ms &nbsp;&nbsp;<b>Update Frequency:</b> 1000ms
<div id="graph1" class="aGraph" style="width:300px; height:30px;"></div>
</p>
<script>

function displayGraphExample(id, width, height, updateDelay, transitionDelay) {
var graph = d3.select(id)
.append("svg:svg")
.attr("width", "100%")
.attr("height", "100%");
var data = [3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 9];
var x = d3.scaleLinear().domain([0, d3.max(data) * 5]).range([-5, width]);
var y = d3.scaleLinear().domain([0, 10]).range([0, height]);
var line = d3.line()
.x(function (d, i) {
return x(i);
})
.y(function (d) {
return y(d);
})
.curve(d3.curveBasis);
graph.append("svg:path").attr("d", line(data));
function redrawWithAnimation() {
graph.selectAll("path")
.data([data])
.attr("transform", "translate(" + x(1) + ")")
.attr("d", line)
.transition()
.ease(d3.easeLinear)
.duration(transitionDelay)
.attr("transform", "translate(" + x(0) + ")");
}
d3.interval(function() {
var v = data.shift();
data.push(v);
redrawWithAnimation();
}, updateDelay);
}

displayGraphExample("#graph1", 300, 30, 1050, 1000);
</script>

</body>
</html>

但是,这不是最好的方法:这里惯用的解决方案是在最后一个过渡结束时调用一个新的过渡。例如:

.on("end", function() {
var v = data.shift();
data.push(v);
redrawWithAnimation();
});

这样我们就不需要猜测或摆弄魔术数字和值,这可能会出错:转换本身会在完成时调用您想要的任何函数。

这是演示:

<html>

<head>
<title>Test</title>
<script src="https://d3js.org/d3.v5.js"></script>
<style>
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}
</style>
</head>

<body>

<p>
<b>Size:</b> 300x30 &nbsp;&nbsp;<b>Interpolation:</b> basis &nbsp;&nbsp;<b>Animation:</b> true &nbsp;&nbsp;<b>Transition:</b> 1000ms &nbsp;&nbsp;<b>Update Frequency:</b> 1000ms
<div id="graph1" class="aGraph" style="width:300px; height:30px;"></div>
</p>
<script>
function displayGraphExample(id, width, height, updateDelay, transitionDelay) {
var graph = d3.select(id)
.append("svg:svg")
.attr("width", "100%")
.attr("height", "100%");
var data = [3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 9];
var x = d3.scaleLinear().domain([0, d3.max(data) * 5]).range([-5, width]);
var y = d3.scaleLinear().domain([0, 10]).range([0, height]);
var line = d3.line()
.x(function(d, i) {
return x(i);
})
.y(function(d) {
return y(d);
})
.curve(d3.curveBasis);
graph.append("svg:path").attr("d", line(data));
redrawWithAnimation();

function redrawWithAnimation() {
graph.selectAll("path")
.data([data])
.attr("transform", "translate(" + x(1) + ")")
.attr("d", line)
.transition()
.ease(d3.easeLinear)
.duration(transitionDelay)
.attr("transform", "translate(" + x(0) + ")")
.on("end", function() {
var v = data.shift();
data.push(v);
redrawWithAnimation();
})
}
}

displayGraphExample("#graph1", 300, 30, 1050, 1000);
</script>

</body>

</html>

关于javascript - 为什么我的折线图过渡看起来滞后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51593943/

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