Skip to content

Instantly share code, notes, and snippets.

@wayneminton
Forked from mbostock/.block
Last active August 29, 2015 14:07

Revisions

  1. wayneminton revised this gist Oct 18, 2014. No changes.
  2. wayneminton revised this gist Oct 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    Combination of mbostock's Pie Chart Example
    [Gist](https://gist.github.com/mbostock/3887235) and ZJONSSON's d3.legend
    [Gist](https://gist.github.com/ZJONSSON/3918369). Useful to me when dat labels
    [Gist](https://gist.github.com/ZJONSSON/3918369). Useful to me when data labels
    are too big to write over the pie chart itself. Probably obvious to
    veterans, but here it is anyway.

  3. wayneminton revised this gist Oct 18, 2014. 4 changed files with 79 additions and 9 deletions.
    12 changes: 7 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    This pie chart is constructed from a CSV file storing the populations of various age groups. The chart employs a number of D3 features:
    ## Pie Chart with Legend

    Combination of mbostock's Pie Chart Example
    [Gist](https://gist.github.com/mbostock/3887235) and ZJONSSON's d3.legend
    [Gist](https://gist.github.com/ZJONSSON/3918369). Useful to me when dat labels
    are too big to write over the pie chart itself. Probably obvious to
    veterans, but here it is anyway.

    * [d3.csv](https://github.com/mbostock/d3/wiki/CSV) - load and parse data
    * [d3.scale.ordinal](https://github.com/mbostock/d3/wiki/Ordinal-Scales) - color encoding
    * [d3.svg.arc](https://github.com/mbostock/d3/wiki/SVG-Shapes#wiki-arc) - display arcs
    * [d3.layout.pie](https://github.com/mbostock/d3/wiki/Pie-Layout) - compute arc angles from data
    55 changes: 55 additions & 0 deletions d3.legend.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    // d3.legend.js
    // (C) 2012 [email protected]
    // MIT licence

    (function() {
    d3.legend = function(g) {
    g.each(function() {
    var g= d3.select(this),
    items = {},
    svg = d3.select(g.property("nearestViewportElement")),
    legendPadding = g.attr("data-style-padding") || 5,
    lb = g.selectAll(".legend-box").data([true]),
    li = g.selectAll(".legend-items").data([true])

    lb.enter().append("rect").classed("legend-box",true)
    li.enter().append("g").classed("legend-items",true)

    svg.selectAll("[data-legend]").each(function() {
    var self = d3.select(this)
    items[self.attr("data-legend")] = {
    pos : self.attr("data-legend-pos") || this.getBBox().y,
    color : self.attr("data-legend-color") != undefined ? self.attr("data-legend-color") : self.style("fill") != 'none' ? self.style("fill") : self.style("stroke")
    }
    })

    items = d3.entries(items).sort(function(a,b) { return a.value.pos-b.value.pos})


    li.selectAll("text")
    .data(items,function(d) { return d.key})
    .call(function(d) { d.enter().append("text")})
    .call(function(d) { d.exit().remove()})
    .attr("y",function(d,i) { return i+"em"})
    .attr("x","1em")
    .text(function(d) { ;return d.key})

    li.selectAll("circle")
    .data(items,function(d) { return d.key})
    .call(function(d) { d.enter().append("circle")})
    .call(function(d) { d.exit().remove()})
    .attr("cy",function(d,i) { return i-0.25+"em"})
    .attr("cx",0)
    .attr("r","0.4em")
    .style("fill",function(d) { console.log(d.value.color);return d.value.color})

    // Reposition and resize the box
    var lbbox = li[0][0].getBBox()
    lb.attr("x",(lbbox.x-legendPadding))
    .attr("y",(lbbox.y-legendPadding))
    .attr("height",(lbbox.height+2*legendPadding))
    .attr("width",(lbbox.width+2*legendPadding))
    })
    return g
    }
    })()
    21 changes: 17 additions & 4 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -10,9 +10,14 @@
    stroke: #fff;
    }

    .legend rect {
    fill:white;
    stroke:black;}

    </style>
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="d3.legend.js"></script>
    <script>

    var width = 960,
    @@ -33,7 +38,7 @@
    var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .append("g")
    .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

    d3.csv("data.csv", function(error, data) {
    @@ -44,19 +49,27 @@

    var g = svg.selectAll(".arc")
    .data(pie(data))
    .enter().append("g")
    .enter().append("g")
    .attr("class", "arc");

    g.append("path")
    .attr("d", arc)
    .attr("data-legend", function(d) { return d.data.age; })
    .attr("data-legend-pos", function(d, i) { return i; })
    .style("fill", function(d) { return color(d.data.age); });

    g.append("text")
    .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
    .attr("dy", ".35em")
    .style("text-anchor", "middle")
    .text(function(d) { return d.data.age; });
    .style("text-anchor", "middle");

    var padding = 20,
    legx = radius + padding,
    legend = svg.append("g")
    .attr("class", "legend")
    .attr("transform", "translate(" + legx + ", 0)")
    .style("font-size", "12px")
    .call(d3.legend);
    });

    </script>
    Binary file modified thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  4. @mbostock mbostock revised this gist Oct 14, 2012. 1 changed file with 0 additions and 0 deletions.
    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.
  5. @mbostock mbostock revised this gist Oct 14, 2012. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    This donut chart is constructed from a CSV file storing the populations of various age groups. The chart employs a number of D3 features:
    This pie chart is constructed from a CSV file storing the populations of various age groups. The chart employs a number of D3 features:

    * [d3.csv](https://github.com/mbostock/d3/wiki/CSV) - load and parse data
    * [d3.scale.ordinal](https://github.com/mbostock/d3/wiki/Ordinal-Scales) - color encoding
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@

    var arc = d3.svg.arc()
    .outerRadius(radius - 10)
    .innerRadius(radius - 70);
    .innerRadius(0);

    var pie = d3.layout.pie()
    .sort(null)
  6. @mbostock mbostock revised this gist Oct 14, 2012. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    This donut chart is constructed from a CSV file storing the populations of various age groups. The chart employs a number of D3 features:

    * [d3.csv](https://github.com/mbostock/d3/wiki/CSV) - load and parse data
    * [d3.scale.ordinal](https://github.com/mbostock/d3/wiki/Ordinal-Scales) - color encoding
    * [d3.svg.arc](https://github.com/mbostock/d3/wiki/SVG-Shapes#wiki-arc) - display arcs
    * [d3.layout.pie](https://github.com/mbostock/d3/wiki/Pie-Layout) - compute arc angles from data
  7. @mbostock mbostock revised this gist Oct 14, 2012. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,14 @@
    <meta charset="utf-8">
    <style>

    body {
    font: 10px sans-serif;
    }

    .arc path {
    stroke: #fff;
    }

    </style>
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
  8. @mbostock mbostock revised this gist Oct 14, 2012. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -44,8 +44,7 @@
    .style("fill", function(d) { return color(d.data.age); });

    g.append("text")
    .attr("x", function(d) { return Math.cos((d.startAngle + d.endAngle) / 2) * (radius - 40); })
    .attr("y", function(d) { return Math.sin((d.startAngle + d.endAngle) / 2) * (radius - 40); })
    .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
    .attr("dy", ".35em")
    .style("text-anchor", "middle")
    .text(function(d) { return d.data.age; });
  9. @mbostock mbostock revised this gist Oct 14, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -44,8 +44,8 @@
    .style("fill", function(d) { return color(d.data.age); });

    g.append("text")
    .attr("x", function(d) { return Math.cos((d.startAngle + d.endAngle) / 2 * Math.PI / 180) * (radius - 40); })
    .attr("y", function(d) { return Math.sin((d.startAngle + d.endAngle) / 2 * Math.PI / 180) * (radius - 40); })
    .attr("x", function(d) { return Math.cos((d.startAngle + d.endAngle) / 2) * (radius - 40); })
    .attr("y", function(d) { return Math.sin((d.startAngle + d.endAngle) / 2) * (radius - 40); })
    .attr("dy", ".35em")
    .style("text-anchor", "middle")
    .text(function(d) { return d.data.age; });
  10. @mbostock mbostock revised this gist Oct 14, 2012. 1 changed file with 12 additions and 4 deletions.
    16 changes: 12 additions & 4 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -8,14 +8,15 @@
    <script>

    var width = 960,
    height = 500;
    height = 500,
    radius = Math.min(width, height) / 2;

    var color = d3.scale.ordinal()
    .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

    var arc = d3.svg.arc()
    .outerRadius(Math.min(width, height) / 2 - 10)
    .innerRadius(Math.min(width, height) / 2 - 70);
    .outerRadius(radius - 10)
    .innerRadius(radius - 70);

    var pie = d3.layout.pie()
    .sort(null)
    @@ -42,6 +43,13 @@
    .attr("d", arc)
    .style("fill", function(d) { return color(d.data.age); });

    g.append("text")
    .attr("x", function(d) { return Math.cos((d.startAngle + d.endAngle) / 2 * Math.PI / 180) * (radius - 40); })
    .attr("y", function(d) { return Math.sin((d.startAngle + d.endAngle) / 2 * Math.PI / 180) * (radius - 40); })
    .attr("dy", ".35em")
    .style("text-anchor", "middle")
    .text(function(d) { return d.data.age; });

    });

    </script>
    </script>
  11. @mbostock mbostock revised this gist Oct 14, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,7 @@
    .innerRadius(Math.min(width, height) / 2 - 70);

    var pie = d3.layout.pie()
    .sort(null)
    .value(function(d) { return d.population; });

    var svg = d3.select("body").append("svg")
  12. @mbostock mbostock revised this gist Oct 14, 2012. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -15,14 +15,16 @@

    var arc = d3.svg.arc()
    .outerRadius(Math.min(width, height) / 2 - 10)
    .innerRadius(Math.min(width, height) / 2 - 50);
    .innerRadius(Math.min(width, height) / 2 - 70);

    var pie = d3.layout.pie()
    .value(function(d) { return d.population; });

    var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);
    .attr("height", height)
    .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

    d3.csv("data.csv", function(error, data) {

  13. @mbostock mbostock revised this gist Oct 14, 2012. 2 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@
    .attr("width", width)
    .attr("height", height);

    d3.tsv("data.tsv", function(error, data) {
    d3.csv("data.csv", function(error, data) {

    data.forEach(function(d) {
    d.population = +d.population;
  14. @mbostock mbostock revised this gist Oct 14, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,9 @@
    var width = 960,
    height = 500;

    var color = d3.scale.ordinal()
    .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

    var arc = d3.svg.arc()
    .outerRadius(Math.min(width, height) / 2 - 10)
    .innerRadius(Math.min(width, height) / 2 - 50);
  15. @mbostock mbostock revised this gist Oct 14, 2012. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,10 @@
    var pie = d3.layout.pie()
    .value(function(d) { return d.population; });

    var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

    d3.tsv("data.tsv", function(error, data) {

    data.forEach(function(d) {
  16. @mbostock mbostock created this gist Oct 14, 2012.
    8 changes: 8 additions & 0 deletions data.tsv
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    age,population
    <5,2704659
    5-13,4499890
    14-17,2159981
    18-24,3853788
    25-44,14106543
    45-64,8819342
    ≥65,612463
    37 changes: 37 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <!DOCTYPE html>
    <meta charset="utf-8">
    <style>

    </style>
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script>

    var width = 960,
    height = 500;

    var arc = d3.svg.arc()
    .outerRadius(Math.min(width, height) / 2 - 10)
    .innerRadius(Math.min(width, height) / 2 - 50);

    var pie = d3.layout.pie()
    .value(function(d) { return d.population; });

    d3.tsv("data.tsv", function(error, data) {

    data.forEach(function(d) {
    d.population = +d.population;
    });

    var g = svg.selectAll(".arc")
    .data(pie(data))
    .enter().append("g")
    .attr("class", "arc");

    g.append("path")
    .attr("d", arc)
    .style("fill", function(d) { return color(d.data.age); });

    });

    </script>