Angular 8 & ChartJs change color in pie chart

aas*_*dar 2 chart.js ng2-charts angular

I am trying to change the default pie chart color. but all 3 pie chart arc showing red color. Apply the first color in pieColor array to all arc. I am using chart.js 2.9.3, ng2-charts 2.3.0 & angular 8. Also, I am trying a different color format like hex code or RGB but not working.

pie-chart.component.html

<div class="chart-wrapper">
    <canvas baseChart 
    [data]="doughnutChartData"
    [labels]="doughnutChartLabels"
    [colors]="pieColor
    [chartType]="doughnutChartType">
  </canvas>
</div>
Run Code Online (Sandbox Code Playgroud)

pie-chart.component.ts

import { Component } from '@angular/core';
import { ChartType } from 'chart.js';
import { MultiDataSet, Label } from 'ng2-charts';

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

export class DoughnutChartComponent {

  doughnutChartLabels: Label[] = ['BMW', 'Ford', 'Tesla'];
  doughnutChartData: MultiDataSet = [
    [55, 25, 20]
  ];
  doughnutChartType: ChartType = 'doughnut';
  public pieColor: Color[] = [
     { backgroundColor: 'red' },
     { backgroundColor: 'green' },
     { backgroundColor: 'blue' }
  ]
}
Run Code Online (Sandbox Code Playgroud)

Output 在此处输入图片说明

asl*_*ary 6

您的问题是颜色数组的形式。backgroundColor您应该只定义一个具有多种颜色的数组,而不是定义每个具有一种颜色的多个数组。尝试下面,它应该工作:

模板

<div class="chart-wrapper">
  <canvas baseChart [data]="doughnutChartData" [labels]="doughnutChartLabels" [colors]="colors"
    [chartType]=" doughnutChartType">
  </canvas>
</div>
Run Code Online (Sandbox Code Playgroud)

控制器

export class AppComponent {
  doughnutChartLabels: Label[] = ['BMW', 'Ford', 'Tesla'];

  doughnutChartData: MultiDataSet = [
    [
      55,
      25,
      20
    ]
  ];

  doughnutChartType: ChartType = 'doughnut';

  colors: Color[] = [
    {
      backgroundColor: [
        'red',
        'green',
        'blue'
      ]
    }
  ];

}
Run Code Online (Sandbox Code Playgroud)