Skip to content

Instantly share code, notes, and snippets.

@SmokeyTheSalmon
Last active August 29, 2015 14:02
Show Gist options
  • Save SmokeyTheSalmon/d7cdf0c0e41e41937b42 to your computer and use it in GitHub Desktop.
Save SmokeyTheSalmon/d7cdf0c0e41e41937b42 to your computer and use it in GitHub Desktop.
Makes a pie chart, regardless of input. You have to add this in the index page, though: <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.1.3"></script>.
<link rel="import" href="../bower_components/polymer/polymer.html">
<!--DONT FORGET THE ATTRIBUTES, YOU ATTRIBRUTE!-->
<polymer-element name="pie-chart" attributes="houseval flatval farmval">
<style>
.slice text {
font-size: 16pt;
}
</style>
<template>
<span class="crust">
<svg id="pie" hs="{{houseval}}" flt="{{flatval}}" frm="{{farmval}}">
</svg> {{lastClose}}
</span>
</template>
<script>
function makePie(form) {
var svg = d3.select(form);
var w = 500, //width
h = w, //height
r = (w / 2), //radius
color = d3.scale.ordinal().range(['rgb(216,179,101)','rgb(245,245,245)','rgb(90,180,172)']);
var data = [{"label":"house", "value": svg.attr("hs")},
{"label":"flat", "value": svg.attr("flt")},
{"label":"farm", "value": svg.attr("frm")}];
var vis = svg //we already have an svg element
.data([data]) //associate our data with the document
.attr("width", w) //set the width and height of our visualization (these will be attributes of the <svg> tag
.attr("height", h)
.append("svg:g") //make a group to hold our pie chart
.attr("transform", "translate(" + r + "," + r + ")"); //move the center of the pie chart from 0, 0 to radius, radius
var arc = d3.svg.arc() //this will create <path> elements for us using arc data
.outerRadius(r);
var pie = d3.layout.pie() //this will create arc data for us given a list of values
.value(function(d) { return d.value; }); //we must tell it out to access the value of each element in our data array
var arcs = vis.selectAll("g.slice") //this selects all <g> elements with class slice (there aren't any yet)
.data(pie) //associate the generated pie data (an array of arcs, each having startAngle, endAngle and value properties)
.enter() //this will create <g> elements for every "extra" data element that should be associated with a selection. The result is creating a <g> for every object in the data array
.append("svg:g") //create a group to hold each slice (we will have a <path> and a <text> element associated with each slice)
.attr("class", "slice"); //allow us to style things in the slices (like text)
arcs.append("svg:path")
.attr("fill", function(d, i) { return color(i); } ) //set the color for each slice to be chosen from the color function defined above
.attr("d", arc); //this creates the actual SVG path using the associated data (pie) with the arc drawing function
arcs.append("svg:text") //add a label to each slice
.attr("transform", function(d) { //set the label's origin to the center of the arc
//we have to make sure to set these before calling arc.centroid
d.innerRadius = 0;
d.outerRadius = r;
return "translate(" + arc.centroid(d) + ")"; //this gives us a pair of coordinates like [50, 50]
})
.attr("text-anchor", "middle") //center the text on it's origin
.text(function(d, i) { return data[i].label; }); //get the label from our original data array
};
Polymer('pie-chart', {
ready: function() {
makePie(this.$.pie);
}
});
</script>
</polymer-element>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment