如何在chart.js中制作弯曲的条形图或弯曲的柱形图?

问题描述 投票:0回答:1

我们正在尝试创建类似于下图的弯曲柱,在chart.js中看起来不可能

Cured Barchart

对此有何建议?

javascript chart.js chart.js2 jscharts
1个回答
0
投票

您可以使用line图表执行此操作,如下面的代码示例所示,需要对其进行进一步改进以获得所需的结果。

new Chart(document.getElementById('myChart'), {
    type: 'line',
    data: {
        labels: [1, 2, 3, 4, 5, 6, 7],
        datasets: [
          {
              data: [0, 5, 0, 0, 0, 0, 0],
              fill: true,
              backgroundColor: 'rgb(255, 0, 0, 0.7)',
              backgroundColor: 'rgb(255, 0, 0, 0.7)',
              lineTension: 0.4,
              pointHitRadius: 0
          },
          {
              data: [0, 0, 9, 0, 0, 0, 0],
              fill: true,
              backgroundColor: 'rgb(255,165,0, 0.7)',
              backgroundColor: 'rgb(255,165,0, 0.7)',
              lineTension: 0.6,
              pointHitRadius: 0
          },
          {
              data: [0, 0, 0, 6, 0, 0, 0],
              fill: true,
              backgroundColor: 'rgb(255, 215, 0, 0.7)',
              backgroundColor: 'rgb(255, 215, 0, 0.7)',
              lineTension: 0.6,
              pointHitRadius: 0
          },
          {
              data: [0, 0, 0, 0, 5, 0, 0],
              fill: true,
              backgroundColor: 'rgb(59, 161, 151, 0.7)',
              backgroundColor: 'rgb(59, 161, 151, 0.7)',
              lineTension: 0.6,
              pointHitRadius: 0
          },
          {
              data: [0, 0, 0, 0, 0, 4, 0],
              fill: true,
              backgroundColor: 'rgb(100, 100, 100, 0.7)',
              backgroundColor: 'rgb(100, 100, 100, 0.7)',
              lineTension: 0.4,
              pointHitRadius: 0
          }
        ]
    },
    options: {
        responsive: true,
        elements: {
          point:{
            radius: 0
          }
        },
        title: {
            display: false,
        },
        legend: {
            display: false
        },
        tooltips: {
            display: false
        },
        scales: {
            yAxes: [{
               ticks: {
                 display: false
               },
               gridLines: {
                 display: false
               }
            }],
            xAxes: [{
               ticks: {
                 display: false
               },
               gridLines: {
                 display: false
               }
            }],
        }
    }
});
canvas {
  max-width: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="200"></canvas>
© www.soinside.com 2019 - 2024. All rights reserved.