From cb87892c8323df2da05bab0d9565933e8f147f6f Mon Sep 17 00:00:00 2001 From: Ravindra Gupta Date: Sun, 10 Sep 2017 00:03:33 +0530 Subject: [PATCH] Added new Hexagon shape in the Network (#3420) * added hexagon shape to the node. * Updated function for hexagon shape.js * Modifid the shape login for hexagon * Remove this.translate from the Shape.js for hexagon * updated hexagon draw logic * Fixed code review comments and update the branch * Updated changes in test.js --- docs/network/nodes.html | 6 +-- lib/network/modules/components/Node.js | 4 ++ .../components/nodes/shapes/Hexagon.js | 44 +++++++++++++++++++ lib/network/options.js | 4 +- lib/network/shapes.js | 17 +++++++ test/Network.test.js | 10 ++++- 6 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 lib/network/modules/components/nodes/shapes/Hexagon.js diff --git a/docs/network/nodes.html b/docs/network/nodes.html index 0a68ff559..ca2d93976 100644 --- a/docs/network/nodes.html +++ b/docs/network/nodes.html @@ -834,7 +834,7 @@

Options

ellipse, circle, database, box, text. Always scalable are: image, circularImage, diamond, dot, - star, triangle, triangleDown, square and + star, triangle, triangleDown, hexagon, square and icon. Keep in mind that when using scaling, the size option is neglected. @@ -975,7 +975,7 @@

Options

ellipse, circle, database, box, text. The ones with the label outside of it are: image, circularImage, diamond, dot, star, triangle, - triangleDown, square and icon. + triangleDown, hexagon, square and icon. @@ -1031,7 +1031,7 @@

Options

25 The size is used to determine the size of node shapes that do not have the label inside of them. These shapes are: image, circularImage, diamond, dot, - star, triangle, triangleDown, square and + star, triangle, triangleDown, hexagon, square and icon diff --git a/lib/network/modules/components/Node.js b/lib/network/modules/components/Node.js index 26428e0db..19c8013b0 100644 --- a/lib/network/modules/components/Node.js +++ b/lib/network/modules/components/Node.js @@ -12,6 +12,7 @@ var Ellipse = require('./nodes/shapes/Ellipse').default; var Icon = require('./nodes/shapes/Icon').default; var Image = require('./nodes/shapes/Image').default; var Square = require('./nodes/shapes/Square').default; +var Hexagon = require('./nodes/shapes/Hexagon').default; var Star = require('./nodes/shapes/Star').default; var Text = require('./nodes/shapes/Text').default; var Triangle = require('./nodes/shapes/Triangle').default; @@ -368,6 +369,9 @@ class Node { case 'square': this.shape = new Square(this.options, this.body, this.labelModule); break; + case 'hexagon': + this.shape = new Hexagon(this.options, this.body, this.labelModule); + break; case 'star': this.shape = new Star(this.options, this.body, this.labelModule); break; diff --git a/lib/network/modules/components/nodes/shapes/Hexagon.js b/lib/network/modules/components/nodes/shapes/Hexagon.js new file mode 100644 index 000000000..6f4618ffd --- /dev/null +++ b/lib/network/modules/components/nodes/shapes/Hexagon.js @@ -0,0 +1,44 @@ +'use strict'; + +import ShapeBase from '../util/ShapeBase' + +/** + * A Hexagon Node/Cluster shape. + * + * @extends ShapeBase + */ +class Hexagon extends ShapeBase { + /** + * @param {Object} options + * @param {Object} body + * @param {Label} labelModule + */ + constructor(options, body, labelModule) { + super(options, body, labelModule) + } + + /** + * + * @param {CanvasRenderingContext2D} ctx + * @param {number} x width + * @param {number} y height + * @param {boolean} selected + * @param {boolean} hover + * @param {{toArrow: boolean, toArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), toArrowType: *, middleArrow: boolean, middleArrowScale: (number|allOptions.edges.arrows.middle.scaleFactor|{number}|Array), middleArrowType: (allOptions.edges.arrows.middle.type|{string}|string|*), fromArrow: boolean, fromArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), fromArrowType: *, arrowStrikethrough: (*|boolean|allOptions.edges.arrowStrikethrough|{boolean}), color: undefined, inheritsColor: (string|string|string|allOptions.edges.color.inherit|{string, boolean}|Array|*), opacity: *, hidden: *, length: *, shadow: *, shadowColor: *, shadowSize: *, shadowX: *, shadowY: *, dashes: (*|boolean|Array|allOptions.edges.dashes|{boolean, array}), width: *}} values + */ + draw(ctx, x, y, selected, hover, values) { + this._drawShape(ctx, 'hexagon', 4, x, y, selected, hover, values); + } + + /** + * + * @param {CanvasRenderingContext2D} ctx + * @param {number} angle + * @returns {number} + */ + distanceToBorder(ctx, angle) { + return this._distanceToBorder(ctx,angle); + } +} + +export default Hexagon; \ No newline at end of file diff --git a/lib/network/options.js b/lib/network/options.js index 3db878dde..06697f103 100644 --- a/lib/network/options.js +++ b/lib/network/options.js @@ -319,7 +319,7 @@ let allOptions = { y: { number }, __type__: { object, boolean: bool } }, - shape: { string: ['ellipse', 'circle', 'database', 'box', 'text', 'image', 'circularImage', 'diamond', 'dot', 'star', 'triangle', 'triangleDown', 'square', 'icon'] }, + shape: { string: ['ellipse', 'circle', 'database', 'box', 'text', 'image', 'circularImage', 'diamond', 'dot', 'star', 'triangle', 'triangleDown', 'square', 'icon', 'hexagon'] }, shapeProperties: { borderDashes: { boolean: bool, array }, borderRadius: { number }, @@ -466,7 +466,7 @@ let configureOptions = { x: [5, -30, 30, 1], y: [5, -30, 30, 1] }, - shape: ['ellipse', 'box', 'circle', 'database', 'diamond', 'dot', 'square', 'star', 'text', 'triangle', 'triangleDown'], + shape: ['ellipse', 'box', 'circle', 'database', 'diamond', 'dot', 'square', 'star', 'text', 'triangle', 'triangleDown','hexagon'], shapeProperties: { borderDashes: false, borderRadius: [6, 0, 20, 1], diff --git a/lib/network/shapes.js b/lib/network/shapes.js index e1665bba3..424892540 100644 --- a/lib/network/shapes.js +++ b/lib/network/shapes.js @@ -320,5 +320,22 @@ if (typeof CanvasRenderingContext2D !== 'undefined') { } }; + /** + * Draw a Hexagon shape with 6 sides + * @param {Number} x horizontal center + * @param {Number} y vertical center + * @param {Number} r radius + */ + CanvasRenderingContext2D.prototype.hexagon = function (x, y, r) { + this.beginPath(); + var sides = 6; + var a = (Math.PI*2)/sides; + this.moveTo(x+r,y); + for (var i = 1; i < sides; i++) { + this.lineTo(x+r*Math.cos(a*i),y+r*Math.sin(a*i)); + } + this.closePath(); + }; + } diff --git a/test/Network.test.js b/test/Network.test.js index 8a5359b09..43a6d76f7 100644 --- a/test/Network.test.js +++ b/test/Network.test.js @@ -190,7 +190,15 @@ describe('Network', function () { after(function() { - this.jsdom_global(); + try { + this.jsdom_global(); + } catch(e) { + if (e.message() === 'window is undefined') { + console.warning("'" + e.message() + "' happened again"); + } else { + throw e; + } + } });