简体   繁体   中英

Render a graph with d3.js

I have been looking for a technology to render a tree based on Json data. I found graphViz and d3. But d3 is taking more advantage on modern browsers.

So, from an extracted Json data, I can render a graph bottom to top, but I want the links to be elbow-like. And I also want it not to be just a binary tree. more like the image...

here is part of my code..

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);
} 

I made a diagram similar to Pedigree, but with orientation that you need. Here is the picture:

在此处输入图片说明

Here is the code on codepen .

This should be your good starting point. Let me know if you have any additional questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM