Reading DOT files in javascript/d3

前端 未结 4 880
眼角桃花
眼角桃花 2020-12-12 15:28

Is there a standard way to read and parse DOT graph files in javascript, ideally in way that will work nicely in d3?

Currently, the only thing I can think of doing i

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 15:58

    To get Graphviz DOT files rendered in Javascript, combine the graphlib-dot and dagre-d3 libraries.

    The graphlibDot.read() method takes a graph or digraph definition in DOT syntax and produces a graph object. The dagreD3.render() method can then output this graph object to SVG.

    You can then use additional D3 methods to add functionality to the graph, retrieving additional node and edge attributes from the graphlib graph object as needed.

    A trivial self-contained example is:

    window.onload = function() {
      // Parse the DOT syntax into a graphlib object.
      var g = graphlibDot.read(
        'digraph {\n' +
        '    a -> b;\n' +
        '    }'
      )
    
      // Render the graphlib object using d3.
      var render = new dagreD3.render();
      render(d3.select("svg g"), g);
    
    
      // Optional - resize the SVG element based on the contents.
      var svg = document.querySelector('#graphContainer');
      var bbox = svg.getBBox();
      svg.style.width = bbox.width + 40.0 + "px";
      svg.style.height = bbox.height + 40.0 + "px";
    }
    svg {
      overflow: hidden;
    }
    .node rect {
      stroke: #333;
      stroke-width: 1.5px;
      fill: #fff;
    }
    .edgeLabel rect {
      fill: #fff;
    }
    .edgePath {
      stroke: #333;
      stroke-width: 1.5px;
      fill: none;
    }
    
    
    
    
    
    
    
      
      
        
      
    
    
    

提交回复
热议问题