在x轴上显示所有标签在图表中。
我是Chart.js的新手,我一直在寻找一种在图表中显示所有标签的方法,无论标签是否有数据,我都尝试了最大和最小值,但它不起作用,所以我遇到了壁虱回调:
let ctx = document.getElementById("chart").getContext("2d");
let config = {
type: "bar",
data: {
datasets: [
{
label: "cost of goods sold",
data: [0, 20, 40, 50],
},
],
//labels: ["January", "February", "March", "April"],
},
options: {
scales: {
x: {
ticks: {
callback: (value, index, ticks) => {
const x = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"october",
"November",
"December",
];
return x;
},
},
},
},
},
};
let chart = new Chart(ctx, config);
但是它没有起作用。 有没有办法显示所有固定标签,例如始终显示12个月,31天,12周
I am new to Chart.js , I have been looking for a way to display all labels in chart.js regardless of whether there is a data for the label or not, I have tried the max and min but it does not work so I came across the ticks callback:
let ctx = document.getElementById("chart").getContext("2d");
let config = {
type: "bar",
data: {
datasets: [
{
label: "cost of goods sold",
data: [0, 20, 40, 50],
},
],
//labels: ["January", "February", "March", "April"],
},
options: {
scales: {
x: {
ticks: {
callback: (value, index, ticks) => {
const x = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"october",
"November",
"December",
];
return x;
},
},
},
},
},
};
let chart = new Chart(ctx, config);
but it didnt work.
is there a way to all display a fixed label like always show 12 month, 31 days, 12 weeks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以定义
data.labels
,并确保data.datasets.data
具有相同数量的条目,null
对于缺失值。You can define
data.labels
and make sure thatdata.datasets.data
has the same number of entries,null
for missing values.