I am trying to display the values from this object on a React Chart.js line chart:
The times are formatted as hours, minutes, and seconds.
{
"totaltime": "10:55:41",
"date": "2018-01-01T00:00:00"
},
{
"totaltime": "339:40:53",
"date": "2019-01-01T00:00:00"
},
{
"totaltime": "227:25:42",
"date": "2020-01-01T00:00:00"
},
{
"totaltime": "286:40:01",
"date": "2021-01-01T00:00:00"
},
]
I then use this function to add the time values to the line chart component:
function getChartData(documents, label, color) {
return {
labels: documents.map((document) => document.date),
datasets: [
{
label: label,
data: documents.map((document) => document.totaltime),
backgroundColor: color,
borderColor: color,
borderWidth: 1,
},
],
};
}
However only the dates display and no data appears on the X-axis. If I change the values from date objects to seconds, they display perfectly but I want users to see the data in hours and minutes instead of seconds.
I have tried sending the data as seconds and then formatting the output to hours and minutes with the date-fns library using the Chart.js options, but the values never changed and stayed as seconds.
2
Answers
Solution to get the time values without replacing your date labels after using Remesh's answer
Send the formattedDate labels to the line chart component:
access it in the line component and change the tick values using a callback:
Here’s an updated version of your getChartData function that handles the conversion and formatting: