Ng2-charts: How can I change pie and doughnut charts colors???

Created on 4 Feb 2018  路  5Comments  路  Source: valor-software/ng2-charts

This is my code app.component.ts

 public doughnutChartLabels:string[] = ['facebook', 'twitter', 'instagram'];
  public doughnutChartData:number[] = [350, 450, 100];
  public doughnutChartType:string = 'doughnut';

 public doughnutColors:Array<any>=[
 { // dark grey
      backgroundColor: '#F97300',
      borderColor: '#F97300',
      pointBackgroundColor: 'rgba(77,83,96,1)',
      pointBorderColor: '#fff',
      pointHoverBackgroundColor: '#fff',
      pointHoverBorderColor: 'rgba(77,83,96,1)'
    },
    { // dark grey
      backgroundColor: 'red',
      borderColor: '#F97300',
      pointBackgroundColor: 'rgba(77,83,96,1)',
      pointBorderColor: '#fff',
      pointHoverBackgroundColor: '#fff',
      pointHoverBorderColor: 'rgba(77,83,96,1)'
    }];  // events
  public chartClicked(e:any):void {
    console.log(e);
  }

  public chartHovered(e:any):void {
    console.log(e);
  }

and app.component.html

  <canvas baseChart
              [data]="doughnutChartData"
              [labels]="doughnutChartLabels"
              [backgroundColor]="doughnutColors"
              [chartType]="doughnutChartType"
              (chartHover)="chartHovered($event)"
              (chartClick)="chartClicked($event)"></canvas>

Most helpful comment

@hayanisaid
in your typescript define which colors you want to specify for each section

 private donutColors=[
    {
      backgroundColor: [
        'rgba(110, 114, 20, 1)',
        'rgba(118, 183, 172, 1)',
        'rgba(0, 148, 97, 1)',
        'rgba(129, 78, 40, 1)',
        'rgba(129, 199, 111, 1)'
    ]
    }
  ];

and in html add [colors]="donutColors"

All 5 comments

@hayanisaid
in your typescript define which colors you want to specify for each section

 private donutColors=[
    {
      backgroundColor: [
        'rgba(110, 114, 20, 1)',
        'rgba(118, 183, 172, 1)',
        'rgba(0, 148, 97, 1)',
        'rgba(129, 78, 40, 1)',
        'rgba(129, 199, 111, 1)'
    ]
    }
  ];

and in html add [colors]="donutColors"

oh Thanks @harshamaddineni it's work for me :smile:

Here is an easy working example for 2 colors :

public doughnutColors:any[] = [
    { backgroundColor: ["#86c7f3", "#ffe199"] },
    { borderColor: ["#AEEBF2", "#FEFFC9"] }];

and in html:
typescript html <canvas baseChart [data]="doughnutChartData" [labels]="doughnutChartLabels" [backgroundColor]="doughnutColors" [chartType]="doughnutChartType" (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)"></canvas>

If the issue is resolved why isn't it closed?

@hayanisaid
in your typescript define which colors you want to specify for each section

 private donutColors=[
    {
      backgroundColor: [
        'rgba(110, 114, 20, 1)',
        'rgba(118, 183, 172, 1)',
        'rgba(0, 148, 97, 1)',
        'rgba(129, 78, 40, 1)',
        'rgba(129, 199, 111, 1)'
    ]
    }
  ];

and in html add [colors]="donutColors"

Not all heroes wear capes, thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hggeorgiev picture hggeorgiev  路  4Comments

shyamal890 picture shyamal890  路  4Comments

grahammutter picture grahammutter  路  4Comments

Maistho picture Maistho  路  3Comments

brandonreid picture brandonreid  路  3Comments