Skip to content

Commit 1986198

Browse files
committed
Merge branch 'develop' of github.com:panodata/grafana-map-panel into develop
2 parents 5317a7c + 4f4765b commit 1986198

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

src/core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ export class WorldmapCore {
145145
// Todo: Don't misuse `this.series` for this as it is a completely different format.
146146
// Better pass the payload to `setJsonValues()` like seen with `setTableValues()`.
147147
series = dataList;
148-
this.dataFormatter.setJsonValues(data);
148+
// we need to pass series here, because setJsonValues accesses the series in the ctrl which is not set at this moment
149+
this.dataFormatter.setJsonValues(series, data);
149150
} else if (this.settings.locationData) {
150151
this.assertDataFormat(dataFormat === DataFormat.Timeseries, dataFormat, DataFormat.Timeseries);
151152
console.info('Interpreting data as timeseries format');

src/data_formatter.ts

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export default class DataFormatter {
127127
}
128128
}
129129

130-
createDataValue(encodedGeohash, decodedGeohash, locationName, value, link) {
130+
createDataValueWithGeohash(encodedGeohash, decodedGeohash, locationName, value, link) {
131131
// Todo: Bring this up to speed with the current state in `setTableValues`.
132132
const dataValue = {
133133
key: encodedGeohash,
@@ -144,6 +144,21 @@ export default class DataFormatter {
144144
return dataValue;
145145
}
146146

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+
147162
decodeGeohashSafe(encodedGeohash) {
148163
// Safely decode the geohash value, either by raising an exception or by ignoring it.
149164
if (!encodedGeohash) {
@@ -201,7 +216,7 @@ export default class DataFormatter {
201216
const value = row[columnNames[this.settings.esMetric]];
202217
const link = this.settings.esLink ? row[columnNames[this.settings.esLink]] : null;
203218

204-
const dataValue = this.createDataValue(encodedGeohash, decodedGeohash, locationName, value, link);
219+
const dataValue = this.createDataValueWithGeohash(encodedGeohash, decodedGeohash, locationName, value, link);
205220

206221
// Add all values from the original datapoint as attributes prefixed with `__field_`.
207222
for (const columnName in columnNames) {
@@ -241,7 +256,7 @@ export default class DataFormatter {
241256
const value = datapoint[this.settings.esMetric];
242257
const link = this.settings.esLink ? datapoint[this.settings.esLink] : null;
243258

244-
const dataValue = this.createDataValue(encodedGeohash, decodedGeohash, locationName, value, link);
259+
const dataValue = this.createDataValueWithGeohash(encodedGeohash, decodedGeohash, locationName, value, link);
245260

246261
// Add all values from the original datapoint as attributes prefixed with `__field_`.
247262
for (let key in datapoint) {
@@ -395,38 +410,40 @@ export default class DataFormatter {
395410
}
396411
}
397412

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));
418424
}
419-
dataValue.valueRounded = Math.round(dataValue.value);
420-
data.push(dataValue);
421425
});
422-
data.highestValue = highestValue;
423-
data.lowestValue = lowestValue;
424-
data.valueRange = highestValue - lowestValue;
426+
427+
this.computeValueRange(data);
425428
} else {
426429
this.addWarning('No data in JSON format received');
427430
}
428431
}
429432

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+
430447
addWarning(message) {
431448
this.ctrl.errors.add(message, { level: 'warning', domain: 'data' });
432449
}

0 commit comments

Comments
 (0)