有没有办法在y轴 - ng2图表中更改图表网格的颜色

时间:2017-09-21 12:41:29

标签: angular chart.js ng2-charts

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

这是我想要制作的https://i.stack.imgur.com/HL5JZ.png

component.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)'
    }
    ];

component.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>

1 个答案:

答案 0 :(得分:1)

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

以下是一个例子:

呈现图表

line-chart

使用的代码

component.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'
            }]
        }
    }
}

component.html

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