-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapa.js
More file actions
executable file
·168 lines (152 loc) · 5.46 KB
/
Copy pathmapa.js
File metadata and controls
executable file
·168 lines (152 loc) · 5.46 KB
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
// Script de construção da chart do RJ
// by Glauco Azevedo - FGV/EMAp - Dez/2016
// Curso de Visualização de Informação - profa.: Asla Sá
// const width = 700;
// const height = 500;
var dimensions = {
width: 700,
height: 500,
};
function listarAnos(anoInicial, numPeriodos) {
let anos = [];
for(let i = 0; i < numPeriodos; i++) {
anos.push(anoInicial);
anoInicial += 4;
}
return anos;
}
var anoInicial = 2004;
var listaAnos = listarAnos(anoInicial, 4);
const ENUM_ORIENTACAO = {
1: "extrema-esquerda",
2: "esquerda",
3: "centro-esquerda",
4: "centro",
5: "centro-direita",
6: "direita",
7: "extrema-direita"
};
window.ENUM_ORIENTACAO = ENUM_ORIENTACAO;
let setAno = function(_) {
if( _ == '+' && anoInicial < listaAnos[listaAnos.length-1] ){
anoInicial += 4;
document.getElementById("ano-atual").textContent = anoInicial;
document.getElementsByClassName("chartTitle").textContent = "Prefeituras "+ anoInicial;
}
else if( _ == '-' && anoInicial > listaAnos[0]) {
anoInicial -= 4;
document.getElementById("ano-atual").textContent = anoInicial;
document.getElementsByClassName("chartTitle").textContent = "Prefeituras "+ anoInicial;
}
else {
console.log('ERRO: Opção Inválida!');
return undefined;
}
console.log('Redesenhar!');
redraw();
};
// M�todo para redesenhar a chart(na prática, só trocar as cores e valores de
// atributos);
let redraw = function() {
d3.select("#mainChart")
.selectAll("path")
.transition().delay(100)
.style("fill",function(d){
let id = d.properties.id;
if(prefeitos[anoInicial][id])
return color(prefeitos[anoInicial][id].ORIENTACAO);
});
}
let color = d3.scale.linear()
.domain([1,6])
.interpolate(d3.interpolateRgb)
.range(["red", "blue"]);
// Metodo para construir a chart do estado, com os parâmetros setados no momento.
let drawChart = function(svg,path,states) {
// console.log(ano);
svg.append("text")
.text("Prefeituras "+anoInicial)
.attr("class","chartTitle")
.attr("transform","translate(350,30)");
svg.append("g")
.attr("class","rj-state")
.selectAll("path")
.data(states.features)
.enter()
.append("path")
.attr("d",path)
.attr("class","cities")
.attr("nome",function(d){return d.properties.nome;})
.transition().delay(0)
.attr("sigla",function (d){return d.properties.sigla;})
.style("fill",function(d){
if(prefeitos[anoInicial][d.properties.id])
return color(prefeitos[anoInicial][d.properties.id].ORIENTACAO);
});
let div = d3.select("body")
.append("div")
.attr("class", "infobox")
.style("opacity", 0);
d3.selectAll("path")
.on("mouseover", function(d) {
const cidade = d.properties;
const msg = [
cidade.nome,
prefeitos[anoInicial][cidade.id].SIGLA,
ENUM_ORIENTACAO[prefeitos[anoInicial][cidade.id].ORIENTACAO]
].join("<br/>");
div.transition()
.duration(300)
.style("opacity", .9);
div.html(msg)
.style({
"left": `${d3.event.pageX}px`,
"top": `${(d3.event.pageY - 28)}px`,
"padding": "10px"
});
})
.on("mouseout", function(d) {
div.transition()
.duration(400)
.style("opacity", 0);
})
.on("click",function(d){
// Chamada do metodo que desenha o Sankey Chart de vereadores.
console.log(`Cidade: ${d.properties.nome}`);
});
};
// Parte principal do script
document.addEventListener('DOMContentLoaded', () => {
// Adicionando Listeners para alguns dos botões da página.
document.getElementById("previous").addEventListener("click",function() {
setAno('-');
});
document.getElementById("next").addEventListener("click",function() {
setAno('+');
});
document.getElementById("ano-atual").textContent = anoInicial;
d3.queue()
.defer(d3.json,"data/prefeitos.json")//Leitura dos dados dos prefeitos eleitos
.defer(d3.json,"data/rj-cidades.json")// Leitura dos dados geograficos dos municípios do RJ
.awaitAll(ready);
//Método que inicializa a chart e desenha
function ready(error, dados) {
if(error) return console.error(error);
else{
window.prefeitos = dados[0];
window.br_states = dados[1];
const svg = d3.select("#mainChart")
.attr("width", dimensions.width)
.attr("height", dimensions.height);
const projection = d3.geo
.mercator()
.center([-43,-22])
// .center([-42,-22])
.scale(8500);
const path = d3.geo.path()
.projection(projection);
const states = topojson.feature(br_states, br_states.objects.states);
drawChart(svg, path, states);
}
};
});