2013-09-24 145 views
0

我一直在寻找一种技术来呈现基于JSON数据树图。我找到了graphViz和d3。但是d3在现代浏览器上占据更多优势。渲染d3.js

所以,从提取的JSON数据,我可以呈现图形底部到顶部,但我想要的链接,是肘部等。我也希望它不只是一棵二叉树。更喜欢的图片

这里是我的代码部分..

var orientation = { 

    "": { 
size: [width, height], 
x: function(d) { return d.x; }, 
y: function(d) { return height - d.y; } 


var svg = d3.select("#graphDiv").selectAll("svg") 
.attr("class","svg_container") 
.data(d3.entries(orientation)) 
.enter().append("svg") 
//.attr("width", width + margin.left + margin.right) 
//.attr("height", height + margin.top + margin.bottom) 
.attr("width", w) 
.attr("height", h) 

.style("overflow", "scroll") 
     .append("svg:g") 
     .attr("class","drawarea") 
     .append("svg:g") 

.append("g") 
.attr("transform", "translate(" + margin.left + "," + margin.top + ")") 
.on("click", function(){update(root);}); 

svg.append("svg:defs").selectAll("marker") 
.data(["suit", "licensing", "resolved"]) 
.enter().append("svg:marker") 
.attr("id", String) 
.attr("viewBox", "0 -5 10 10") 
.attr("refX", 15) 
.attr("refY", -1.5) 
.attr("markerWidth", 6) 
.attr("markerHeight", 6) 
.attr("orient", "auto") 
.append("svg:path") 
.attr("d", "M0,-5L10,0L0,5"); 

svg.append("text") 
.attr("x", 6) 
.attr("y", 6) 
.attr("dy", ".71em") 
.text(function(d) { return d.key; }); 

function graph2() 
{ 
d3.json("tree2.json", function(root) { 
svg.each(function(orientation) { 
var svg = d3.select(this), 
    o = orientation.value; 

// Compute the layout. 
var tree = d3.layout.tree().size(o.size), 
    nodes = tree.nodes(root), 
    links = tree.links(nodes); 

// Create the link lines. 
svg.selectAll(".link") 
    .data(links) 
    .enter().append("path") 
    .attr("class", "link") 
    .attr("d", d3.svg.diagonal().projection(function(d) { return [o.x(d), o.y(d)]; })); 

// Create the node circles. 
var node = svg.selectAll(".node") 
    .data(nodes) 
    .attr("class", "node") 
    .enter().append("g")  
    .on("click", click);     

    node.append("circle") 
    .attr("r", 4) 
    .attr("cx", o.x) 
    .attr("cy", o.y); 


    node.append("text") 
     .attr("class", "name") 
     .attr("x", o.x) 
     .attr("y", o.y) 
     .text(function(d) { return d.gid+" : "+d.name; }); 

    }); 
}); 

d3.select("svg") 
    .call(d3.behavior.zoom() 
      .scaleExtent([0.5, 5]) 
      .on("zoom", zoom)); 
    update(code); 
} 
+0

[这](http://bl.ocks.org/mbostock/2966094)可能是你在做什么 – user1614080

+0

后,这可能会成为你的答案。 http://stackoverflow.com/questions/11137238/d3js-tree-square –

+0

是的。但是,我很难让图形显示为从下到上,使根部位于最底部 –

回答

0

我做了类似的谱系图,但与方向,你所需要的。下面是图片:

enter image description here

这里是codepen的代码。

这应该是你很好的起点。如果您有任何其他问题,请告诉我。