-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.js
145 lines (121 loc) · 4.48 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
function map(el, state) {
const width = window.innerWidth;
const height = window.innerHeight;
const normalize = d => (d || '').toLowerCase().replace(/í/, 'i')
const projection = d3.geoMercator()
.translate([width / 2, height / 2])
.scale((width - 1) / 2 / Math.PI);
const path = d3.geoPath()
.projection(projection);
const div = d3.select('body').append('div')
.attr('class', 'tooltip')
const map = d3.map()
const zoom = d3.zoom()
.scaleExtent([0, 16])
.on('zoom', zoomed);
const svg = d3.select(el).append('svg')
.attr('width', width)
.attr('height', height);
const g = svg.append('g')
.attr('class', 'world')
const b = svg.append('g')
.attr('class', 'bolivia')
svg.call(zoom);
const {elections, dataTypes, colors} = state.get()
const partidos = Object.keys(colors)
const processData = (propName) => (d) => {
const key = normalize(d.properties[propName])
const dataPoint = map.get(key)
if (!dataPoint) {
return 'green'
}
const sorted = partidos.map(p => ({
name: p,
votes: parseInt(dataPoint[p])
})).sort((a, b) => a.votes > b.votes ? -1 : 1)
const [first, second] = [sorted[0].votes, sorted[1].votes]
const diff = (first - second)/(first + second)
const color = d3.color(colors[sorted[0].name])
return color.darker(diff) + ""
}
const loadMundo = () =>
d3.json('./data/mundo.topo.json')
.then(world => {
console.error('world', world)
g.append('path')
.datum({ type: 'Sphere' })
.attr('class', 'sphere')
.attr('d', path);
g.append('path')
.datum(topojson.merge(world, world.objects.mundo.geometries))
.attr('class', 'land')
.attr('d', path);
})
const loadDepartamentos = () =>
d3.json('./data/departamentos-quant.topo.json')
.then(bolivia => {
b.selectAll('path.departamento')
.data(topojson.feature(bolivia, bolivia.objects.departamentos).features)
.enter().append('path')
.attr('class', 'departamento')
.attr('d', path)
})
const loadMunicipios = () =>
d3.json('./data/municipios-quant.topo.json')
.then(bolivia => {
b.selectAll('path.municipio')
.data(topojson.feature(bolivia, bolivia.objects.municipios).features)
.enter().append('path')
.attr('class', 'municipio')
.attr('d', path)
})
function handleState(e) {
let {dataPoint, view} = state.get()
console.error('view', view, map)
elections[dataPoint][dataTypes[view]].map(d =>
map.set(normalize(d[view]), d))
render(view)
}
Promise.all([loadMundo(), loadDepartamentos(), loadMunicipios()])
.then(handleState)
state.addEventListener('state:changed', handleState);
function render(view) {
const propName = view === 'Departamento' ? 'NOM_DEP':'NOM_MUN'
const selector = view.toLowerCase()
b.selectAll('path')
.attr('fill', 'transparent')
.attr('stroke-opacity', 0.2)
.on('mouseover', () => {})
b.selectAll(`path.${selector}`)
.attr('fill', processData(propName))
.attr('stroke-opacity', 1)
.attr('d', path)
}
function onMouseOver(
prop,
notFound = data => ({
Pais: data.properties.name,
resultados: 'no hay datos'
})) {
return (data) => {
const key = normalize(data.properties[prop])
d = map.get(key)
if (!d) {
d = notFound(data)
}
div.transition()
.duration(200)
.style('opacity', 0.9)
div.html(
Object.keys(d).map(f => `<span><b>${f}:</b> ${d[f]}</span>`).join('')
)
.style('left', (d3.event.pageX) + 'px')
.style('top', (d3.event.pageY - 28) + 'px')
}
}
function zoomed() {
svg
.selectAll('path') // To prevent stroke width from scaling
.attr('transform', d3.event.transform);
}
}