Skip to content
Snippets Groups Projects
Commit fa85a3c5 authored by iniaran's avatar iniaran
Browse files

Upload New File

parent f6bc012c
Branches
No related tags found
No related merge requests found
/*Inigo Aranzadi Bouzas*/
/*Dibujar el grafico
como parametros:
zona:posicion donde se va a representar el grafico
seleccionado:poblacion elegida para la representacion
*/
function dibujar(zona,seleccionado){
/*Definimos los margenes*/
var margin = {top: 70, right: 20, bottom: 100, left: 100},
w = 750 - margin.left - margin.right,
h = 500 - margin.top - margin.bottom;
/*Definimos las escalas*/
var x = d3.scaleBand().rangeRound([0,w]).padding(.1);
var y = d3.scaleLinear().range([h, 0]);
/*Definimos los ejes*/
var xAxis = d3.axisBottom(x);
var yAxis = d3.axisLeft(y);
/*Definimos el grid*/
var yGrid = d3.axisLeft().scale(y).tickSize(-w, 0, 0).tickFormat("");
var xGrid = d3.axisBottom().scale(x).tickSize(h,0,0).tickFormat("");
var svg = d3.select(zona).append("svg")
.attr("width", w + margin.left + margin.right)
.attr("height", h + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + ", " + margin.top + ")");
/*lectura de datos*/
d3.csv("nacimientos.csv").then(function(data){
x.domain(data.map(function(d) { return d.year; }));
y.domain([0, d3.max(data, function(d) { return d.total; })]);
/*Dibujamos los ejes y el grid*/
svg.append("g").attr("class", "x axis").attr("transform", "translate(0, " + h + ")").attr("stroke-width",1.5).call(xAxis)
.selectAll("text").attr("transform", "rotate(90)").attr("x", "10").attr("y", "-3").attr("fill","#43662E").style("text-anchor", "start").attr("font-family", "calibri"); //pone los años en vertical
svg.append("g").attr("class", "y axis").attr("fill","#43662E").attr("stroke-width",1.5).attr("font-family", "calibri").call(yAxis);
svg.append("g").attr("class", "y grid").call(yGrid);
/*Ponemos el titulo*/
var title = svg.append("g")
.attr("class", "title");
title.append("text")
.attr("x", (w/2))
.attr("y", -30 )
.attr("text-anchor", "middle")
.style("font-size", "30px").style("font-weight","bold")
.attr("font-family", "calibri")
.attr("fill","#43662E")
.text("Natalidad entre 1969 y 2008");
/*etiqueta del eje de la y*/
var labels = svg.append("g").attr("class", "y labels");
labels.append("text").attr("transform", "rotate(-90)").attr("fill","#43662E").attr("y", -80).attr("dy", ".71em").attr("x",-80).style("text-anchor", "end").attr("font-family", "calibri").text("Número de nacimientos");
/*etiqueta del eje de la x*/
var labels2 = svg.append("g").attr("class", "x labels");
labels2.append("text").attr("y", 400).attr("x",(w/2)).attr("fill","#43662E").attr("font-family", "calibri").attr("text-anchor", "middle").text("Años");
/*Creamos la lineas de puntos*/
var thombres = d3.line()
.x(function(d) { return x(d.year);})
.y(function(d) { return y(d.hombres);});
var tmujeres = d3.line()
.x(function(d) { return x(d.year);})
.y(function(d) { return y(d.mujeres);});
var ttotal = d3.line()
.x(function(d) { return x(d.year);})
.y(function(d) { return y(d.total);});
if(seleccionado==1 || seleccionado==2){
/*Dibujamos la linea de los hombres*/
svg.append("path")
.attr("class", "line")
.attr("d", thombres(data))
.attr("stroke","#A1D57E")
.attr("stroke-width",2.5)
.attr("fill","none");
}
if(seleccionado==1 || seleccionado==3){
/*Dibujamos la linea de las mujeres*/
svg.append("path")
.attr("class", "line")
.attr("d", tmujeres(data))
.attr("stroke","#F0F786")
.attr("stroke-width",2.5)
.attr("fill","none");
}
if(seleccionado==1 || seleccionado==4){
/*Dibujamos la linea total*/
svg.append("path")
.attr("class", "line")
.attr("d", ttotal(data))
.attr("stroke","#36AF57")
.attr("stroke-width",2.5)
.attr("fill","none");
}
});
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment