我正在尝试使用nvd3.js创建一个实时图形,该图形将定期更新,并给人以数据在实时处理的印象。
目前,我已经能够创建一个会定期更新图形的函数,但是我无法使“状态”之间的过渡平滑,例如线条向左进行过渡。
这里是我使用nvd3.js所做的内容,这里有有趣的代码:
d3.select('#chart svg')
.datum(data)
.transition().duration(duration)
.call(chart);
现在,我已经能够使用d3.js制作我想要的内容,但我更希望能够使用nvd3.js提供的所有工具。这里是我希望使用nvd3制作的内容。
使用d3.js进行转换的有趣代码是:
function tick() {
// update the domains
now = new Date();
x.domain([now - (n - 2) * duration, now - duration]);
y.domain([0, d3.max(data)]);
// push the accumulated count onto the back, and reset the count
data.push(Math.random()*10);
count = 0;
// redraw the line
svg.select(".line")
.attr("d", line)
.attr("transform", null);
// slide the x-axis left
axis.transition()
.duration(duration)
.ease("linear")
.call(x.axis);
// slide the line left
path.transition()
.duration(duration)
.ease("linear")
.attr("transform", "translate(" + x(now - (n - 1) * duration) + ")")
.each("end", tick);
// pop the old data point off the front
data.shift();
}