-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
70 lines (58 loc) · 1.75 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
67
68
69
70
const topojson = require('topojson')
const topoJson = require('./us.json')
const D3Node = require('d3-node')
const d3 = D3Node.d3
const defaultStyles = `
.states { fill: none; stroke: #fff; stroke-linejoin: round;}
.counties { fill: none;}
`
function choroplethMap ({ data, colors, scale, idField = 0, metricField = 1, styles = defaultStyles } = {}) {
const d3n = new D3Node({ styles })
const path = d3.geoPath()
let id = idField
if (Number.isInteger(idField)) {
id = Object.keys(data[0])[idField]
}
let metricProp = metricField
if (Number.isInteger(metricField)) {
metricProp = Object.keys(data[0])[metricField]
}
// create Map object for the County lookup
const dataMap = new Map()
data.forEach((item) => {
dataMap.set(item[id], item[metricProp])
})
// County color fill
const fill = function (d) {
const colorScale = d3.scaleThreshold()
.domain(scale)
.range(colors)
const metric = dataMap.get(d.id)
return colorScale(metric)
}
const svg = d3n.createSVG(960, 700)
svg.append('g')
.attr('class', 'counties')
.selectAll('path')
.data(topojson.feature(topoJson, topoJson.objects.counties).features)
.enter()
.append('path')
.attr('class', 'county')
.attr('data-metric', function (d) {
return dataMap.get(d.id)
})
.attr('data-id', function (d) {
return d.id
})
.attr('fill', fill)
.attr('d', path)
svg.append('path')
.datum(topojson.mesh(topoJson, topoJson.objects.states, function (a, b) { return a !== b }))
.attr('class', 'states')
.attr('d', path)
return d3n
}
module.exports = choroplethMap
module.exports.d3 = d3
module.exports.dsvFormat = d3.dsvFormat
module.exports.csvParse = d3.csvParse