Roy*_*oyw 1 javascript d3.js c3.js
我有一个C3.js图表,每分钟格式化的时间值为%H:%M.现在我想加载新数据,但是我想每小时显示数据,而不是每小时数据,以%d-%m显示.因此刻度格式需要改变,但我该怎么做?
用C3.js生成图表
chart = c3.generate({
bindto: element,
data: {
x: "keys",
xFormat: '%Y-%m-%d %H:%M:%S',
url: dataurl + '/today',
mimeType: 'json',
type: 'bar',
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%H:%M'
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
加载新数据
chart.load({
x: "keys",
xFormat: '%Y-%m-%d %H:%M:%S',
url: url,
mimeType: 'json',
});
Run Code Online (Sandbox Code Playgroud)
来自Github的Masayuki的最新消息-表示没有API.
但是你可以定义一个全局变量来动态地改变滴答声,参见Masayuki的小提琴
var formatter;
function updateFormatter(byMonth) {
formatter = d3.time.format(byMonth ? '%Y-%m' : '%Y-%m-%d')
}
updateFormatter();
var chart = c3.generate({
data: {
x: 'date',
columns: [
['date', '2014-01-01', '2014-01-02', '2014-01-03'],
['data1', 100, 200, 300],
]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: function (x) { // x comes in as a time string.
return formatter(x);
}
}
}
}
});
d3.select('#btnByMonth').on('click', function () {
updateFormatter(true);
chart.flush();
});
d3.select('#btnByWeek').on('click', function () {
updateFormatter(false);
chart.flush();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5491 次 |
| 最近记录: |