你能用 Chart.js 在折线图中做区域吗?

Can you do regions in line charts with Chart.js?

我正在寻找生成具有 "regions" 的折线图(见下文)并且我一直在查看 chart.js 但我不确定它是否可以做到这一点,因为我没有看到它的任何例子。

这是我想要实现的一个例子:

我不关心不同的阴影,我说的是图表中从上到下的 green/red 背景颜色。

如果你可以用 chart.js 做到这一点,有人可以提供一个例子 and/or 引导我找到实现这个目标所需的文档吗?

您可以使用名为 - chartjs-plugin-annotation

的 ChartJS 插件实现此目的

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ

var ctx = c.getContext('2d');
var chart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
      datasets: [{
         label: 'Statistics',
         data: [3, 1, 2, 5, 4],
         backgroundColor: 'rgba(0, 119, 204, 0.2)',
         borderColor: 'rgba(0, 119, 204, 0.8)',
         borderWidth: 2,
         fill: false,
         lineTension: 0
      }]
   },
   options: {
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true,
               stepSize: 1
            }
         }]
      },
      annotation: {
         annotations: [{
            type: 'box',
            drawTime: 'beforeDatasetsDraw',
            id: 'region-1',
            xScaleID: 'x-axis-0',
            yScaleID: 'y-axis-0',
            xMin: 'Jan',
            xMax: 'May',
            yMin: 2.5,
            yMax: 4.5,
            backgroundColor: 'rgba(200,230,201,0.5)'
         }, {
            type: 'box',
            drawTime: 'beforeDatasetsDraw',
            id: 'region-2',
            xScaleID: 'x-axis-0',
            yScaleID: 'y-axis-0',
            xMin: 'Jan',
            xMax: 'May',
            yMin: 0,
            yMax: 2,
            backgroundColor: 'rgba(255,205,210 ,0.5)'
         }]
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<script src="https://cdn.rawgit.com/chartjs/chartjs-plugin-annotation/master/chartjs-plugin-annotation.min.js"></script>
<canvas id="c"></canvas>