Skip to content

Instantly share code, notes, and snippets.

@bgschiller
Created August 23, 2014 20:03
Show Gist options
  • Save bgschiller/73ea8cbb677571fc1219 to your computer and use it in GitHub Desktop.
Save bgschiller/73ea8cbb677571fc1219 to your computer and use it in GitHub Desktop.
Grandpa's Dow Jones moving average graph
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
svg text {
font-size:24px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var margin = {top: 40, right: 150, bottom: 50, left: 100},
width = window.innerWidth - margin.left - margin.right,
height = window.innerHeight - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y%m%d").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.dollars); });
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 today = new Date(),
start = new Date(),
formatDate = d3.time.format("%Y-%m-%d");
start.setFullYear(today.getFullYear() - 2);
d3.json(("http://www.quandl.com/api/v1/datasets/BCB/UDJIAD1.json?trim_start=" + formatDate(start) + "&trim_end=" + formatDate(today)), function(error, data) {
data = data.data.map(function(d) {
return {
date: formatDate.parse(d[0]),
DOW: d[1]
};
}).reverse();
var moving_sum = 0,
MA_length = 21;
for(var ix=0; ix < data.length; ix++){
moving_sum += data[ix].DOW;
if (ix - MA_length >= 0){
moving_sum -= data[ix - MA_length].DOW;
}
var divisor = Math.min(ix + 1, MA_length);
data[ix][MA_length + '- Day Avg'] = moving_sum / divisor;
data[ix]['Avg + 3.5%'] = (moving_sum / divisor) * (1 + 0.035);
data[ix]['Avg - 3.5%'] = (moving_sum / divisor) * (1 - 0.035);
}
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; }));
var series = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, dollars: +d[name]};
})
};
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([
d3.min(series, function(c) { return d3.min(c.values, function(v) { return v.dollars; }); }),
d3.max(series, function(c) { return d3.max(c.values, function(v) { return v.dollars; }); })
]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("dollars");
var city = svg.selectAll(".city")
.data(series)
.enter().append("g")
.attr("class", "city");
city.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.name); });
city.append("text")
.datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
.attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.dollars) + ")"; })
.attr("x", 3)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment