首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用日期-fn适配器在CHARTJS中设置动态min和最大日期

使用日期-fn适配器在CHARTJS中设置动态min和最大日期
EN

Stack Overflow用户
提问于 2022-06-27 18:19:32
回答 1查看 133关注 0票数 2

我目前正在开发一个分析仪表板,我选择了Chartjs来可视化图形,除了我似乎不知道如何动态地设置chartjs time对象的MIN和MAX日期外,一切都很好。它被硬编码如下:

代码语言:javascript
运行
复制
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个月前显示?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-27 18:37:57

您只需将一个函数传递给min和max,就可以这样计算出min和max:

代码语言:javascript
运行
复制
options: {
  scales: {
    x: {
      type: 'time',
      min: () => {
        return calculateMin()
      }
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72776735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档