I am trying to implement the stacked bar chart (horizontal, responsive).
I am using Chart.js in Angular 10.
It should have following specific colors for the 3 series (shown as labels in legend on top):
- green for
Both data
, - yellow for
Only data
and - red for
No data
Issue
However, I am getting random colors apart from what is being coded in barchartColors
.
Actual output
This is as screenshot from the generated chart:
TypeScript
public barChartOptions: any = {
scaleShowVerticalLines: false,
responsive: true
};
public barChartLabels: string[] = ['Dept1', 'Dept2', 'Dept3', 'Dept4', 'Dept5', 'Dept6'];
public barChartType = 'horizontalBar';
public barChartLegend = true;
public barChartData: any[] = [
{data: [0, 16,4, 3, 10, 0], label: 'Both data'},
{data: [0, 5, 0,5, 8, 0], label: 'Only data'},
{data: [41, 6, 6,0, 48, 12], label: 'No Data'}
];
public barchartColors=[
{backgroundColor: [
'rgba(92, 184, 92,1)', //green
'rgba(255, 193, 7,1)', //yellow
'rgba(217, 83, 79,1)', //red
]}
];
HTML
<div class="card">
<div class="card-header">
Data Availability Department Wise
<div class="card-header-actions">
<a href="http://www.chartjs.org"><small class="text-muted">View</small></a>
</div>
</div>
<div class="card-body">
<div class="chart-wrapper">
<canvas baseChart class="chart" id ="data"
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[legend]="barChartLegend"
[colors]="barchartColors"
[chartType]="barChartType"
></canvas>
</div>
</div>
</div>