Node-red-dashboard Chart rewrite epoch time X axis labels

Dave CJ - There are several Weather Company APIs that return historical data that I would like to plot in charts. I build the chart data series with Epoch time as the X axis. It looks fine but the X axis labels are somewhat meaningless. I was wondering if there is a way to set the labels to a date format. Something like 06-23-2020 06-24-2020 06-25-2020
-- John

Two examples:

Selection_042

The timestamps should be in javascript milliseconds.
I looks like they are wrong. Show us some of the data that you are sending to the chart and tell us what the timestamps represent.

The timestamps are in seconds 1593342000 (one timestamp per day)
The chart is correct but the x-axis autoselects meaningless tick marks.
Here's a function that builds a chart of 30 days of temperature data (the data is looking backwards in time so I reverse it for the chart)

var MonthlyHistoryTempChart = [{
    "series":["Daily Temp Max", "Daily Temp Min"],
    "data":[[],[]],
    "labels":["Daily Max Temp", "Daily Min Temp"]
}];

for( var i=(msg.payload.validTimeUtc.length)-1; i >= 0 ; i--) {
    MonthlyHistoryTempChart[0].data[0].push( {"x":msg.payload.validTimeUtc[i],"y":msg.payload.temperatureMax[i] } ) ;
    MonthlyHistoryTempChart[0].data[1].push( {"x":msg.payload.validTimeUtc[i],"y":msg.payload.temperatureMin[i] } ) ;
}

msg.payload = MonthlyHistoryTempChart;
return msg;

Essentially, I want to set the underlying chart.js options to label the x-axis as days.

From the chart.js docs I see:
https://www.chartjs.org/docs/latest/axes/cartesian/time.html#time-units
which tells me to set the options object. How can I pass this through the node-red-dashboard chart node to the underlying chart.js?

var chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            xAxes: [{
                type: 'time',
                time: {
                    unit: 'day'
                }
            }]
        }
    }
});

Searching through this forum, I see some chart.js examples which use the ui_template node. I was hopeful that I wouldn't need to go down that path for a common time series use case.

gah - I'm going to crawl under my rock again. Dave is brilliant and has already provided this capability. Not certain when it became an option in the Chart node.

2 Likes

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