Lightning Web Component and Chart.js - how to create a LWC with dynamic stacked bar chart?
03:00 04 Feb 2026

I have a working version of standard bar charts. I have a need of creating a stacked bar chart for revenues with different categories. I can see a lot of examples how to configure it in lwc with static data but not with dynamic data.

My case is that - I need to display revenues for last X months (depending if they exist). Each month can have 1 or more categories which should be stacked.

E.g. 1.2025 (cat 1, cat 2) / 2.2025 (cat 3) / 5.2025 (cat 1) / 8.2025 (cat 1, cat 2, cat 3).

For standard bar chart my apex method returns:

Map

And it works fine - allows me to return single value for each month.

Because now I need to have multiple categories I've created apex method which returns:

Map>

then in my component I do this (this is for standard bar chart):

wiredRevenueMap({ data, error }) {
        if (data) {
            const revenueMap = data;

            for (const key in revenueMap) {
                this.revenueLabels.push(key);
                this.revenueValues.push(revenueMap[key]);
            }
...
initializeChart() {
        if(this.template.querySelector('canvas')) {
            const ctx = this.template.querySelector('canvas').getContext('2d');

            
            this.chart = new Chart(ctx, {
                type: 'bar',
                data: {
                    labels: this.revenueLabels,
                    datasets: [
                        {
                            label: 'Revenue',
                            data: this.revenueValues,
                            backgroundColor: 'rgba(14, 112, 199, 1)',
                            borderColor: 'rgba(14, 112, 199, 1)',
                            borderWidth: 1,
                            order: 1
                        }
                    ]
                },
...

I have a problem with writing code to create datasets for my stacked bar chart. Would you be able to support me on that?

chart.js salesforce-lightning lwc