Relative Time in chart

98 views
Skip to first unread message

Thomas Bruyere

unread,
Jun 14, 2017, 5:11:48 PM6/14/17
to Node-RED UI
Hi,
Is it possible to record informations with relative time in chart ?

For exemple : time increase after a top.

I need to synchronise 2 graphics in the same chart with a recall and x-axis is the time.

Thanks for help

Thomas
 

steve rickus

unread,
Jun 14, 2017, 11:00:49 PM6/14/17
to Node-RED UI
The x-axis data does not have to be a timestamp -- so if I understand your question, you could have it be the number of minutes (or seconds, or hours) since a particular event, with 0 at the left side and increasing numbers to the right. Is this what you mean? I'm just not sure how the chart will handle displaying the x-axis labels...

Thomas Bruyere

unread,
Jun 15, 2017, 1:40:53 AM6/15/17
to Node-RED UI
Yes it is what i need.
Is it possible to do this directly when it record or do i affect the array?
Thx

steve rickus

unread,
Jun 15, 2017, 9:41:07 AM6/15/17
to Node-RED UI
Not sure I understand the question --
but you will need to convert the x-axis values from dates to offsets before passing the dataset into the chart.

In other words, there is not a "relative" mode built in to the chart node (although that would be a simple thing to add... Dave?)
If you run into trouble converting your data from absolute to relative time, feel free to post the data here and I'm sure someone will be able to help.

Thomas Bruyere

unread,
Jun 16, 2017, 8:22:45 AM6/16/17
to Node-RED UI
I need to start chart  to 0 at the left side and increasing numbers to the right yes.
I think to do it i have 2 solutions :

- Record chart directly with 0 at left side and increase ( relative mode )
- Record with absolute time ( normal mode ) and converting data after.

My primary question was :  is possible to record with the first solution ( relative mode ) ?

If not i will try to do the second solution (normal mode) + convert.

I hope i'am clear.
Sorry french are not english professional. :(




steve rickus

unread,
Jun 16, 2017, 11:53:33 AM6/16/17
to Node-RED UI
Well, if your data is coming in real time, you can keep an array of relative data points in the context, and feed that into the chart every time a new msg is received. It's not the most efficient way, but until the chart node supports relative time mode, I think it's the only way to do it. Here is the function code:

// get current time and context array
var now = Date.now();
var off = new Date(70, 0, 1) - new Date(0);
var arr = context.get("data") || [];

// reset the data if empty array is received
if (Array.isArray(msg.payload)) {
   
if (msg.payload.length === 0) {
        context
.set("data", msg.payload);
       
return msg; // to clear the chart
   
}
   
else {
        arr
= msg.payload;
   
}
}
else {
   
// get/set the initial (absolute) timestamp
   
var dt0 = arr.length ? arr[0][0] : now;
   
if (arr.length === 0) {
        arr
.push([ dt0, 0 ]);
   
}
   
   
// calculate time since last msg
   
var dt1 = msg.timestamp || now;
   
var dlt = (dt1 - dt0) + off;
   
   
// add new relative data point to array
    arr
.push([ dlt, msg.payload ]);
}
context
.set("data", arr);

// build the chart msg with the array
var key = msg.topic || "Test Data";
msg
.payload = [{ "key": key, "values": arr.slice(1) }];
return msg;

Here is the flow I set up to test the function code -- since the chart logic always assumes the x-axis is time-based, I adjusted all my relative timestamps to be offset from the UTC epoch, and set the chart to display elapsed time in HH:mm:ss format. Hope this helps!
--
Steve

Thomas Bruyere

unread,
Jun 18, 2017, 7:45:43 AM6/18/17
to Node-RED UI
"hope this helps" lol
God it is a perfect answer !!!
I'am in holiday for now but i will try this directly after return.
Really thanks for your Time !!
I tell you my feedback quickly.
Thomas

Thomas Bruyere

unread,
Jul 11, 2017, 4:14:44 AM7/11/17
to Node-RED UI
Hi,
I have tested your code today, and all works perfectly !!

Thanks again Steve !
Thomas

Reply all
Reply to author
Forward
0 new messages