【问题标题】:Chart JS not working with date图表 JS 不适用于日期
【发布时间】:2018-08-24 18:00:06
【问题描述】:

根据Chart JS documentation,它应该能够支持时间。我有一个现有的Chart JS jsfiddle,我将数据更改为

data: [{x:new Date(), y: "15.375"},
       {x:new Date(), y: "25.375"}]

但是当我再次运行它时,有nothing in the graph

【问题讨论】:

    标签: chart.js


    【解决方案1】:

    使用Time Cartesian AxisChart.js 时还需要moment.js 库; 检查http://www.chartjs.org/docs/latest/getting-started/installation.html#stand-alone-build

    var options = {
      type: 'scatter',
      data: {
        datasets: [{
          label: "Server 2",
          backgroundColor: "rgba(196, 93, 105, 0.3)",
          showLine: true,
          data: [{
              x: new Date(),
              y: "15.375"
            },
            {
              x: '2018-03-10',
              y: "25.375"
            }
          ]
        }]
      },
      options: {
        scales: {
          xAxes: [{
            type: 'time',
            time: {
              unit: 'day'
            }
          }],
          yAxes: [{}]
        }
      }
    }
    
    var ctx = document.getElementById('chartJSContainer').getContext('2d');
    new Chart(ctx, options);
    canvas { background-color : #eee;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.js"></script>
    
    
    <canvas id="chartJSContainer" width="600" height="400"></canvas>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      相关资源
      最近更新 更多