Skip to content

Commit e0726b5

Browse files
committed
intelligenceMap - static province centroid values
1 parent e0571de commit e0726b5

File tree

6 files changed

+183
-87
lines changed

6 files changed

+183
-87
lines changed

src/components/generic-map.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const propTypes = {
4747
showTooltip: PropTypes.bool,
4848
renderTooltip: PropTypes.func,
4949
pitch: PropTypes.number,
50-
controller: PropTypes.bool
50+
controller: PropTypes.bool,
5151
}
5252

5353
const defaultProps = {
@@ -61,7 +61,7 @@ const defaultProps = {
6161
showTooltip: false,
6262
renderTooltip: undefined,
6363
pitch: 0,
64-
controller: true
64+
controller: true,
6565
}
6666

6767
// DeckGL react component
@@ -79,7 +79,7 @@ const Map = ({
7979
pitch,
8080
mapboxApiAccessToken,
8181
mapStyle,
82-
controller
82+
controller,
8383
}) => {
8484
const deckRef = useRef()
8585
const [viewState, setViewState] = useState(INIT_VIEW_STATE)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"centroid": [
3+
{
4+
"properties": {
5+
"NAME": "Alberta",
6+
"CODE": "AB"
7+
},
8+
"coordinates": [-114.921, 56.535]
9+
},
10+
{
11+
"properties": {
12+
"NAME": "Saskatchewan",
13+
"CODE": "SK"
14+
},
15+
"coordinates": [-105.571, 54.463]
16+
},
17+
{
18+
"properties": {
19+
"NAME": "Manitoba",
20+
"CODE": "MB"
21+
},
22+
"coordinates": [-97.135, 55.534]
23+
},
24+
{
25+
"properties": {
26+
"NAME": "Newfoundland & Labrador",
27+
"CODE": "NL"
28+
},
29+
"coordinates": [-60.642, 54.945]
30+
},
31+
{
32+
"properties": {
33+
"NAME": "PEI",
34+
"CODE": "PE"
35+
},
36+
"coordinates": [-61.304, 48.007]
37+
},
38+
{
39+
"properties": {
40+
"NAME": "Nova Scotia",
41+
"CODE": "NS"
42+
},
43+
"coordinates": [-61.642, 43.522]
44+
},
45+
{
46+
"properties": {
47+
"NAME": "Northwest Territories",
48+
"CODE": "NT"
49+
},
50+
"coordinates": [-118.069, 63.099]
51+
},
52+
{
53+
"properties": {
54+
"NAME": "Nunavut",
55+
"CODE": "NU"
56+
},
57+
"coordinates": [-97.063, 62.997]
58+
},
59+
{
60+
"properties": {
61+
"NAME": "Ontario",
62+
"CODE": "ON"
63+
},
64+
"coordinates": [-87.572, 51.515]
65+
},
66+
{
67+
"properties": {
68+
"NAME": "New Brunswick",
69+
"CODE": "NB"
70+
},
71+
"coordinates": [-70.959, 45.570]
72+
},
73+
{
74+
"properties": {
75+
"NAME": "Yukon Territory",
76+
"CODE": "YT"
77+
},
78+
"coordinates": [-134.181, 61.766]
79+
},
80+
{
81+
"properties": {
82+
"NAME": "British Columbia",
83+
"CODE": "BC"
84+
},
85+
"coordinates": [-126.443, 54.904]
86+
},
87+
{
88+
"properties": {
89+
"NAME": "Quebec",
90+
"CODE": "QC"
91+
},
92+
"coordinates": [-73.504, 51.220]
93+
}
94+
]
95+
}

src/components/intelligenceMap/hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ export const getCityFillColor = (value, max, min) => {
3030
const totalLightnessDiff = maxLightness - minLightness
3131
const lightness = maxLightness - ((value - min) / totalDiff * totalLightnessDiff)
3232
return hslToRgbConverter(210, 63, lightness)
33-
}
33+
}

src/components/intelligenceMap/intelligence-map.js

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useMemo } from 'react'
1+
import React, { useState } from 'react'
22
import PropTypes from 'prop-types'
33

44
import { FlyToInterpolator } from '@deck.gl/core'
@@ -13,11 +13,10 @@ import {
1313
} from '../../shared/map-props'
1414

