Built with blockbuilder.org
forked from romsson's block: loading multiple stock data from dataset
forked from alicemontel's block: DataViz_TP3
Rendu pour le TP3 de l'UE DataViz - M2DS Univ. Claude Bernard Lyon 1
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;
}
</style>
</head>
<body>
<script>
var margin = {top: 20, right: 30, bottom: 20, left: 100},
width = 760 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
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 + ")");
var parseDate = d3.timeParse("%b %Y");
var x = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
c = d3.scaleOrdinal(d3.schemeCategory10);
var stack = d3.stack();
var line = d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.price); });
d3.text('https://gist.githubusercontent.com/mbostock/1256572/raw/44c086a6019de56dce35a47197a19468b3e4ad57/stocks.csv', function(error, raw) {
var dsv = d3.dsvFormat(',')
var data = dsv.parse(raw);
var keys = data.columns.slice(1);
var area = d3.area()
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
// Nest stock values by symbol.
var symbols = d3.nest()
.key(function(d) { return d.symbol; })
.entries(data);
// Parse and caculate some values for each symbols
symbols.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(symbols.map(function(d) { return d.maxPrice; }))])
// show the texts
var texts=svg.selectAll(".symbols").data(symbols).enter().append("text");
texts
.attr("x", x(x.domain()[1])+5)
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(function(d) { return d.key; });
show4areas();
// STEP 1
function show4areas(){
area
.y0(function(d){
switch(d.symbol){
case "AAPL":
return height-height*(3/4);
break;
case "IBM":
return height-height/4;
break;
case "MSFT":
return height;
break;
default:
return height-height/2;
}})
.y1(function(d)
{
switch(d.symbol){
case "AAPL":
return (y(d.price)/4);
break;
case "IBM":
return y(d.price)/4+(height*2)/4;
break;
case "MSFT":
return y(d.price)/4 +(height*3)/4;
break;
default:
return y(d.price)/4+height/4;
}});
texts
.attr("y", function(d){return y(d.maxPrice);})
var areas = svg.selectAll(".area").data(symbols).enter().append("path")
.attr("class","area")
.attr("d",function(d,i) {y.domain([0, d.maxPrice]); return area(d.values,i); })
.attr("fill", function(d) { return c(d.key); })
.attr("opacity", 1);
}
// draw axis
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)); */
});
</script>
</body>
https://d3js.org/d3.v4.min.js