undirected_graph.js 2.1 KB
Newer Older
1 2
'use strict';

3
const DirectedGraphTracer = require('./directed_graph');
N
nem035 已提交
4

J
Jason Park 已提交
5 6 7
class UndirectedGraphTracer extends DirectedGraphTracer {
  static getClassName() {
    return 'UndirectedGraphTracer';
J
Jason Park 已提交
8
  }
J
Jason Park 已提交
9

J
Jason Park 已提交
10 11
  constructor(name) {
    super(name);
J
Jason Park 已提交
12

J
Jason Park 已提交
13 14
    if (this.isNew) initView(this);
  }
J
Jason Park 已提交
15

16 17 18 19
  setTreeData(G, root) {
    return super.setTreeData(G, root, true);
  }

J
Jason Park 已提交
20 21 22
  setData(G) {
    return super.setData(G, true);
  }
J
Jason Park 已提交
23

J
Jason Park 已提交
24
  e(v1, v2) {
J
Jason Park 已提交
25 26 27 28 29 30
    if (v1 > v2) {
      var temp = v1;
      v1 = v2;
      v2 = temp;
    }
    return 'e' + v1 + '_' + v2;
J
Jason Park 已提交
31 32 33
  }

  drawOnHover(node, context, settings, next) {
J
Jason Park 已提交
34
    var tracer = this;
J
Jason Park 已提交
35

J
Jason Park 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    context.setLineDash([5, 5]);
    var nodeIdx = node.id.substring(1);
    this.graph.edges().forEach(function (edge) {
      var ends = edge.id.substring(1).split("_");
      if (ends[0] == nodeIdx) {
        var color = '#0ff';
        var source = node;
        var target = tracer.graph.nodes('n' + ends[1]);
        tracer.drawEdge(edge, source, target, color, context, settings);
        if (next) next(edge, source, target, color, context, settings);
      } else if (ends[1] == nodeIdx) {
        var color = '#0ff';
        var source = tracer.graph.nodes('n' + ends[0]);
        var target = node;
        tracer.drawEdge(edge, source, target, color, context, settings);
        if (next) next(edge, source, target, color, context, settings);
      }
    });
J
Jason Park 已提交
54 55 56
  }

  drawEdge(edge, source, target, color, context, settings) {
J
Jason Park 已提交
57 58
    var prefix = settings('prefix') || '',
      size = edge[prefix + 'size'] || 1;
J
Jason Park 已提交
59

J
Jason Park 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
    context.strokeStyle = color;
    context.lineWidth = size;
    context.beginPath();
    context.moveTo(
      source[prefix + 'x'],
      source[prefix + 'y']
    );
    context.lineTo(
      target[prefix + 'x'],
      target[prefix + 'y']
    );
    context.stroke();
  }
J
Jason Park 已提交
73 74 75 76 77 78 79 80 81 82 83
}

const initView = (tracer) => {
  tracer.s.settings({
    defaultEdgeType: 'def',
    funcEdgesDef(edge, source, target, context, settings) {
      var color = tracer.getColor(edge, source, target, settings);
      tracer.drawEdge(edge, source, target, color, context, settings);
    }
  });
};
J
Jason Park 已提交
84

85
module.exports = UndirectedGraphTracer;