weighted_directed_graph.js 6.6 KB
Newer Older
1
const DirectedGraphTracer = require('./directed_graph');
N
nem035 已提交
2 3

const {
J
Jason Park 已提交
4
  refineByType
5
} = require('../../tracer_manager/util/index');
N
nem035 已提交
6

J
Jason Park 已提交
7
function WeightedDirectedGraphTracer() {
J
Jason Park 已提交
8 9 10 11 12
  if (DirectedGraphTracer.apply(this, arguments)) {
    WeightedDirectedGraphTracer.prototype.init.call(this);
    return true;
  }
  return false;
J
Jason Park 已提交
13 14 15
}

WeightedDirectedGraphTracer.prototype = $.extend(true, Object.create(DirectedGraphTracer.prototype), {
J
Jason Park 已提交
16 17 18 19
  constructor: WeightedDirectedGraphTracer,
  name: 'WeightedDirectedGraphTracer',
  init: function () {
    var tracer = this;
J
Jason Park 已提交
20

J
Jason Park 已提交
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 86 87
    this.s.settings({
      edgeLabelSize: 'proportional',
      defaultEdgeLabelSize: 20,
      edgeLabelSizePowRatio: 0.8,
      funcLabelsDef: function (node, context, settings) {
        tracer.drawNodeWeight(node, context, settings);
        tracer.drawLabel(node, context, settings);
      },
      funcHoversDef: function (node, context, settings) {
        tracer.drawOnHover(node, context, settings, tracer.drawEdgeWeight);
      },
      funcEdgesArrow: function (edge, source, target, context, settings) {
        var color = tracer.getColor(edge, source, target, settings);
        tracer.drawArrow(edge, source, target, color, context, settings);
        tracer.drawEdgeWeight(edge, source, target, color, context, settings);
      }
    });
  },
  _weight: function (target, weight) {
    this.manager.pushStep(this.capsule, {
      type: 'weight',
      target: target,
      weight: weight
    });
    return this;
  },
  _visit: function (target, source, weight) {
    this.manager.pushStep(this.capsule, {
      type: 'visit',
      target: target,
      source: source,
      weight: weight
    });
    return this;
  },
  _leave: function (target, source, weight) {
    this.manager.pushStep(this.capsule, {
      type: 'leave',
      target: target,
      source: source,
      weight: weight
    });
    return this;
  },
  processStep: function (step, options) {
    switch (step.type) {
      case 'weight':
        var targetNode = this.graph.nodes(this.n(step.target));
        if (step.weight !== undefined) targetNode.weight = refineByType(step.weight);
        break;
      case 'visit':
      case 'leave':
        var visit = step.type == 'visit';
        var targetNode = this.graph.nodes(this.n(step.target));
        var color = visit ? this.color.visited : this.color.left;
        targetNode.color = color;
        if (step.weight !== undefined) targetNode.weight = refineByType(step.weight);
        if (step.source !== undefined) {
          var edgeId = this.e(step.source, step.target);
          var edge = this.graph.edges(edgeId);
          edge.color = color;
          this.graph.dropEdge(edgeId).addEdge(edge);
        }
        if (this.logTracer) {
          var source = step.source;
          if (source === undefined) source = '';
          this.logTracer.print(visit ? source + ' -> ' + step.target : source + ' <- ' + step.target);
J
Jason Park 已提交
88
        }
J
Jason Park 已提交
89 90 91 92 93 94 95
        break;
      default:
        DirectedGraphTracer.prototype.processStep.call(this, step, options);
    }
  },
  setData: function (G) {
    if (Tracer.prototype.setData.apply(this, arguments)) return true;
J
Jason Park 已提交
96

J
Jason Park 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    this.graph.clear();
    var nodes = [];
    var edges = [];
    var unitAngle = 2 * Math.PI / G.length;
    var currentAngle = 0;
    for (var i = 0; i < G.length; i++) {
      currentAngle += unitAngle;
      nodes.push({
        id: this.n(i),
        label: '' + i,
        x: .5 + Math.sin(currentAngle) / 2,
        y: .5 + Math.cos(currentAngle) / 2,
        size: 1,
        color: this.color.default,
        weight: 0
      });
      for (var j = 0; j < G[i].length; j++) {
        if (G[i][j]) {
          edges.push({
            id: this.e(i, j),
            source: this.n(i),
            target: this.n(j),
            color: this.color.default,
            size: 1,
            weight: refineByType(G[i][j])
          });
J
Jason Park 已提交
123
        }
J
Jason Park 已提交
124 125
      }
    }
J
Jason Park 已提交
126

J
Jason Park 已提交
127 128 129 130 131 132 133 134 135 136 137
    this.graph.read({
      nodes: nodes,
      edges: edges
    });
    this.s.camera.goTo({
      x: 0,
      y: 0,
      angle: 0,
      ratio: 1
    });
    this.refresh();
J
Jason Park 已提交
138

J
Jason Park 已提交
139 140 141 142
    return false;
  },
  clear: function () {
    DirectedGraphTracer.prototype.clear.call(this);
143

J
Jason Park 已提交
144 145 146 147 148 149 150 151 152 153
    this.clearWeights();
  },
  clearWeights: function () {
    this.graph.nodes().forEach(function (node) {
      node.weight = 0;
    });
  },
  drawEdgeWeight: function (edge, source, target, color, context, settings) {
    if (source == target)
      return;
J
Jason Park 已提交
154

J
Jason Park 已提交
155 156
    var prefix = settings('prefix') || '',
      size = edge[prefix + 'size'] || 1;
J
Jason Park 已提交
157

J
Jason Park 已提交
158 159
    if (size < settings('edgeLabelThreshold'))
      return;
J
Jason Park 已提交
160

J
Jason Park 已提交
161 162
    if (0 === settings('edgeLabelSizePowRatio'))
      throw '"edgeLabelSizePowRatio" must not be 0.';
J
Jason Park 已提交
163

J
Jason Park 已提交
164 165 166 167 168 169
    var fontSize,
      x = (source[prefix + 'x'] + target[prefix + 'x']) / 2,
      y = (source[prefix + 'y'] + target[prefix + 'y']) / 2,
      dX = target[prefix + 'x'] - source[prefix + 'x'],
      dY = target[prefix + 'y'] - source[prefix + 'y'],
      angle = Math.atan2(dY, dX);
J
Jason Park 已提交
170

J
Jason Park 已提交
171 172 173 174 175
    fontSize = (settings('edgeLabelSize') === 'fixed') ?
      settings('defaultEdgeLabelSize') :
    settings('defaultEdgeLabelSize') *
    size *
    Math.pow(size, -1 / settings('edgeLabelSizePowRatio'));
J
Jason Park 已提交
176

J
Jason Park 已提交
177
    context.save();
J
Jason Park 已提交
178

J
Jason Park 已提交
179 180 181 182 183 184
    if (edge.active) {
      context.font = [
        settings('activeFontStyle'),
        fontSize + 'px',
        settings('activeFont') || settings('font')
      ].join(' ');
J
Jason Park 已提交
185

J
Jason Park 已提交
186 187 188 189 190 191 192
      context.fillStyle = color;
    } else {
      context.font = [
        settings('fontStyle'),
        fontSize + 'px',
        settings('font')
      ].join(' ');
J
Jason Park 已提交
193

J
Jason Park 已提交
194 195
      context.fillStyle = color;
    }
J
Jason Park 已提交
196

J
Jason Park 已提交
197 198
    context.textAlign = 'center';
    context.textBaseline = 'alphabetic';
J
Jason Park 已提交
199

J
Jason Park 已提交
200 201 202 203 204 205 206
    context.translate(x, y);
    context.rotate(angle);
    context.fillText(
      edge.weight,
      0,
      (-size / 2) - 3
    );
J
Jason Park 已提交
207

J
Jason Park 已提交
208 209 210 211 212 213
    context.restore();
  },
  drawNodeWeight: function (node, context, settings) {
    var fontSize,
      prefix = settings('prefix') || '',
      size = node[prefix + 'size'];
J
Jason Park 已提交
214

J
Jason Park 已提交
215 216
    if (size < settings('labelThreshold'))
      return;
J
Jason Park 已提交
217

J
Jason Park 已提交
218 219 220
    fontSize = (settings('labelSize') === 'fixed') ?
      settings('defaultLabelSize') :
    settings('labelSizeRatio') * size;
J
Jason Park 已提交
221

J
Jason Park 已提交
222 223 224 225 226
    context.font = (settings('fontStyle') ? settings('fontStyle') + ' ' : '') +
      fontSize + 'px ' + settings('font');
    context.fillStyle = (settings('labelColor') === 'node') ?
      (node.color || settings('defaultNodeColor')) :
      settings('defaultLabelColor');
J
Jason Park 已提交
227

J
Jason Park 已提交
228 229 230 231 232 233 234
    context.textAlign = 'left';
    context.fillText(
      node.weight,
      Math.round(node[prefix + 'x'] + size * 1.5),
      Math.round(node[prefix + 'y'] + fontSize / 3)
    );
  }
J
Jason Park 已提交
235 236
});

237
module.exports = WeightedDirectedGraphTracer;