我有一个 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'
}
}
}
});
加载新数据
chart.load({
x: "keys",
xFormat: '%Y-%m-%d %H:%M:%S',
url: url,
mimeType: 'json',
});
最佳答案
最新来自 Masayuki on Github -说没有 API。
但是你可以定义一个全局变量来动态改变刻度,see Masayuki's fiddle
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();
});
关于javascript - C3js 在加载时更改轴时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27549681/