2013-02-19 18 views
8

collapsible tree exampleのノードテキストにハイパーリンクを追加します。折りたたみ可能なツリーのノードテキストにハイパーリンクを追加

どうすればいいですか?

+0

そうしない理由を、各ノードのクリックハンドラ関数は、すでにありますがハイパーリンクを使用して実行したいアクションを実行するために使用します。 –

+0

まあ、クリックハンドラは例の円に付けられていて、クリックハンドラをテキスト要素に追加する方法はありません(afaik)ので、これは本当に有効な質問です。実際、私はこれを行う方法も分かりません。今、私はリダイレクトする 'click'ハンドラを使って2番目の要素を描画します。 – Marijn

+0

円のクリックハンドラが展開/折りたたみに添付されています。 – Marijn

答えて

5

IはJavascript/SVG/d3jsのnoobのだが、私は、テキストの上にハイパーリンクされた透明の四角形を配置することで、これを "解決"、この回避策がbl.ockとして提供されています:。

nodeEnter 
    .append("a") 
    .attr("xlink:href", function (d) { return "http://www.example.com/flare/" + d.id; }) 
    .append("rect") 
     .attr("class", "clickable") 
     .attr("y", -6) 
     .attr("x", function (d) { return d.children || d._children ? -60 : 10; }) 
     .attr("width", 50) //2*4.5) 
     .attr("height", 12) 
     .style("fill", "lightsteelblue") 
     .style("fill-opacity", .3)  // set to 1e-6 to make transparent   
     ; 

は、私が追加( g)要素の代わりに clickable styleadd .on("click", click) to the circleを追加してください。

これは機能しますが、クリック可能な矩形のサイズがラベルのテキストのサイズにならないという欠点があります。

私は本当に良いソリューションを楽しみにしていますので、あなたの質問に+1してください!

0

私はこのようなリンクに関するいくつかの情報、とノードにテキストの別の行を追加しました:パスとラベルを使用して、各ノードのために必要なデータを持っている

nodeEnter.append("a") 
    .attr("xlink:href", function(d) { return d.path; }) 
    .append("text") 
    .attr("class", "clickable") 
    .attr("y", 16) 
    .attr("x", function (d) { return d.children || d._children ? -10 : 10; }) 
    .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; }) 
    .text(function(d) { return d.label; }) 

4

あなたがグローバル・ノード上のクリックハンドラを削除し、2つの異なるクリックハンドラを添付する場合は、次のテキスト

ため

  • サークルの一つ
  • 一つをあなたは異なる動作を持つことができますテキストをクリックするとその要素のスタイルを少し変えると、ハイパーリンクと同じように見えます。

    ここに私のバイオリンをチェックアウト: http://jsfiddle.net/empie/EX83X/

    クリックハンドラ

    var nodeEnter = node.enter().append("g") 
          .attr("class", "node") 
          .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; }); 
    
         nodeEnter.append("circle") 
          .attr("r", 1e-6) 
          .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; }).on("click", click);; 
    
         nodeEnter.append("text") 
          .attr("x", function(d) { return d.children || d._children ? -10 : 10; }) 
          .attr("dy", ".35em") 
          .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; }) 
          .text(function(d) { return d.name; }) 
          .style("fill-opacity", 1e-6) 
         .attr("class", "hyper").on("click", clack); 
    
        function clack(d) { 
         alert(d.name); 
        } 
    

    とCSS:

    .hyper { 
        color: blue; 
        text-decoration: underline; 
    } 
    
    .hyper:hover { 
        color: yellow; 
        text-decoration: none; 
    } 
    
関連する問題