2016-06-20 55 views
0

您好,我正在嘗試將arctween轉換添加到我的餅圖。我能夠在我的餅圖中顯示工具提示和圖例,但是當我試圖將arctween過渡添加到我的餅圖時,工具提示不起作用。如何使這成爲可能在此餅圖中顯示工具提示和arctween過渡。無法在d3餅圖中顯示工具提示和arctween轉換ata一次

 (function(d3) { 
 
     'use strict'; 
 
     var margin = {top: 50, right: 50, bottom: 50, left: 50}; 
 
     var width = 800 - margin.left - margin.right, 
 
      height = 500 - margin.top - margin.bottom; 
 
     var radius = Math.min(width, height)/3;  
 
     var legendRectSize = 18; 
 
     var legendSpacing = 4; 
 

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

 
     var color = d3.scale.category10(); 
 

 
     var svg = d3.select('#chart') 
 
      .append('svg') 
 
      .attr('width', width+margin.left+margin.right) 
 
      .attr('height', height+margin.left+margin.right) 
 
      .append('g') 
 
      .attr('transform', 'translate(' + (width/2) + ',' + (height/2) + ')'); 
 

 
     var arc = d3.svg.arc() 
 
      .innerRadius(0) 
 
      .outerRadius(radius); 
 

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

 
     var pie = d3.layout.pie() 
 
    \t \t .sort(null) 
 
    \t \t .startAngle(1.1*Math.PI) 
 
    \t \t .endAngle(3.1*Math.PI) 
 
    \t \t .value(function(d) { return d.count; }); 
 

 
     var tooltip = d3.select('#chart')        
 
      .append('div')             
 
      .attr('class', 'tooltip');          
 
         
 
     tooltip.append('div')           
 
      .attr('class', 'label');          
 
       
 
     tooltip.append('div')           
 
      .attr('class', 'count');          
 

 
     tooltip.append('div')           
 
      .attr('class', 'percent'); 
 

 
     var path = svg.selectAll('path') 
 
      .data(pie(data)) 
 
      .enter() 
 
      .append('path') 
 
      .attr('d', arc) 
 
      .attr('fill', function(d,i) { 
 
       return color(d.data.IP); 
 
      }); 
 
      path.on('mouseover', function(d) { 
 
      d3.select(this).transition() 
 
        .ease("exp") 
 
        .duration(3000) 
 
        .attrTween("d", tweenPie) 
 
        .duration(200) 
 
        .attr("d", arcOver) 
 
        .style('opacity',0.7) 
 

 
        function tweenPie(b) { 
 
         var i = d3.interpolate({startAngle: 1.1*Math.PI, endAngle: 1.1*Math.PI}, b); 
 
         return function(t) { return arc(i(t)); }; 
 
        } 
 

 
      var total = d3.sum(data.map(function(d) {     
 
       return d.count;           
 
      })); 
 

 
      var percent = Math.round(1000 * d.data.count/total)/10; 
 
      tooltip.select('.label').html(d.data.IP);     
 
      tooltip.select('.count').html(d.data.count);     
 
      tooltip.select('.percent').html(percent + '%');    
 
      tooltip.style('display', 'block');         
 
      });               
 
       
 
      path.on("mouseout", function(d) { 
 
       d3.select(this).transition() 
 
        .duration(100) 
 
        .attr("d", arc) 
 
        .style('opacity','1'); 
 
       tooltip.style('display', 'none'); 
 
       });        
 

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

 
     })(window.d3);
 #chart { 
 
     margin-top: 100px;            
 
     position: absolute; 
 
     margin-right: 50px; 
 
     margin-left: 50px;           
 
     }                 
 
     .tooltip {               
 
     background: #eee;            
 
     box-shadow: 0 0 5px #999999;          
 
     color: #900C3F;              
 
     display: inline-block;             
 
     font-size: 12px;             
 
     left: 600px;              
 
     padding: 10px;             
 
     position: absolute;            
 
     text-align: center;            
 
     top: 95px;              
 
     width: 100px;              
 
     z-index: 10; 
 
     opacity: 1;             
 
     }                 
 
     rect { 
 
     stroke-width: 2; 
 
     } 
 
     path { 
 
    \t stroke: #ffffff; 
 
    \t stroke-width: 0.5; 
 
\t }
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
    <title>Testing Pie Chart</title> 
 
    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> 
 
    </head> 
 
    <body> 
 
    <div id="chart"></div> 
 
    </body> 
 
</html>

請幫助我。上面提到的是我已經嘗試過的代碼。 感謝您的幫助。

+0

tootip變得不重要,但不是轉換之間的結果。 –

+0

