我的数据数组在chart.js中有空值,是否有办法在两个段之间画一条线以保持可视进度?

oknwwptz  于 2023-03-18  发布在  Chart.js
关注(0)|答案(2)|浏览(122)

如屏幕截图所示,有些天的值为空......我想用一条线连接数据集,而不管某个特定的天是否为空。这在chart.js 2.x中可能吗?

jogvjijk

jogvjijk1#

您可以将spangap设置为true:

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, null, 2, 3],
        borderColor: 'pink',
        fill: false,
        spanGaps: true
      },
      {
        label: '# of Points',
        data: [7, null, null, 8, 3, 7],
        borderColor: 'orange',
        fill: false,
        spanGaps: true
      }
    ]
  },
  options: {}
}

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/2.9.4/Chart.js"></script>
</body>
xcitsw88

xcitsw882#

您应该将spanGaps: true更改为spanGaps: false
https://www.chartjs.org/docs/latest/charts/line.html#line-styling

相关问题