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.

441 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. if (this.options.smooth.enabled === true && this.options.smooth.dynamic === true) {
  180. if (this.via === undefined) {
  181. this.via.pptions.physics = status;
  182. }
  183. }
  184. this.options.physics = status;
  185. }
  186. /**
  187. * Connect an edge to its nodes
  188. */
  189. connect() {
  190. this.disconnect();
  191. this.from = this.body.nodes[this.fromId] || undefined;
  192. this.to = this.body.nodes[this.toId] || undefined;
  193. this.connected = (this.from !== undefined && this.to !== undefined);
  194. if (this.connected === true) {
  195. this.from.attachEdge(this);
  196. this.to.attachEdge(this);
  197. }
  198. else {
  199. if (this.from) {
  200. this.from.detachEdge(this);
  201. }
  202. if (this.to) {
  203. this.to.detachEdge(this);
  204. }
  205. }
  206. }
  207. /**
  208. * Disconnect an edge from its nodes
  209. */
  210. disconnect() {
  211. if (this.from) {
  212. this.from.detachEdge(this);
  213. this.from = undefined;
  214. }
  215. if (this.to) {
  216. this.to.detachEdge(this);
  217. this.to = undefined;
  218. }
  219. this.connected = false;
  220. }
  221. /**
  222. * get the title of this edge.
  223. * @return {string} title The title of the edge, or undefined when no title
  224. * has been set.
  225. */
  226. getTitle() {
  227. return this.title;
  228. }
  229. /**
  230. * check if this node is selecte
  231. * @return {boolean} selected True if node is selected, else false
  232. */
  233. isSelected() {
  234. return this.selected;
  235. }
  236. /**
  237. * Retrieve the value of the edge. Can be undefined
  238. * @return {Number} value
  239. */
  240. getValue() {
  241. return this.options.value;
  242. }
  243. /**
  244. * Adjust the value range of the edge. The edge will adjust it's width
  245. * based on its value.
  246. * @param {Number} min
  247. * @param {Number} max
  248. * @param total
  249. */
  250. setValueRange(min, max, total) {
  251. if (this.options.value !== undefined) {
  252. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  253. var widthDiff = this.options.scaling.max - this.options.scaling.min;
  254. if (this.options.scaling.label.enabled === true) {
  255. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  256. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  257. }
  258. this.options.width = this.options.scaling.min + scale * widthDiff;
  259. }
  260. else {
  261. this.options.width = this.baseWidth;
  262. this.options.font.size = this.baseFontSize;
  263. }
  264. }
  265. /**
  266. * Redraw a edge
  267. * Draw this edge in the given canvas
  268. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  269. * @param {CanvasRenderingContext2D} ctx
  270. */
  271. draw(ctx) {
  272. let via = this.edgeType.drawLine(ctx, this.selected, this.hover);
  273. this.drawArrows(ctx, via);
  274. this.drawLabel (ctx, via);
  275. }
  276. drawArrows(ctx, viaNode) {
  277. if (this.options.arrows.from.enabled === true) {this.edgeType.drawArrowHead(ctx,'from', viaNode, this.selected, this.hover);}
  278. if (this.options.arrows.middle.enabled === true) {this.edgeType.drawArrowHead(ctx,'middle', viaNode, this.selected, this.hover);}
  279. if (this.options.arrows.to.enabled === true) {this.edgeType.drawArrowHead(ctx,'to', viaNode, this.selected, this.hover);}
  280. }
  281. drawLabel(ctx, viaNode) {
  282. if (this.options.label !== undefined) {
  283. // set style
  284. var node1 = this.from;
  285. var node2 = this.to;
  286. var selected = (this.from.selected || this.to.selected || this.selected);
  287. if (node1.id != node2.id) {
  288. var point = this.edgeType.getPoint(0.5, viaNode);
  289. ctx.save();
  290. // if the label has to be rotated:
  291. if (this.options.font.align !== "horizontal") {
  292. this.labelModule.calculateLabelSize(ctx,selected,point.x,point.y);
  293. ctx.translate(point.x, this.labelModule.size.yLine);
  294. this._rotateForLabelAlignment(ctx);
  295. }
  296. // draw the label
  297. this.labelModule.draw(ctx, point.x, point.y, selected);
  298. ctx.restore();
  299. }
  300. else {
  301. var x, y;
  302. var radius = this.options.selfReferenceSize;
  303. if (node1.width > node1.height) {
  304. x = node1.x + node1.width * 0.5;
  305. y = node1.y - radius;
  306. }
  307. else {
  308. x = node1.x + radius;
  309. y = node1.y - node1.height * 0.5;
  310. }
  311. point = this._pointOnCircle(x, y, radius, 0.125);
  312. this.labelModule.draw(ctx, point.x, point.y, selected);
  313. }
  314. }
  315. }
  316. /**
  317. * Check if this object is overlapping with the provided object
  318. * @param {Object} obj an object with parameters left, top
  319. * @return {boolean} True if location is located on the edge
  320. */
  321. isOverlappingWith(obj) {
  322. if (this.connected) {
  323. var distMax = 10;
  324. var xFrom = this.from.x;
  325. var yFrom = this.from.y;
  326. var xTo = this.to.x;
  327. var yTo = this.to.y;
  328. var xObj = obj.left;
  329. var yObj = obj.top;
  330. var dist = this.edgeType.getDistanceToEdge(xFrom, yFrom, xTo, yTo, xObj, yObj);
  331. return (dist < distMax);
  332. }
  333. else {
  334. return false
  335. }
  336. }
  337. /**
  338. * Rotates the canvas so the text is most readable
  339. * @param {CanvasRenderingContext2D} ctx
  340. * @private
  341. */
  342. _rotateForLabelAlignment(ctx) {
  343. var dy = this.from.y - this.to.y;
  344. var dx = this.from.x - this.to.x;
  345. var angleInDegrees = Math.atan2(dy, dx);
  346. // rotate so label it is readable
  347. if ((angleInDegrees < -1 && dx < 0) || (angleInDegrees > 0 && dx < 0)) {
  348. angleInDegrees = angleInDegrees + Math.PI;
  349. }
  350. ctx.rotate(angleInDegrees);
  351. }
  352. /**
  353. * Get a point on a circle
  354. * @param {Number} x
  355. * @param {Number} y
  356. * @param {Number} radius
  357. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  358. * @return {Object} point
  359. * @private
  360. */
  361. _pointOnCircle(x, y, radius, percentage) {
  362. var angle = percentage * 2 * Math.PI;
  363. return {
  364. x: x + radius * Math.cos(angle),
  365. y: y - radius * Math.sin(angle)
  366. }
  367. }
  368. select() {
  369. this.selected = true;
  370. }
  371. unselect() {
  372. this.selected = false;
  373. }
  374. }
  375. export default Edge;