admin管理员组文章数量:1332359
I have a C3.js chart with time values formatted per minute as %H:%M. Now I want to load new data, but instead of data per hour I want to show the data per day as %d-%m. So the tick format needs to change, but how do I do this?
chart generation with 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'
}
}
}
});
Loading new data
chart.load({
x: "keys",
xFormat: '%Y-%m-%d %H:%M:%S',
url: url,
mimeType: 'json',
});
I have a C3.js chart with time values formatted per minute as %H:%M. Now I want to load new data, but instead of data per hour I want to show the data per day as %d-%m. So the tick format needs to change, but how do I do this?
chart generation with 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'
}
}
}
});
Loading new data
chart.load({
x: "keys",
xFormat: '%Y-%m-%d %H:%M:%S',
url: url,
mimeType: 'json',
});
Share
Improve this question
asked Dec 18, 2014 at 15:10
RoywRoyw
5995 silver badges26 bronze badges
1 Answer
Reset to default 7Latest from Masayuki on Github - said there was no API for that.
But you can define a global variable to change ticks dynamically, see Masayuki's fiddle
From Github -
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 es 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();
});
本文标签: javascriptC3js change axis time format on loadStack Overflow
版权声明:本文标题:javascript - C3js change axis time format on load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742318089a2452221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论