This repository has been archived by the owner on May 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Map.js
183 lines (172 loc) · 4.93 KB
/
Map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React, {Component} from 'react'
import {geoAlbers} from 'd3-geo'
import {css} from 'glamor'
import {timer} from 'd3-timer'
import {
colors, fontFamilies
} from '@project-r/styleguide'
const toGeoJson = data => ({
type: 'FeatureCollection',
features: data.map(d => ({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [d.lon, d.lat]
}
}))
})
const styles = {
circlePos: css({
// transition: 'cx 1s ease-in-out, cy 1s ease-in-out, r 1s ease-in-out'
}),
labelOutline: css({
fill: '#fff',
fontFamily: fontFamilies.sansSerifRegular,
fontSize: 12,
stroke: '#fff',
strokeWidth: 2
}),
label: css({
fill: colors.primary,
fontFamily: fontFamilies.sansSerifRegular,
fontSize: 12
})
}
class PostalCodeMap extends Component {
constructor (...args) {
super(...args)
this.state = {}
this.projection = geoAlbers()
.rotate([0, 0])
.center([8.23, 46.8])
.scale(13000)
this.containerRef = ref => {
this.container = ref
}
this.measure = () => {
const width = this.container.getBoundingClientRect().width
const height = Math.min(width / 1.5, window.innerHeight * 0.65)
const extentData = this.props.extentData || this.props.data
if (
width !== this.state.width ||
extentData !== this.state.extentData
) {
const targetProjection = geoAlbers()
.rotate([0, 0])
.center([8.23, 46.8])
.fitExtent(
[[10, 10], [width - 10, height - 10]],
toGeoJson(extentData)
)
const currentScale = this.projection.scale()
const targetScale = targetProjection.scale()
const currentTranslate = this.projection.translate()
const targetTranslate = targetProjection.translate()
const duration = 1000
if (this.timer) {
this.timer.stop()
}
this.timer = timer(elapsed => {
const t = Math.min(elapsed / duration, 1)
this.projection.scale(
currentScale * (1 - t) + targetScale * t
)
this.projection.translate(
[
currentTranslate[0] * (1 - t) + targetTranslate[0] * t,
currentTranslate[1] * (1 - t) + targetTranslate[1] * t
]
)
this.setState({t})
if (t >= 1) {
this.timer.stop()
}
})
// this.projection.fitExtent(
// [[10, 10], [width - 10, height - 10]],
// toGeoJson(extentData)
// )
this.setState({
width,
height,
extentData
})
}
}
}
componentDidMount () {
window.addEventListener('resize', this.measure)
this.measure()
}
componentDidUpdate () {
this.measure()
}
componentWillUnmount () {
window.removeEventListener('resize', this.measure)
}
render () {
const {width, height} = this.state
const {data, labels, labelOptions, filter} = this.props
const {projection} = this
const scale = projection.scale()
const radius = d => (
Math.max(0.3, Math.sqrt(d.count) * scale * 0.00001)
)
return (
<div ref={this.containerRef}>
<svg width={width || '100%'} height={height || 300}>
{
data.map((d, i) => {
const [x, y] = projection([d.lon, d.lat])
return (
<circle
key={`bubble${i}`} {...styles.circlePos}
cx={x}
cy={y}
fill={colors.primary}
fillOpacity={0.1}
stroke={colors.primary}
strokeOpacity={(
filter
? (d.postalCode && d.postalCode.startsWith(filter) ? 1 : 0)
: 1
)}
r={radius(d)}>
<title>{`${d.postalCode} ${d.name}: ${d.count}`}</title>
</circle>
)
})
}
{
labels.map((d, i) => {
let [x, y] = projection([d.lon, d.lat])
if (!labelOptions.center) {
x += radius(d)
}
if (labelOptions.xOffset) {
x += labelOptions.xOffset
}
const textStyle = {
textAnchor: labelOptions.center ? 'middle' : 'start'
}
const text = labelOptions.postalCode
? d.postalCode : d.name
return (
<g key={`label${i}`}
transform={`translate(${x} ${y})`}>
<text dy='0.3em' {...styles.labelOutline} style={textStyle}>
{text}
</text>
<text dy='0.3em' {...styles.label} style={textStyle}>
{text}
</text>
</g>
)
})
}
</svg>
</div>
)
}
}
export default PostalCodeMap