Repository navigation
Expand file tree
/
Copy pathcamara.js
More file actions
executable file
·213 lines (187 loc) · 6.07 KB
/
Copy pathcamara.js
File metadata and controls
executable file
·213 lines (187 loc) · 6.07 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Esse arquivo será responsável por controlar a chart com os dados
// das câmaras municipais.
function drawSankey(error, vereadores) {
// dados dos vereadores eleitos do RJ de 2004 a 2016
// window.vereadores = vereadores;
// AINDA NÃO FUNCIONA!
// TODO: Consertar essa coisa.
// window.geraGrafo = function (d, anos, id) {
// var graph = {
// "nodes": [],
// "links": []
// };
// if (anos.length > 1) {
// for (var k = 0; k < anos.length - 1; k++) {
// for (var i in d[anos[k]][id].SIGLAS) {
// graph.nodes.push({
// "name": d[anos[k]] + "_" + d[anos[k]][id].SIGLAS[i].ORIENTACAO
// });
// graph.nodes.push({
// "name": d[anos[k + 1]] + "_" + d[anos[k + 1]][id].SIGLAS[i].ORIENTACAO
// }); +
// graph.links.push({
// "source": ENUM_ORIENTACAO[d[anos[k]][id].SIGLAS[i].ORIENTACAO],
// "target": ENUM_ORIENTACAO[d[anos[k + 1]][id].SIGLAS[i].ORIENTACAO],
// "value": d[anos[k]][id].SIGLAS[i].CONT
// });
// // console.log(d[ano][id].SIGLAS);
// }
// }
// } else {
// console.log("ERRO: O parametro anos deve ter no minimo 2 elementos.");
// return undefined;
// }
// return graph;
// };
const UPLOAD_FILENAME = "data/camara-rj.csv";
const CHART_TITLE = "Câmara Municipal do Rio";
const units = "Cadeiras";
const margin = {
top: 20,
right: 10,
bottom: 50,
left: 10
},
chartWidth = 850,
chartHeight = 600;
const sankey_nodes = [100, 55];
const sankey_links = [100, 55];
const title_position = [350, 25];
// zero decimal places
function format(d) {
return d3.format(",.0f")(d) + " " + units;
}
function translate(x, y) { return `translate(${x}, ${y})` };
const color = d3.scale
.linear()
.domain([1, 6])
.interpolate(d3.interpolateRgb)
.range(["red", "blue"]);
// Inicializa o elemento SVG da chart
const svg = d3
.select("#sankeyChart")
.attr("width", chartWidth)
.attr("height", chartHeight)
.append("g");
// Coloca o título na chart
svg.append("text")
.text(CHART_TITLE)
.attr("class", "chartTitle2")
.attr("transform", translate(...title_position));
// Set the sankey diagram properties
var sankey = d3.sankey(chartWidth, chartHeight - 100)
.nodeWidth(50)
.nodePadding(30)
.size([chartWidth, chartHeight - 100]);
var path = sankey.link();
// load the data (using the timelyportfolio csv method)
d3.csv(UPLOAD_FILENAME, function (error, data) {
//set up graph in same style as original example but empty
graph = {
"nodes": [],
"links": []
};
data.forEach(function (d) {
graph.nodes.push({
"name": d.source
});
graph.nodes.push({
"name": d.target
});
graph.links.push({
"source": d.source,
"target": d.target,
"value": parseInt(d.value)
});
console.log({
"source": d.source,
"target": d.target,
"value": parseInt(d.value)
});
});
// return only the distinct / unique nodes
graph.nodes = d3.keys(d3.nest().key(d => d.name).map(graph.nodes));
// loop through each link replacing the text with its index from node
graph.links.forEach(d => {
d.source = graph.nodes.indexOf(d.source);
d.target = graph.nodes.indexOf(d.target);
});
//now loop through each nodes to make nodes an array of objects
// rather than an array of strings
graph.nodes.forEach((d, i) => {
graph.nodes[i] = {"name": d};
});
sankey
.nodes(graph.nodes)
.links(graph.links)
.layout([]);
// add in the links
var link = svg.append("g")
.attr("transform", translate(...sankey_links))
.selectAll(".link")
.data(graph.links)
.enter().append("path")
.attr("class", d => `link ${!d.value ? 'zero' : ''}`)
.attr("d", path)
.style("stroke-width", function (d) {
return Math.max(1, d.dy);
})
.sort((a, b) => b.dy - a.dy);
// add the link titles
link.append("title")
.text(d => `${d.source.name} ${d.target.name} \n ${d.value}`);
console.log(graph.nodes);
// add in the nodes
const node = svg.append("g")
.attr("transform", translate(...sankey_nodes))
.selectAll(".node")
.data(graph.nodes)
.enter()
.append("g")
.attr("class", d => `node ${!d.value ? 'zero' : ''}`)
.attr("transform", d => translate(d.x, d.y))
.call(d3.behavior.drag()
.origin(d => d)
.on("dragstart", function () {
this.parentNode.appendChild(this);
})
.on("drag", dragmove));
// add the rectangles for the nodes
node.append("rect")
.attr("height", function (d) {
return d.dy;
})
.attr("width", sankey.nodeWidth())
.style("fill", d => d.color = color(d.name[6]) )
.style("stroke", d => d3.rgb(d.color).darker(2) )
.append("title")
.text(d => d.name + "\n" + format(d.value) );
// add in the title for the nodes
node.append("text")
.attr("x", -6)
.attr("y", d => d.dy / 2)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.attr("transform", null)
.text(d => d.name)
.filter(d => d.x < chartWidth / 2)
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start");
// the function for moving the nodes
function dragmove(d) {
d.y = Math.max(0, Math.min(chartHeight - d.dy, d3.event.y));
d3.select(this)
.attr("transform",
translate(d.x, d.y));
sankey.relayout();
link.attr("d", path);
}
});
};
document.addEventListener('DOMContentLoaded', () => {
// Leitura dos dados dos vereadores
// d3.queue()
// .defer(d3.json, "vereadores1.json")
// .await(drawSankey);
drawSankey({}, {});
});