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