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

时间:2017-12-18 19:19:21

标签: javascript date chart.js ejs

我似乎无法让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>

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

4 个答案:

答案 0 :(得分:8)

您必须在dataset内移动数据。如果您查看usage manual,则会使用datasets数组。 time文档中数据集的&#34;提示&#34; 也不那么明显(参见标题)。

请参阅以下代码段:

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

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

var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: [new Date("2015-3-15 13:3").toLocaleString(), new Date("2015-3-25 13:2").toLocaleString(), new Date("2015-4-25 14:12").toLocaleString()],
    datasets: [{
      label: 'Demo',
      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
        }
      ],
      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
    }]
  }
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.bundle.js"></script>
<div class="container">
  <canvas id="examChart"></canvas>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

在@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>

答案 2 :(得分:0)

我的建议是将moment.js库添加到您的项目中,并使用片刻格式化您的日期。另外我不确定在这里使用“t”和“y”。我将时间添加为标签并将其与x数据匹配:

var data = {
  labels: [moment().format("YYYY-MM-DD"), moment().format("YYYY-MM-DD"), moment().format("YYYY-MM-DD")],
  datasets: [
    {
        data: [12,21,32],
    }
  ]
};

var myBarChart = Chart.Line(canvas,{
data:data,
});

有一个小提琴示例here.

答案 3 :(得分:0)

使用时间时,必须使用UTC时间,因为会加上本地时间(澳大利亚+10小时/ 36000秒),因此每个国家/地区都有不同的时区。 关键是在任何时间转换中都使用moment.utc 0秒= moment.utc('1970-01-01 00:00:00')

使用 BEGIN TRY BEGIN TRAN delete // copy code DELETE * from table COMMIT TRAN delete END TRY BEGIN CATCH -- ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ CATCH EXCEPTION ROLLBACK TRAN delete END CATCH 添加一个值,然后 y轴值或工具提示项中的内容,您必须参考

moment.utc('1970-01-01').add(3600, 'seconds')

https://lopeys.com/ironman/IMAUS_Charts_StackedTimes.htm