Skip to content

Instantly share code, notes, and snippets.

@joar
Last active December 12, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joar/4747134 to your computer and use it in GitHub Desktop.
Save joar/4747134 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<body>
<div id="chart">
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var raw_series = [{"note": "TEXAS LONGHORN", "date": "2013-01-10T00:00:00", "amount": 110.0, "id": 74, "account_id": 1}, {"note": "GOOGLE *FEO Medi", "date": "2013-01-10T00:00:00", "amount": 22.0, "id": 73, "account_id": 1}, {"note": "Pressbyran 5122", "date": "2013-01-10T00:00:00", "amount": 13.0, "id": 77, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-01-10T00:00:00", "amount": 106.0, "id": 76, "account_id": 1}, {"note": "H\u00c5R 3000", "date": "2013-01-10T00:00:00", "amount": 345.0, "id": 75, "account_id": 1}, {"note": "Pressbyran 5122", "date": "2013-01-11T00:00:00", "amount": 19.0, "id": 72, "account_id": 1}, {"note": "BIRKA PUNKTEN", "date": "2013-01-11T00:00:00", "amount": 79.0, "id": 71, "account_id": 1}, {"note": "DROPBOX", "date": "2013-01-11T00:00:00", "amount": 67.0, "id": 70, "account_id": 1}, {"note": "KAFFESTUGAN", "date": "2013-01-11T00:00:00", "amount": 165.0, "id": 69, "account_id": 1}, {"note": "BIRKA PUNKTEN", "date": "2013-01-12T00:00:00", "amount": 79.0, "id": 67, "account_id": 1}, {"note": "GOOGLE *Chozabu", "date": "2013-01-13T00:00:00", "amount": 15.0, "id": 63, "account_id": 1}, {"note": "VIDEO RULLEN", "date": "2013-01-13T00:00:00", "amount": 171.0, "id": 66, "account_id": 1}, {"note": "\u00c5SGATAN 2 K\u00d6K &", "date": "2013-01-13T00:00:00", "amount": 224.0, "id": 65, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-01-13T00:00:00", "amount": 209.0, "id": 64, "account_id": 1}, {"note": "GOOGLE *Camel Ga", "date": "2013-01-13T00:00:00", "amount": 7.0, "id": 62, "account_id": 1}, {"note": "THE RACKSPACE CL", "date": "2013-01-16T00:00:00", "amount": 252.0, "id": 59, "account_id": 1}, {"note": "Pressbyran 8535", "date": "2013-01-16T00:00:00", "amount": 33.0, "id": 61, "account_id": 1}, {"note": "BIRKA PUNKTEN", "date": "2013-01-16T00:00:00", "amount": 79.0, "id": 60, "account_id": 1}, {"note": "KAFFESTUGAN", "date": "2013-01-16T00:00:00", "amount": 75.0, "id": 58, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-01-18T00:00:00", "amount": 80.0, "id": 56, "account_id": 1}, {"note": "STATION", "date": "2013-01-18T00:00:00", "amount": 89.0, "id": 55, "account_id": 1}, {"note": "FREE SOFTWARE FO", "date": "2013-01-19T00:00:00", "amount": 132.0, "id": 52, "account_id": 1}, {"note": "\u00d6verf\u00f6ring 100/m\u00e5n", "date": "2013-01-19T00:00:00", "amount": 100.0, "id": 53, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-01-19T00:00:00", "amount": 321.0, "id": 51, "account_id": 1}, {"note": "VIDEO RULLEN", "date": "2013-01-21T00:00:00", "amount": 179.0, "id": 46, "account_id": 1}, {"note": "Automatuttag", "date": "2013-01-21T00:00:00", "amount": 300.0, "id": 49, "account_id": 1}, {"note": "\u00c5SGATAN 2 K\u00d6K &", "date": "2013-01-21T00:00:00", "amount": 26.0, "id": 48, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-01-21T00:00:00", "amount": 174.0, "id": 47, "account_id": 1}, {"note": "MSFT *XBOX LIV", "date": "2013-01-22T00:00:00", "amount": 69.0, "id": 45, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-01-23T00:00:00", "amount": 293.0, "id": 43, "account_id": 1}, {"note": "PIZZERIA CUOCO P", "date": "2013-01-23T00:00:00", "amount": 140.0, "id": 42, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-01-24T00:00:00", "amount": 283.0, "id": 41, "account_id": 1}, {"note": "HOTELL NORRTULL", "date": "2013-01-25T00:00:00", "amount": 98.0, "id": 38, "account_id": 1}, {"note": "MAT P\u00c5 JOBB", "date": "2013-01-25T00:00:00", "amount": 80.0, "id": 39, "account_id": 1}, {"note": "\u00d6verf\u00f6ring LOVA/peng", "date": "2013-01-26T00:00:00", "amount": 9000.0, "id": 37, "account_id": 1}, {"note": "Pressbyran 5122", "date": "2013-01-26T00:00:00", "amount": 18.0, "id": 33, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-01-26T00:00:00", "amount": 373.0, "id": 34, "account_id": 1}, {"note": "J\u00c4RNA W\u00c4RDSHUS", "date": "2013-01-26T00:00:00", "amount": 60.0, "id": 35, "account_id": 1}, {"note": "GETSENTRY LLC", "date": "2013-01-26T00:00:00", "amount": 59.0, "id": 36, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-01-27T00:00:00", "amount": 556.0, "id": 31, "account_id": 1}, {"note": "WWW.NETFLIX.COM", "date": "2013-01-27T00:00:00", "amount": 79.0, "id": 30, "account_id": 1}, {"note": "DIREKTEN GOTTCEN", "date": "2013-01-27T00:00:00", "amount": 175.0, "id": 32, "account_id": 1}, {"note": "\u00c5SGATAN 2 K\u00d6K &", "date": "2013-01-28T00:00:00", "amount": 154.0, "id": 27, "account_id": 1}, {"note": "HYRESG\u00c4STF\u00d6R", "date": "2013-01-28T00:00:00", "amount": 80.0, "id": 26, "account_id": 1}, {"note": "Automatuttag", "date": "2013-01-28T00:00:00", "amount": 800.0, "id": 25, "account_id": 1}, {"note": "REST KINA MUREN", "date": "2013-01-28T00:00:00", "amount": 428.0, "id": 24, "account_id": 1}, {"note": "KOLINGSBORG", "date": "2013-01-28T00:00:00", "amount": 200.0, "id": 23, "account_id": 1}, {"note": "KOLINGSBORG", "date": "2013-01-28T00:00:00", "amount": 105.0, "id": 22, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-01-28T00:00:00", "amount": 199.0, "id": 21, "account_id": 1}, {"note": "SPOTIFY PREMIUM", "date": "2013-01-28T00:00:00", "amount": 99.0, "id": 19, "account_id": 1}, {"note": "7-Eleven 16110 G", "date": "2013-01-28T00:00:00", "amount": 136.0, "id": 20, "account_id": 1}, {"note": "\u00d6verf\u00f6ring Spar, 3k", "date": "2013-01-28T00:00:00", "amount": 3000.0, "id": 29, "account_id": 1}, {"note": "\u00c5SGATAN 2 K\u00d6K &", "date": "2013-01-28T00:00:00", "amount": 235.0, "id": 28, "account_id": 1}, {"note": "FREE SOFTWARE FO", "date": "2013-01-30T00:00:00", "amount": 65.0, "id": 17, "account_id": 1}, {"note": "PAYPAL *SQUAD-KS", "date": "2013-01-30T00:00:00", "amount": 154.0, "id": 18, "account_id": 1}, {"note": "SJ REGIONAL STOC", "date": "2013-01-31T00:00:00", "amount": 56.0, "id": 16, "account_id": 1}, {"note": "CIAO CIAO QUATTR", "date": "2013-02-01T00:00:00", "amount": 220.0, "id": 13, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-02-01T00:00:00", "amount": 50.0, "id": 15, "account_id": 1}, {"note": "APOTEKET DRAKEN", "date": "2013-02-01T00:00:00", "amount": 48.0, "id": 14, "account_id": 1}, {"note": "GRET INDIAN REST", "date": "2013-02-01T00:00:00", "amount": 85.0, "id": 12, "account_id": 1}, {"note": "LINODE.COM", "date": "2013-02-02T00:00:00", "amount": 613.0, "id": 11, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-02-03T00:00:00", "amount": 110.0, "id": 10, "account_id": 1}, {"note": "ICA SUPERMARKET", "date": "2013-02-04T00:00:00", "amount": 196.0, "id": 9, "account_id": 1}, {"note": "VIDEO RULLEN", "date": "2013-02-04T00:00:00", "amount": 169.0, "id": 8, "account_id": 1}, {"note": "OLEARYS 917", "date": "2013-02-06T00:00:00", "amount": 309.0, "id": 4, "account_id": 1}, {"note": "GRET INDIAN REST", "date": "2013-02-06T00:00:00", "amount": 85.0, "id": 6, "account_id": 1}, {"note": "TAXI STOCKHOLM", "date": "2013-02-06T00:00:00", "amount": 875.0, "id": 5, "account_id": 1}, {"note": "BURGER KING ODEN", "date": "2013-02-06T00:00:00", "amount": 89.0, "id": 3, "account_id": 1}, {"note": "\u00c5SGATAN 2 K\u00d6K &", "date": "2013-02-07T00:00:00", "amount": 226.0, "id": 2, "account_id": 1}, {"note": "Loopia AB", "date": "2013-02-07T00:00:00", "amount": 178.0, "id": 1, "account_id": 1}];
var margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var format = d3.time.format('%Y-%m-%dT%H:%M:%S')
raw_series.forEach(function (d) { d.date = format.parse(d.date); })
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var z = d3.scale.category20c();
var xAxis = d3.svg.axis()
.scale(x)
.orient('bottom')
.ticks(d3.time.days)
var yAxis = d3.svg.axis()
.scale(y)
.orient('left');
var stack = d3.layout.stack()
.offset('zero')
.values(function (d) { return d.values; })
.x(function (d) { return d.date; })
.y(function (d) { return d.amount; });
var nest = d3.nest()
.key(function (d) { return d.note })
.key(function (d) { return d.date })
var area = d3.svg.area()
.interpolate('monotone')
.x(function (d) { return x(d.date); })
.y0(function (d) { return y(d.y0); })
.y1(function (d) { return y(d.y0 + d.y); });
var svg = d3.select('#chart').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 layers = stack(nest.entries(raw_series));
x.domain(d3.extent(raw_series, function (d) { return d.date; }));
y.domain([0, d3.max(raw_series, function (d) { d.y0 + d.y; })]);
svg.selectAll(".layer")
.data(layers)
.enter().append("path")
.attr("class", "layer")
.attr("d", function(d) { return area(d.values); })
.style("fill", function(d, i) { return z(i); });
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
</script>
<style>
.node {
stroke: black;
}
#chart {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment