sigma.canvas.edges.labels.def.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ;(function(undefined) {
  2. 'use strict';
  3. if (typeof sigma === 'undefined')
  4. throw 'sigma is not declared';
  5. // Initialize packages:
  6. sigma.utils.pkg('sigma.canvas.edges.labels');
  7. /**
  8. * This label renderer will just display the label on the line of the edge.
  9. * The label is rendered at half distance of the edge extremities, and is
  10. * always oriented from left to right on the top side of the line.
  11. *
  12. * @param {object} edge The edge object.
  13. * @param {object} source node The edge source node.
  14. * @param {object} target node The edge target node.
  15. * @param {CanvasRenderingContext2D} context The canvas context.
  16. * @param {configurable} settings The settings function.
  17. */
  18. sigma.canvas.edges.labels.def =
  19. function(edge, source, target, context, settings) {
  20. if (typeof edge.label !== 'string' || source == target)
  21. return;
  22. var prefix = settings('prefix') || '',
  23. size = edge[prefix + 'size'] || 1;
  24. if (size < settings('edgeLabelThreshold'))
  25. return;
  26. if (0 === settings('edgeLabelSizePowRatio'))
  27. throw '"edgeLabelSizePowRatio" must not be 0.';
  28. var fontSize,
  29. x = (source[prefix + 'x'] + target[prefix + 'x']) / 2,
  30. y = (source[prefix + 'y'] + target[prefix + 'y']) / 2,
  31. dX = target[prefix + 'x'] - source[prefix + 'x'],
  32. dY = target[prefix + 'y'] - source[prefix + 'y'],
  33. sign = (source[prefix + 'x'] < target[prefix + 'x']) ? 1 : -1,
  34. angle = Math.atan2(dY * sign, dX * sign);
  35. // The font size is sublineraly proportional to the edge size, in order to
  36. // avoid very large labels on screen.
  37. // This is achieved by f(x) = x * x^(-1/ a), where 'x' is the size and 'a'
  38. // is the edgeLabelSizePowRatio. Notice that f(1) = 1.
  39. // The final form is:
  40. // f'(x) = b * x * x^(-1 / a), thus f'(1) = b. Application:
  41. // fontSize = defaultEdgeLabelSize if edgeLabelSizePowRatio = 1
  42. fontSize = (settings('edgeLabelSize') === 'fixed') ?
  43. settings('defaultEdgeLabelSize') :
  44. settings('defaultEdgeLabelSize') *
  45. size *
  46. Math.pow(size, -1 / settings('edgeLabelSizePowRatio'));
  47. context.save();
  48. if (edge.active) {
  49. context.font = [
  50. settings('activeFontStyle'),
  51. fontSize + 'px',
  52. settings('activeFont') || settings('font')
  53. ].join(' ');
  54. context.fillStyle =
  55. settings('edgeActiveColor') === 'edge' ?
  56. (edge.active_color || settings('defaultEdgeActiveColor')) :
  57. settings('defaultEdgeLabelActiveColor');
  58. }
  59. else {
  60. context.font = [
  61. settings('fontStyle'),
  62. fontSize + 'px',
  63. settings('font')
  64. ].join(' ');
  65. context.fillStyle =
  66. (settings('edgeLabelColor') === 'edge') ?
  67. (edge.color || settings('defaultEdgeColor')) :
  68. settings('defaultEdgeLabelColor');
  69. }
  70. context.textAlign = 'center';
  71. context.textBaseline = 'alphabetic';
  72. context.translate(x, y);
  73. context.rotate(angle);
  74. context.fillText(
  75. edge.label,
  76. 0,
  77. (-size / 2) - 3
  78. );
  79. context.restore();
  80. };
  81. }).call(this);