xxxxxxxxxx
<meta charset="utf-8">
<head>
<meta charset="utf-8">
<title>Line Chart with Multiple Lines</title>
<style type="text/css">
<style>
body {
background-color: white;
font-family: Helvetica, Arial, sans-serif;
}
h1 {
font-size: 50px;
margin: 0;
font-family: Helvetica, Arial, sans-serif;
}
p {
font-size: 16px;
margin: 10px 0 0 0;
font-family: Helvetica, Arial, sans-serif;
}
svg {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
fill:none;
stroke:#000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke-width: 1.5px;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
<body>
<h1>Veedel Köln: Alg II</h1>
<p>ALG II: Source: <a href="https://www.offenedaten-koeln.de/dataset/resource/95ecfe27-83c4-4b7a-8644-a37d5f4fe010">Offene Daten Köln</a>, 2014</p>
<script src="https://d3js.org/d3.v3.js"></script>
<script type="text/javascript">
var margin = {top: 20, right: 80, bottom: 30, left: 50},
width = 900 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y").parse;
var x = d3.time.scale()
.range([0,width]);
var y = d3.scale.linear()
.range([height,0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.jahr); })
.y(function(d) { console.log(d); return y(d.zahl); });
var svg = d3.select("body").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 + ")");
d3.csv("alg2.csv", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) { return key == "zeile"; }));
// first we need to corerce the data into the right formats
data = data.map( function (d) {
return {
zeile: d.zeile,
jahr: parseDate(d.jahr),
zahl: +d.zahl };
});
// then we need to nest the data on city since we want to only draw one
// line per city
data = d3.nest().key(function(d) { return d.zeile; }).entries(data);
x.domain([d3.min(data, function(d) { return d3.min(d.values, function (d) { return d.jahr; }); }),
d3.max(data, function(d) { return d3.max(d.values, function (d) { return d.jahr; }); })]);
y.domain([0, d3.max(data, function(d) { return d3.max(d.values, function (d) { return d.zahl; }); })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
var zeile = svg.selectAll(".zeile")
.data(data, function(d) { return d.key; })
.enter().append("g")
.attr("class", "zeile");
zeile.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.key); });
zeile.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Anzahl ALG-II Empfänger");
});
</script>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js