Chart.js 使用日期创建折线图

新手上路,请多包涵

我似乎无法让 Chart.js 处理日期。我尝试了很多不同的方法:

 let examChart = document.getElementById("examChart").getContext("2d");
let examLineChart = new Chart(examChart, {
    type: "line",
    data: [
            { t: new Date("2015-3-15 13:3"), y: 12 },
            { t: new Date("2015-3-25 13:2"), y: 21 },
            { t: new Date("2015-4-25 14:12"), y: 32 }
    ],
    options: {
        label: "placeholder"
    }
});

和:

 let examChart = document.getElementById("examChart").getContext("2d");
let examLineChart = new Chart(examChart, {
    type: "line",
    data: [
            { t: "2015-3-15 13:3", y: 12 },
            { t: "2015-3-25 13:2", y: 21 },
            { t: "2015-4-25 14:12", y: 32 }
    ],
    options: {
        label: "placeholder"
    }
});

和:

 let examChart = document.getElementById("examChart").getContext("2d");
let examLineChart = new Chart(examChart, {
    type: "line",
    data: [
            { t: "Sun Mar 15 2015 12:03:45 GMT+0000 (GMT Standard Time)", y: 12 },
            { t: "Wed Mar 25 2015 12:02:15 GMT+0000 (GMT Standard Time)", y: 21 },
            { t: "Sat Apr 25 2015 13:12:30 GMT+0100 (GMT Daylight Time)", y: 32 }
    ],
    options: {
        label: "placeholder"
    }
});

为了仅涵盖我的一些尝试,即使阅读了文档( http://www.chartjs.org/docs/latest/axes/cartesian/time.html ),我似乎也无法正确设置日期(他们实际上并没有举个例子)

正在使用的 HTML:

 <div class="container">
    <canvas id="examChart"></canvas>
</div>

我只是不知道,虽然我认为这是一个相当简单的问题,但我们将不胜感激任何帮助。

原文由 Jonathan Woollett-light 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 436
2 个回答

您必须在 dataset 中移动数据。如果您查看 使用手册,则会使用 datasets 数组。文档中 时间 数据集的 “提示” 也不是那么明显(参见标题)。

请参阅下面的代码段:

我刚刚复制了基本用法示例并插入了您第一次尝试的数据(+ 添加了几个标签)

更新 18.03.2020

我已经更新了代码片段以使用 Chart.js 2.8.0 并添加了以下代码,正如 @Erik 在评论中所建议的那样

options: {
    scales: {
      xAxes: [{
        type: 'time'
      }]
    }
  }

更新 16.02.2021

正如 @habib 所指出的,2.9.4 版本抛出了几个错误。我猜这些是由于缺少依赖项 (moment.js) 造成的。看:

注意:从 v2.8 开始,Moment.js 是 Chart.js 和 Chart.min.js 的可选依赖项。为了在 Moment.js 中使用时间刻度,您需要确保在需要 Chart.js 之前完全加载 Moment.js。您可以使用垫片

资源

因此,我更改了以下内容:

  • 将 Chart.js 更新到 2.9.4
  • 添加 moment.js (2.29.1) 作为依赖项
  • 将时间格式调整为符合 ISO8601(根据 soiboi 的建议)
 var ctx = document.getElementById("examChart").getContext("2d");

var myChart = new Chart(ctx, {
  type: 'line',
  options: {
    scales: {
      xAxes: [{
        type: 'time',
      }]
    }
  },
  data: {
    labels: ["2015-03-15T13:03:00Z", "2015-03-25T13:02:00Z", "2015-04-25T14:12:00Z"],
    datasets: [{
      label: 'Demo',
      data: [{
          t: '2015-03-15T13:03:00Z',
          y: 12
        },
        {
          t: '2015-03-25T13:02:00Z',
          y: 21
        },
        {
          t: '2015-04-25T14:12:00Z',
          y: 32
        }
      ],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],
      borderColor: [
        'rgba(255,99,132,1)',
        'rgba(54, 162, 235, 1)',
        'rgba(255, 206, 86, 1)',
        'rgba(75, 192, 192, 1)',
        'rgba(153, 102, 255, 1)',
        'rgba(255, 159, 64, 1)'
      ],
      borderWidth: 1
    }]
  }
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js"></script>
<div class="container">
  <canvas id="examChart"></canvas>
</div>

原文由 Ramiz Wachtler 发布,翻译遵循 CC BY-SA 4.0 许可协议

基于@RamizWachtler 的回答,您可以添加到图表的选项部分以正确扩展时间。我会注意到这似乎不适用于 Charts.js 2.3。添加了一个工作片段,该片段使用截至 2019 年 4 月的最新 Charts.js 版本。

此外,我已将时间格式更改为符合 ISO8601 标准。

 var chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            xAxes: [{
                type: 'time',
                distribution: 'linear'
            }]
        }
    }
});

来源 - https://www.chartjs.org/docs/latest/axes/cartesian/time.html

 var ctx = document.getElementById("examChart").getContext("2d");

var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ["2015-03-15T13:03:00Z", "2015-03-25T13:02:00Z", "2015-04-25T14:12:00Z"],
    datasets: [{
      label: 'Demo',
      data: [{
          t: "2015-03-15T13:03:00Z",
          y: 12
        },
        {
          t: "2015-03-25T13:02:00Z",
          y: 21
        },
        {
          t: "2015-04-25T14:12:00Z",
          y: 32
        }
      ],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],
      borderColor: [
        'rgba(255,99,132,1)',
        'rgba(54, 162, 235, 1)',
        'rgba(255, 206, 86, 1)',
        'rgba(75, 192, 192, 1)',
        'rgba(153, 102, 255, 1)',
        'rgba(255, 159, 64, 1)'
      ],
      borderWidth: 1
    }]
  },
  options: {
    scales: {
      xAxes: [{
        type: 'time',
        distribution: 'linear'
      }]
    }
  }
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0-rc.1/Chart.bundle.js"></script>
<div class="container">
  <canvas id="examChart"></canvas>
</div>

原文由 SoiBoi 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