Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 439a1a5

Browse files
authored
Merge pull request #354 from grafana/validate-color
2 parents 8060bea + 2741cb4 commit 439a1a5

File tree

5 files changed

+578
-531
lines changed

5 files changed

+578
-531
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22
## Entries
33

4+
## v1.0.4
5+
6+
- Validate legend colors
7+
8+
49
## v1.0.3
510

611
- Release for Grafana 6+ support

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
"@types/jest": "27.0.2",
3232
"@types/lodash": "^4.14.194",
3333
"@types/moment": "^2.13.0",
34+
"@types/tinycolor2": "1.4.3",
35+
"tinycolor2": "1.6.0",
3436
"jest": "26.6.3",
3537
"jest-canvas-mock": "2.3.0",
3638
"jest-config": "^27.5.1",

src/worldmap.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @jest-environment jsdom
33
*/
4-
import WorldMap from './worldmap';
4+
import WorldMap, { DEFAULT_COLOR } from './worldmap';
55
import DataBuilder from './test/data_builder';
66
import * as _ from 'lodash';
77

@@ -308,7 +308,7 @@ describe('Worldmap', () => {
308308
'<div class="info legend leaflet-control"><div class="legend-item">' +
309309
'<i style="background:red"></i> &lt; 2</div><div class="legend-item"><i style="background:blue"></i> 2–4</div>' +
310310
'<div class="legend-item"><i style="background:green"></i> 4–6</div>' +
311-
'<div class="legend-item"><i style="background:undefined"></i> 6+</div></div>'
311+
`<div class="legend-item"><i style="background:${DEFAULT_COLOR}"></i> 6+</div></div>`
312312
);
313313
});
314314
});

src/worldmap.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as _ from 'lodash';
22
import * as L from './libs/leaflet';
33
import WorldmapCtrl from './worldmap_ctrl';
4+
import tinycolor from 'tinycolor2';
45

56
const tileServers = {
67
'CartoDB Positron': {
@@ -69,15 +70,15 @@ export default class WorldMap {
6970
let legendHtml = '';
7071
legendHtml +=
7172
'<div class="legend-item"><i style="background:' +
72-
this.ctrl.panel.colors[0] +
73+
getColor(this.ctrl.panel.colors[0]) +
7374
'"></i> ' +
7475
'&lt; ' +
7576
thresholds[0] +
7677
'</div>';
7778
for (let index = 0; index < thresholds.length; index += 1) {
7879
legendHtml +=
7980
'<div class="legend-item"><i style="background:' +
80-
this.ctrl.panel.colors[index + 1] +
81+
getColor(this.ctrl.panel.colors[index + 1]) +
8182
'"></i> ' +
8283
thresholds[index] +
8384
(thresholds[index + 1] ? '&ndash;' + thresholds[index + 1] + '</div>' : '+');
@@ -264,3 +265,11 @@ export default class WorldMap {
264265
this.map.remove();
265266
}
266267
}
268+
269+
export const DEFAULT_COLOR = '#CCC';
270+
function getColor(c: string): string {
271+
if (tinycolor(c).isValid()) {
272+
return c;
273+
}
274+
return DEFAULT_COLOR;
275+
}

0 commit comments

Comments
 (0)