d3圆环图无法呈现

时间:2014-07-01 07:07:34

标签: javascript d3.js

我正在尝试执行http://bl.ocks.org/mbostock/3887193提到的圆环图。当我执行它时,图表不会呈现,它显示我的空白页。请帮助。两天来敲我的脑袋。感谢帮助。

<apex:includeScript value="{!URLFOR($Resource.jquery1)}"/>     
<apex:includeScript value="{!URLFOR($Resource.D3)}"/>
<apex:includeScript value="{!URLFOR($Resource.nvD3)}"/> 
 <apex:includeScript value="{!URLFOR($Resource.data)}"/> 

<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.3.3/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.0.0-beta/nv.d3.min.js"></script>  -->

<script>
var width = 960,
    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(radius - 10)
    .innerRadius(0);

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

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

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

 console.log(data); 
 data.forEach(function(d) {
    if (isNaN(+d.age)) console.log(d.population);
    d.age = +d.age;
    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); });

  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; });

});

</script>

</apex:page>

1 个答案:

答案 0 :(得分:0)

首先,您应该检查对d3库的引用,我在那里收到错误。尝试使用

<script src="http://d3js.org/d3.v3.min.js" type="text/javascript"></script>

第二,你必须在脚本之前定位你的body-div,所以代码的开头看起来应该是

<div id="body" height="50%" width="500px" ></div> 
<script>
var width = 960,
    height = 500,
    radius = Math.min(width, height) / 2;
//...and so on