Built with blockbuilder.org
forked from romsson's block: loading multiple stock data from dataset
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>
// NOTE : some of the functions here were copy-pasted from a colleague
// I tried to do different chart among the one we were supposed to do, but
// accidentaly deleted everything. Drawareas and drawlines are thus close to
// what I ahd achieved on my own.
// I got stuck withdrawstreams.
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 line = d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.price); });
//NESTING
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);
// 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; });
});
// SCALES
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(symbols.map(function(d) { return d.maxPrice; }))])
drawlines()
//drawallarea()
//drawareas()
drawstream()
function drawstream(){
// STREAMLINE : https://bl.ocks.org/mbostock/4060954
var stack = d3.stack()
.keys(["AAPL","MSFT", "IBM", "AMZN"])
//.offset(d3.stackOffsetWiggle)
.value(function(d,key){return d.price})
layer_stream = stack(data)
console.log(layer_stream)
var area = d3.area()
.x(function(d) { return x(d.data.date)})
.y1(function(d) {console.log(y(d[0]));return y([0]);})
.y0(function(d) {return y(d.data.price)});
var svgs = svg.selectAll(".stream")
.data(layer_stream)
.enter().append("path")
.attr("class","area")
.attr("d", function(d) { return area(d);})
.attr("fill", function(d,i) {return c(d.key);})
.attr("opacity", 0.6)
}
function drawallarea(){
// create areas
var area = d3.area()
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d.price); });
//create divs for the areas
var svgs= svg.selectAll(".symbols").data(symbols).enter().append("path")
.attr("class", "area")
.attr("d", function(d) { return area(d.values); })
.attr("fill", function(d) {return c(d.key);})
.attr("opacity","0.8");
}
function drawareas(){
// create areas
var area = d3.area()
.x(function(d) { return x(d.date); })
.y0(height/4)
.y1(function(d) { return y(d.price)/4; });
//create divs for the areas
var svgs= d3.selectAll(".symbols")
.data(symbols)
.enter().append("svg")
.attr("width", width +margin.right+margin.left)
.attr("height", height/4 )
.append("g")
.attr("transform", "translate("+margin.left+","+margin.top+")");
// fill the areas
svgs.append("path")
.attr("class", "areas")
.attr("d", function(d) {y.domain([0, d.maxPrice]); return area(d.values); })
.attr("fill", function(d) {return c(d.key);})
.attr("opacity",1.0);
}
function drawlines(){
//creates divs fot texts and lines
var lines=svg.selectAll(".symbols").data(symbols).enter().append("path")
var texts=svg.selectAll(".symbols").data(symbols).enter().append("text");
// get the lines points
lines
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return c(d.key); });
// draw the texts
texts
.attr("x", x(x.domain()[1])+5)
.attr("y", function(d){return y(d.maxPrice);})
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(function(d) { return d.key; });
}
});
</script>
</body>
https://d3js.org/d3.v4.min.js