Chartjs-如何获得最近7天的X轴标签? [英] Chartjs - How to get last 7 days on x-axis labels?

查看:56
本文介绍了Chartjs-如何获得最近7天的X轴标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在折线图的x轴上获取最近的7天(使用chartjs).最好的方法是什么?

I am trying to get the last seven days on the x-axis of my line-chart (using chartjs). What is the best way to do this?

谢谢

推荐答案

您可以使用以下代码实例化过去7天的图表:

You can instantiate a chart for the last seven days with the following code:

let start = new Date(),
  end = new Date();

start.setDate(start.getDate() - 7); // set to 'now' minus 7 days.
start.setHours(0, 0, 0, 0); // set to midnight.

new Chart(document.getElementById("chart"), {
  type: "line",
  options: {
    scales: {
      xAxes: [{
        type: "time",
        time: {
          min: start,
          max: end,
          unit: "day"
        }
      }]
    }
  }
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script>
<canvas id="chart"></canvas>

由于您需要将值提供为 x (或 t )& y 属性,如指定在文档中.

You'll need to provide your values as x (or t) & y properties, as specified in the documentation.

这篇关于Chartjs-如何获得最近7天的X轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