如何制作d3.js饼图的单独文件并将其加载到html页面

时间:2014-11-21 03:01:47

标签: javascript jquery html d3.js

我将该代码粘贴到jack.js文件

     <script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
        <script type="text/javascript">
        //Width and height
        var w = 300;
        var h = 300;
        var dataset = [ 5, 10, 20, 45, 6, 25 ];
        var outerRadius = w / 2;
        var innerRadius = 0;
        var arc = d3.svg.arc()
                        .innerRadius(innerRadius)
                        .outerRadius(outerRadius);

        var pie = d3.layout.pie();
        var color = d3.scale.category10();
        var svg = d3.select("body").append("svg").attr("width", w)
                    .attr("height", h);     
        var arcs = svg.selectAll("g.arc").data(pie(dataset)).enter()
                      .append("g").attr("class", "arc")
        .attr("transform", "translate(" + outerRadius + "," +outerRadius + ")");
                   arcs.append("path").attr("fill", function(d, i) {
                return color(i);})
              .attr("d", arc).on("mouseenter", function(d) {
              //console.log("mousein")
              text = arcs.append("text").attr("transform", arc.centroid(d))
              .attr("dy", ".5em").style("text-anchor", "middle")
              .style("fill", "blue").attr("class", "on")
              .text(d.data.place);})
              .on("mouseout", function(d) {
             text.remove();});
            //Labels
             arcs.append("text").attr("transform", function(d) {
                return "translate(" + arc.centroid(d) + ")"; })
            .attr("text-anchor", "middle").text(function(d) {
                return d.value;
            });</script>

并将其链接到我的html页面。

问题:此jack.js文件未在HTML页面中显示饼图。我能告诉我应该做什么吗?

1 个答案:

答案 0 :(得分:0)

这是你想要的行为吗? plnkr

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>

  </head>

  <body>
    <script type="text/javascript" src="jack.js"></script>
    <h1>Hello Plunker!</h1>
  </body>

</html>