簡體   English   中英

如何更改 ng2-charts 中特定標簽的顏色?

[英]How do I change the colours of specific labels in ng2-charts?

我在 Angular 8 中使用 ng2-charts 制作了一個圓環圖。我的圓環圖中有 3 個標簽,“通過”、“失敗”和“打開”。 我想分別為這些標簽設置“綠色”、“紅色”和“藍色”。 我該怎么做呢? 我的 compliance.component.html 文件

<div>
    <div>
        <div style="display: block">
            <canvas baseChart 
            [data]="doughnutChartData" 
            [labels]="doughnutChartLabels" 
            [chartType]="doughnutChartType"
            [options]="doughnutChartOptions"
            [colors]="doughnutChartColor">
            </canvas>
        </div>
    </div>
</div>

我的 compliance.component.ts 文件

export class ComplianceComponent implements OnInit {

  public doughnutChartLabels: Label[] = ['Passed', 'Open', 'Fail'];
  public doughnutChartData: MultiDataSet = [
    [30, 20, 50]
  ];

  public doughnutChartType: ChartType = 'doughnut';
  constructor() { }

  ngOnInit() {
  }

  public doughnutChartColor: Array<any> = [
    { // first color
      'Passed'-color:green;
      'Failed'-color:red;
      'Open'-color:blue;
    }];

    public doughnutChartOptions={
      responsive: true
    };
}

謝謝你。

colors必須根據以下代碼片段定義為interface Color array

import { Color } from 'ng2-charts';
...
public doughnutChartColor: Color[] = [
    { backgroundColor: 'green' },
    { backgroundColor: 'red' },
    { backgroundColor: 'blue' }
];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM