我目前正在开发一个分析仪表板,我选择了Chartjs来可视化图形,除了我似乎不知道如何动态地设置chartjs time对象的MIN和MAX日期外,一切都很好。它被硬编码如下:
const config = {
type: "bar",
data: {
datasets: [
{
label: "base",
data: monthly_data,
backgroundColor: ["rgba(255, 99, 132, 0.2)"],
borderColor: ["rgba(255, 99, 132, 1)"],
borderWidth: 1,
minBarLength: 7,
parsing: {
xAxisKey: "x",
yAxisKey: "base",
},
},
{
label: "Case",
data: monthly_data,
backgroundColor: ["rgba(54, 162, 235, 0.2)"],
borderColor: ["rgba(54, 162, 235, 1)"],
borderWidth: 1,
minBarLength: 7,
parsing: {
xAxisKey: "x",
yAxisKey: "case",
},
},
{
label: "Standard",
data: monthly_data,
backgroundColor: ["rgba(255, 206, 86, 0.2)"],
borderColor: ["rgba(255, 206, 86, 1)"],
borderWidth: 1,
minBarLength: 7,
parsing: {
xAxisKey: "x",
yAxisKey: "standard",
},
},
],
},
options: {
scales: {
x: {
type: "time",
min: "2021-03-01",
max: "2022-03-31",
time: {
unit: "month",
},
},
},
},
};我是否可以调用一个函数来动态地设置这个值,例如,在一个月视图中,图表应该在12个月前显示?
发布于 2022-06-27 18:37:57
您只需将一个函数传递给min和max,就可以这样计算出min和max:
options: {
scales: {
x: {
type: 'time',
min: () => {
return calculateMin()
}
}
}
}https://stackoverflow.com/questions/72776735
复制相似问题