I am new to C3.js and trying to create a dynamic timeseries chart, please run the code in fiddle http://jsfiddle.net/roytirthadeep/54v7r0ab/
var chart = c3.generate({
data: {
x: 'x',
xFormat: '%Y-%m-%dT%H:%M:%S',
columns: []
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%H:%M:%S',
}
}
},
legend: {
position: 'right'
}
});
//"2013-01-01T00:00:01"
var timeInc = 1;
var value = 1;
var interval = setInterval(function () {
value = value + 1;
timeInc++;
var str;
if (timeInc > 59) {
clearInterval(interval);
return;
}
if (timeInc >= 10) {
str = ''+timeInc;
} else {
str = '0'+timeInc;
}
xValue = "2013-01-01T00:00:"+str;
if (value)
if (value < 7) {
console.log("xValue",xValue);
console.log("value",value);
chart.flow({
columns: [
['x', xValue],
['data3', value]
],
length:0
});
} else {
chart.flow({
columns: [
['x', xValue],
['data3', value],
['data4', value*2],
['data5', value/2],
['data6', value-1]
],
length:0
});
}
}, 1000);
Q1. Is this the right way to achieve the desired behaivor in C3.js ?
Q2. How to achieve this http://www.highcharts.com/stock/demo/dynamic-update behaivor of highcharts (starting from a blank chart)
I got a solution and added it as an answer.
There is C3's flow api which does what you ask with less code, but annoyingly (especially after I wrote most of this answer) it has a bug where it doesn't remove points if the tab is hidden - https://github.com/c3js/c3/issues/1097
var chart = c3.generate({
data: {
x: 'x',
columns: [
['x', '2012-12-28', '2012-12-29', '2012-12-30', '2012-12-31'],
['data1', 100, 230, 300, 330],
['data2', 250, 190, 230, 200],
['data3', 120, 90, 130, 180],
]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%m/%d',
}
}
}
});
var date = new Date ("2013-01-01");
setInterval(function () {
chart.flow({
columns: [
['x', new Date (date)],
['data1', 200 + (Math.random() * 300)],
['data2', 200 + (Math.random() * 300)],
['data3', 200 + (Math.random() * 300)],
],
duration: 750,
});
date.setDate(date.getDate() + 1);
}, 2000);
http://jsfiddle.net/54v7r0ab/7/
Until that bug's fixed, I'd go with your answer as it doesn't exhibit the same problem.
EDIT: Possible fix. When using the flow api's done function, new data stops getting added to hidden tabs which avoids the 'pile-up' issue. However this is only useful if you don't want data constantly added when you're not looking :-), it wouldn't be a solution for a real-time graph.
http://jsfiddle.net/54v7r0ab/10/
var date = new Date ("2013-01-01");
var pushNew = function () {
chart.flow({
columns: [
['x', new Date (date)],
['data1', 200 + (Math.random() * 300)],
['data2', 200 + (Math.random() * 300)],
['data3', 200 + (Math.random() * 300)],
],
duration: 750,
done: function () {
date.setDate(date.getDate() + 1);
window.setTimeout (pushNew, 1);
}
});
}
pushNew();
I got a solution to achieve the desired behavior like Highcharts, by resetting the x axis ranges and checking for threshold value.
This might be helpful to others http://jsfiddle.net/54v7r0ab/5/
var chart = c3.generate({
data: {
x: 'x',
columns: []
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%H:%M:%S',
}
}
},
legend: {
position: 'right'
}
});
var chartObj = {
"chart": chart,
"redrawArgs": {},
"truncThreshold": undefined
}
//"2013-01-01T00:00:01"
var date = 1463017666000;
var timeInc = 1000;
var value = 1;
var interval = setInterval(function () {
var dataCols = [];
date = date + timeInc;
var minX = date - 10000;
var maxX = date;
var redrawArgs = chartObj.redrawArgs;
if (!chartObj.truncThreshold) {
chartObj.truncThreshold = maxX;
} else if (minX > chartObj.truncThreshold) {
redrawArgs.length = 1;
} else {
redrawArgs.length = 0;
}
chartObj.chart.axis.range({max: {x: maxX}, min: {x: minX}});
value++;
redrawArgs.duration = 0;
if (value < 7) {
dataCols.push(['x', date]);
dataCols.push(['data 1', value]);
redrawArgs.columns = dataCols;
} else {
dataCols.push(['x', date]);
dataCols.push(['data 1', value]);
dataCols.push(['data 2', value*Math.random()])
dataCols.push(['data 3', value/Math.random()]);
dataCols.push(['data 4', value+Math.random()]);
redrawArgs.columns = dataCols;
}
chartObj.chart.flow(redrawArgs);
}, 1000);