2015-04-06 125 views
0

如何在強制佈局中添加和刪除鏈接中的類。考慮如果我的d.source.x == d.target.x然後我必須添加類,否則我必須從鏈接中刪除類。添加和刪除鏈接中的類d3強制佈局

path.attr("d", function(d) { 
    var x1 = d.source.x, 
     y1 = d.source.y, 
     x2 = d.target.x, 
     y2 = d.target.y, 
     dx = Math.abs(x2 - x1), 
     dy = Math.abs(y2 - y1), 
     dr = dx * dx + dy * dy; 

    var rotation = 0; 
    if (x1 === x2) { 

     var dr = Math.sqrt(dx * dx + dy * dy)/1.8; // note that this is always equal to Math.abs(dy) 
     var sweep = 1; 
     if (y1 > y2) { 
      sweep = 0; 

     } 

     return "M" + 
      d.source.x + "," + d.source.y + 
      "A" + dr + "," + dr + 
      " 0, 0" + sweep + " " + 
      d.target.x + "," + d.target.y; 
    } 



    return "M" + 
     d.source.x + "," + 
     d.source.y + "A" + 
     dr + "," + dr + " 0 0,1 " + 
     d.target.x + "," + 
     d.target.y; 
}); 

回答

2

使用d3分類算子。

if(d.source.x == d.target.x){ 
    d3.select(this).classed("your-class-name",true); //Adding class 
} else{ 
    d3.select(this).classed("your-class-name",false); //Removing class 
} 

有關詳細信息,請參閱D3 Class Operations在jaketrent.com

0

簡單,你可以使用jQuery選擇器that--

假設你的鏈接包含。鏈路級和要添加或刪除任何其他類例如。 「你的班級」,那麼你可以簡單地寫,

if(d.source.x == d.target.x){ 
    $(".link").addClass("your-class"); //Adding class 
} else{ 
    $(".link").removeClass("your-class"); //Removing class 
} 

D3是簡單的JavaScript庫,所以它是完全支持jQuery的。

+0

但它選擇所有的鏈接和addClass。我只需要當前(這個)鏈接。 – fekkyDEV 2015-04-07 04:20:57

+0

當前鏈接意味着你點擊了那個鏈接? – Arjun 2015-04-07 05:19:05

+0

是的,點擊或拖動鏈接 – fekkyDEV 2015-04-07 05:23:45