PHP:d3.js脚本不显示任何内容

时间:2014-08-12 11:34:25

标签: php jquery html d3.js

问题:我有以下代码,可以作为独立的.html文件运行。现在,我希望在.php文件中显示它。在.php文件中,我复制了相同的代码。

主页面中有一些页眉/页脚和其他html,用于显示此.php文件。但是,当我运行它时,除了“测试”这一行之外,图表不会显示。

应该进行哪些其他更改?

<!DOCTYPE html>

<html>
  <head>    
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">

<div>
      <h4>Testing</h4>
</div>

    <!-- <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.4.5"></script> -->
    <script src="d3.v3.js"></script>

    <!-- Source for example located at: http://bl.ocks.org/1203641 -->

    <style type="text/css">
        .slice text {
            font-size: 16pt;
            font-family: Arial;
        }   
    </style>

    <script>

    var canvasWidth = 500, //width
      canvasHeight = 700,   //height
      outerRadius = 200,   //radius
      color = d3.scale.category20(); //builtin range of colors

    var dataSet = [
      {"legendLabel":"Your child's Percentile", "magnitude":90}, 
      {"legendLabel":"Ahead of your child", "magnitude":10},
//      {"legendLabel":"Three", "magnitude":50}, 
//      {"legendLabel":"Four", "magnitude":16}, 
//      {"legendLabel":"Five", "magnitude":50}, 
//      {"legendLabel":"Six", "magnitude":8}, 
//      {"legendLabel":"Seven", "magnitude":30}
];

    var vis = d3.select("body")
      .append("svg:svg") //create the SVG element inside the <body>
        .data([dataSet]) //associate our data with the document
        .attr("width", canvasWidth) //set the width of the canvas
        .attr("height", canvasHeight) //set the height of the canvas
        .append("svg:g") //make a group to hold our pie chart
          .attr("transform", "translate(" + 1.5*outerRadius + "," + 1.5*outerRadius + ")") // relocate center of pie to 'outerRadius,outerRadius'

    // This will create <path> elements for us using arc data...
    var arc = d3.svg.arc()
      .outerRadius(outerRadius);

    var pie = d3.layout.pie() //this will create arc data for us given a list of values
      .value(function(d) { return d.magnitude; }) // Binding each value to the pie
      .sort( function(d) { return null; } );

    // Select all <g> elements with class slice (there aren't any yet)
    var arcs = vis.selectAll("g.slice")
      // Associate the generated pie data (an array of arcs, each having startAngle,
      // endAngle and value properties) 
      .data(pie)
      // 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
      .enter()
      // Create a group to hold each slice (we will have a <path> and a <text>
      // element associated with each slice)
      .append("svg:g")
      .attr("class", "slice");    //allow us to style things in the slices (like text)

    arcs.append("svg:path")
      //set the color for each slice to be chosen from the color function defined above
      .attr("fill", function(d, i) { return color(i); } )
      //this creates the actual SVG path using the associated data (pie) with the arc drawing function
      .attr("d", arc);

    // Add a legendLabel to each arc slice...
    arcs.append("svg:text")
      .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.outerRadius = outerRadius + 50; // Set Outer Coordinate
        d.innerRadius = outerRadius + 45; // Set Inner Coordinate
        return "translate(" + arc.centroid(d) + ")";
      })
      .attr("text-anchor", "middle") //center the text on it's origin
      .style("fill", "Purple")
      .style("font", "bold 12px Arial")
      .text(function(d, i) { return dataSet[i].legendLabel; }); //get the label from our original data array

    // Add a magnitude value to the larger arcs, translated to the arc centroid and rotated.
    arcs.filter(function(d) { return d.endAngle - d.startAngle > .2; }).append("svg:text")
      .attr("dy", ".35em")
      .attr("text-anchor", "middle")
      //.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")rotate(" + angle(d) + ")"; })
      .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.outerRadius = outerRadius; // Set Outer Coordinate
        d.innerRadius = outerRadius/2; // Set Inner Coordinate
        return "translate(" + arc.centroid(d) + ")rotate(" + angle(d) + ")";
      })
      .style("fill", "White")
      .style("font", "bold 12px Arial")
      .text(function(d) { return d.data.magnitude; });

    // Computes the angle of an arc, converting from radians to degrees.
    function angle(d) {
      var a = (d.startAngle + d.endAngle) * 90 / Math.PI - 90;
      return a > 90 ? a - 180 : a;
    }


                  </script>
            </meta>
      </head>
</html>

1 个答案:

答案 0 :(得分:0)

对我来说很好。唯一的区别是我用远程标签替换了脚本标签

<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://dimplejs.org/dist/dimple.v2.1.0.min.js"></script>

如果您要从其他PHP文件中包含此文件,请删除doctype,html,body和head标记 - 它们不能在生成的文件中重复。

更新:顺便说一下,div标签不属于头部