Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I figured it out. I used this code and customized it for JSON</p> <p><a href="https://gist.github.com/enjalot/1203641" rel="nofollow">https://gist.github.com/enjalot/1203641</a></p> <p>Here is what I got</p> <pre><code>(function() { var w = 670, //width h = 326, //height r = 150, //radius color = d3.scale.category20c(); //builtin range of colors d3.json("script.php", function (data) { var vis = d3.select("body") .append("svg:svg") //create the SVG element inside the &lt;body&gt; .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 &lt;svg&gt; 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 &lt;path&gt; 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 &lt;g&gt; 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 &lt;g&gt; elements for every "extra" data element that should be associated with a selection. The result is creating a &lt;g&gt; for every object in the data array .append("svg:g") //create a group to hold each slice (we will have a &lt;path&gt; and a &lt;text&gt; 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].col; }); //get the label from our original data array }) })(); </code></pre> <p>So what was wrong was that I wasn't connecting the key values from the JSON, to the variables in the JS. Here are the lines to change:</p> <pre><code>var vis = d3.select("&lt;PUT DIV ID HERE&gt;") .value(function(d) { return d.&lt;PUT NUMBER VALUE KEY NAME HERE&gt;; }); .text(function(d, i) { return data[i].&lt;PUT SLICE CATEGORY HERE&gt;; }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload