Built with blockbuilder.org
forked from romsson's block: simple line chart from dataset
forked from romsson's block: TP3-InClass-2-Stock
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: 1.5px;
}
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
var width = 800;
var height = 400;
// https://bl.ocks.org/zanarmstrong/raw/05c1e95bf7aa16c4768e/
var parseDate = d3.timeFormat("%Y-%m");
// %b shows month in written name; %Y shows year in numbers
var displayDate = d3.timeFormat("%b %Y");
var displayValue = d3.format(",.0f");
// Ordinal scale
var x = d3.scaleOrdinal()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, height - 200]);
var line = d3.line()
// .curve(d3.curveBasis) allows smoothing of the curve
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.price); });
var g = svg.append("g")
.attr("transform", "translate(50, 0)")
// Replacing "dataset.json" with Data CSV file URL: https://gist.githubusercontent.com/mbostock/1256572/raw/44c086a6019de56dce35a47197a19468b3e4ad57/stocks.csv
// Parsing the CSV file is preliminary
// d3.csv exists as a function but the flexibility in coding a CSV file makes it difficult to adopt directly this function.
// Manual parsing is hence recommended.
// function(error, raw) allows console.log(raw) to observe raw data
d3.json("https://gist.githubusercontent.com/mbostock/1256572/raw/44c086a6019de56dce35a47197a19468b3e4ad57/stocks.csv", function(error, raw) {
console.log(raw)
// Parsing starts here
var dsv = d3.dsvFormat(',');
var data = dsv.parse(raw);
// Pre-processing
data.forEach(function(d) {
d.date = parseDate(d.date);
d.price = parseInt(d.price);
});
console.log(data)
// Nesting
// Pull data and nest them under nested_data
// Make as many nests as as many grouped data
var nested_data = d3.nest()
.key(function(d) { return d.symbol; })
// .rollup show 'value' in console in one numeric value instead of value content
.rollup(function(leaves) {
return d3.sum(leaves, function(d) {
return d.price;
});
})
.entries(data);
console.log(nested_data)
x.domain(data.map(function(d) { return d.name; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
/*** REMOVED CODE CONVERNTING TEXT AND VALUE ***/
// Path-/line-drawing
// nested_data is a table of a table (in replacement of [data] in general)
g.selectAll(".line").data(nested_data).enter()
.append("path")
.attr("class", "line")
// Draw values of 'd'
.attr("d", function(d) { return line(d.values); });
// Adding labels and placing them in a certain position
g.selectAll("#label").data(nested_data).enter()
.append("text")
.attr("id", "label")
.attr("transform", function(d, i) {
return "translate(50, " + (i * 20 + 30) + ")"
})
.text(function(d) { console.log(d.key); return d.key; })
});
</script>
</body>
https://d3js.org/d3.v4.min.js