Chart.js时间间隔(条形图)

ecfdbz9o  于 2022-11-07  发布在  Chart.js
关注(0)|答案(1)|浏览(209)

js是否可以建立一个时间间隔?就我而言,使用堆叠条形图是我能得到的最接近的方法,但并不是我想要的
我想达到的是this时间间隔
这是密码

const data = {
      labels: ['Mon', 'Tue', 'Wed' ],
      datasets: [{
        label: 'Bars',
        data: [
            [14, 16],
            [17, 22],
            [13, 19],
        ],
        backgroundColor: [
          'rgba(255, 26, 104, 0.2)',
        ],
        borderColor: [
          'rgba(255, 26, 104, 1)',
        ],
        borderWidth: 2,
        borderSkipped: false
      }]
    };

    const config = {
      type: 'bar',
      data,
      options: {
        indexAxis: 'y',
        scales: {
          x: {
            min: 12,
            max: 26,
            ticks: {
                stepSize: 1,
                callback: (val, index) => {
                    return `${String(val % 24).padStart(2, '0')}:00`
                }
            },
          },
          y: {
            beginAtZero: true
          }
        }
      }
    };

    const myChart = new Chart(
      document.getElementById('myChart'),
      config
    );
gab6jxml

gab6jxml1#

您可以使用horizontalBar作为条形图类型。例如:

{
  type: 'horizontalBar',
  data: {
  labels: ["a","b","c","d","e","f","g"],
  datasets: [{
    axis: 'y',
    label: 'My First Dataset',
    data: [[5,65], [30,90], [20,80], [15,71], [20,56], [24,55], [15,70]],
    fill: false,
    backgroundColor: [
      'rgba(255, 99, 132, 0.2)',
      'rgba(255, 159, 64, 0.2)',
      'rgba(255, 205, 86, 0.2)',
      'rgba(75, 192, 192, 0.2)',
      'rgba(54, 162, 235, 0.2)',
      'rgba(153, 102, 255, 0.2)',
      'rgba(201, 203, 207, 0.2)'
    ],
    borderColor: [
      'rgb(255, 99, 132)',
      'rgb(255, 159, 64)',
      'rgb(255, 205, 86)',
      'rgb(75, 192, 192)',
      'rgb(54, 162, 235)',
      'rgb(153, 102, 255)',
      'rgb(201, 203, 207)'
    ],
    borderWidth: 1
  }]
},
  options: {
    indexAxis: 'y',
     scales: {
            x: {
                stacked: true
            },
            y: {
                stacked: true
            }
        }
  }
}

这里是result
我希望能帮到你。

相关问题