Unable to show labels on bar chart

With this function I read data from a txt file. Because there may be more lines of data for the same month, the function will use the last one for each month.

// Function to process load shedding data
const processLastEntryPerMonth = (lines) => {
    let monthlyData = {};
    lines.forEach(line => {
        let parts = line.split(': ');
        if (parts.length === 2) {
            let date = new Date(parts[0]);
            let month = date.toLocaleString('en-US', { month: 'long' });
            let timeParts = parts[1].split(':');
            let hours = parseInt(timeParts[0]) + parseInt(timeParts[1]) / 60;
            monthlyData[month] = hours; // Store the last value for each month
        }
    });
    return monthlyData;
};

// Assuming msg.payload is the raw data from your file
const lines = msg.payload.split('\n').filter(line => line.trim() !== '');
let monthlyData = processLastEntryPerMonth(lines);

// Format data for the chart
let m = {
    series: [""],
    data: [],
    labels: []
};

for (let month in monthlyData) {
    m.labels.push(month); // Month names as labels
    m.data.push(Math.round(monthlyData[month])); // Load shedding hours for each month
}

return { payload: [m], topic: msg.topic };

My problem is that I am unable to show the labels (Month name) for each bar.
This is my actual result

What am I doing wrong? Why the labels (January, February) are not showing under the relevant bars?
Thanks for helping

Try

 m.data[0].push(Math.round(monthlyData[month]));

As data should be a array of array/s.
And

let m = {
    series: [""],
    data: [[]],
    labels: []
};
1 Like

Thank you. That did it!

Hi, what version of node-red Dashboard are you using? I and using Dashboard v2 and also having problems finding the right way to build my data.

If you have an issue with the dashborad 2 ui-chart please start a topic with your question, include incoming data and describe in full your expected output, and issues you are gaving.
More Info of chart in dashboard 2 here Chart ui-chart | Node-RED Dashboard 2.0

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.