4

I'm using ApexChart for a project, and I want to set different colors for each bar. I pass the data array and the color array to the component, based on the color array index the relevant data bar should be colored.

Current implementation as below, I tired colors: method but it only gets the first color of the color array.

const options: ApexOptions = {
    chart: {
        height: 200,
        type: 'bar',
        offsetY: 16,
        toolbar: {
            show: false,
        },
    },
    plotOptions: {
        bar: {
            horizontal: true,
            barHeight: '85%',
        },
    },
    dataLabels: {
        enabled: false,
    },
    xaxis: {
        position: 'top',
    },
    yaxis: {
        show: false,
    },
    grid: {
        padding: {
            top: 0,
            right: 0,
            bottom: 0,
            left: 0
        },  
    },     
};

const series = [
    {
        data: exampleChartData.data || [],

    }
];

return (
    <Chart
        options={options}
        series={series}
        type="bar"
        height={740}
        className="apex-charts"
        dir="ltr"
    />
);

current chart

Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42

1 Answers1

18

You need to add distributed: true option inside bar key of plotOptions as well as adding the colors option as array. You can take a look at this sandbox for a live working example. And your final code will be like this:

const options: ApexOptions = {
    chart: {
        height: 200,
        type: 'bar',
        offsetY: 16,
        toolbar: {
            show: false,
        },
    },
    plotOptions: {
        bar: {
            distributed: true, // this line is mandatory
            horizontal: true,
            barHeight: '85%',
        },
    },
    colors: [ // this array contains different color code for each data
        "#33b2df",
        "#546E7A",
        "#d4526e",
        "#13d8aa",
        "#A5978B",
        "#2b908f",
        "#f9a3a4",
        "#90ee7e",
        "#f48024",
        "#69d2e7"
    ],
    dataLabels: {
        enabled: false,
    },
    xaxis: {
        position: 'top',
    },
    yaxis: {
        show: false,
    },
    grid: {
        padding: {
            top: 0,
            right: 0,
            bottom: 0,
            left: 0
        },  
    },     
};

const series = [
    {
        data: exampleChartData.data || [],
    }
];

return (
    <Chart
        options={options}
        series={series}
        type="bar"
        height={740}
        className="apex-charts"
        dir="ltr"
    />
);
Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42
  • Nice and solid, Thanks for the answer. I also used distributed: true but the graph was suddenly disappeared from the canvas, just check and found some html issues, fixed all and good now. – Sudaraka Senevirathne Feb 08 '22 at 19:18
  • This was what I was missing also thanks. Just wanted to add that for my project I had legends added when setting distributed: true, I have labels I need on the Y axis. So I checked the API and indeed there is a simple option to add: legend: { show: false, } – SuperTed Apr 19 '23 at 09:01