2

I'm trying to set colors for charts made using chart.js library.

I need a specific color for each data label. but when data of specific labels is zero colors get confused :(.

it looks like the library drops zero-data labels and labels colored differently.

For example, when data is:

  • colors: green, red, yellow,
  • labels: passed, failed, in progress
  • data: 0, 5, 80

passed labeled data will be skipped as its value is zero, so failed labeled data gets the first color: green, and so on.

How can I force colors matching?


Current code:

html:

<canvas baseChart height="150px" width="150px" [data]="graphData" [labels]="graphLabels" [colors]="graphColors"
    [options]="graphOptions" [legend]="false" [chartType]="'doughnut'">
</canvas>

TypeScript:

this.graphData = this.data.map(r => r.cnt);
this.graphLabels = this.data.map(r => r.city);
this.graphOptions = {
    layout: {padding: 20},
    cutoutPrecentage: 90,
    legend: {display: false}
};

thanks!

Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36
rk fred
  • 59
  • 10

1 Answers1

2
var graphData = {
    labels: ['Passed', 'Failed', 'In Progress'],
    datasets: [{
        label: 'Data',
        data: [0, 5, 80],
        backgroundColor: [
            "green",
            "red",
            "yellow"
        ],
    }]
};

Or in your chart creation add the data

Only if you do the new Chart(...)

graphData as parameter to the canvas is great

var chr = new Chart(ctx, {
    data: graphData,
    type: 'pie',
    options: options,
});

jsFiddle

Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36