【问题标题】:How to define chart.js time scale date formats如何定义 chart.js 时间刻度日期格式
【发布时间】:2017-03-14 09:01:06
【问题描述】:

我正在使用 chart.js,我想为我的数据设置自定义日期格式。我该怎么做?

例如,这是我的 x 轴日期配置:

 scales: {
                    xAxes: [{
                        type: 'time',
                        position: 'bottom',
                        time: {
                            displayFormats: {
                                **'day': 'Y M D',
                                'week': 'Y M D',
                                'month': 'Y M D',
                                'year': 'Y M D',**
                            }
                        },

如何控制工具提示和轴刻度标签格式。

【问题讨论】:

  • 您能更具体地说明您要达到的目标吗?
  • 在图表上有点,当我悬停每个点时,我会得到该点“由”组成的数据,例如:point (x: 02/05/2017, y: 70) ,当我在图表上看到它时,我得到了完整的日期,而不仅仅是 02/05/2017,我想编辑这些数据,我该怎么做?
  • 目前还不是很清楚你到底想做什么,但我的回答为你提供了各种选择的例子

标签: angularjs charts chart.js chart.js2


【解决方案1】:

如果您尝试格式化数据tbat出现在点悬停工具提示中,那么在使用时间刻度时,您可以使用tooltipFormat属性设置用于工具提示的时刻js格式字符串。

scales: {
  xAxes: [{
    type: 'time',
    position: 'bottom',
    time: {
      tooltipFormat: "MM-DD-YYYY",
    },
  }],
}

如果您尝试设置 x 轴刻度标签的格式,使其仅显示日期的日期部分,则可以使用 unit 属性。

scales: {
  xAxes: [{
    type: 'time',
    position: 'bottom',
    time: {
      unit: "day",
    },
  }],
}

以上示例使用为day 定义的默认显示格式,即'll'(例如,1986 年9 月4 日)。如果您想将此格式更改为其他格式(例如 09/04/1986),则可以像这样重新定义 day 显示格式。

scales: {
  xAxes: [{
    type: 'time',
    position: 'bottom',
    time: {
      unit: "day",
      displayFormats: {
        day: "l",
      },
    },
  }],
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-08-31
  • 2020-07-30
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多