【问题标题】:chart.js version 3 - How to make an overlapping bar chart?chart.js 版本 3 - 如何制作重叠条形图?
【发布时间】:2022-01-18 20:28:14
【问题描述】:

是否可以使用 Chart.js 版本 3 创建这样的图表(下图)?

任何建议将不胜感激。

【问题讨论】:

    标签: javascript chart.js chart.js3


    【解决方案1】:

    您可以将 x 轴与内部条的较小条百分比堆叠在一起:

    const options = {
      type: 'bar',
      data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Points',
            data: [7, 11, 5, 8, 3, 7],
            backgroundColor: 'orange',
            barPercentage: 0.6
          },
          {
            label: '# of Votes',
            data: [12, 19, 8, 10, 5, 13],
            backgroundColor: 'pink'
          }
        ]
      },
      options: {
        scales: {
          x: {
            stacked: true
          }
        }
      }
    }
    
    const ctx = document.getElementById('chartJSContainer').getContext('2d');
    new Chart(ctx, options);
    <body>
      <canvas id="chartJSContainer" width="600" height="400"></canvas>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.2/chart.js"></script>
    </body>

    【讨论】:

      猜你喜欢
      • 2017-09-02
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2012-10-21
      • 1970-01-01
      • 2020-11-22
      相关资源
      最近更新 更多