2013-10-11 82 views
1

在下面的聚類圖中,聚類從左到右流動。將圖改爲自下而上,而不是從左到右

我想顯示爲「自下而上」像這樣的集羣:

enter image description here

下面是當前顯示的圖:

enter image description here

這裏生成此圖的代碼:

var width = 460, 
    height = 500; 

var cluster = d3.layout.cluster() 
    .size([height, width - 160]); 

var diagonal = d3.svg.diagonal() 
    .projection(function (d) { 
    return [d.y, d.x]; 
}); 

var svg = d3.select("body").append("svg") 
    .attr("width", width) 
    .attr("height", height) 
    .append("g") 
    .attr("transform", "translate(40,0)"); 

    var nodes = cluster.nodes(getData()), 
     links = cluster.links(nodes); 

    var link = svg.selectAll(".link") 
     .data(links) 
     .enter().append("path") 
     .attr("class", "link") 
     .attr("d", diagonal); 

    var node = svg.selectAll(".node") 
     .data(nodes) 
     .enter().append("g") 
     .attr("class", "node") 
     .attr("transform", function (d) { 
     return "translate(" + d.y + "," + d.x + ")"; 
    }) 

    node.append("circle") 
     .attr("r", 4.5); 

    node.append("text") 
     .attr("dx", function (d) { return d.children ? -8 : 8; }) 
     .attr("dy", 3) 
     .style("text-anchor", function (d) { return d.children ? "end" : "start"; }) 
     .text(function (d) { return d.name; }); 

d3.select(self.frameElement).style("height", height + "px"); 


function getData() { 
    return {"children":[{"children":[{"name":"cluster","children":[{"name":"AgglomerativeCluster","size":3938},{"name":"TestCLuster","size":3938}]}],"name":"analytics"}],"name":"flare"} 
} 

.node circle { 
    fill: #fff; 
    stroke: steelblue; 
    stroke-width: 1.5px; 
} 
.node { 
    font: 10px sans-serif; 
} 
.link { 
    fill: none; 
    stroke: #ccc; 
    stroke-width: 1.5px; 
} 

這裏是一個小提琴:http://jsfiddle.net/drW3e/25/

如何更新代碼,使圖表顯示爲「自下而上」而不是「從左到右」?與它玩耍,交換X & y值

回答

1

位似乎是一個良好的開端,

所以

return [d.y, d.x]; 

將成爲

return [d.x, d.y]; 

等等

http://jsfiddle.net/drW3e/27/

+0

看起來「flare」現在已經溢出了屏幕,因爲它的圖標的一部分被隱藏了。該圖可以推倒以抵制這種情況嗎? –

相關問題