我可以使用setTimeout
(在底部,在创建的事件上)使用chart.update.bind(chart)
.
谁能建议我可以将新数据传递到该图表对象的方法?也许使用原型方法?还是我应该创建一个全新的图表对象并重绘它?
最终,我只想要一个图表,它可以通过漂亮的动画自动更改数据,主要是出于审美目的。
var chart = new Chartist.Line('.ct-chart-main', {
labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
series: [
[1, 4, 5, 12, 4, 3, 2, 4, 5, 1]
]
}, {
low: 0,
showArea: true,
fullWidth: true,
width: '600px',
height: '250px',
lineSmooth: Chartist.Interpolation.cardinal({
tension: 1
})
});
chart.on('created', function() {
if(window.__exampleAnimateTimeout) {
clearTimeout(window.__exampleAnimateTimeout);
window.__exampleAnimateTimeout = null;
}
window.__exampleAnimateTimeout = setTimeout(chart.update.bind(chart), 5000);
});
chart.on('draw', function(data) {
if(data.type === 'point') {
var circle = new Chartist.Svg('circle', {
cx: [data.x], cy:[data.y], r:[5],
}, 'ct-circle');
data.element.replace(circle);
}
if(data.type === 'line' || data.type === 'area') {
data.element.animate({
d: {
begin: 2000 * data.index,
dur: 1000,
from: data.path.clone().scale(1, 0).translate(0, data.chartRect.height()).stringify(),
to: data.path.clone().stringify(),
easing: Chartist.Svg.Easing.easeOutQuint
}
});
}
});
chart.on('created', function() {
if(window.__exampleAnimateTimeout) {
clearTimeout(window.__exampleAnimateTimeout);
window.__exampleAnimateTimeout = null;
}
window.__exampleAnimateTimeout = setTimeout(chart.update.bind(chart), 5000);
});