2016-09-10 6 views
2

任意のノードをクリックしてグラフを再描画したいとします。ユーザーが任意のノードをクリックすると、マスターJsonから対応するノードのデータがフィルタリングされ、リングが再描画されます。私は次のコードを試しましたが、動作しません。続きd3plusのノードをクリックすると、フィルタリングされたデータでリングを再描画します

<!doctype html> 
<meta charset="utf-8"> 
<script src="//d3plus.org/js/d3.js"></script> 
<script src="//d3plus.org/js/d3plus.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<div id="viz"></div> 
<script> 
var flag =0; 
var curr_data = []; 
var connections = [ 
    {source : "alpha", target : "beta", read: 1, write : 0}, 
    {source : "alpha", target : "gamma", read: 0, write: 1}, 
    {source : "alpha", target : "delta", read: 1, write: 0}, 
    {source : "alpha", target : "epsilon", read: 0, write : 0}, 
    {source : "alpha", target : "peta" ,read: 1, write : 0}, 
    {source : "alpha", target : "zeta", read: 0, write : 0}, 
    {source : "alpha", target : "eta", read: 1, write : 1}, 
    {source : "beta", target : "alpha", read: 0, write : 0}, 
    {source : "beta", target : "gamma", read: 0, write: 1}, 
    {source : "beta", target : "delta", read: 1, write: 1}, 
    {source : "beta", target : "epsilon", read: 0, write : 1}, 
    {source : "beta", target : "peta" ,read: 1, write : 0}, 
    {source : "beta", target : "zeta", read: 0, write : 0}, 
    {source : "beta", target : "eta", read: 1, write : 1}, 
    {source : "gamma", target : "beta", read: 1, write : 0}, 
    {source : "gamma", target : "iota", read: 0, write: 1}, 
    {source : "gamma", target : "delta", read: 1, write: 0}, 
    {source : "gamma", target : "epsilon", read: 0, write : 0}, 
    {source : "gamma", target : "peta" ,read: 1, write : 0}, 
    {source : "gamma", target : "zeta", read: 1, write : 1}, 
    {source : "gamma", target : "eta", read: 1, write : 1}, 
    {source : "delta", target : "beta", read: 1, write : 0}, 
    {source : "delta", target : "gamma", read: 0, write: 1}, 
    {source : "delta", target : "neta", read: 1, write: 0}, 
    {source : "delta", target : "epsilon", read: 0, write : 0}, 
    {source : "delta", target : "peta" ,read: 1, write : 0}, 
    {source : "delta", target : "zeta", read: 0, write : 0}, 
    {source : "delta", target : "eta", read: 1, write : 1} 
]; 
var focused_node = "alpha"; 
for (var v in connections){ 
var item = connections[v]; 
if (item.source.localeCompare("alpha") == 0) { 
curr_data.push({ 
    "source": item.source, 
    "target": item.target 
}); 
} 
} 
$(function(){ 
    var visualization = d3plus.viz() 
    .container("#viz") 
    .type("rings")  
    .edges(curr_data) 
    .edges({"arrows":true, "color":"#000000"}) 
    .edges({"arrows" : {"value": ["source","target"], "direction": "source" }}) 
    .focus(focused_node)  
    .draw(); 
    visualization.mouse({ 
     click: function(dataPoint, currInstance) { 
     curr_data = []; 
     focused_node = dataPoint.id; 
      for (var i in connections) { 
       var item = connections[i]; 
       if (item.source.localeCompare(dataPoint.id) == 0) { 
        curr_data.push({ 
         "source": item.source, 
         "target": item.target 
        }); 
       } 
      }; 
     } 
    }); 
    visualization.edges(curr_data).focus(focused_node).draw(); 
}); 
</script> 

はplnkrの2つのバージョンのとおりです。 First VersionSecond version

This Scatter plot but in rings

答えて

1

に似た何かが、このようにそれを試してみてください。

var flag =0; 
// This represents the edges that are shown right now. 
var activeEdges = []; 
// The node that is currently focused. 
var focusedNode = "alpha"; 
var connections = [ 
    {source : "alpha", target : "beta", read: 1, write : 0}, 
    {source : "alpha", target : "gamma", read: 0, write: 1}, 
    {source : "alpha", target : "delta", read: 1, write: 0}, 
    {source : "alpha", target : "epsilon", read: 0, write : 0}, 
    {source : "alpha", target : "peta" ,read: 1, write : 0}, 
    {source : "alpha", target : "zeta", read: 0, write : 0}, 
    {source : "alpha", target : "eta", read: 1, write : 1}, 
    {source : "beta", target : "alpha", read: 0, write : 0}, 
    // ... 
    // ... 
]; 


// Resets and creates the edges based on the focusedNode. 
function refreshEdges(focusedNode) { 
    activeEdges = []; 
    connections.forEach(function(connection) { 
    if(connection.source == focusedNode) { 
     activeEdges.push({ 
     source: connection.source, 
     target: connection.target 
     }); 
    } 
    }); 
} 

// Draws the ring. 
function drawRing(focusedNode) { 
    // Clear the contents in the container. 
    $("#viz").html(''); 
    var visualization = d3plus.viz() 
    .container("#viz") 
    .type("rings")  
    .edges(activeEdges) 
    .edges({arrows: true, color:"#000000"}) 
    .edges({arrows : {value: focusedNode, direction: "target" }}) 
    .focus(focusedNode)  
    .mouse({ 
     click: function(dataPoint, currInstance) { 
     refreshEdges(dataPoint.id); 
     drawRing(focusedNode); 
     } 
    }) 
    .draw(); 
} 

// Set the edges and draw ring. 
refreshEdges(focusedNode); 
drawRing(focusedNode); 

+1

をすごいです。単一の '.html( '')'はすべてが必要としました。ありがとう:) –

関連する問題