2017-02-14 122 views
1

基於我以前的問題How to center (horizontal and vertical) text along an textPath inside an arc using d3.js?,我想知道如何使用鼠標懸停和鼠標懸停事件突出顯示圓弧的邊界。D3在鼠標懸停時改變路徑筆畫顏色

請看下面的示例代碼。如果我將鼠標移到弧上,邊框會變黑。但不完全。它看起來像突出顯示的邊框被相鄰的弧/路徑元素覆蓋。

(?)是否有辦法完全改變邊框的顏色,而不僅僅是其中的一部分?

var dataset = { 
 
    "2":[{"degree1":0,"degree2":1.5707963267949,"label":"Sample Text Test"},  
 
     {"degree1":1.5707963267949,"degree2":3.1415926535898,"label":"Lorem ipsum sample text"}, 
 
     {"degree1":3.1415926535898,"degree2":4.7123889803847,"label":"Sample Text Text"}, 
 
     {"degree1":4.7123889803847,"degree2":6.2831853071796,"label":"Lorem ipsum"}], 
 
    "1":[{"degree1":0,"degree2":3.1415926535898,"label":"Sample"}, 
 
     {"degree1":3.1415926535898,"degree2":6.2831853071796,"label":"Text"}], 
 
    "0":[{"degree1":0,"degree2":6.2831853071796,"label":""}] 
 
    }, 
 
    width = 450, 
 
    height = 450, 
 
    radius = 75; 
 

 
// Helper methods 
 
var innerRadius = function(d, i, j) { 
 
    return 1 + radius * j; 
 
}; 
 

 
var outerRadius = function(d, i, j) { 
 
    return radius * (j + 1); 
 
}; 
 

 
var startAngle = function(d, i, j) { 
 
    return d.data.degree1; 
 
}; 
 

 
var endAngle = function(d, i, j) { 
 
    return d.data.degree2; 
 
}; 
 

 
var pie = d3.layout.pie() 
 
    .sort(null); 
 

 
var arc = d3.svg.arc() 
 
    .innerRadius(innerRadius) 
 
    .outerRadius(outerRadius) 
 
    .startAngle(startAngle) 
 
    .endAngle(endAngle); 
 

 
function centerRadius(d, i, j) { 
 
    return innerRadius(d, i, j)/2 + outerRadius(d, i, j)/2; 
 
} 
 

 
var labelArc = d3.svg.arc() 
 
    .innerRadius(centerRadius) 
 
    .outerRadius(centerRadius) 
 
    .startAngle(startAngle) 
 
    .endAngle(endAngle); 
 

 
var svg = d3.select('body').append('svg') 
 
    .attr('width', width) 
 
    .attr('height', height) 
 
    .append('g') 
 
    .attr('transform', 'translate(' + (width >> 1) + ',' + (height >> 1) + ')'); 
 

 
var level = svg.selectAll('g') 
 
    .data(function(d) { 
 
     return d3.values(dataset); 
 
    }) 
 
    .enter() 
 
    .append('g'); 
 

 
var entry = level.selectAll('g') 
 
    .data(function(d, i) { 
 
     return pie(d); 
 
    }) 
 
    .enter() 
 
    .append('g'); 
 

 
entry.append('path') 
 
    .attr('fill', '#aaa') 
 
    .attr('class', 'border') 
 
    .attr('d', arc) 
 
    .attr('id', function(d, i, j) { 
 
     return 'arc' + i + '-' + j; 
 
    }) 
 
\t .on('mouseover', function (d) { 
 
\t \t \t d3.select(this).style('stroke', 'black'); 
 
\t }) 
 
    .on('mouseout', function (d) { 
 
\t \t \t d3.select(this).style('stroke', '#e1e1e1'); 
 
\t }); 
 

 
entry.append('path') 
 
    .attr('fill', 'none') 
 
    .attr('stroke', 'none') 
 
    .attr('d', labelArc) 
 
    .attr('id', function (d, i, j) { 
 
     return 'arc-label' + i + '-' + j; 
 
    }); 
 

 
var label = entry.append('text') 
 
    .style('font-size', '20px') 
 
    .attr('text-anchor', 'middle'); 
 
/* .attr('dx', function(d, i, j) { 
 
     return Math.round((d.data.degree2 - d.data.degree1) * 180/Math.PI); 
 
    }) 
 
    .attr('dy', function(d, i, j) { 
 
     return ((radius * (j + 1)) - (1 + radius * j)) >> 1; 
 
    }); */ 
 

 

 
label.append('textPath') 
 
    .attr('startOffset', '25%') 
 
    .attr('xlink:href', function(d, i, j) { 
 
     return '#arc-label' + i + '-' + j; 
 
    }) 
 
    .style('fill', '#000') 
 
    .text(function(d) { 
 
     return d.data.label; 
 
    });
text { 
 

 
    font-family: Verdana; 
 

 
} 
 

 
path.border { 
 
    stroke: #e1e1e1; 
 
    stroke-width: 3px; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
<div style="font-family: Verdana; font-size: 20px;">Lorem ipsum sample text</div>

回答

1

問題是你是在後面的另一個路徑的路徑。 因此,沿着路徑的筆劃線寬度是不同的(因爲某些路徑位於另一條路徑的後面)。

您可以通過重新追加發生鼠標懸停/鼠標懸停的組來解決此問題,以便將鼠標懸停在頂部的路徑上。

.on('mouseover', function(d) { 
    this.parentNode.parentNode.appendChild(this.parentNode);//the path group is on the top with in its parent group 
    this.parentNode.parentNode.parentNode.appendChild(this.parentNode.parentNode);//the parent group is on the top with in its parent group 
    d3.select(this).style('stroke', 'black'); 
    }) 

這將使您在其懸停在年底reappended等所有邊境行程線路將是可見的組。

工作代碼here