gpt4 book ai didi

javascript - 动态时间序列 C3js 图表

转载 作者:行者123 更新时间:2023-11-30 08:31:33 25 4
gpt4 key购买 nike

我是 C3.js 的新手,正在尝试创建动态时间序列图表,请运行 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。这是在 C3.js 中实现所需行为的正确方法吗?

Q2。如何做到这一点 http://www.highcharts.com/stock/demo/dynamic-update highcharts 的行为(从空白图表开始)

我得到了一个解决方案并将其添加为答案。

最佳答案

我找到了一个解决方案来实现像 Highcharts 这样的预期行为,方法是重置 x 轴范围并检查阈值。这可能对其他人有帮助 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);

关于javascript - 动态时间序列 C3js 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37155966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com