【问题标题】:Time graphs chart.js时间图表chart.js
【发布时间】:2023-03-19 09:00:01
【问题描述】:

我正在学习如何使用 chart.js,我想要一个显示一天中不同时间(x 轴)的随机值的图表,格式为“h?h:mm”。

在 x 轴上,我想要一小时的固定步长,从上午 12 点 (0:00) 开始,到上午 8 点 (8:00) 结束。

而数据,例如 (x = 4:45, y = 67) 在 te 对应的 x 刻度(4:00 和 5:00 之间的 3/4)

我尝试了以下方法:

var cfg = {
  type: 'line',
  data: {
    labels: ['00:00', '01:35', '02:20', '03:05', '04:07', '04:57', '05:25', '06:08', '07:10'],
    datasets: [{
      data: [
        randomScalingFactor(18, 45),
        randomScalingFactor(18, 45),
        randomScalingFactor(18, 45),
        randomScalingFactor(18, 45),
        randomScalingFactor(18, 45),
        randomScalingFactor(18, 45),
        randomScalingFactor(18, 45),
        randomScalingFactor(18, 45),
        randomScalingFactor(18, 45)
      ],
      label: "Improved ETA",
      borderColor: "#e67e22",
      borderWidth: 2,
      fill: false,
      lineTension: 0
    }]
  },
  options: {
    responsive: true,
    maintainAspectRatio: false,
    title: {
      display: true,
      text: 'Improved ETA',
      fontSize: 14,
      fontFamily: 'Arial',
      fontColor: '#000'
    },
    legend: {
      display: false
    },
    scales: {
      xAxes: [{
        ticks: {
          padding: 12
        },
        gridLines: {
          color: 'rgba(0, 0, 0, 0.06)',
          zeroLineColor: 'rgba(0, 0, 0, 0.06)',
          drawTicks: false
        }
      }],
      yAxes: [{
        ticks: {
          suggestedMin: 0,
          suggestedMax: 80,
          padding: 12
        },
        scaleLabel: {
          display: true,
          labelString: 'mins'
        },
        gridLines: {
          color: 'rgba(0, 0, 0, 0.06)',
          zeroLineColor: 'rgba(0, 0, 0, 0.06)',
          drawTicks: false
        }
      }]
    }
  }
};

window.onload = function() {
  var ctx = document.getElementById('foo').getContext('2d');
  window.myLine = new Chart(ctx, cfg);
};
<div style="width: 600px; height: 400px;">
  <canvas id="foo"></canvas>
</div>

但这不是我想要的,因为 X 轴刻度采用标签的值(x 数据)。更糟糕的是,刻度之间的间隔始终相同(例如,从 10:30 到 10:40 与 8:00 到 9:00 之间的间隔相同)。

PD:由于时间图,我还阅读了有关使用 moment.js chart.js bundle 的信息:https://www.chartjs.org/samples/latest/scales/time/line.html

【问题讨论】:

    标签: javascript charts chart.js


    【解决方案1】:

    首先您应该省略labels,但将您的数据定义为individual points,其中每个元素都有一个t 和一个y 属性。

    data: [
      { t: '4:45', y: 67 },
      { t: '6:12', y: 45 },
      { t: '7:33', y: 56 },
    ],
    

    根据以上数据,xAxes.time 选项必须定义如下:

    time: {
      parser: 'H:mm',
      unit: 'hour',
      stepSize: 1,
      displayFormats: {
        hour: 'H:mm'
      },
      tooltipFormat: 'H:mm'
    },
    

    您还可以按如下方式定义xAxis.ticks 选项:

    ticks: {
      min: '0:00', 
      max: '8:00' 
    }
    

    您已经注意到,Chart.js 在内部使用Moment.js 来实现time axis 的功能。因此,您应该使用在单个文件中包含 Moment.js 的 Chart.js 的 bundled version

    请看看下面的可运行代码,看看它是如何工作的。

    new Chart('myChart', {
      type: 'line',
      data: {
        datasets: [{
          label: 'My Dataset',      
          data: [
            { t: '4:45', y: 67 },
            { t: '6:12', y: 45 },
            { t: '7:33', y: 56 },
          ],
          borderColor: 'blue',
          fill: 'false',
        }]
      },
      options: {
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true
            }
          }],
          xAxes: [{
            type: 'time',
            time: {
              parser: 'H:mm',
              unit: 'hour',
              stepSize: 1,
              displayFormats: {
                hour: 'H:mm'
              },
              tooltipFormat: 'H:mm'
            },
            ticks: {
              min: '0:00', 
              max: '8:00' 
            }
          }]      
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.min.js"></script>
    <canvas id="myChart" height="90"></canvas>

    【讨论】:

      猜你喜欢
      • 2022-06-12
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      相关资源
      最近更新 更多