I copied the demo for the apexcharts javascript Column Charts > Column with Group Label and I want to change the colors grouped by categories. How to do this? Here’s from the sample demo
var options = {
series: [{
name: "sales",
data: [{
x: '2019/01/01',
y: 400
}, {
x: '2019/04/01',
y: 430
}, {
x: '2019/07/01',
y: 448
}, {
x: '2019/10/01',
y: 470
}, {
x: '2020/01/01',
y: 540
}, {
x: '2020/04/01',
y: 580
}, {
x: '2020/07/01',
y: 690
}, {
x: '2020/10/01',
y: 690
}]
}],
chart: {
type: 'bar',
height: 380
},
xaxis: {
type: 'category',
labels: {
formatter: function(val) {
return "Q" + dayjs(val).quarter()
}
},
group: {
style: {
fontSize: '10px',
fontWeight: 700
},
groups: [
{ title: '2019', cols: 4 },
{ title: '2020', cols: 4 }
]
}
},
title: {
text: 'Grouped Labels on the X-axis',
},
tooltip: {
x: {
formatter: function(val) {
return "Q" + dayjs(val).quarter() + " " + dayjs(val).format("YYYY")
}
}
},
};
var chart = new ApexCharts(document.querySelector("#chart"), options);
chart.render();
I tried below but the first color applied to all bar columns.
colors: ['#FF0000', '#00FFFF']
Other try is but still the same. I cant get to work to update bar columns per category:
fill: {
colors: [function({ value, seriesIndex, w }) {
index = Math.floor(seriesIndex / $group_count);
return '#7E36AF';
}]
},
2
Answers
I tried below but the first color applied to all bar columns.
Another try is still the same. I can’t get to work to update bar columns per category:
Axis groups can only be used to format that axis’ labels; the groups
cannot be used to target properties of the bars themselves.
If colors are the only thing you want to change, the simplest way is to
assign individual colors for each data point, see the
docs on column charts at "Changing the color of a particular data-point":
Of course, that can be performed automatically, by mapping a function to the
data array, e.g.,
Full snippet: