Skip to content

Commit 254532c

Browse files
committed
fixed coloring bug
1 parent 281b5f3 commit 254532c

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/MapBox.svelte

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
mapFirstLoaded,
2121
sensorMap,
2222
} from './stores.js';
23+
import * as d3 from 'd3';
2324
2425
let LAT = -0.5;
2526
let LON = -0.5;
@@ -54,6 +55,9 @@
5455
const onMouseEnter = level => e => {
5556
map.getCanvas().style.cursor = 'pointer';
5657
popup.setLngLat(e.lngLat).addTo(map);
58+
59+
map.setFeatureState({ source: level, id: hoveredId }, { hover: false });
60+
map.setFeatureState({ source: 'mega-county', id: megaHoveredId }, { hover: false });
5761
};
5862
5963
const onMouseMove = level => e => {
@@ -68,22 +72,53 @@
6872
6973
map.setFeatureState({ source: level, id: hoveredId }, { hover: false });
7074
map.setFeatureState({ source: 'mega-county', id: megaHoveredId }, { hover: false });
75+
76+
var fillColor;
7177
if (level === 'mega-county') {
7278
if (hoveredId === null) {
7379
megaHoveredId = e.features[0].id;
7480
map.setFeatureState({ source: level, id: megaHoveredId }, { hover: true });
81+
// get hover color for mega county
82+
fillColor = e.features[0].layer.paint['fill-color'].toString();
7583
} else {
7684
megaHoveredId = null;
7785
}
7886
} else {
7987
hoveredId = e.features[0].id;
8088
map.setFeatureState({ source: level, id: hoveredId }, { hover: true });
89+
90+
//get hover color for regular county
91+
var color_stops = map.getLayer(level).getPaintProperty('fill-color')['stops'];
92+
var value_range = [];
93+
var color_range = [];
94+
95+
for (var i = 0; i < color_stops.length; i++) {
96+
value_range.push(color_stops[i][0]);
97+
color_range.push(color_stops[i][1].match(/\d+/g));
98+
}
99+
100+
var ramp = d3
101+
.scaleLinear()
102+
.domain(value_range)
103+
.range(color_range);
104+
105+
var stat_high = color_stops[color_stops.length - 1][0];
106+
var stat_low = color_stops[0][0];
107+
const value = e.features[0].properties.value;
108+
if (value <= stat_low) {
109+
fillColor = color_stops[0][1];
110+
} else if (value > stat_high) {
111+
fillColor = color_stops[color_stops.length - 1][1];
112+
} else {
113+
var arr = ramp(value);
114+
fillColor = 'rgb(' + arr.join(', ') + ')';
115+
}
81116
}
82117
83118
if (hoveredId !== null && level === 'mega-county') return;
84119
// popup
85120
const { value, direction, NAME, Population } = e.features[0].properties;
86-
const fillColor = e.features[0].layer.paint['fill-color'].toString();
121+
//const fillColor = e.features[0].layer.paint['fill-color'].toString();
87122
88123
const sens = $sensorMap.get($currentSensor);
89124
let title =
@@ -92,9 +127,8 @@
92127
($currentLevel === 'county' && level !== 'mega-county' ? ' County' : '');
93128
94129
let body;
95-
//console.log(e.features[0].properties);
96130
if ($signalType === 'value') {
97-
// More information displayed when deaths is shown
131+
// More information displayed when counts is shown
98132
if ($currentSensor.match(/deaths_incidence_num/)) {
99133
const death_num = e.features[0].properties.value;
100134
const ratio = e.features[0].properties.value1;

0 commit comments

Comments
 (0)