Built with blockbuilder.org
forked from romsson's block: simple line chart from dataset
forked from anonymous's block: TP2 Data viz UCBL 2017
forked from Renaud-D-Harreville's block: TP2 Data viz UCBL 2017
forked from Renaud-D-Harreville's block: TP2 Data viz small multiples UCBL 2017
forked from Renaud-D-Harreville's block: TP2 Data viz areas UCBL 2017
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
svg {
font: 10px sans-serif;
}
.line {
fill: none;
stroke: black;
stroke-width: 2px;
}
.area {
fill: lightsteelblue;
opacity : 0.2
}
</style>
</head>
<body>
<script>
// Code CSS
var margin = {top: 20, right: 30, bottom: 20, left: 50},
width = 960 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// Mise en place du layout
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 + ")");
// Parsing de la date pour la mettre au format voulu (mois année)
var parseDate = d3.timeParse("%b %Y");
var x = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
c = d3.scaleOrdinal(d3.schemeCategory10);
// Mise en forme du style de ligne
var line = d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.price); });
var area = d3.area()
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
.y1(function(d) { return y(d.price); })
// récupération des données
var data = d3.text('dataset.csv', function(error, raw) {
// parsing du fichier csv
var dsv = d3.dsvFormat(',')
var data = dsv.parse(raw);
return data;
});
console.log(data)
// Nest stock values by symbol.
var dataNested = d3.nest()
.key(function(d) { return d.symbol; })
.entries(data);
// Parse and caculate some values for each symbols
dataNested.forEach(function(s) {
s.values.forEach(function(d) {
d.date = parseDate(d.date);
d.price = +d.price;
});
s.maxPrice = d3.max(s.values, function(d) { return d.price; });
//s.sumPrice = d3.sum(s.values, function(d) { return d.price; });
//console.log(symbols);
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(dataNested.map(function(d) { return d.maxPrice; }))])
area.y0(y(0));
// add the area
svg.selectAll(".area").data(dataNested).enter()
.append("path")//.data(dataNested)
.attr("class", "area")
.attr("d", function(d) { return area(d.values); })
.style("fill", function(d) { return c(d.key); });
svg.selectAll(".line").data(dataNested).enter()
.append("path")//.data(dataNested)
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return c(d.key); });
svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(20));
// Add a small label for the symbol name.
svg.append("text")
.data(dataNested)
.attr("x", width - 6)
.attr("y", height - 6)
.style("text-anchor", "end")
.text(function(d) { return d.key; });
</script>
</body>
https://d3js.org/d3.v4.min.js