你在哪裏應用過渡到你的派,我看到的唯一過渡是在你的鼠標懸停。請在下面的答案中看到jsbin,我已經對你的代碼做了一些更改,並讓我知道你是否期待這一點。 – SiddP

回答

0

你想是這樣的(http://jsbin.com/guqozu/edit?html,js,output):

.on("mouseover", function(d) { 
    var htmlMsg=""; 
    div.transition()  
    .style("opacity", .9);  
    div.html(
    "IP :"+d.data.IP+""+"<br/>"+ 
    "Count : " + d.data.count +"<br/>" + 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 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); 
+0

是的。這正是我想要的。非常感謝。現在通過你的代碼。 :) –

0

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

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

 
var width = 300, 
 
\t height = 300; 
 
var margin = {top: 15, right: 15, bottom: 20, left: 40}, 
 
\t radius = Math.min(width, height)/2 - 10; 
 
var legendRectSize = 18, 
 
    legendSpacing = 4; 
 
\t \t \t 
 

 
\t \t var color = d3.scale.category20b(); 
 

 
\t \t var arc = d3.svg.arc() 
 
\t \t  .outerRadius(radius); 
 
\t \t 
 
\t \t var arcOver = d3.svg.arc() 
 
\t \t   .outerRadius(radius + 10); 
 

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

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

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

 
\t \t 
 
\t \t arcs.append("path") 
 
\t \t  .attr("fill", function(d, i) { return color(i); }) 
 
\t \t  .on("mouseover", function(d) { 
 
\t \t \t \t var htmlMsg=""; 
 
\t  \t \t \t div.transition() \t \t 
 
      \t \t \t .style("opacity",0.9); \t \t 
 
     \t \t div.html(
 
     \t \t "IP :"+d.data.IP+""+"<br/>"+ 
 
     \t \t "Count : " + d.data.count +"<br/>" + htmlMsg) 
 
\t   \t \t .style("left", (d3.event.pageX) + "px") 
 
      \t \t .style("top", (d3.event.pageY) + "px"); \t 
 

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

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

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

 
     var k=0; 
 
\t \t arcs2.append("text") 
 
\t \t .transition() 
 
\t \t  .ease("elastic") 
 
\t \t \t .duration(2000) 
 
\t \t \t .delay(function (d, i) { 
 
\t \t \t \t return i * 250; 
 
\t \t \t }) 
 
\t \t  .attr("x","6") 
 
\t \t  .attr("dy", ".35em") 
 
\t \t  .text(function(d) { if(d.data.count >0){ k = k+1; return d.data.count;} }) 
 
\t \t  .attr("transform", function(d) { if (k >1){return "translate(" + labelArc.centroid(d) + ") rotate(" + angle(d) + ")";} else{return "rotate(-360)";} }) 
 
\t \t  .attr("font-size", "10px"); 
 
\t  
 

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

 
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", 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; });
 #chart { 
 
     margin-top: 100px;            
 
     position: absolute; 
 
     margin-right: 50px; 
 
     margin-left: 50px;           
 
     }                 
 
     .tooltip {               
 
     background: #eee;            
 
     box-shadow: 0 0 5px #999999;          
 
     color: #900C3F;              
 
     display: inline-block;             
 
     font-size: 12px;             
 
     left: 600px;              
 
     padding: 10px;             
 
     position: absolute;            
 
     text-align: center;            
 
     top: 95px;              
 
     width: 150px;              
 
     z-index: 10; 
 
     opacity: 1;             
 
    }                 
 
    rect { 
 
     stroke-width: 2; 
 
    } 
 
    path { 
 
    \t stroke: #ffffff; 
 
    \t stroke-width: 0.5; 
 
\t } 
 
\t div.tooltip { 
 
\t position: absolute; 
 
\t z-index: 999; 
 
\t padding: 10px; 
 
\t background: #f4f4f4; 
 
\t border: 0px; 
 
\t border-radius: 3px; 
 
\t pointer-events: none; 
 
\t font-size: 11px; 
 
\t color: #000; 
 
\t line-height: 16px; 
 
\t border: 1px solid #d4d4d4; 
 
\t } 
 

 
\t .legend{ 
 
\t \t margin-left: 300px; 
 
\t }
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
    <title>Testing Pie Chart</title> 
 
    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> 
 
    </head> 
 
<body> 
 
    <div id="chart"></div> 
 
    <div id="toolTip" class="tooltip" style="opacity: 0;"></div> 
 
    <script type="text/javascript"> 
 
     
 
    </script> 
 
    </body> 
 
</html>

感謝您對SiddP建議一些有用的提示。基於他的代碼和我的研究,我在這個餅圖中添加了一個圖例以及arctween和工具提示,以使其完成。我希望這可以幫助有人在D3餅圖上掙扎。 謝謝你stackoverflow。 :}