Skip to content

Instantly share code, notes, and snippets.

@DelgadoPanadero
Created February 20, 2017 16:29
Show Gist options
  • Save DelgadoPanadero/ab16a195a569e65c7a026538f803c7e6 to your computer and use it in GitHub Desktop.
Save DelgadoPanadero/ab16a195a569e65c7a026538f803c7e6 to your computer and use it in GitHub Desktop.
Practica D3
license: mit
height: 0
region gasto esperanza
1 South Asia 88.64 65.85
2 Europe & Central Asia 1782.67 74.53
3 Middle East & North Africa 537.55 71.96
4 East Asia & Pacific 578.73 70.99
5 Sub-Saharan Africa 81.17 54.75
6 Latin America & Caribbean 388.58 72.20
7 North America 5239.50 78.51
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Amatic+SC:400,700" rel="stylesheet">
<script src="https://d3js.org/d3.v4.min.js"></script>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div id='title'>
</div>
<div id='summary'>
</div>
<div id='graphic'>
</div>
<div id='footer'>
</div>
<script src="viz.js"></script>
</body>
body {
font-size: 20px;
font-family: sans-serif;
color: #000000;
background: #ffffff;
}
h3 {
font-family: 'Calibri';
margin-top: 20px;
margin-bottom: 10px;
font-size: 40px;
text-align: center;
}
#summary{
font-family: 'Calibri';
margin-left: 40px;
margin-top: 30px;
font-size: 15px;
text-align: justify;
}
/*chart*/
#xAxis {
font-size: 8px;
fill: none;
stroke: #3a3a3a;
}
.stacked_tooltip {
top: 0;
right: 0;
position: absolute;
padding: 5px;
line-height: 140%;
width: 220px;
height: 65px;
font-size: 80%;
color: #a00d02;
background: #FAF7F5;
border: 1px solid #424242;
border-radius: 5px;
pointer-events: none;
text-align: right;
}
.stacked_tooltip strong {
font-size: 120%;
font-weight: bold;
}
// Se escribe el título
d3.select('#title')
.append('h3')
.text('¿Cuanto cuesta nuestra salud?');
d3.select('#summary')
.text('La esperanza de vida de cada país está fuertemente ligada a la región del mundo a la que pertenezca. Una de estas causas es el gasto en sanidad que se hace en cada zona del mundo. En esta gráfica podemos ver cuanto es lo que se gasta en cada región por individuo al año en relación con la esperanza de vida promedio de este.');
// Se definen dimensiones
var margin = { top: 10, right:10, bottom: 10, left: 40 },
width = 850 - margin.left - margin.right,
height = 350 - margin.top - margin.bottom;
d3.select('#footer')
.append('g')
.attr('transform', 'translate(' + margin.left + ',0 )');
// Se inserta el svg
var svg = d3.select('#graphic')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + (margin.top) + ')');
// Viñeta
d3.csv('dataset.csv', function(error, data) {
if (error) throw error;
var tooltip = d3.select("body").append("div")
.attr("class", "stacked_tooltip")
.style("opacity", 0);
// Se hace el escalado
var gastoScale = d3.scaleLinear()
//.domain([0,d3.min(data, function(d) { return d.gasto; })])
.domain([0,5300])
.range([height-margin.bottom, margin.top]);
var esperanzaScale = d3.scaleLinear()
.domain([d3.min(data, function(d) { return d.esperanza; })-5,
d3.max(data, function(d) { return d.esperanza; })-(-5)])
.range([height-margin.bottom, margin.top]);
var regionScale = d3.scaleOrdinal()
.domain(['','South Asia', 'Europe & Central Asia', 'Middle East & North Africa', 'East Asia & Pacific', 'Sub-Saharan Africa', 'Latin America & Caribbean', 'North America',''])
.range([40,100,200,300,400,500,600,700,760]);
// Declaramos los eje
var y1Axis = d3.axisLeft(esperanzaScale);
var y2Axis = d3.axisRight(gastoScale);
var xAxis = d3.axisBottom(regionScale);
svg.append('g')
.attr('id','y2Axis')
.attr('transform', 'translate(' +( width + width -margin.left)+ ',0)')
.call(y2Axis);
svg.append('g')
.attr('transform', 'translate(' + margin.left + ',0)')
.call(y1Axis);
svg.append("text")
.attr("text-anchor", "middle")
.attr("transform", "translate("+ (0) +","+(height/2)+")rotate(-90)")
.text("Esperanza de vida (años)")
.style("font-size", "15px");
svg.append("text")
.attr("id", "y2text")
.attr("text-anchor", "middle")
.attr("transform", "translate("+ ( width + width -margin.left) +","+(height/2)+")rotate(90)")
.text("Gasto en salud")
.style("font-size", "15px");
svg.append('g')
.attr('id','xAxis')
.attr('transform', 'translate(0,'+ (height-margin.bottom)+ ')')
.call(xAxis);
//Creamos las columnas
svg.append('g')
.attr('id','nuevas')
.selectAll('rect')
.data(data)
.enter()
.append('rect')
//.attr('id',function(d){return ( 'esperanza ' + d.region );} )
.attr('class', function(d){return d.gasto;})
.attr('x', function(d) {return regionScale(d.region) - margin.left*0.7})
//.attr('x', function(d,i) {return i*120+75})
.attr('y', function(d) { return height - margin.top; })
.attr('width', margin.left*1.4)
.attr('height', function(d) { return 0; })
.style('fill','#f81f10')
.style('opacity', 1);
svg.append('g')
.attr('id','viejas')
.selectAll('rect')
.data(data)
.enter()
.append('rect')
//.attr('id',function(d){return ( 'esperanza ' + d.region );} )
.attr('class', function(d){return d.gasto;})
.attr('x', function(d) {return regionScale(d.region) - margin.left*0.7})
//.attr('x', function(d,i) {return i*120+75})
.attr('y', function(d) { return esperanzaScale(d.esperanza); })
.attr('width', margin.left*1.4)
.attr('height', function(d) { return height-margin.top- esperanzaScale(d.esperanza); })
.style('fill','#023D4A')
.style('opacity', 0.5)
.on('mouseover', function(d) {
var thisdata = d3.select(this).data()[0];
//Columnas de gasto
svg.select('#nuevas')
.selectAll('rect')
.filter(function(d) { return d.region == thisdata.region; })
.transition()
.attr('y', function(d) { return gastoScale(d.gasto); })
.attr('height', function(d) { return height-margin.top- gastoScale(d.gasto); })
.duration(500)
.style('fill','#f81f10')
.style('opacity', 1);
//Eje derecho
svg.select('#y2Axis')
.transition()
.duration(300)
.attr('transform', 'translate(' +(regionScale(thisdata.region)+margin.left*0.7)+ ',0)')
.call(y2Axis);
svg.select("#y2text")
.transition()
.duration(300)
.attr("transform", 'translate(' +(regionScale(thisdata.region) + margin.left*2) +","+(height/2)+")rotate(90)")
.text("Gasto en salud (USA $)");
//Etiqueta
tooltip.transition()
.duration(500)
.style("opacity", .8);
tooltip.html(thisdata.region + '<br> Gasto en salud: <strong>' + thisdata.gasto + '</strong> <br> Esperanza de Vida: <strong>' + thisdata.esperanza + '</strong> ')
.style("left", (regionScale(thisdata.region))-160)
.style("top",( gastoScale(thisdata.gasto) ));
})
.on('mouseout', function(d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
svg.select('#y2Axis')
.transition()
.duration(300)
.attr('transform', 'translate(' +( width + width -margin.left)+ ',0)')
.call(y2Axis);
svg.select("#y2text")
.transition()
.duration(300)
.attr("transform", 'translate('+( width + width -margin.left)+","+(height/2)+")rotate(90)")
.text("Gasto en salud");
svg.select('#nuevas')
.selectAll('rect')
.transition()
.duration(300)
.attr('y', function(d) { return height - margin.top; ; })
.attr('height', function(d) { return 0; })
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment