Skip to content

Commit 5e7ae9f

Browse files
authored
Support multiple metrics in popup content (#77)
This allows us to inspect the `__field_` info and display it if we have data beyond what's used in creating the circle size/color. This is elasticsearch-centric for the moment. We can add support for other data sources once we have them available.
1 parent f143b47 commit 5e7ae9f

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

src/worldmap.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,28 @@ describe('Worldmap', () => {
138138
});
139139
});
140140

141+
describe('when the data has multiple metrics', () => {
142+
beforeEach(() => {
143+
ctrl.data = new DataBuilder()
144+
.withCountryAndValue('SE', 1, {
145+
__field_device_urn: 'safecast:903348716',
146+
'__field_ingest.location': 'wecnv3p07bjj',
147+
'__field_Average pms_pm02_5': 33,
148+
'__field_Average lnd_7318u': 110,
149+
})
150+
.build();
151+
152+
ctrl.panel.esGeoPoint = 'ingest.location';
153+
ctrl.panel.esLocationName = 'device_urn';
154+
ctrl.panel.esMetric = 'Average pms_pm02_5';
155+
worldMap.drawCircles();
156+
});
157+
158+
it('should create a circle popup with additional metrics', () => {
159+
expect(worldMap.circles[0]._popup._content).toBe('Sweden: 1 <br />Average lnd_7318u: 110');
160+
});
161+
});
162+
141163
describe('when the data has three points', () => {
142164
beforeEach(() => {
143165
ctrl.data = new DataBuilder()

src/worldmap.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export default class WorldMap {
297297
});
298298

299299
this.createClickthrough(circle, dataPoint);
300-
const content = this.getPopupContent(dataPoint.locationName, dataPoint.valueRounded);
300+
const content = this.getPopupContent(dataPoint);
301301
this.createPopup(circle, content);
302302
return circle;
303303
}
@@ -319,7 +319,7 @@ export default class WorldMap {
319319

320320
// Re-create popup.
321321
circle.unbindPopup();
322-
const content = this.getPopupContent(dataPoint.locationName, dataPoint.valueRounded);
322+
const content = this.getPopupContent(dataPoint);
323323
this.createPopup(circle, content);
324324

325325
// Re-create clickthrough-link.
@@ -418,12 +418,16 @@ export default class WorldMap {
418418
extendPopupContent(circle, dataPoint) {
419419
const popup = circle.getPopup();
420420
let popupContent = popup._content;
421-
popupContent += `\n${this.getPopupContent(dataPoint.locationName, dataPoint.valueRounded)}`;
421+
popupContent += `\n${this.getPopupContent(dataPoint)}`;
422422
circle.setPopupContent(popupContent);
423423
}
424424

425-
getPopupContent(locationName, value) {
425+
getPopupContent(dataPoint) {
426426
let unit;
427+
428+
let locationName = dataPoint.locationName;
429+
let value = dataPoint.value;
430+
427431
if (_.isNaN(value)) {
428432
value = 'n/a';
429433
} else {
@@ -433,7 +437,27 @@ export default class WorldMap {
433437
if (this.ctrl.settings.formatOmitEmptyValue && value === 'n/a') {
434438
return `${locationName}`.trim();
435439
} else {
436-
return `${locationName}: ${value} ${unit || ''}`.trim();
440+
let fieldPrefix = '__field_';
441+
442+
let specialFields = [
443+
fieldPrefix + this.ctrl.settings.esLocationName,
444+
fieldPrefix + this.ctrl.settings.esMetric,
445+
fieldPrefix + this.ctrl.settings.esGeoPoint,
446+
];
447+
448+
let freeDataFields = Object.keys(dataPoint).filter(
449+
(key: string) => key.startsWith(fieldPrefix) && !specialFields.includes(key)
450+
);
451+
452+
let freeDataDisplay = freeDataFields
453+
.map((field: string) => {
454+
let name = field.slice(fieldPrefix.length);
455+
let value = dataPoint[field];
456+
return `<br />${name}: ${value}`;
457+
})
458+
.join('');
459+
460+
return `${locationName}: ${value} ${unit || ''}${freeDataDisplay}`.trim();
437461
}
438462
}
439463

test/data_builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class DataBuilder {
77
this.data.categories = [];
88
}
99

10-
withCountryAndValue(countryCode, value) {
10+
withCountryAndValue(countryCode, value, overrides?: {[key: string]: any}) {
1111
let dataPoint;
1212
if (countryCode === 'SE') {
1313
dataPoint = {
@@ -39,7 +39,7 @@ export default class DataBuilder {
3939
} else {
4040
throw new Error(`Unable to create fixture for country code ${countryCode}`);
4141
}
42-
this.data.push(dataPoint);
42+
this.data.push({...dataPoint, ...overrides});
4343

4444
return this;
4545
}

0 commit comments

Comments
 (0)