-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinal.html
334 lines (279 loc) · 8.77 KB
/
final.html
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>D3 Test</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<link type="text/css" rel="stylesheet" href="./css/estilos.css"/>
</head>
<body>
<div id="all" style="width: 1100px; height: 450px"></div>
<script type="text/javascript">
var first_year;
var number_years;
var energy_type;
var place = "Total";
var places;
var max;
//create SVG and graph
function createSVG(energy_type, number_years, first_year,place, places, data){
places = orderPlaces(places,place);
var width = 850;
var height = 400;
var max = getMax(energy_type.renewable_energy,energy_type.non_renewable_energy);
var data_distance = 770/number_years;
var change_factor = 340/Math.max(max,1);
var tx = 75;
var ty = 370;
var colors = ["#009900","#990000"];
var type = ["energia renovable", "energia no renovable"];
var widthC = 230;
var heightC = 200;
var x = d3.scale.linear().domain([parseInt(first_year),parseInt(first_year)+parseInt(number_years)-1]).range([75,795]);
var y = d3.scale.linear().domain([0, max]).range([height-30,30]);
var xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(number_years);
var yAxis = d3.svg.axis().scale(y).orient("left").ticks(5);
//create SVG
var svg = d3.select("#all")
.append("svg")
.attr("width",width)
.attr("height",height)
.style('border','2px solid black');
//bottom axis
svg.append("g")
.attr("class","axis")
.attr("transform","translate(0,"+ty+")")
.call(xAxis);
//left axis
svg.append("g")
.attr("class","axis")
.attr("transform","translate("+tx+",0)")
.call(yAxis);
for(var i=0;i<number_years;i++){
//renewable energy
svg.append("circle")
.style("fill",colors[0])
.attr("r",4)
.attr("cx",tx+i*data_distance)
.attr("cy",ty)
.transition()
.duration(1000)
.attr("cy",ty-energy_type.renewable_energy[i]*change_factor)
.attr("value",energy_type.renewable_energy[i])
.style("fill",colors[0]);
if(i!=0){
svg.append("line")
.style("stroke-width",2)
.style("stroke",colors[0])
.attr("x1",tx+(i-1)*data_distance)
.attr("x2",tx+i*data_distance)
.attr("y1",ty)
.attr("y2",ty)
.transition()
.duration(1000)
.attr("y1",ty-energy_type.renewable_energy[i-1]*change_factor)
.attr("y2",ty-energy_type.renewable_energy[i]*change_factor);
}
//non-renewable energy
svg.append("circle")
.style("fill",colors[1])
.attr("cx",tx+i*data_distance)
.attr("cy",0)
.transition()
.duration(1000)
.attr("cy",ty-energy_type.non_renewable_energy[i]*change_factor)
.attr("value",energy_type.non_renewable_energy[i])
.attr("r",4);
if(i!=0){
svg.append("line")
.style("stroke-width",2)
.style("stroke",colors[1])
.attr("x1",tx+(i-1)*data_distance)
.attr("x2",tx+i*data_distance)
.attr("y1",0)
.attr("y2",0)
.transition()
.duration(1000)
.attr("y1",ty-energy_type.non_renewable_energy[i-1]*change_factor)
.attr("y2",ty-energy_type.non_renewable_energy[i]*change_factor);
}
}
//Caption
var svgC = d3.select("#all")
.append("svg")
.attr("width",widthC)
.attr("height",heightC);
svgC.append("text")
.attr("x",widthC/2-50)
.attr("y",20)
.text(place)
.style("font-family","arial")
.style("text-decoration","underline")
.style("font-size","18px");
for(i=0;i<2;i++){
svgC.append("circle")
.attr("cx", 20)
.attr("cy",50+i*40)
.attr("r",8)
.style("fill",colors[i]);
svgC.append("text")
.attr("x",40)
.attr("y",56+i*40)
.text(type[i])
.style("font-family","arial")
.style("font-weight","bold")
.style("font-size","18px");
}
//menu
var select = d3.select('div')
.attr("id","all")
.append('select')
.attr('class','select')
.on('change',onchange)
var options = select
.selectAll('option')
.data(places).enter()
.append('option')
.text(function (d) { return d; });
function onchange() {
place = d3.select('select').property('value')
d3.selectAll("svg").remove();
d3.selectAll("text").remove();
d3.selectAll('select').remove();
energy_type = groupData(data,number_years,first_year,place);
createSVG(energy_type,number_years,first_year,place,places,data);
};
}
//return the first year of the dataset
function firstYear(data){
var year = data[0].Fecha_validez;
for(i=0;i<data.length;i++){
if(data[i].Fecha_validez<year){
year=parseInt(data[i].Fecha_validez);
}
}
return year;
}
//return the number of years between the max and min
function nYears(data,first_year){
var year=0;
for (i=0;i<data.length;i++){
if(parseInt(data[i].Fecha_validez)>year){
year=parseInt(data[i].Fecha_validez);
}
}
return parseInt(year-first_year)+1;
}
//group energy in renewable and non-renewable
function groupData(data,number_years,first_year,place){
energy_type={renewable_energy:[],non_renewable_energy:[]};
//initialize the data with 0"s
for(i=0;i<number_years;i++){
energy_type.renewable_energy[i] = 0;
energy_type.non_renewable_energy[i] = 0;
}
if (place == "Total"){
//non-renewable energy
for(i=0;i<data.length;i++){
if(data[i].Indicador =="Produccion de energia con carbon"){
energy_type.non_renewable_energy[data[i].Fecha_validez-first_year]+=parseInt(data[i].Valor);
}
if(data[i].Indicador =="Producción de energía nuclear"){
energy_type.non_renewable_energy[data[i].Fecha_validez-first_year]+=parseInt(data[i].Valor);
}
//renewable energy
if(data[i].Indicador =="Produccion de energia eolica"){
energy_type.renewable_energy[data[i].Fecha_validez-first_year]+=parseInt(data[i].Valor);
}
if(data[i].Indicador =="Produccion de energia hidraulica"){
energy_type.renewable_energy[data[i].Fecha_validez-first_year]+=parseInt(data[i].Valor);
}
if(data[i].Indicador =="Produccion energia solar en Castilla y Leon"){
energy_type.renewable_energy[data[i].Fecha_validez-first_year]+=parseInt(data[i].Valor);
}
}
}
//non-renewable energy
for(i=0;i<data.length;i++){
if(data[i].Provincia==place){
if(data[i].Indicador =="Produccion de energia con carbon"){
energy_type.non_renewable_energy[data[i].Fecha_validez-first_year]+=parseInt(data[i].Valor);
}
if(data[i].Indicador =="Producción de energía nuclear"){
energy_type.non_renewable_energy[data[i].Fecha_validez-first_year]+=parseInt(data[i].Valor);
}
//renewable energy
if(data[i].Indicador =="Produccion de energia eolica"){
energy_type.renewable_energy[data[i].Fecha_validez-first_year]+=parseInt(data[i].Valor);
}
if(data[i].Indicador =="Produccion de energia hidraulica"){
energy_type.renewable_energy[data[i].Fecha_validez-first_year]+=parseInt(data[i].Valor);
}
if(data[i].Indicador =="Produccion energia solar en Castilla y Leon"){
energy_type.renewable_energy[data[i].Fecha_validez-first_year]+=parseInt(data[i].Valor);
}
}
}
return energy_type;
}
//return the maximun of the 2 sets
function getMax(data,data2){
var max1=0;
for(i=0;i<data.length;i++){
if(max1<data[i]){
max1=data[i];
}
}
if(data2!=null){
for(i=0;i<data2.length;i++){
if(max1<data2[i]){
max1=data2[i];
}
}
}
return max1;
}
//return true if place is in array
function isIn(array,place){
for(i=0;i<array.length;i++){
if(place==array[i]){
return true;
}
}
return false;
}
//return the places that appear on the dataset
function getPlaces(data){
var places = ["Total"];
var place;
for(j=0;j<data.length;j++){
place = data[j].Provincia;
if(!isIn(places,place)){
places[places.length]=place;
}
}
return places;
}
//order the set placeing place on first place
function orderPlaces(places, place){
var array = [];
var temp;
array[0]=place;
for(i=0;i<places.length;i++){
temp=places[i];
if(!isIn(array,temp)){
array[array.length]=temp;
}
}
return array;
}
d3.csv("./datos/Produccion_energia3.csv",function(error, data){
first_year = firstYear(data);
number_years = nYears(data,first_year);
energy_type = groupData(data,number_years,first_year,place);
places = getPlaces(data);
createSVG(energy_type,number_years,first_year,place,places,data);
});
</script>
</body>
</html>