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;
}
.area {
//fill: #e7e7e7;
}
</style>
</head>
<body>
<script>
var margin = {top: 20, right: 20, bottom: 20, left: 100},
width = 760 - margin.left - margin.right,
height = 100 - 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); });
var area = d3.area() /* */
.x(function(d) { return x(d.date); }) /* */
.y0(height) // ajouté
.y1(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);
symbols = d3.nest()
.key(function(d) { return d.symbol; })
.entries(stocks = 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; }); // Compute the //maximum price per symbol, needed for the y-domain.
s.sumPrice = d3.sum(s.values, function(d) { return d.price; });
console.log(symbols);
});
// Sort by maximum price, descending.
symbols.sort(function(a, b) { return b.maxPrice - a.maxPrice; });
//on définit un domaine des X par symbole
x.domain([
d3.min(symbols, function(s) { return s.values[0].date; }),
d3.max(symbols, function(s) { return s.values[s.values.length - 1].date; })
]);
// x.domain(d3.extent(data, function(d) { return d.date; }));
// 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.selectAll("path").data(symbols).enter()
.append("path")
.attr("class", "area")
.attr("fill", "steelblue")
.style("fill-opacity", 0.1)
.attr("d", function(d) { y.domain([0, d.maxPrice]); return area(d.values); })
.style("fill", function(d) { return c(d.key); })
svg.selectAll(".line").data(symbols).enter() // on selectionne tous les el.
// dont la classe est "line"
.append("path") // on rajoute un "path" a chaque "line"
.attr("class", "line") // de classe "line"
.attr("fill", "steelblue") /* */
//.attr("d", area);
.attr("d", function(d) { return line(d.values); })
.attr("d", function(d) { y.domain([0, d.maxPrice]); 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 + ")") // place l'axe des abscisses
.call(d3.axisBottom(x)); // en bas
// svg.append("g") définit l'axe des ordonnées graduée
// .attr("class", "axis axis--y")
// .call(d3.axisLeft(y).ticks(20));
// var legend = svg.selectAll(".legend")
// .data(c.domain())
// .enter().append("g")
// .attr("class", "legend")
// .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
// legend.append("rect")
// .attr("x", 30)
// .attr("width", 18)
// .attr("height", 18)
// .style("fill", c);
// legend.append("text")
// .attr("x", 50)
// .attr("y", 9)
// .attr("dy", ".35em")
// .style("text-anchor", "start")
// .text(function(d) { return d; });
});
</script>
</body>
https://d3js.org/d3.v4.min.js