Built with blockbuilder.org
forked from romsson's block: simple line chart from dataset
forked from Rade-Mathis's block: TP3-p1
forked from Rade-Mathis's block: TP3-p3-1
license: wtfpl |
Built with blockbuilder.org
forked from romsson's block: simple line chart from dataset
forked from Rade-Mathis's block: TP3-p1
forked from Rade-Mathis's block: TP3-p3-1
[{"name": "A", "value": 10, "date": "2016-01"}, | |
{"name": "B", "value": 30, "date": "2016-02"}, | |
{"name": "C", "value": 20, "date": "2016-03"}, | |
{"name": "D", "value": 40, "date": "2016-04"}, | |
{"name": "E", "value": 50, "date": "2016-05"}, | |
{"name": "F", "value": 20, "date": "2016-06"}, | |
{"name": "G", "value": 10, "date": "2016-07"} | |
] |
<!DOCTYPE html> | |
<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: 2.5px; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
var margin = {top: 20, right: 70, bottom: 20, left: 100}; | |
var width = 760 - margin.left - margin.right; | |
var height = 150 - margin.top - margin.bottom; | |
// See formats http://bl.ocks.org/zanarmstrong/raw/05c1e95bf7aa16c4768e/ | |
// Data sample {"name": "G", "value": 10, "date": "2016-07"} | |
var parseDate = d3.timeParse("%b %Y"); | |
var displayDate = d3.timeFormat("%y"); | |
var displayValue = d3.format(",.0f"); | |
// Ordinal scale | |
var x = d3.scaleTime() | |
.range([0, width]); | |
// Linear scale | |
var y = d3.scaleLinear() | |
.range([height, 0]); | |
var color = d3.scaleOrdinal(d3.schemeCategory10); | |
var area = d3.area() | |
.curve(d3.curveBasis) | |
.x(function(d) { return x(d.date); }) | |
.y0(height) | |
.y1(function(d) { return y(d.price); }); | |
d3.text( | |
"https://gist.githubusercontent.com/mbostock/1256572/raw/44c086a6019de56dce35a47197a19468b3e4ad57/stocks.csv", | |
function (err,rawData) { | |
var dsv = d3.dsvFormat (',') ; | |
var data = dsv.parse (rawData) ; | |
data.forEach(function(d) { | |
d.price = +d.price; | |
d.date = parseDate(d.date); | |
}); | |
var n_data = d3.nest () | |
.key (function (d) { return d.symbol ; }) | |
.entries (data) ; | |
n_data.forEach (function (act) { | |
act.max_price = d3.max (act.values, | |
function (d) { return d.price ; }) | |
/*act.values.forEach (function (val, i_val) { | |
var price = val.price ; | |
var lower_price = 0 ; | |
n_data.forEach (function (other_act) { | |
var other_price = other_act.values[i_val].price ; | |
if (other_price < price) { | |
lower_price = Math.max (lower_price, other_price) ; | |
} | |
}) | |
val.lower_price = lower_price ; | |
})*/ | |
}); | |
console.log(n_data); | |
svg = d3.select("body").selectAll("svg") | |
.data (n_data) | |
.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 + ")"); | |
//var g = svg.append("g"); | |
x.domain(d3.extent(data, function(d) { return d.date; })); | |
y.domain([0, d3.max(data, function(d) { return d.price; })]); | |
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") | |
.attr("transform", "translate(0,0)") | |
.call(d3.axisLeft(y).ticks(5)); | |
svg.append("path") | |
.attr("class", "area") | |
.attr("d", function (d) { return area (d.values) } ) | |
.attr("fill", function(d,i) { return color (d.key) ; }) | |
.attr("transform", "translate(1,0)"); // don't overlap legend ... | |
var legend = svg.append("g") | |
.data(color.domain()) | |
//.enter().append("g") | |
.attr("class", "legend") | |
.attr("transform", "translate("+ (width - 10) + "," | |
+ (height / 2) + ")") ; | |
legend.append("rect") | |
.attr("x", 30) | |
.attr("width", 18) | |
.attr("height", 18) | |
.style("fill", color); | |
legend.append("text") | |
.attr("x", 50) | |
.attr("y", 9) | |
.attr("dy", ".35em") | |
.style("text-anchor", "start") | |
.text(function(d) { return d; }); | |
}); | |
</script> | |
</body> |