@@ -127,7 +127,7 @@ export default class DataFormatter {
127
127
}
128
128
}
129
129
130
- createDataValue ( encodedGeohash , decodedGeohash , locationName , value , link ) {
130
+ createDataValueWithGeohash ( encodedGeohash , decodedGeohash , locationName , value , link ) {
131
131
// Todo: Bring this up to speed with the current state in `setTableValues`.
132
132
const dataValue = {
133
133
key : encodedGeohash ,
@@ -144,6 +144,21 @@ export default class DataFormatter {
144
144
return dataValue ;
145
145
}
146
146
147
+ createDataValueFromPoint ( point ) {
148
+ const dataValue = {
149
+ key : point . key ,
150
+ locationName : point . name ,
151
+ locationLatitude : point . latitude ,
152
+ locationLongitude : point . longitude ,
153
+ value : point . value !== undefined ? point . value : 1 ,
154
+ valueRounded : 0 ,
155
+ } ;
156
+
157
+ dataValue . valueRounded = kbn . roundValue ( dataValue . value , this . settings . decimals || 0 ) ;
158
+
159
+ return dataValue ;
160
+ }
161
+
147
162
decodeGeohashSafe ( encodedGeohash ) {
148
163
// Safely decode the geohash value, either by raising an exception or by ignoring it.
149
164
if ( ! encodedGeohash ) {
@@ -201,7 +216,7 @@ export default class DataFormatter {
201
216
const value = row [ columnNames [ this . settings . esMetric ] ] ;
202
217
const link = this . settings . esLink ? row [ columnNames [ this . settings . esLink ] ] : null ;
203
218
204
- const dataValue = this . createDataValue ( encodedGeohash , decodedGeohash , locationName , value , link ) ;
219
+ const dataValue = this . createDataValueWithGeohash ( encodedGeohash , decodedGeohash , locationName , value , link ) ;
205
220
206
221
// Add all values from the original datapoint as attributes prefixed with `__field_`.
207
222
for ( const columnName in columnNames ) {
@@ -241,7 +256,7 @@ export default class DataFormatter {
241
256
const value = datapoint [ this . settings . esMetric ] ;
242
257
const link = this . settings . esLink ? datapoint [ this . settings . esLink ] : null ;
243
258
244
- const dataValue = this . createDataValue ( encodedGeohash , decodedGeohash , locationName , value , link ) ;
259
+ const dataValue = this . createDataValueWithGeohash ( encodedGeohash , decodedGeohash , locationName , value , link ) ;
245
260
246
261
// Add all values from the original datapoint as attributes prefixed with `__field_`.
247
262
for ( let key in datapoint ) {
@@ -395,38 +410,40 @@ export default class DataFormatter {
395
410
}
396
411
}
397
412
398
- setJsonValues ( data ) {
399
- if ( this . ctrl . series && this . ctrl . series . length > 0 ) {
400
- let highestValue = 0 ;
401
- let lowestValue = Number . MAX_VALUE ;
402
-
403
- this . ctrl . series . forEach ( point => {
404
- // Todo: Bring this up to speed with the current state in `setTableValues`.
405
- const dataValue = {
406
- key : point . key ,
407
- locationName : point . name ,
408
- locationLatitude : point . latitude ,
409
- locationLongitude : point . longitude ,
410
- value : point . value !== undefined ? point . value : 1 ,
411
- valueRounded : 0 ,
412
- } ;
413
- if ( dataValue . value > highestValue ) {
414
- highestValue = dataValue . value ;
415
- }
416
- if ( dataValue . value < lowestValue ) {
417
- lowestValue = dataValue . value ;
413
+ setJsonValues ( series , data ) {
414
+ if ( series && series . length > 0 ) {
415
+ series . forEach ( serie => {
416
+ if ( serie . datapoints && serie . datapoints . length > 0 ) {
417
+ serie . datapoints . forEach ( point => {
418
+ // Todo: Bring this up to speed with the current state in `setTableValues`.
419
+ data . push ( this . createDataValueFromPoint ( point ) ) ;
420
+ } ) ;
421
+ } else {
422
+ // Todo: Bring this up to speed with the current state in `setTableValues`.
423
+ data . push ( this . createDataValueFromPoint ( serie ) ) ;
418
424
}
419
- dataValue . valueRounded = Math . round ( dataValue . value ) ;
420
- data . push ( dataValue ) ;
421
425
} ) ;
422
- data . highestValue = highestValue ;
423
- data . lowestValue = lowestValue ;
424
- data . valueRange = highestValue - lowestValue ;
426
+
427
+ this . computeValueRange ( data ) ;
425
428
} else {
426
429
this . addWarning ( 'No data in JSON format received' ) ;
427
430
}
428
431
}
429
432
433
+ computeValueRange ( data ) {
434
+ const sortedValues = data . map ( datapoint => {
435
+ return datapoint . value ;
436
+ } ) ;
437
+
438
+ sortedValues . sort ( ( a , b ) => a - b ) ;
439
+
440
+ data . highestValue = sortedValues [ sortedValues . length - 1 ] ;
441
+ data . lowestValue = sortedValues [ 0 ] ;
442
+ data . valueRange = data . highestValue - data . lowestValue ;
443
+
444
+ return data ;
445
+ }
446
+
430
447
addWarning ( message ) {
431
448
this . ctrl . errors . add ( message , { level : 'warning' , domain : 'data' } ) ;
432
449
}
0 commit comments