@@ -61,19 +61,36 @@ module.exports = function (RED) {
6161 if ( removeOlder > 0 ) {
6262 const removeOlderUnit = parseFloat ( config . removeOlderUnit )
6363 const ago = ( removeOlder * removeOlderUnit ) * 1000 // milliseconds ago
64- const cutoff = ( new Date ( ) ) . getTime ( ) - ago
65- const _msg = datastore . get ( node . id ) . filter ( ( msg ) => {
64+ const cutOff = ( new Date ( ) ) . getTime ( ) - ago
65+ const filterFn = ( msg ) => {
6666 let timestamp = msg . _datapoint . x
6767 // is x already a millisecond timestamp?
6868 if ( typeof ( msg . _datapoint . x ) === 'string' ) {
6969 timestamp = ( new Date ( msg . _datapoint . x ) ) . getTime ( )
7070 }
71- return timestamp > cutoff
72- } )
73- datastore . save ( base , node , _msg )
71+ return timestamp > cutOff
72+ }
73+ datastore . filter ( base , node , filterFn )
7474 }
7575 }
7676
77+ /**
78+ * For categorical xaxis and types other than histogram then only keep the latest data point for
79+ * each category in each series
80+ */
81+ function clearOldCategoricalPoints ( ) {
82+ const points = datastore . get ( node . id )
83+ const latestSet = { }
84+ for ( const item of points ) {
85+ const { category, x } = item . _datapoint
86+ const key = JSON . stringify ( [ category , x ] ) // a unique key for each category/series combination
87+ latestSet [ key ] = item
88+ }
89+
90+ const filtered = Object . values ( latestSet )
91+ datastore . save ( base , node , filtered )
92+ }
93+
7794 // ensure sane defaults
7895 if ( ! [ 'msg' , 'str' , 'property' , 'timestamp' ] . includes ( config . xAxisPropertyType ) ) {
7996 config . xAxisPropertyType = 'timestamp' // default to 'timestamp'
@@ -269,6 +286,10 @@ module.exports = function (RED) {
269286 if ( config . xAxisType === 'time' && config . removeOlder && config . removeOlderUnit ) {
270287 // remove any points older than the specified time
271288 clearOldPoints ( )
289+ } else if ( config . xAxisType === 'category' && config . chartType !== 'histogram' ) {
290+ // for categorical xaxis and types other than histogram then only keep the latest data point for
291+ // each category in each series
292+ clearOldCategoricalPoints ( )
272293 }
273294 }
274295 }
0 commit comments