-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
66 lines (54 loc) · 1.83 KB
/
index.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
const topojson = require('topojson')
const topoJson = require('./us-states.json')
const D3Node = require('d3-node')
const d3 = D3Node.d3
const defaultStyles = `
.states { pointer-events: none; fill: #ccc; stroke: #fff; stroke-width: 1px; stroke-linejoin: round;}
`
const defaultRadius = function (d) {
return d.POP_2010 / 150000
}
const defaultFill = function (d) {
const colorScale = d3.scaleThreshold()
.domain([1, 4, 8, 15, 20, 30, 40, 50, 60])
.range([
'rgb(255,245,240)', 'rgb(254,224,210)', 'rgb(252,187,161)',
'rgb(252,146,114)', 'rgb(251,106,74)', 'rgb(239,59,44)',
'rgb(203,24,29)', 'rgb(165,15,21)', 'rgb(103,0,13)'
])
return colorScale(d.POP_2010 / 150000)
}
function statesMap ({ markers, radius = defaultRadius, fill = defaultFill, styles = defaultStyles } = {}) {
const d3n = new D3Node({ styles })
const projection = d3.geoAlbersUsa()
const path = d3.geoPath().projection(projection)
const rows = markers.filter(function (d) { return projection([d.longitude, d.latitude]) })
const svg = d3n.createSVG(960, 500)
svg.selectAll('.states')
.data(topojson.feature(topoJson, topoJson.objects.states).features)
.enter()
.append('path')
.attr('class', 'states')
.attr('d', path)
const circleGroup = svg.selectAll('circle')
.data(rows)
.enter()
.append('g')
.attr('transform', function (d) {
const coords = projection([ d.longitude, d.latitude ])
return `translate(${coords})`
})
.attr('class', 'map-point')
circleGroup
.append('circle')
.style('fill', fill)
.style('stroke', 'grey')
.style('fill-opacity', 0.8)
.attr('r', radius)
return d3n
}
module.exports = statesMap
module.exports.d3 = d3
module.exports.dsvFormat = d3.dsvFormat
module.exports.csvParse = d3.csvParse
module.exports.scaleThreshold = d3.scaleThreshold