Skip to content

Instantly share code, notes, and snippets.

@Rade-Mathis
Last active November 25, 2017 14:18
Show Gist options
  • Save Rade-Mathis/bc70895253e42c4aa73365ae281f379f to your computer and use it in GitHub Desktop.
Save Rade-Mathis/bc70895253e42c4aa73365ae281f379f to your computer and use it in GitHub Desktop.
TP3-p3-4
license: wtfpl
[{"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 { /* useless here */
fill: none;
stroke: black;
stroke-width: 2.5px;
}
.area {
//opacity: 0.5;
}
</style>
</head>
<body>
<script>
var margin = {top: 20, right: 40, bottom: 20, left: 100};
var width = 760 - margin.left - margin.right;
var 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 + ")");
// 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(function(d) { return y(d.sum_others); })
.y1(function(d) { return y(d.price); });
var g = svg.append("g")
;//.attr("transform", "translate(0, 0)")
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) ;
for (i = 0; i < n_data.length; ++i) {
for (j = 0; j < n_data[i].values.length; ++j) {
var elem = n_data[i].values[j] ;
elem.sum_others = 0 ;
for (k = i + 1; k < n_data.length; ++k) {
elem.sum_others += n_data[k].values[j].price ;
}
elem.price += elem.sum_others ;
}
}
x.domain(d3.extent(data, function(d) { return d.date; }));
var maxY = d3.max(data, function(d) { return d.price; }) ;
y.domain([0, maxY]);
/* center all of this */
for (i = 0; i < n_data[0].values.length; ++i) {
var blank_under = (maxY - n_data[0].values[i].price) / 2 ;
for (j = 0; j < n_data.length; ++j) {
n_data[j].values[i].price += blank_under ;
n_data[j].values[i].sum_others += blank_under ;
}
}
console.log(n_data);
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(20));
g.selectAll(".area").data(n_data).enter().append("path")
.attr("class", "area")
.attr("d", function (d) { return area (d.values) } )
.attr("fill", function(d,i) { return color (d.key) ; })
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) {
var vals = n_data[i].values ;
return "translate(" + (width - 40) + ","
+ (y((vals[vals.length - 1].price
+ vals[vals.length - 1].sum_others) / 2))
+ ")"; });
legend.append("text")
.attr("x", 50)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(function(d) { return d; });
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment