vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

437 lines
13 KiB

9 years ago
10 years ago
  1. var util = require('../../../util');
  2. import Label from './shared/Label'
  3. import BezierEdgeDynamic from './edges/BezierEdgeDynamic'
  4. import BezierEdgeStatic from './edges/BezierEdgeStatic'
  5. import StraightEdge from './edges/StraightEdge'
  6. /**
  7. * @class Edge
  8. *
  9. * A edge connects two nodes
  10. * @param {Object} properties Object with options. Must contain
  11. * At least options from and to.
  12. * Available options: from (number),
  13. * to (number), label (string, color (string),
  14. * width (number), style (string),
  15. * length (number), title (string)
  16. * @param {Network} network A Network object, used to find and edge to
  17. * nodes.
  18. * @param {Object} constants An object with default values for
  19. * example for the color
  20. */
  21. class Edge {
  22. constructor(options, body, globalOptions) {
  23. if (body === undefined) {
  24. throw "No body provided";
  25. }
  26. this.options = util.bridgeObject(globalOptions);
  27. this.body = body;
  28. // initialize variables
  29. this.id = undefined;
  30. this.fromId = undefined;
  31. this.toId = undefined;
  32. this.selected = false;
  33. this.hover = false;
  34. this.labelDirty = true;
  35. this.colorDirty = true;
  36. this.baseWidth = this.options.width;
  37. this.baseFontSize = this.options.font.size;
  38. this.from = undefined; // a node
  39. this.to = undefined; // a node
  40. this.edgeType = undefined;
  41. this.connected = false;
  42. this.labelModule = new Label(this.body, this.options);
  43. this.setOptions(options);
  44. }
  45. /**
  46. * Set or overwrite options for the edge
  47. * @param {Object} options an object with options
  48. * @param doNotEmit
  49. */
  50. setOptions(options) {
  51. if (!options) {
  52. return;
  53. }
  54. this.colorDirty = true;
  55. Edge.parseOptions(this.options, options);
  56. if (options.id !== undefined) {this.id = options.id;}
  57. if (options.from !== undefined) {this.fromId = options.from;}
  58. if (options.to !== undefined) {this.toId = options.to;}
  59. if (options.title !== undefined) {this.title = options.title;}
  60. // A node is connected when it has a from and to node that both exist in the network.body.nodes.
  61. this.connect();
  62. // update label Module
  63. this.updateLabelModule();
  64. let dataChanged = this.updateEdgeType();
  65. return dataChanged;
  66. }
  67. static parseOptions(parentOptions, newOptions) {
  68. var fields = [
  69. 'id',
  70. 'font',
  71. 'from',
  72. 'hidden',
  73. 'hoverWidth',
  74. 'label',
  75. 'length',
  76. 'line',
  77. 'opacity',
  78. 'physics',
  79. 'selfReferenceSize',
  80. 'to',
  81. 'title',
  82. 'value',
  83. 'width',
  84. 'widthMin',
  85. 'widthMax',
  86. 'widthSelectionMultiplier'
  87. ];
  88. // only deep extend the items in the field array. These do not have shorthand.
  89. util.selectiveDeepExtend(fields, parentOptions, newOptions);
  90. util.mergeOptions(parentOptions, newOptions, 'smooth');
  91. util.mergeOptions(parentOptions, newOptions, 'dashes');
  92. util.mergeOptions(parentOptions, newOptions, 'shadow');
  93. // set the scaling newOptions
  94. if (newOptions.scaling !== undefined) {
  95. if (newOptions.scaling.min !== undefined) {parentOptions.scaling.min = newOptions.scaling.min;}
  96. if (newOptions.scaling.max !== undefined) {parentOptions.scaling.max = newOptions.scaling.max;}
  97. util.mergeOptions(parentOptions.scaling, newOptions.scaling, 'label');
  98. }
  99. // hanlde multiple input cases for arrows
  100. if (newOptions.arrows !== undefined) {
  101. if (typeof newOptions.arrows === 'string') {
  102. let arrows = newOptions.arrows.toLowerCase();
  103. if (arrows.indexOf("to") != -1) {parentOptions.arrows.to.enabled = true;}
  104. if (arrows.indexOf("middle") != -1) {parentOptions.arrows.middle.enabled = true;}
  105. if (arrows.indexOf("from") != -1) {parentOptions.arrows.from.enabled = true;}
  106. }
  107. else if (typeof newOptions.arrows === 'object') {
  108. util.mergeOptions(parentOptions.arrows, newOptions.arrows, 'to');
  109. util.mergeOptions(parentOptions.arrows, newOptions.arrows, 'middle');
  110. util.mergeOptions(parentOptions.arrows, newOptions.arrows, 'from');
  111. }
  112. else {
  113. throw new Error("The arrow newOptions can only be an object or a string. Refer to the documentation. You used:" + JSON.stringify(newOptions.arrows));
  114. }
  115. }
  116. // hanlde multiple input cases for color
  117. if (newOptions.color !== undefined) {
  118. if (util.isString(newOptions.color)) {
  119. parentOptions.color.color = newOptions.color;
  120. parentOptions.color.highlight = newOptions.color;
  121. parentOptions.color.hover = newOptions.color;
  122. parentOptions.color.inherit = false;
  123. }
  124. else {
  125. let colorsDefined = false;
  126. if (newOptions.color.color !== undefined) {parentOptions.color.color = newOptions.color.color; colorsDefined = true;}
  127. if (newOptions.color.highlight !== undefined) {parentOptions.color.highlight = newOptions.color.highlight; colorsDefined = true;}
  128. if (newOptions.color.hover !== undefined) {parentOptions.color.hover = newOptions.color.hover; colorsDefined = true;}
  129. if (newOptions.color.inherit !== undefined) {parentOptions.color.inherit = newOptions.color.inherit;}
  130. if (newOptions.color.opacity !== undefined) {parentOptions.color.opacity = Math.min(1,Math.max(0,newOptions.color.opacity));}
  131. if (newOptions.color.inherit === undefined && colorsDefined === true) {
  132. parentOptions.color.inherit = false;
  133. }
  134. }
  135. }
  136. }
  137. updateLabelModule() {
  138. this.labelModule.setOptions(this.options);
  139. if (this.labelModule.baseSize !== undefined) {
  140. this.baseFontSize = this.labelModule.baseSize;
  141. }
  142. }
  143. updateEdgeType() {
  144. let dataChanged = false;
  145. let changeInType = true;
  146. if (this.edgeType !== undefined) {
  147. if (this.edgeType instanceof BezierEdgeDynamic && this.options.smooth.enabled === true && this.options.smooth.dynamic === true) {changeInType = false;}
  148. if (this.edgeType instanceof BezierEdgeStatic && this.options.smooth.enabled === true && this.options.smooth.dynamic === false){changeInType = false;}
  149. if (this.edgeType instanceof StraightEdge && this.options.smooth.enabled === false) {changeInType = false;}
  150. if (changeInType === true) {
  151. dataChanged = this.edgeType.cleanup();
  152. }
  153. }
  154. if (changeInType === true) {
  155. if (this.options.smooth.enabled === true) {
  156. if (this.options.smooth.dynamic === true) {
  157. dataChanged = true;
  158. this.edgeType = new BezierEdgeDynamic(this.options, this.body, this.labelModule);
  159. }
  160. else {
  161. this.edgeType = new BezierEdgeStatic(this.options, this.body, this.labelModule);
  162. }
  163. }
  164. else {
  165. this.edgeType = new StraightEdge(this.options, this.body, this.labelModule);
  166. }
  167. }
  168. else {
  169. // if nothing changes, we just set the options.
  170. this.edgeType.setOptions(this.options);
  171. }
  172. return dataChanged;
  173. }
  174. /**
  175. * Enable or disable the physics.
  176. * @param status
  177. */
  178. togglePhysics(status) {
  179. this.options.physics = status;
  180. this.edgeType.togglePhysics(status);
  181. }
  182. /**
  183. * Connect an edge to its nodes
  184. */
  185. connect() {
  186. this.disconnect();
  187. this.from = this.body.nodes[this.fromId] || undefined;
  188. this.to = this.body.nodes[this.toId] || undefined;
  189. this.connected = (this.from !== undefined && this.to !== undefined);
  190. if (this.connected === true) {
  191. this.from.attachEdge(this);
  192. this.to.attachEdge(this);
  193. }
  194. else {
  195. if (this.from) {
  196. this.from.detachEdge(this);
  197. }
  198. if (this.to) {
  199. this.to.detachEdge(this);
  200. }
  201. }
  202. }
  203. /**
  204. * Disconnect an edge from its nodes
  205. */
  206. disconnect() {
  207. if (this.from) {
  208. this.from.detachEdge(this);
  209. this.from = undefined;
  210. }
  211. if (this.to) {
  212. this.to.detachEdge(this);
  213. this.to = undefined;
  214. }
  215. this.connected = false;
  216. }
  217. /**
  218. * get the title of this edge.
  219. * @return {string} title The title of the edge, or undefined when no title
  220. * has been set.
  221. */
  222. getTitle() {
  223. return this.title;
  224. }
  225. /**
  226. * check if this node is selecte
  227. * @return {boolean} selected True if node is selected, else false
  228. */
  229. isSelected() {
  230. return this.selected;
  231. }
  232. /**
  233. * Retrieve the value of the edge. Can be undefined
  234. * @return {Number} value
  235. */
  236. getValue() {
  237. return this.options.value;
  238. }
  239. /**
  240. * Adjust the value range of the edge. The edge will adjust it's width
  241. * based on its value.
  242. * @param {Number} min
  243. * @param {Number} max
  244. * @param total
  245. */
  246. setValueRange(min, max, total) {
  247. if (this.options.value !== undefined) {
  248. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  249. var widthDiff = this.options.scaling.max - this.options.scaling.min;
  250. if (this.options.scaling.label.enabled === true) {
  251. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  252. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  253. }
  254. this.options.width = this.options.scaling.min + scale * widthDiff;
  255. }
  256. else {
  257. this.options.width = this.baseWidth;
  258. this.options.font.size = this.baseFontSize;
  259. }
  260. }
  261. /**
  262. * Redraw a edge
  263. * Draw this edge in the given canvas
  264. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  265. * @param {CanvasRenderingContext2D} ctx
  266. */
  267. draw(ctx) {
  268. let via = this.edgeType.drawLine(ctx, this.selected, this.hover);
  269. this.drawArrows(ctx, via);
  270. this.drawLabel (ctx, via);
  271. }
  272. drawArrows(ctx, viaNode) {
  273. if (this.options.arrows.from.enabled === true) {this.edgeType.drawArrowHead(ctx,'from', viaNode, this.selected, this.hover);}
  274. if (this.options.arrows.middle.enabled === true) {this.edgeType.drawArrowHead(ctx,'middle', viaNode, this.selected, this.hover);}
  275. if (this.options.arrows.to.enabled === true) {this.edgeType.drawArrowHead(ctx,'to', viaNode, this.selected, this.hover);}
  276. }
  277. drawLabel(ctx, viaNode) {
  278. if (this.options.label !== undefined) {
  279. // set style
  280. var node1 = this.from;
  281. var node2 = this.to;
  282. var selected = (this.from.selected || this.to.selected || this.selected);
  283. if (node1.id != node2.id) {
  284. var point = this.edgeType.getPoint(0.5, viaNode);
  285. ctx.save();
  286. // if the label has to be rotated:
  287. if (this.options.font.align !== "horizontal") {
  288. this.labelModule.calculateLabelSize(ctx,selected,point.x,point.y);
  289. ctx.translate(point.x, this.labelModule.size.yLine);
  290. this._rotateForLabelAlignment(ctx);
  291. }
  292. // draw the label
  293. this.labelModule.draw(ctx, point.x, point.y, selected);
  294. ctx.restore();
  295. }
  296. else {
  297. var x, y;
  298. var radius = this.options.selfReferenceSize;
  299. if (node1.width > node1.height) {
  300. x = node1.x + node1.width * 0.5;
  301. y = node1.y - radius;
  302. }
  303. else {
  304. x = node1.x + radius;
  305. y = node1.y - node1.height * 0.5;
  306. }
  307. point = this._pointOnCircle(x, y, radius, 0.125);
  308. this.labelModule.draw(ctx, point.x, point.y, selected);
  309. }
  310. }
  311. }
  312. /**
  313. * Check if this object is overlapping with the provided object
  314. * @param {Object} obj an object with parameters left, top
  315. * @return {boolean} True if location is located on the edge
  316. */
  317. isOverlappingWith(obj) {
  318. if (this.connected) {
  319. var distMax = 10;
  320. var xFrom = this.from.x;
  321. var yFrom = this.from.y;
  322. var xTo = this.to.x;
  323. var yTo = this.to.y;
  324. var xObj = obj.left;
  325. var yObj = obj.top;
  326. var dist = this.edgeType.getDistanceToEdge(xFrom, yFrom, xTo, yTo, xObj, yObj);
  327. return (dist < distMax);
  328. }
  329. else {
  330. return false
  331. }
  332. }
  333. /**
  334. * Rotates the canvas so the text is most readable
  335. * @param {CanvasRenderingContext2D} ctx
  336. * @private
  337. */
  338. _rotateForLabelAlignment(ctx) {
  339. var dy = this.from.y - this.to.y;
  340. var dx = this.from.x - this.to.x;
  341. var angleInDegrees = Math.atan2(dy, dx);
  342. // rotate so label it is readable
  343. if ((angleInDegrees < -1 && dx < 0) || (angleInDegrees > 0 && dx < 0)) {
  344. angleInDegrees = angleInDegrees + Math.PI;
  345. }
  346. ctx.rotate(angleInDegrees);
  347. }
  348. /**
  349. * Get a point on a circle
  350. * @param {Number} x
  351. * @param {Number} y
  352. * @param {Number} radius
  353. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  354. * @return {Object} point
  355. * @private
  356. */
  357. _pointOnCircle(x, y, radius, percentage) {
  358. var angle = percentage * 2 * Math.PI;
  359. return {
  360. x: x + radius * Math.cos(angle),
  361. y: y - radius * Math.sin(angle)
  362. }
  363. }
  364. select() {
  365. this.selected = true;
  366. }
  367. unselect() {
  368. this.selected = false;
  369. }
  370. }
  371. export default Edge;