javascript - Making pie charts using d3.js? -


i using mysql , php backend. there example illustrate use of d3.js php , mysql.

this how draw pie chart static data . can extract script in .js file , use dynamic data.

<html> <head>     <title>         pie chart     </title> <script type="text/javascript" src='d3.v3.min.js' charset='utf-8' ></script> </head>     <body>         <script type="text/javascript">              var metadata = {                 'x' : 'name' ,                 'y' : 'percent' };              var data = [                 {label : 'name1', value : 25},                 {label : 'name2', value : 20},                 {label : 'name3', value : 40},                 {label : 'name4', value : 15} ];              var width = 1100 , height = 650 , radius = 250 ,             color = ["#c5aaf5","#fb7374","#a3cbf1","#79bfa1","#f5a352","#94b3c8", "#f9d08b","#b2ac4e","#64bd4f","#c09372"];              var colordescriptions = [];              var svgcontainer = d3.select("body") // create svg container                 .append("svg:svg")                 .data([data])                 .attr("width", width)                 .attr("height", height)                 .append("svg:g")                 .attr("transform", "translate(" + 300 + "," + 300 + ")") ;;              var arc = d3.svg.arc() // draw arc of given radius                 .outerradius(radius);              var pie = d3.layout.pie() // assign data pie chart                 .value(function(d) { return d.value; });              var arcs = svgcontainer.selectall("g.slice") // slice circle                 .data(pie)                    .enter()                 .append("svg:g")                 .attr("class", "slice");              arcs.append("svg:path") // fill color in each slice                 .attr("fill", function(d, i) {                  var colorselected =  color[i];                 colordescriptions.push({"colorselected": colorselected, "label": data[i].label});                 return colorselected; } )                 .attr("d", arc)              arcs.append("svg:text") // write slice information values                 .attr("transform", function(d) {                 d.innerradius = 0;                 d.outerradius = radius;                     return "translate(" + arc.centroid(d) + ")";                 })                 .attr("text-anchor", "middle")                 .text(function(d, i) { return data[i].value; })                 .style("font-family","monospace")                 .style("fill", "#3f3f3f")                 .style("font-size", "20px");              descriptiontext = "pie chart of " + metadata.x + " " + metadata.y; // pie chart description              var description = svgcontainer.append("g").attr("class", "description"); // pie chart description             var desc_label = description.append("text")                 .attr("class", "description")                 .attr("y", 300)                 .attr("x", 000)                 .text(descriptiontext)                 .style("font-weight", "bold")                 .style("font-size", "20px")                 .style("text-anchor", "middle");               var piechartlabels = svgcontainer.append("g").attr("id","pie-chart-labels");   //index pie chart : name             piechartlabels.selectall("text").data(colordescriptions).enter().append("svg:text")                 .text(function(d) { return d.label; } ).attr("x",440)                 .attr("y",function(d, i) { return 14 + i*30; })                 .style("font-size", "20px");              var piechartlabelscolors = svgcontainer.append("g").attr("id","pie-chart-labels-colors");              piechartlabelscolors.selectall("rect").data(colordescriptions).enter().append("rect")                  .attr("x",400)                 .attr("y",function(d, i) { return i*30; })                 .attr("width", 25)                 .attr("height", 15)                 .style("fill" , function(d) { return d.colorselected; }); //index pie chart : color         </script>     </body> </html> 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -