我正在修改path transition(code)上第二次可视化的代码,以向路径的末尾添加一个圆,以便与转换过渡一起,圆的“ cy”可以也可以过渡。
但是,我无法获得圆滑的过渡,而是出现了生涩的行为。我知道是因为tick
函数每500毫秒被调用一次。我无法理解在转换过渡期间如何更新圆的“ cy”属性。有人可以帮我实现这一目标。
代码:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
}
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var n = 40,
random = d3.randomNormal(0, .2),
data = d3.range(n).map(random);
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 20, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleLinear()
.domain([0, n - 1])
.range([0, width]);
var y = d3.scaleLinear()
.domain([-1, 1])
.range([height, 0]);
var line = d3.line()
.x(function(d, i) { return x(i); })
.y(function(d, i) { return y(d); });
g.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y(0) + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y));
///////////////Modified Code//////////////////
var circle = g.append("circle")
.attr("r", 6)
.attr("cx", x(n-1));
var path = g.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.datum(data)
.attr("class", "line")
.transition()
.duration(500)
.ease(d3.easeLinear)
.on("start", tick);
//////////////////////////////////////////////
function tick() {
data.push(random());
d3.select(this)
.attr("d", line)
.attr("transform", null);
///////////////Modified Code//////////////////
circle.transition()
.attr("cy", getPoint(path).y)
//////////////////////////////////////////////
d3.active(this)
.attr("transform", "translate(" + x(-1) + ",0)")
.transition()
.on("start", tick);
data.shift();
}
///////////////Modified Code//////////////////
function getPoint(path){
var length = path.node().getTotalLength();
return path.node().getPointAtLength(length)
}
//////////////////////////////////////////////
</script>
答案 0 :(得分:3)
如果匹配线路的难易程度和持续时间,则可以使其平滑:
circle.transition()
.duration(500)
.ease(d3.easeLinear)
.attr("cy", getPoint(path).y);
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
}
</style>
<svg width="500" height="500"></svg>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var n = 40,
random = d3.randomNormal(0, .2),
data = d3.range(n).map(random);
var svg = d3.select("svg"),
margin = {
top: 20,
right: 20,
bottom: 20,
left: 40
},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleLinear()
.domain([0, n - 1])
.range([0, width]);
var y = d3.scaleLinear()
.domain([-1, 1])
.range([height, 0]);
var line = d3.line()
.x(function(d, i) {
return x(i);
})
.y(function(d, i) {
return y(d);
});
g.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y(0) + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y));
///////////////Modified Code//////////////////
var circle = g.append("circle")
.attr("r", 6)
.attr("cx", x(n - 1));
var path = g.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.datum(data)
.attr("class", "line")
.transition()
.duration(500)
.ease(d3.easeLinear)
.on("start", tick);
//////////////////////////////////////////////
function tick() {
data.push(random());
d3.select(this)
.attr("d", line)
.attr("transform", null);
///////////////Modified Code//////////////////
circle.transition()
.duration(500)
.ease(d3.easeLinear)
.attr("cy", getPoint(path).y)
//////////////////////////////////////////////
d3.active(this)
.attr("transform", "translate(" + x(-1) + ",0)")
.transition()
.on("start", tick);
data.shift();
}
///////////////Modified Code//////////////////
function getPoint(path) {
var length = path.node().getTotalLength();
return path.node().getPointAtLength(length)
}
//////////////////////////////////////////////
</script>