传奇在饼图之后对齐但之前已经对齐

时间:2016-06-22 08:51:03

标签: javascript d3.js

您好我想在饼图后右对齐我的图例。但是使用我的代码,它们会在饼图之前与左边对齐。我应该做什么改变CSS来做到这一点。

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Testing Pie Chart</title>
    <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
     <style type="text/css">

/*     #container {
        margin: 5%;
        height: 50%;
        width: 50%;
      }*/

     #chart {  
        position: absolute;  
        background-color: #eee; 
/*        height: 50%;
        width: 50%;   */                                      
      }
      #chart legend{  
        position: absolute;                                   
      }                                                                 
      .tooltip {                                                        
        background: #eee;                                               
        box-shadow: 0 0 5px #999999;                                    
        color: #900C3F;                                                    
        display: inline-block;                                                  
        font-size: 12px;                                                
        position: absolute;                                             
        text-align: center;                                             
        width: 10%;                                                    
        z-index: 10;  
        opacity: 1;                                                 
    }                                                                 
    rect {
       stroke-width: 2;
    }
    path {
        stroke: #ffffff;
        stroke-width: 0.5;
    }
    div.tooltip {
    position: absolute;
    z-index: 999;
    padding: 10px;
    background: #f4f4f4;
    border: 0px;
    border-radius: 3px;
    pointer-events: none;
    font-size: 11px;
    color:  #080808;
    line-height: 16px;
    border: 1px solid #d4d4d4;
    }

     </style>
      </head>
  <body>

    <div id="container">
    <svg id="chart" viewBox="0 0 960 500" perserveAspectRatio="xMinYMid">
    <div id="toolTip" class="tooltip" style="opacity: 0;"></div>
    <script type="text/javascript">

var div = d3.select("#toolTip");

var data = [
      ["192.168.12.1", 20], 
      ["76.09.45.34", 40], 
      ["34.91.23.76", 80],
      ["192.168.19.32", 16], 
      ["192.168.10.89", 50], 
      ["192.178.34.07", 18],
      ["192.168.12.98", 30]];

var data = data.map(function(d) {
    return {
      IP: d[0],
      count: d[1]
    };
});

var width = 500,
    height = 300;
var radius = Math.min(width, height) / 2 - 50;
var legendRectSize = 18,
    legendSpacing = 4;
            

        var color = d3.scale.category20b();

        var arc = d3.svg.arc()
            .outerRadius(radius);
          
          var arcOver = d3.svg.arc()
                .outerRadius(radius + 5);

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

        var labelArc = d3.svg.arc()
            .outerRadius(radius - 40)
            .innerRadius(radius - 40);
          
        var svg = d3.select("#chart").append("svg")
            .datum(data)
            .attr("width", width)
            .attr("height", height)
            .append("g")
            .attr("transform", "translate(" + width/2  + "," + height/2 + ")");

        var arcs = svg.selectAll(".arc")
            .data(pie)
            .enter().append("g")
            .attr("class", "arc");
          
         var arcs2 = svg.selectAll(".arc2")
            .data(pie)
            .enter().append("g")
            .attr("class", "arc2");

          
        arcs.append("path")
            .attr("fill", function(d, i) { return color(i); })
            .on("mouseover", function(d) {
                var htmlMsg="";
                div.transition()        
                    .style("opacity",0.9);
                var total = d3.sum(data.map(function(d) {                   
                    return d.count;                                           
                })); 
                var percent = Math.round(1000 * d.data.count / total) / 10;         
                div.html(
                 "IP :"+ d.data.IP +""+"<br/>"+
                 "Count : " +  d.data.count +"<br/>" + 
                 "Percent: " + percent + '%'+ htmlMsg)
                .style("left",  (d3.event.pageX) + "px")
                .style("top",  (d3.event.pageY) + "px");    

                svg.selectAll("path").sort(function (a, b) { 
                    if (a != d) return -1;               
                    else return 1;                             
                });
          
                var endAngle = d.endAngle + 0.1;
                var startAngle = d.startAngle - 0.1;
                var arcOver = d3.svg.arc()
                    .outerRadius(radius + 10).endAngle(endAngle).startAngle(startAngle);
                        d3.select(this)
                        .attr("stroke","white")
                        .transition()
                        .ease("bounce")
                        .duration(1000)
                        .attr("d", arcOver)             
                        .attr("stroke-width",6);
                    })

            .on("mouseout", function(d) {
                div.transition()        
                .duration(500)      
                .style("opacity", 0); 
                    d3.select(this).transition()            
                       .attr("d", arc)
                       .attr("stroke","none");
                })
                .transition()
                .ease("bounce")
                .duration(2000)
                .attrTween("d", tweenPie);

        function tweenPie(b) {
          b.innerRadius = 0;
          var i = d3.interpolate({startAngle: 0, endAngle: 0}, b);
          return function(t) { return arc(i(t)); };
        }

        var k=0;
        arcs2.append("text")
        .transition()
            .ease("elastic")
            .duration(2000)
            .delay(function (d, i) {
                return i * 250;
            })
              .attr("x","6")
              .attr("dy", ".35em")
              .text(function(d) { if(d.data.count >0){ k = k+1; return d.data.count;} }) //We can define any value for (d.data.count >__ according to our need. Like if we have smaller values less than 10 then no need to dislay them as they can be overlapped on pie slice margins)// If not needed to display make this comment
              .attr("transform", function(d) { if (k >1){return "translate(" + labelArc.centroid(d) + ") rotate(" + angle(d) + ")";} else{return "rotate(-360)";} })
              .attr("font-size", "10px");
        

        function type(d) {
          d.count = +d.count;
          return d;
        }
        function angle(d) {
              var a = (d.startAngle + d.endAngle) * 90 / Math.PI - 90;
              return a > 90 ? a - 180 : a;
        }

 var legend = d3.select("#chart")
    .append("svg")
    .attr("class", "legend")
    .attr("width", radius+50)
    .attr("height", radius * 2)
    .selectAll("g")
    .data(color.domain())
    .enter()
    .append("g")
    .attr("transform", "translate(" + width * 1/4  + "," + height * 1/4 + ")")
    .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

 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)
    .data(data)
    .text(function(d,i) { return d.IP; });

  </script>   
  </svg>
  </div>

  <script type="text/javascript">
var chart = $("#chart"),
    aspect = chart.width() / chart.height(),
    container = chart.parent();
$(window).on("resize", function() {
    var targetWidth = container.width();
    var targetHeight = container.height();
    chart.attr("width", targetWidth);
    chart.attr("height", Math.round(targetWidth / aspect));
}).trigger("resize");
</script>

    </script>
  </body>
 </html>

请介绍一下如何解决这个问题。我陷入了这段代码。 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您需要将x和y属性添加到图例中。

legend.attr("x", width - 65)
      .attr("y", 25)

以下是full code and working example for you