2016-05-21 15 views

答えて

1

まず、階層データをn配列にするflatten関数を作成します。選択ボックスで

function flatten(root) { 
    var nodes = [], 
    i = 0; 

    function recurse(node) { 
    if (node.children) node.children.forEach(recurse); 
    if (node._children) node._children.forEach(recurse); 
    if (!node.id) node.id = ++i; 
    nodes.push(node); 
    } 

    recurse(root); 
    return nodes; 
} 

次のように変更リスナーを追加します。あなたの更新機能色インサイド

var select = d3.select("body") 
     .append("select") 
     .on("change", function() { 
    //get the value of the select 
    var select = d3.select("select").node().value; 
    //find selected data from flattened root record 
    var find = flatten(root).find(function(d) { 
     if (d.name == select) 
     return true; 
    }); 
    //reset all the data to have color undefined. 
    flatten(root).forEach(function(d) { 
     d.color = undefined; 
    }) 
    //iterate over the selected node and set color as red. 
    //till it reaches it reaches the root 
    while (find.parent) { 
     find.color = "red"; 
     find = find.parent; 
    } 
    update(find);//call update to reflect the color change 
     }); 

パスこのような(セレクト機能で設定)のデータによると:

d3.selectAll("path").style("stroke", function(d) { 
      if (d.target.color) { 
      return d.target.color;//if the value is set 
      } else { 
      return "gray" 
      } 
     }) 

作業コードhere

+0

ドロップダウンメニューから折りたたまれたノードが選択されている場合...パスは内部的にハイライト表示されますが、そのフルパスも展開する必要があります。リセットボタンをクリックすると、ツリーは元の状態にリセットされます。 ...........とにかく、私を助けてくれてありがとう、ありがとう。 – Jason

+1

あなたは質問してみる。私はそれに答えた。あなたは範囲を広げました。私はまだそれに答えました。あなたはちょうど私があなたのためにプロジェクトをすることはできません...完全な明確な質問をして、範囲を拡大しないでください。誰かが答えたらそれを受け入れます。新しい質問がある場合は、新しい質問をしてください。 – Cyril

+0

Ok cyril、ありがとう....私は実際に範囲を拡大していない、それは私が最初に欲しかったもので、それはかなり論理的です....あなたの答えを受け入れる:) .....ありがとうあなたが私をさらに助けることができれば本当に素晴らしいだろう...新しい質問がありますhttp://stackoverflow.com/questions/37370444/highlight-and-un-highlight-path-from-selected-node-to-root-折りたたみ式のノード – Jason

関連する問題