xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Line Chart with Multiple Lines</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: #F2F2F2;
max-width: 900px;
}
svg {
background-color: #F2F2F2;
}
h1 {
font-family: Helvetica, Arial, sans-serif;
font-size: 36px;
font-weight: bold;
color: #A01E0C;
border-bottom: solid 8px #A01E0C;
}
h2 {
font-family: Helvetica, Arial, sans-serif;
font-size: 24px;
font-weight: bold;
color: black;
}
h3 {
font-family: Helvetica, Arial, sans-serif;
font-size: 14px;
font-weight: normal;
font-style: italic;
color: black;
}
p {
font-family: Helvetica;
font-size: 16px;
font-weight: bold;
color: black;
}
g.highlight path {
stroke: #6E6E6E;
stroke-width: 3;
}
path:hover {
stroke: #A01E0C;
stroke-width: 4;
}
.axis path,
.axis line {
fill: none;
stroke: #A01E0C;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 12px;
}
</style>
</head>
<body>
<h1>UNEMPLOYMENT IN SPAIN </h1>
<h2> Evolution of the unemployment rate in Spain by region (2007-2014)</h2>
<h3> Source: Economically Active Population Survey. Spanish Statistical Office, <a href="https://www.ine.es">INE</a> </h3>
<p> Unemployment is one of the greatest concerns within Spanish society. After having reached a minimum of around 8% in 2007, with the Spanish economic crisis that began in 2008 the rate of unemployment grew quickly reaching almost 26% in 2013. <br> Unemployment rates vary according to the different regions. It is a big problem in the southern regions of the country, like Andalucía, Canarias or Extremadura, especially in the least industrialized areas. Unemployment in these regions was higher than 30% in 2014. <br> Although unemployment in the northern regions such as Navarra or País Vasco is much lower than in the rest of the country, the figures are still very worrying: around 15% in 2014. </p>
<script type="text/javascript">
var w = 650;
var h = 500;
var padding = [ 20, 20, 50, 150 ];
var dateFormat = d3.time.format("%Y");
var xScale = d3.time.scale()
.range([ padding[3], w - padding[1] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(8)
.tickFormat(function(d) { return dateFormat(d); });
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.tickFormat(function(d) { return d + "%"; });
var line = d3.svg.line()
.x(function(d) {
return xScale(dateFormat.parse(d.year));
})
.y(function(d) {
return yScale(+d.amount);
});
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("unemp_rate.csv", function(data) {
var years = ["2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014"];
var dataset = [];
for (var i = 0; i < data.length; i++) {
dataset[i] = {
region: data[i].Region,
rate: []
};
for (var j = 0; j < years.length; j++) {
if (data[i][years[j]]) {
dataset[i].rate.push({
year: years[j],
amount: data[i][years[j]]
});
}
}
}
//console.log(data);
xScale.domain([
d3.min(years, function(d) {
return dateFormat.parse(d);
}),
d3.max(years, function(d) {
return dateFormat.parse(d);
})
]);
yScale.domain([
d3.max(dataset, function(d) {
return d3.max(d.rate, function(d) {
return +d.amount;
});
}),
0
]);
var groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.classed("highlight", function(d) {
if (d.region == "Total Espanya" ) {
return true;
} else {
return false;
}
});
groups.append("title")
.text(function(d) {return d.region;
});
groups.selectAll("path")
.data(function(d) {
return [ d.rate ];
})
.enter()
.append("path")
.attr("class", "line")
.attr("d", line)
.attr("fill", "none")
.attr("stroke", "#A4A4A4")
.attr("stroke-width", 1.5);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding[3] - 5) + ",0)")
.call(yAxis);
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js