首页 > 解决方案 > 如何在 chart.js 中制作曲线条形图或曲线柱形图?

问题描述

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

固化条形图

对此有何建议?

标签: javascriptchart.jschart.js2jscharts

解决方案


您可以使用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>


推荐阅读