我很新的D3,我試圖讓鬆散的基礎上的一個片段從這裏一個簡單的現場圖:https://bost.ocks.org/mike/path/D3過渡線發生兩次
我希望能夠多條線路添加到移動現場圖表,讓他們可以從網絡插口更新(我知道如何做到這一點位!)
當我嘗試添加第二行,圖表不會順利更新,我覺得過渡被稱爲兩次。任何幫助感激地收到。以下,但這裏
代碼是一個小提琴https://jsfiddle.net/q8qgbj58/
var n = 243;
var random = d3.randomNormal(0, .2);
var duration = 500;
var now = new Date(Date.now() - duration);
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.scaleTime()
.domain([now - (n - 2) * duration, now - duration])
.range([0, width]);
var y = d3.scaleLinear()
.domain([-1, 1])
.range([height, 0]);
var lineNames = [];
var lines = {};
var data = {};
var line = d3.line()
.curve(d3.curveBasis)
.x(function(d, i) { return x(now - (n - 1 - i) * duration); })
.y(function(d, i) { return y(d); });
g.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var axis = 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));
createLine("one");
createLine("two");
d3.selectAll(".line")
.transition()
.duration(duration)
.ease(d3.easeLinear)
.on("start", tick);
function createLine(name) {
lineNames.push(name);
data[name] = d3.range(n).map(random);
lines[name] = d3.line()
.curve(d3.curveBasis)
.x(function(d, i) { return x(now - (n - 1 - i) * duration); })
.y(function(d, i) { return y(d); });
g.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.datum(data[name])
.attr("class", "line");
}
function tick() {
var index;
var i;
for (i = 0; i < lineNames.length; ++i) {
index = lineNames[i];
// Push a new data point onto the back.
data[index].push(random());
// Redraw the line.
d3.select(this)
.attr("d", lines[index])
.attr("transform", null);
// Pop the old data point off the front.
data[index].shift();
}
now = new Date();
x.domain([now - (n - 2) * duration, now - duration]);
axis.transition()
.duration(duration)
.ease(d3.easeLinear)
.call(d3.axisBottom(x));
// Slide it to the left.
d3.active(this)
.attr("transform", "translate(" + x(now - (n - 1) * duration) + ")")
.transition()
.on("start", tick);
}
我初步意向爲蜱函數被調用兩次儘可能經常。我可能是錯的,但它看起來像函數被每行調用一次,但每次調用都影響到兩行。我會預計每條線都會打勾並影響該線。或者打勾被調用一次並影響兩行。 –
我已添加小提琴以顯示問題。 – SlimCheney