javascript - d3 pie chart not loading -
this question has answer here:
- importing data .csv using d3.js 1 answer
all want load basic pie/donut chart, (actually few bar plots in addition , too), looks there error in . if comment same, able serve bare-bones python rendered page(but not pie chart, though).
<!doctype html> <html> <head> <meta charset="utf-8"> <title>{{title}}</title> <link rel="stylesheet" href="/static/css/style.css"> <style> .legend{ font-size: : 12px; } rect{ stroke-width: 2; } #chart_imp,#chart_bid{ width: 50%; } .axis{ font: 10px sans-serif; } .axis path, .axis line{ fill: none; stroke: #000; shape-rendering: crispedges; } </style> </head> <body> <div style="max-width: 800px; border:2px black solid"> <h1>{{haider_imp}}</h1> <div id="chart_imp"></div> <h1>{{haider_bid}}</h1> <div id="chart_bid"></div> </div> <div id="bar-plots"> <div id="bar-plots 1st set"> <h1>{{haider_cpa}}</h1> <div id="cpa"></div> <h1>{{haider_cpc}}</h1> <div id="cpc"></div> <h1>{{haider_cpm}}</h1> <div id="cpm"></div> </div> <div id="bar-plots 2nd set"> <h1>{{haider_avgbid}}</h1> <div id="avg_bid"></div> <h1>{{haider_winrate}}</h1> <div id="winrate"></div> </div> </div> <script src="/static/script/d3.min.js"></script> <script> (function(d3){ 'use strict'; var width = 360; var height = 360; var radius = math.min(width,height)/2; var donutwidth = 75; var legendrectsize = 18; var legendspacing = 4; var color = d3.scale.category20b(); var svg = d3.select('#chart_imp') .append('svg') .attr('width',width) .attr('height',height) .append('g') .attr('transform','translate('+(width/2)+','+(height/2)+')'); var arc = d3.svg.arc() .innerradius(radius-donutwidth) .outerradius(radius); var pie = d3.layout.pie() .value(function(d) { return d.impsplit; }) .sort(null); d3.csv('./static/summary.csv',function(error,dataset){ dataset.foreach(function(d) { d.impsplit = +d.impsplit; }); var path = svg.selectall('path') .data(pie(dataset)) .enter() .append('path') .attr('d',arc) .attr('fill',function(d,i) { return color(d.data.label); }); var legend = svg.selectall('.legend1') .data(color.domain()) .enter() .append('g') .attr('class', 'legend') .attr('transform', function(d, i) { var height = legendrectsize + legendspacing; var offset = height * color.domain().length / 2; var horz = -2 * legendrectsize; var vert = * height - offset; return 'translate(' + horz + ',' + vert + ')'; }); legend.append('rect') .attr('width', legendrectsize) .attr('height', legendrectsize) .style('fill', color) .style('stroke', color); legend.append('text') .attr('x', legendrectsize + legendspacing) .attr('y', legendrectsize - legendspacing) .text(function(d) { return d; }); }); (window.d3); var margin = {top: 20,right:20, bottom: 70, left: 40}, width = 600-margin.left-margin.right, height = 300 - margin.top - margin.bottom; </script> </body> </html>
it might because use piechart dataset outside of .csv callback function. under impression that, when using d3.csv() or d3.tsv() functions, need use retrieved data in callback function. however, use data outside callback function.
check out this answer, might out.
Comments
Post a Comment