Chart.js - 设定时间序列 x 轴的起始周

9
使用ChartJS 2.4,我正在查询两个日期之间的一组记录:
      startDate: '2016-11-05T18:06:17.762Z'
      endDate: '2016-12-05T18:06:17.762Z'

给定比例配置,我想以用户选择的日期范围开始一周...
 isoWeekday: true,
 type: 'time',
 unitStepSize: 1,
 time: {
   displayFormats: {
     'week': 'MMM DD'
   },
   unit: 'week',
 },

这会导致我得到一个截断的图表:

enter image description here

因此,如果没有记录返回给开始日期11/5,我会手动在数组开头插入一个值为0的数据,以确保显示用户期望的开始日期:

  if (scope.chart_data.labels.indexOf('11/5') == -1) {
    scope.chart_data.labels.unshift('11/5');
    scope.chart_data.datasets[0].data.unshift(0);
  }

除了这样笨拙地将图表向后延长一周且没有数据外... 而且不会从11/5开始这个星期:

enter image description here

如何在我想要的任何一周开始图表,并使unit: week配置从那里递增?
如果删除unit: week,并使用unitStepSize: 7手动设置步骤大小,则在开头做我想做的事情,但由于某种原因压缩了最后一个标签:

enter image description here

使用time对象中的min max
   max: scope.end_display, // 12/05/2016
   min: scope.start_display // 11/05/2016

我得到:

输入图像描述

为什么它不会在11/5开始线形图?

标签:

["11/05/2016", "11/06/2016", "11/07/2016", "11/08/2016", "11/09/2016", "11/13/2016", "11/15/2016", "11/16/2016", "11/17/2016", "11/20/2016", "11/27/2016", "11/28/2016", "11/29/2016", "12/01/2016", "12/04/2016"]

数据:

[0, 3, 2, 1, 1, 7, 3, 2, 26, 2, 6, 3, 1, 1, 1]
3个回答

3

我知道我晚了很久,但是也许你应该尝试将 distribution 选项设置为 series。请参阅此处的文档:

Scale Distribution

The distribution property controls the data distribution along the scale:

'linear': data are spread according to their time (distances can vary)
'series': data are spread at the same distance from each other
这应该能解决你的奇怪格式问题,其中间隔都混乱了。

1

您需要修改轴设置以设置最大和最小值(可能是开始-结束日期范围)。以下是一个示例(注意!请使用您自己所需的格式):

time: {
  unit: 'month',
  displayFormats: {
     month: 'MM'
  },
  max: moment(data.end_date).format('MM'),
  min: monent(data.start_date).format('MM')
}

谢谢您的回复,但它导致了相同的问题。请参见上面的编辑。 - user3871
你说它不会在11/5启动,但是你提供的截图显示它可以?你能试着创建一个fiddle吗? - kristjan reinhold

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接