Y轴线使用D3闪烁
Y axis line is flickering using D3
我正在尝试创建一个 运行 折线图,它在其中动态显示变化以及 X 轴和 Y 轴以及线条本身。
我从基本的动画解释开始:
https://observablehq.com/@d3/learn-d3-animation?collection=@d3/learn-d3
并创建了我自己的(参见下面的代码和 link)
我的问题是,一旦我添加 await chartBug.update
,Y 轴就开始闪烁。
请参阅下面的 gif 动画:
我觉得需要await,等待动画完成再开始下一个动画,控制动画的流畅速度
虽然我在这里,但我还想问一下如何强制轴绘制开始和结束刻度。
感谢帮助
这是一个link to the page on www.observablehq.com
这是问题的动画 gif - 源代码如下:
chartBug = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const zx = x.copy(); // x, but with a new domain.
const zy = y.copy(); // x, but with a new domain.
const line = d3
.line()
.x(d => zx(d.date))
.y(d => y(d.close));
const path = svg
.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-miterlimit", 1)
.attr("d", line(data));
const gx = svg.append("g").call(xAxis, zx);
const gy = svg.append("g").call(yAxis, y);
return Object.assign(svg.node(), {
update(step) {
const partialData = [];
for (let i = 0; i < step; i++) {
partialData.push(data[i]);
}
const t = svg.transition().duration(50);
zx.domain(d3.extent(partialData, d => d.date));
gx.transition(t).call(xAxis, zx);
zy.domain([0, d3.max(partialData, d => d.close)]);
gy.transition(t).call(yAxis, zy);
path.transition(t).attr("d", line(data));
return t.end();
}
});
}
{
replay2;
for (let i = 0, n = data.length; i < n; ++i) {
await chartBug.update(i);
yield i;
}
}
问题似乎出在您的 yAxis 函数上,每次更新和重新缩放 yAxis 都会重新创建,域会重新绘制并删除,如果您不介意保留它,可以从中删除 call(g => g.select(".domain").remove()
。
转换完成后,您需要等待更新。我想这也回答了你的另一个问题
yAxis = (g, scale = y) => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(scale).ticks(height / 40))
我正在尝试创建一个 运行 折线图,它在其中动态显示变化以及 X 轴和 Y 轴以及线条本身。
我从基本的动画解释开始: https://observablehq.com/@d3/learn-d3-animation?collection=@d3/learn-d3
并创建了我自己的(参见下面的代码和 link)
我的问题是,一旦我添加 await chartBug.update
,Y 轴就开始闪烁。
请参阅下面的 gif 动画:
我觉得需要await,等待动画完成再开始下一个动画,控制动画的流畅速度
虽然我在这里,但我还想问一下如何强制轴绘制开始和结束刻度。
感谢帮助
这是一个link to the page on www.observablehq.com
这是问题的动画 gif - 源代码如下:
chartBug = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const zx = x.copy(); // x, but with a new domain.
const zy = y.copy(); // x, but with a new domain.
const line = d3
.line()
.x(d => zx(d.date))
.y(d => y(d.close));
const path = svg
.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-miterlimit", 1)
.attr("d", line(data));
const gx = svg.append("g").call(xAxis, zx);
const gy = svg.append("g").call(yAxis, y);
return Object.assign(svg.node(), {
update(step) {
const partialData = [];
for (let i = 0; i < step; i++) {
partialData.push(data[i]);
}
const t = svg.transition().duration(50);
zx.domain(d3.extent(partialData, d => d.date));
gx.transition(t).call(xAxis, zx);
zy.domain([0, d3.max(partialData, d => d.close)]);
gy.transition(t).call(yAxis, zy);
path.transition(t).attr("d", line(data));
return t.end();
}
});
}
{
replay2;
for (let i = 0, n = data.length; i < n; ++i) {
await chartBug.update(i);
yield i;
}
}
问题似乎出在您的 yAxis 函数上,每次更新和重新缩放 yAxis 都会重新创建,域会重新绘制并删除,如果您不介意保留它,可以从中删除 call(g => g.select(".domain").remove()
。
转换完成后,您需要等待更新。我想这也回答了你的另一个问题
yAxis = (g, scale = y) => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(scale).ticks(height / 40))