1515
const INIT_VIEW_STATE = {
16-
pitch: 0,
17-
bearing: 0,
16+
bearing: -4,
1817
transitionDuration: 1000,
1918
transitionInterpolator: new FlyToInterpolator(),
20-
latitude: 58,
19+
latitude: 57,
2120
longitude: -94,
2221
zoom: 2.6,
2322
}
@@ -38,6 +37,7 @@ const IntelligenceMap = ({
3837
geoProvinceCentroidJson,
3938
intelligenceProvince,
4039
intelligenceCity,
40+
pitch,
4141
}) => {
4242
const [hoverProvince, setHoverProvince] = useState({})
4343
const [hoverCity, setHoverCity] = useState({})
@@ -67,7 +67,7 @@ const IntelligenceMap = ({
6767
})
6868

6969
const handleFillColor = (type, d) => {
70-
let fillColor;
70+
let fillColor
7171

7272
if (type === 'outer') {
7373
fillColor = getProvinceFillColor(d.value.total, maxP, minP)
@@ -101,53 +101,50 @@ const IntelligenceMap = ({
101101
return toolTipText
102102
}
103103

104-
const layers = useMemo(() => {
105-
return [
106-
new GeoJsonLayer({
107-
id: 'outerGeo-layer',
108-
data: intelligenceProvince,
109-
pickable: true,
110-
stroked: true,
111-
filled: true,
112-
getLineColor: [0, 0, 0],
113-
lineWidthMinPixels: 1,
114-
getFillColor: d => handleFillColor('outer', d),
115-
updateTriggers: {
116-
getFillColor: { hoverProvince },
117-
},
118-
onHover: data => setHoverProvince(data),
119-
}),
120-
121-
new GeoJsonLayer({
122-
id: 'innerGeo-layer',
123-
data: intelligenceCity,
124-
pickable: true,
125-
stroked: true,
126-
filled: true,
127-
getLineColor: [0, 0, 0],
128-
lineWidthMinPixels: 1,
129-
getFillColor: d => handleFillColor('inner', d),
130-
updateTriggers: {
131-
getFillColor: [hoverCity],
132-
},
133-
onHover: data => setHoverCity(data),
134-
}),
135-
136-
new TextLayer({
137-
id: 'text-layer',
138-
data: geoProvinceCentroidJson,
139-
billboard: false,
140-
pickable: false,
141-
getPosition: d => d.centroid.coordinates,
142-
getText: d => `${d.name}\n ${d.value.percentage}%`,
143-
getPixelOffset: d => [d.offset.x, d.offset.y],
144-
getSize: 12,
145-
getAngle: 0,
146-
getTextAnchor: 'middle',
147-
getAlignmentBaseline: 'center',
148-
}),
149-
]
150-
})
104+
const layers = [
105+
new GeoJsonLayer({
106+
id: 'outerGeo-layer',
107+
data: intelligenceProvince,
108+
pickable: true,
109+
stroked: true,
110+
filled: true,
111+
getLineColor: [0, 0, 0],
112+
lineWidthMinPixels: 1,
113+
getFillColor: d => handleFillColor('outer', d),
114+
updateTriggers: {
115+
getFillColor: { hoverProvince },
116+
},
117+
onHover: data => setHoverProvince(data),
118+
}),
119+
120+
new GeoJsonLayer({
121+
id: 'innerGeo-layer',
122+
data: intelligenceCity,
123+
pickable: true,
124+
stroked: true,
125+
filled: true,
126+
getLineColor: [0, 0, 0],
127+
lineWidthMinPixels: 1,
128+
getFillColor: d => handleFillColor('inner', d),
129+
updateTriggers: {
130+
getFillColor: [hoverCity],
131+
},
132+
onHover: data => setHoverCity(data),
133+
}),
134+
135+
new TextLayer({
136+
id: 'text-layer',
137+
data: geoProvinceCentroidJson,
138+
billboard: false,
139+
pickable: false,
140+
getPosition: d => d.coordinates,
141+
getText: d => `${d.properties.NAME}\n ${d.value.percentage}%`,
142+
getSize: 12,
143+
getAngle: 0,
144+
getTextAnchor: 'middle',
145+
getAlignmentBaseline: 'center',
146+
}),
147+
]
151148

152149
return (
153150
<Map
@@ -162,7 +159,7 @@ const IntelligenceMap = ({
162159
},
163160
}}
164161
controller={false}
165-
pitch={20}
162+
pitch={pitch}
166163
viewStateOverride={INIT_VIEW_STATE}
167164
mapboxApiAccessToken={mapboxApiAccessToken}
168165
mapStyle="mapbox://styles/chiuleung/ckon6rngz3wpg17pkwuj12hrx"
@@ -179,4 +176,4 @@ IntelligenceMap.defaultProps = {
179176
...commonDefaultProps,
180177
}
181178

182-
export default IntelligenceMap
179+
export default IntelligenceMap

src/hooks/index.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -324,30 +324,30 @@ export const useResizeObserver = (ref) => {
324324
* @return {Array} The RGB representation
325325
*/
326326
export const hslToRgbConverter = (hue, saturation, light) => {
327-
let h = hue/360;
328-
let s = saturation/100;
329-
let l = light/100
330-
331-
var r, g, b;
332-
333-
if(s == 0){
334-
r = g = b = l; // achromatic
335-
}else{
336-
var hue2rgb = function hue2rgb(p, q, t){
337-
if(t < 0) t += 1;
338-
if(t > 1) t -= 1;
339-
if(t < 1/6) return p + (q - p) * 6 * t;
340-
if(t < 1/2) return q;
341-
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
342-
return p;
343-
}
344-
345-
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
346-
var p = 2 * l - q;
347-
r = hue2rgb(p, q, h + 1/3);
348-
g = hue2rgb(p, q, h);
349-
b = hue2rgb(p, q, h - 1/3);
327+
let h = hue/360
328+
let s = saturation/100
329+
let l = light/100
330+
331+
var r, g, b
332+
333+
if(s == 0){
334+
r = g = b = l // achromatic
335+
}else{
336+
var hue2rgb = function hue2rgb(p, q, t){
337+
if(t < 0) t += 1
338+
if(t > 1) t -= 1
339+
if(t < 1/6) return p + (q - p) * 6 * t
340+
if(t < 1/2) return q
341+
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6
342+
return p
350343
}
351344

352-
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
345+
var q = l < 0.5 ? l * (1 + s) : l + s - l * s
346+
var p = 2 * l - q
347+
r = hue2rgb(p, q, h + 1/3)
348+
g = hue2rgb(p, q, h)
349+
b = hue2rgb(p, q, h - 1/3)
350+
}
351+
352+
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]
353353
}

stories/intelligence-map.stories.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/* eslint-disable react/prop-types */
22
import React, { useEffect, useState } from 'react'
33
import { storiesOf } from '@storybook/react'
4+
45
import IntelligenceMap from '../src/components/intelligenceMap/intelligence-map'
5-
import geoProvinceValueJson from './data/geo-province-value.json'
66
import intelligenceProvince from '../src/components/intelligenceMap/data/intelligence-province.json'
77
import intelligenceCity from '../src/components/intelligenceMap/data/intelligence-city.json'
8-
9-
import { getCircleRadiusCentroid } from '../src/shared/utils/index'
8+
import provinceCentroid from '../src/components/intelligenceMap/data/province-centroid.json'
109

1110
const MAPBOX_ACCESS_TOKEN = process.env.MAPBOX_ACCESS_TOKEN
1211

@@ -19,8 +18,12 @@ const useGeoJsonCentroidData = () => {
1918

2019
await Promise.all(geoJson.map(async (el, index) => {
2120
try {
22-
let centroid = await getCircleRadiusCentroid({ polygon: el.geometry })
23-
response.push({ name: el.properties.NAME, value: el.value, centroid, offset: el.offset ? el.offset : { x: 0, y: 0 } })
21+
response.push(
22+
{
23+
...provinceCentroid.centroid[index],
24+
value: el.value,
25+
},
26+
)
2427
} catch (error) {
2528
console.error(error)
2629
}
@@ -45,6 +48,7 @@ storiesOf('Intellignce Map', module)
4548
intelligenceProvince={ intelligenceProvince }
4649
intelligenceCity={ intelligenceCity }
4750
geoProvinceCentroidJson={ centroidJson }
51+
pitch={25}
4852
/>
4953
)
5054
})

0 commit comments

Comments
 (0)