Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active June 19, 2020 15:08
Show Gist options
  • Save d3indepth/3c7cb147aab8cf0cdbfb83b4ee7a847c to your computer and use it in GitHub Desktop.
Save d3indepth/3c7cb147aab8cf0cdbfb83b4ee7a847c to your computer and use it in GitHub Desktop.

Revisions

  1. @animateddata animateddata revised this gist Aug 2, 2017. 2 changed files with 0 additions and 0 deletions.
    Binary file added preview.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
    Binary file added thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  2. @peterrcook peterrcook created this gist Dec 15, 2016.
    3 changes: 3 additions & 0 deletions .block
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    license: gpl-3.0
    height: 180
    border: no
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    Stack generator (stream).

    From [D3 in Depth](http://d3indepth.com) book by [Peter Cook](http://animateddata.co.uk).
    64 changes: 64 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    <!DOCTYPE html>
    <meta charset="utf-8">
    <head>
    <title>Stack generator (stream)</title>
    </head>

    <body>
    <svg width="700" height="170">
    <g transform="translate(20, -50)"></g>
    </svg>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
    <script>
    var yScale = d3.scaleLinear().domain([0, 800]).range([200, 0]);

    var areaGenerator = d3.area()
    .x(function(d, i) {
    return i * 50;
    })
    .y0(function(d) {
    return yScale(d[0]);
    })
    .y1(function(d) {
    return yScale(d[1]);
    })
    .curve(d3.curveCatmullRom);

    var colors = ['#FBB65B', '#FBCF3B', '#de3163', '#4A79A4'];

    var data = [
    {day: 1, apricots: 100, bananas: 140, cherries: 105, damsons: 80},
    {day: 2, apricots: 110, bananas: 150, cherries: 105, damsons: 40},
    {day: 3, apricots: 130, bananas: 160, cherries: 115, damsons: 50},
    {day: 4, apricots: 110, bananas: 200, cherries: 110, damsons: 90},
    {day: 5, apricots: 100, bananas: 220, cherries: 105, damsons: 120},
    {day: 6, apricots: 120, bananas: 240, cherries: 105, damsons: 150},
    {day: 7, apricots: 80, bananas: 230, cherries: 105, damsons: 150},
    {day: 8, apricots: 100, bananas: 215, cherries: 110, damsons: 100},
    {day: 9, apricots: 60, bananas: 185, cherries: 105, damsons: 150},
    {day: 10, apricots: 120, bananas: 180, cherries: 130, damsons: 150}
    ];

    var stack = d3.stack()
    .keys(['apricots', 'bananas', 'cherries', 'damsons'])
    .order(d3.stackOrderInsideOut)
    .offset(d3.stackOffsetWiggle);

    var stackedSeries = stack(data);

    d3.select('g')
    .selectAll('path')
    .data(stackedSeries)
    .enter()
    .append('path')
    .style('fill', function(d, i) {
    return colors[i];
    })
    .attr('d', areaGenerator)

    </script>
    </body>
    </html>