admin管理员组文章数量:1355532
Concerning this jsfiddle which uses chartjs v3.9.1 and the date-fns adapter:
/
I'm trying to format the tick labels in the function
tick_label
but I'm only getting a string as input. Is there a way to get the full Date object so I can include the month and day-of-month in the tick label if I want?Is there a way to specify which times to use for tick lines along the time axis?
Here is the code from the fiddle:
var chart1
function tick_label(xvalue) {
console.log("xvalue:", xvalue, "type:", typeof(xvalue))
return xvalue
}
function doit() {
let t0 = 1742662800*1000; // Sat Mar 22 12:00:00 CDT 2025
let N = 31
data = [...Array(N)].map((a,i) =>
({ 'x': new Date(i*3600000+t0),
'y': ~~(Math.random()*40)
}))
config = {
type: "scatter",
data: { datasets: [{ label: "Random Values", data: data }] },
options: {
scales: {
x: { type: 'time', ticks: { callback: tick_label, color: 'red' } }
}
}
}
chart1 = new Chart(document.getElementById('chart1'), config);
}
doit()
Concerning this jsfiddle which uses chartjs v3.9.1 and the date-fns adapter:
https://jsfiddle/0a6c1ty5/22/
I'm trying to format the tick labels in the function
tick_label
but I'm only getting a string as input. Is there a way to get the full Date object so I can include the month and day-of-month in the tick label if I want?Is there a way to specify which times to use for tick lines along the time axis?
Here is the code from the fiddle:
var chart1
function tick_label(xvalue) {
console.log("xvalue:", xvalue, "type:", typeof(xvalue))
return xvalue
}
function doit() {
let t0 = 1742662800*1000; // Sat Mar 22 12:00:00 CDT 2025
let N = 31
data = [...Array(N)].map((a,i) =>
({ 'x': new Date(i*3600000+t0),
'y': ~~(Math.random()*40)
}))
config = {
type: "scatter",
data: { datasets: [{ label: "Random Values", data: data }] },
options: {
scales: {
x: { type: 'time', ticks: { callback: tick_label, color: 'red' } }
}
}
}
chart1 = new Chart(document.getElementById('chart1'), config);
}
doit()
Share
Improve this question
asked Mar 29 at 19:32
ErikRErikR
52.1k9 gold badges77 silver badges136 bronze badges
1
- 1 For question 2, see stackoverflow/questions/61984352/… – James Commented Mar 29 at 20:42
1 Answer
Reset to default 1The tick_label function is additionally passed the index and the ticks array (docs). You can get the numeric value from there.
function tick_label(xvalue, i, ticks) {
let numeric = ticks[i].value;
let date = new Date(numeric);
console.log("xvalue:", xvalue, "type:", typeof(xvalue), "numeric:", numeric, "date:", date);
return xvalue;
}
本文标签: javascriptControlling tick marks and lines in chartjs time scatter plotStack Overflow
版权声明:本文标题:javascript - Controlling tick marks and lines in chart.js time scatter plot - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744005475a2574622.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论