D3 - 使用div放置SVG元素

时间:2014-11-19 20:17:54

标签: html svg d3.js

我正在尝试理解如何使用Div在页面上放置一个SVG元素....我们的代码如下所示:(原谅有一百万件东西可能更干净了......)

我试图将Div放入体内,给它一个id,然后将我的SVG元素放入其中。 div是正方形,我在传说的SVG元素下调用square div,然后传说消失了,我只得到一个div ....什么给出了?

<!DOCTYPE html>
<meta charset="utf-8">
<style type="text/css">
body {
  font: 12px sans-serif;}

svg {
  padding: 20px 0 0 10px;}

.arc {
  stroke: #fffff; }

    a:link {
    color:#58B341;
    background-color:transparent;
    text-decoration:none;
}
a:visited {
    color:#000000;
    background-color:transparent;
    text-decoration:none;
}
a:hover {
    color:#1AA079;
    background-color:transparent;
    text-decoration:underline;
}
a:active {
    color:#ff0000;
    background-color:transparent;
    text-decoration:underline;
}

</style>

  <body>
  **<div id="squares">**
   </div> 
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script>

        var radius = 230,
            padding = 10;

        var color = d3.scale.ordinal()
            .range(["#FFF587", "#FFF9B3", "#E0ECBD", "#C4E0A3", "#C3DD97", "#B9D87A", "#A1CE60", "#7DC050", "#58B341", "#38AC3D", "#26A74E", "#1FA465", "#1AA079", "#1A9A8D", "#199A8B", "#1D859D", "#1264AC", "#15559E", "#173572", "#1E194F"]);

        //WIDTH OF CIRCLE STROKE
        var arc = d3.svg.arc()
            .outerRadius(radius)
            .innerRadius(radius - 100);

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

        d3.csv("data1992.csv", function(error, data) {
          color.domain(d3.keys(data[0]).filter(function(key) { return key !== "State"; }));

          data.forEach(function(d) {
            d.ages = color.domain().map(function(name) {
              return {name: name, population: +d[name]};
            });
          });
//position of legend
          **var legend = d3.select("div.squares").append("svg")**
              .attr("class", "legend")
              .attr("width",radius * 2)
              .attr("height", radius * 2)
            .selectAll("g")
              .data(color.domain().slice().reverse())
            .enter().append("g")
              .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

        //CONTROLS WIDTH OF SQUARE IN LEGEND
          legend.append("rect")
              .attr("width", 18)
              .attr("height", 18)
              .style("fill", color);

        //CONTROLS PLACEMENT OF LIST NEXT TO SQUARES
          legend.append("text")
              .attr("x", 24)
              .attr("y", 9)
              .attr("dy", ".35em")
              .text(function(d) { return d; });

        //HEIGHT OF PLACEMENT OF CIRCLE
          var svg = d3.select("body").selectAll(".pie")
              .data(data)
            .enter().append("svg")
              .attr("class", "pie")
              .attr("width", radius * 2)
              .attr("height", radius * 2)
            .append("g")
              .attr("transform", "translate(" + radius + "," + radius + ")");

          svg.selectAll(".arc")
              .data(function(d) { return pie(d.ages); })
            .enter().append("path")
              .attr("class", "arc")
              .attr("d", arc)
              .style("fill", function(d) { return color(d.data.name); });

          svg.append("text")
              .attr("dy", ".35em")
              .style("text-anchor", "middle")
              .text(function(d) { return d.State; });

        });

    </script>
    </body>

1 个答案:

答案 0 :(得分:1)

根据我上面的评论,请确保您选择正确。目前,您正在使用id设置div(并且没有特定的类),因此您必须将其选为:

var legend = d3.select("div#squares")...

或者,你可以在div上申请一个课程(不能说什么更好 - 取决于你打算用它做什么)

要了解选择器,请参阅例如在这里:http://www.w3schools.com/cssref/css_selectors.asp