如何为折线图设置Chart.js网格颜色

4

我想使用选项字段更改线图的网格颜色,但我不知道从哪里开始。首先尝试使用渐变更改画布背景颜色,但结果不理想。

canvas{
background:linear-gradient(top, #ea1a07 0%, #f4b841 75%,#f2dd43 100%);
}

然而,我没有得到我想要的东西,因为如上图所示,不仅网格被着色,而且x值、y标签和图例也被着色了。
使用此CSS代码获得的结果图片如下: Picture of the result I'm getting with this css code 我想要的样子如下所示: Example of what I want to get 我的chart.js选项如下:
options = {
        scales: {
          xAxes: [{
            gridLines: {
              color: 'rgba(171,171,171,1)',
              lineWidth: 1
            }
          }],
          yAxes: [{
            ticks: {
              beginAtZero: true,
              max: 100,
              min: 0,
              stepSize: 10
            },
            gridLines: {
              color: 'rgba(171,171,171,1)',
              lineWidth: 0.5
            }
          }]
        },
        responsive: true
      };

那么,有没有一种方法可以将网格的背景设置为三种不同的颜色(带有渐变或不带渐变)?注意:我正在使用带有Angular 2(ng2-charts)的chart.js。


@PauloCoghi 我的问题涉及背景颜色,而提供的链接指向一个关于更改网格线颜色的问题。 - PhiloJunkie
1
你是对的!移除重复标志。 :) - Paulo Coghi
1个回答

7
最简单的方法是使用chartjs-plugin-annotation插件,并配置3个框注释绑定到您的Y轴(每个框都有不同的颜色)。
下面是一个示例(您可以通过此codepen查看它的实际效果)。
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    datasets: [{
      label: 'Points',
      data: [
        {x: 0, y: 2},
        {x: 1, y: 3}, 
        {x: 2, y: 2},
        {x: 1.02, y: 0.4},
        {x: 0, y: -1}
      ],
      backgroundColor: 'rgba(123, 83, 252, 0.8)',
      borderColor: 'rgba(33, 232, 234, 1)',
      borderWidth: 1,
      fill: false,
    }],
  },
  options: {
    title: {
      display: true,
      text: 'Chart.js - Gridline Background',
    },
    scales: {
      xAxes: [{
        type: 'linear',
        position: 'bottom',
        ticks: {
          min: -1,
          max: 8,
          stepSize: 1,
          fixedStepSize: 1,
        },
        gridLines: {
          color: 'rgba(171,171,171,1)',
          lineWidth: 1
        }
      }],
      yAxes: [{
        afterUpdate: function(scaleInstance) {
          console.dir(scaleInstance);
        },
        ticks: {
          min: -2,
          max: 4,
          stepSize: 1,
          fixedStepSize: 1,
        },
        gridLines: {
          color: 'rgba(171,171,171,1)',
          lineWidth: 0.5
        }
      }]
    },
    annotation: {
      annotations: [{
        type: 'box',
        yScaleID: 'y-axis-0',
        yMin:  1,
        yMax: 4,
        borderColor: 'rgba(255, 51, 51, 0.25)',
        borderWidth: 2,
        backgroundColor: 'rgba(255, 51, 51, 0.25)',
      }, {
        type: 'box',
        yScaleID: 'y-axis-0',
        yMin:  -1,
        yMax: 1,
        borderColor: 'rgba(255, 255, 0, 0.25)',
        borderWidth: 1,
        backgroundColor: 'rgba(255, 255, 0, 0.25)',
      }, {
        type: 'box',
        yScaleID: 'y-axis-0',
        yMin:  -2,
        yMax: -1,
        borderColor: 'rgba(0, 204, 0, 0.25)',
        borderWidth: 1,
        backgroundColor: 'rgba(0, 204, 0, 0.25)',
      }],
    }
  }
});

重要的是要注意注释是绘制在图表的顶部,因此您的注释颜色需要包含一些透明度才能看到其后面的内容。
使用此插件与ng2-charts没有问题。只需将源代码添加到应用程序的html文件中的<script>中,并将annotation属性添加到您的options对象中即可。这是一个Angular2 / ng2-charts示例,演示了如何使用注释插件。example

感谢您的提议,但正如我所提到的,我正在使用Angular 2,它是使用TypeScript而不是JavaScript制作的。我没有找到有关如何在Angular 2中使用此插件的信息。 - PhiloJunkie
现在我可以包含插件并且它完美地工作了。答案已被接受! - PhiloJunkie
你抢在我之前做出了回答。我刚刚更新了我的答案,并附上了一个示例,展示了在ng2-charts中的工作效果。 - jordanwillis
再次感谢,你救了我一命。 - PhiloJunkie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接