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

Phi*_*kie 3 chart.js css-gradients ng2-charts

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

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

但是,我没有得到我想要的东西,因为正如你在上面的图片中看到的那样,不仅网格被着色而且x值,y标签和图表图例也被着色.

我用这个css代码得到的结果的图片

我想要的例子

我的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
      };
Run Code Online (Sandbox Code Playgroud)

那么,有没有办法将网格的背景设置为3种不同的颜色(渐变与否)?注意:我正在使用带有角度2的图表.(ng2-charts)

jor*_*lis 7

最简单的方法是使用chartjs-plugin-annotation插件并配置绑定到Y轴的3个框注释(每个框具有不同的颜色).

下面是一个示例(您可以使用此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)',
      }],
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

重要的是要注意注释被绘制在图表的顶部,因此您的注释颜色需要包含一些透明度以查看其背后的东西.

使用ng2-charts的这个插件没有问题.只需<script>在应用程序的html文件中添加源代码,然后将annotation属性添加到options对象中.这是一个演示如何使用注释插件的Angular2/ng2-charts 示例.