angular - 有没有办法改变 y 轴图表网格的颜色 - ng2-charts

标签 angular chart.js ng2-charts

我正在使用 ng2-charts 在我的项目中显示图表,但找不到更改网格表颜色的方法。我想根据其速率更改表格的网格颜色。例如,在 1 到 20 之间,我希望它是蓝色的,在 21 到 40 之间,我希望它是绿色的,等等..

这就是我想做的/image/HL5JZ.png

组件.ts

public lineChartData:Array<any> = [
    {data: [79, 22, 87, 12], label:'Series A'}
  ];
  public lineChartLabels:Array<any> = ['January', 'February', 'March', 'April'];
  public lineChartOptions:any = {
    responsive: true
  };
  public lineChartColors:Array<any> = [
    { // grey
      backgroundColor: 'rgba(148,159,177,0.2)',
      borderColor: 'rgba(148,159,177,1)',
      pointBackgroundColor: 'rgba(148,159,177,1)',
      pointBorderColor: '#fff',
      pointHoverBackgroundColor: '#fff',
      pointHoverBorderColor: 'rgba(148,159,177,0.8)'
    }
  ];
  public lineChartLegend:boolean = false;
  public lineChartType:string = 'line';
  public chartColors: Array<any> = [
    { // first color
      backgroundColor: 'red',
      borderColor: 'rgba(225,10,24,0.2)',
      pointBackgroundColor: 'rgba(225,10,24,0.2)',
      pointBorderColor: '#fff',
      pointHoverBackgroundColor: '#fff',
      pointHoverBorderColor: 'rgba(225,10,24,0.2)'
    }
    ];

组件.html

<div style="display: block;">
                                <canvas baseChart width="400" height="200"
                                            [datasets]="lineChartData"
                                            [labels]="lineChartLabels"
                                            [options]="lineChartOptions"
                                            [colors]="lineChartColors"
                                            [legend]="lineChartLegend"
                                            [chartType]="lineChartType"
                                            (chartHover)="chartHovered($event)"
                                            (chartClick)="chartClicked($event)">

                                </canvas>
                            </div>

最佳答案

是!! 这可以使用名为 - chartjs-plugin-annotation 的 ChartJS 插件来实现。 .

这是一个例子:

渲染图表

line-chart

使用的代码

组件.ts

import { Component } from '@angular/core';
import 'chartjs-plugin-annotation';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})

export class AppComponent {
    public chartType: string = 'line';
    public chartLabels: string[] = ['Jan', 'Feb', 'Mar', 'Apr', 'May'];
    public chartData: any[] = [{
        data: [3, 1, 4, 2, 5],
        label: 'Anthracnose',
        fill: false
    }];
    public chartColors: any[] = [{
        backgroundColor: 'rgba(0, 0, 0, 0.2)',
        borderColor: 'rgba(0, 0, 0, 1)'
    }];
    public chartOptions: any = {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        },
        annotation: {
            drawTime: 'beforeDatasetsDraw',
            annotations: [{
                type: 'box',
                id: 'a-box-1',
                yScaleID: 'y-axis-0',
                yMin: 0,
                yMax: 1,
                backgroundColor: '#4cf03b'
            }, {
                type: 'box',
                id: 'a-box-2',
                yScaleID: 'y-axis-0',
                yMin: 1,
                yMax: 2.7,
                backgroundColor: '#fefe32'
            }, {
                type: 'box',
                id: 'a-box-3',
                yScaleID: 'y-axis-0',
                yMin: 2.7,
                yMax: 5,
                backgroundColor: '#fe3232'
            }]
        }
    }
}

组件.html

<div class="chart-container">
  <canvas baseChart
          [chartType]="chartType"
          [labels]="chartLabels"
          [datasets]="chartData"
          [colors]="chartColors"
          [options]="chartOptions">
  </canvas>
</div>

关于angular - 有没有办法改变 y 轴图表网格的颜色 - ng2-charts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46344216/

相关文章:

jquery - Chartjs 条形图显示未定义的值

javascript - Chart.js:4 Uncaught ReferenceError: require 未在 ionic 2 中定义

javascript - Angular 2 图表 - 改变点半径

angular - Angular + Kubernetes + Nginx上的路由问题

javascript - 多个图表不工作 Chart.js

javascript - Chart JS 尝试仅针对一个数据集堆叠条形图工具提示

ionic-framework - ng2-charts 和 chart.js 错误 0x80004005 (NS_ERROR_FAILURE) computeLabelSizes 仅在 Firefox 中

angular - 如何访问组件中 FormArray 中定义的 HTML 中的控件 - Angular2

angular - 类型 'intlTelInput' 上不存在属性 'Window'

javascript - 在 Angular 中渲染基于时间的 Observables 而无需压倒性的变化检测