xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body {
margin-top:30px;
position:fixed;
top:0;
right:0;
bottom:0;
left:0;
}
svg {
font: 10px sans-serif;
}
.line {
fill: none;
stroke: black;
stroke-width: 2px;
}
</style>
</head>
<body>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 20},
width = 960 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var duration = 1500,
delay = 500;
var parseDate = d3.timeParse("%b %Y");
var x = d3.scaleTime().range([0, width]),
c = d3.scaleOrdinal(d3.schemeCategory10);
var y = d3.scaleLinear().range([height, 0]);
var line = d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.price); });
d3.text('dataset.csv', function(error, raw) {
var dsv = d3.dsvFormat(',')
var data = dsv.parse(raw);
// Nest stock values by symbol.
var symbols = d3.nest()
.key(function(d) { return d.symbol; })
.entries(data);
var i = 0;
// Parse and caculate some values for each symbols
symbols.forEach(function(s) {
var j = 0;
s.values.forEach(function(d) {
d.date = parseDate(d.date);
d.price = +d.price;
if(i == 0){ // pas de petit frere
d.beforePrice = 0;
}else{
d.beforePrice = symbols[i-1].values[j].cumuPrice;
}
d.cumuPrice = d.beforePrice + d.price;
j++;
});
i++;
s.maxPrice = d3.max(s.values, function(d) { return d.price; });
s.sumPrice = d3.sum(s.values, function(d) { return d.price; });
s.maxCumuPrice = d3.max(s.values, function(d) { return d.cumuPrice; });
});
setTimeout(multiple, duration);
function multiple() {
var stack = d3.stack();
var area = d3.area()
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d.price); });
height = 100 - margin.top - margin.bottom;
x.domain(d3.extent(data, function(d) { return d.date; }));
y = d3.scaleLinear().range([height, 0])
y.domain([0, d3.max(symbols.map(function(d) { return d.maxPrice; }))])
// Add an SVG element for each symbol, with the desired dimensions and margin.
var svg = d3.select("body").selectAll("svg")
.data(symbols)
.enter().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 + ")");
// Add the area path elements. Note: the y-domain is set per element.
svg.append("path")
.attr("class", "area")
.attr("d", function(d) { y.domain([0, d.maxPrice]); return area(d.values); })
.style("fill", function(d) { return c(d.key); });
// Add the line path elements. Note: the y-domain is set per element.
svg.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return c(d.key); });
// Add a small label for the symbol name.
svg.append("text")
.attr("x", 12)
.attr("dy", ".31em")
.style("text-anchor", "end")
.style("font-size",12)
.text(function(d) { return d.key; });
}
});
</script>
</body>
https://d3js.org/d3.v4.min.js