File: d3-graphviz-template.html

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (85 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/@hpcc-js/wasm@0.3.11/dist/index.min.js"></script>
<script src="https://unpkg.com/d3-graphviz@3.0.5/build/d3-graphviz.js"></script>
<div id="graph" style="text-align: center;"></div>
<script>
var dotSrc = `
<INSERT_DOT>
`;

var dotSrcLines;
// Label to assembly line mapping
var labelAsm = {};
// regex to find node label line
const re = /^"(?<node>[^"]+)" \[label="\1/;
var graphviz = d3.select("#graph").graphviz();

function render() {
  var t = d3.transition().delay(100).duration(500);
  graphviz.transition(t).renderDot(dotSrc).on("end", interactive);
}

function setup() {
  dotSrcLines = dotSrc.split('\n');
  console.log("Removing assembly lines from nodes");
  // find the assembly line for each label and preserve it in labelAsm
  for (i = 0; i < dotSrcLines.length;) {
      console.log("checking line %d: %s", i, dotSrcLines[i]);
      match = dotSrcLines[i].match(re);
      if (match && dotSrcLines[i+2].startsWith(' ')) {
          console.log(match);
          node = match.groups['node'];
          console.log('Found node "%s" on line %d', node, i);
          labelAsm[node] = dotSrcLines[i+2];
          console.log(labelAsm);
          console.log('Deleting line %d: %s', i+2, dotSrcLines[i+2]);
          dotSrcLines.splice(i+2, 1);
          i = i+3;
      } else {
          i++;
      }
  }
  dotSrc = dotSrcLines.join('\n');
  render();
}

function interactive() {
  nodes = d3.selectAll('.node');
  nodes.on("click", function () {
    var title = d3.select(this).selectAll('title').text().trim();
    var text = d3.select(this).selectAll('text').text();
    var id = d3.select(this).attr('id');
    var class1 = d3.select(this).attr('class');
    dotElement = title.replace('->',' -> ');
    console.log('Element id="%s" class="%s" title="%s" text="%s" dotElement="%s"', id, class1, title, text, dotElement);
    console.log('Inserting assembly line for "%s"', dotElement);
    for (i = 0; i < dotSrcLines.length;) {
        var match = dotSrcLines[i].match(re);
        if (match) {
            var node = match.groups['node'];
            if (node === dotElement) {
                // check if we have an assembly line
                var asm = labelAsm[node];
                if (asm) {
                    // toggle the assembly line
                    if (dotSrcLines[i+2].startsWith(' ')) {
                        dotSrcLines.splice(i+2, 1);
                    } else {
                        dotSrcLines.splice(i+2, 0, asm);
                    }
                    break;
                }
            }
        }
        i++;
    }
    dotSrc = dotSrcLines.join('\n');
    render();
  });
}

setup();
</script>