图表JS-x轴的使用时间

彼得:

我添加了此代码

scales: 
{
        xAxes: 
        [{
            type: 'time',
            time: 
            {
                unit: 'month',
                displayFormats: { month: 'MM' },
                max: '2017-10-09 18:43:53',
                min: '2017-10-02 18:43:53'
            }
        }]
},

的选项,但它不起作用。有什么想法我做错了吗?

字段-> https://jsfiddle.net/o473y9pw/

ɢʀᴜɴᴛ:

由于您希望将时间用于x轴,因此您的labels数组应为日期/时间字符串labels数组(该数组与x轴相对应)。

您还需要设置parser属性(以正确解析日期/时间),并将x轴的刻度源设置(正确生成x轴的刻度)data

更新

如果只有最小和最大日期,则可以创建一个不错的小插件动态填充labels (date)数组,如下所示:

plugins: [{
   beforeInit: function(chart) {
      var time = chart.options.scales.xAxes[0].time, // 'time' object reference
         // difference (in days) between min and max date
         timeDiff = moment(time.max).diff(moment(time.min), 'd');
      // populate 'labels' array
      // (create a date string for each date between min and max, inclusive)
      for (i = 0; i <= timeDiff; i++) {
         var _label = moment(time.min).add(i, 'd').format('YYYY-MM-DD HH:mm:ss');
         chart.data.labels.push(_label);
      }
   }
}]

注意: moment.js用于简化计算。

ᴡᴏʀᴋɪɴɢᴇxᴀᴍᴘʟᴇ
(用于演示目的,我已经改变了时间unitday

$(document).ready(function() {

   new Chart(document.getElementById("chartBox"), {
      type: 'line',
      data: {
         datasets: [{
            data: [12, 19, 3, 5, 2, 3, 32, 15],
            label: "",
            borderWidth: 2,
            borderColor: "#3e95cd",
            fill: false,
            pointRadius: 0
         }]
      },
      options: {
         scales: {
            xAxes: [{
               type: 'time',
               time: {
                  parser: 'YYYY-MM-DD HH:mm:ss',
                  unit: 'day',
                  displayFormats: {
                     day: 'ddd'
                  },
                  min: '2017-10-02 18:43:53',
                  max: '2017-10-09 18:43:53'
               },
               ticks: {
                  source: 'data'
               }
            }]
         },
         legend: {
            display: false
         },
         animation: {
            duration: 0,
         },
         hover: {
            animationDuration: 0,
         },
         responsiveAnimationDuration: 0
      },
      plugins: [{
         beforeInit: function(chart) {
            var time = chart.options.scales.xAxes[0].time, // 'time' object reference
               timeDiff = moment(time.max).diff(moment(time.min), 'd'); // difference (in days) between min and max date
            // populate 'labels' array
            // (create a date string for each date between min and max, inclusive)
            for (i = 0; i <= timeDiff; i++) {
               var _label = moment(time.min).add(i, 'd').format('YYYY-MM-DD HH:mm:ss');
               chart.data.labels.push(_label);
            }
         }
      }]
   });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@latest/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>

<canvas id="chartBox"></canvas>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章