Skip to content

Commit 235aa10

Browse files
committed
Documentation Improvments
1 parent 75797b9 commit 235aa10

File tree

1 file changed

+78
-77
lines changed

1 file changed

+78
-77
lines changed

Diff for: src/components/graph/graph.coords.js

+78-77
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { logWarning } from "../../utils";
33

44
/**
55
* Computes the normalized vector from a vector.
6-
* @param {Object} vector a 2D vector with x and y components
7-
* @param {number} vector.x x coordinate
8-
* @param {number} vector.y y coordinate
9-
* @returns {Object} normalized vector
6+
*
7+
* @param {Object} vector - A 2D vector with x and y components.
8+
* @param {number} vector.x - The X coordinate.
9+
* @param {number} vector.y - The Y coordinate.
10+
* @returns {Object} Normalized vector.
1011
* @memberof Graph/helper
1112
*/
1213
function normalize(vector) {
@@ -15,13 +16,12 @@ function normalize(vector) {
1516
}
1617

1718
/**
18-
* Calculate the length of a vector, from the center of a rectangle,
19-
* to one of it's edges.
19+
* Calculates the vector length from the center of a rectangle to the closest edge following a direction.
2020
* This calculation is taken from https://stackoverflow.com/a/3197924.
2121
*
22-
* @param {Object.<string, number>} RectangleCoords, The coords of the left-top vertex, and the right-bottom vertex.
23-
* @param {Object.<string, number>} VectorOriginCoords The center of the rectangle coords.
24-
* @param {Object.<string, number>} directionVector a 2D vector with x and y components.
22+
* @param {Object.<string, number>} RectangleCoords - The coords of the left-top vertex, and the right-bottom vertex.
23+
* @param {Object.<string, number>} VectorOriginCoords - The center of the rectangle coords.
24+
* @param {Object.<string, number>} directionVector - A 2D vector with x and y components.
2525
* @returns {number} The length of the vector from the center of the symbol to it's closet edge, considering the given direction vector.
2626
*/
2727
function calcRectangleVectorLengthFromCoords({ x1, y1, x2, y2 }, { px, py }, directionVector) {
@@ -46,10 +46,10 @@ function calcRectangleVectorLengthFromCoords({ x1, y1, x2, y2 }, { px, py }, dir
4646
}
4747

4848
/**
49-
* Calculate a the vector length from the center of a circle to it's perimeter.
49+
* Calculates the radius of the node.
5050
*
51-
* @param {number} nodeSize The size of the circle, when no viewGenerator is specified, else the size of an edge of the viewGenerator square.
52-
* @param {boolean} isCustomNode is viewGenerator specified.
51+
* @param {number} nodeSize - The size of the circle, when no viewGenerator is specified, else the size of an edge of the viewGenerator square.
52+
* @param {boolean} isCustomNode - Is viewGenerator specified.
5353
* @returns {number} The length of the vector from the center of the symbol to it's closet edge, considering the given direction vector.
5454
*/
5555
function calcCircleVectorLength(nodeSize, isCustomNode) {
@@ -58,20 +58,20 @@ function calcCircleVectorLength(nodeSize, isCustomNode) {
5858
// nodeSize equals the Diameter in the case of custome-node.
5959
radiusLength = nodeSize / 10 / 2;
6060
} else {
61-
// because this is a circle and A = pi * r^2
62-
// we multiply by 0.95, because if we don't the link is not melting properly
61+
// because this is a circle and A = pi * r^2.
6362
radiusLength = Math.sqrt(nodeSize / Math.PI);
6463
}
65-
return radiusLength;
64+
// We multiply by 0.95, so that our link will melt properly to its destination component.
65+
return radiusLength * 0.95;
6666
}
6767

6868
/**
69-
* Calculate a the vector length from the center of a square to it's perimeter.
69+
* Calculates the vector length from the center of a square to the closest edge following a direction.
7070
*
71-
* @param {number} nodeSize The size of the square, when no viewGenerator is specified, else the size of an edge of the viewGenerator square.
72-
* @param {Object.<string, number>} nodeCoords The coords of a the square node.
73-
* @param {Object.<string, number>} directionVector a 2D vector with x and y components
74-
* @param {boolean} isCustomNode is viewGenerator specified.
71+
* @param {number} nodeSize - The size of the square, when no viewGenerator is specified, else the size of an edge of the viewGenerator square.
72+
* @param {Object.<string, number>} nodeCoords - The coords of a the square node.
73+
* @param {Object.<string, number>} directionVector - A 2D vector with x and y components.
74+
* @param {boolean} isCustomNode - Is viewGenerator specified.
7575
* @returns {number} The length of the vector from the center of the symbol to it's closet edge, considering the given direction vector.
7676
*/
7777
function calcSquareVectorLength(nodeSize, { x, y }, directionVector, isCustomNode) {
@@ -80,11 +80,11 @@ function calcSquareVectorLength(nodeSize, { x, y }, directionVector, isCustomNod
8080
// nodeSize equals the edgeSize in the case of custome-node.
8181
edgeSize = nodeSize / 10;
8282
} else {
83-
// All the edges of a square are equal, inorder to calc it's size we multplie two edges.
83+
// All the edges of a square are equal, inorder to calc its size we multplie two edges.
8484
edgeSize = Math.sqrt(nodeSize);
8585
}
8686

87-
// The x and y coords in this library, represent the top center of the component.
87+
// The x and y coords represent the top center of the component
8888
const leftSquareX = x - edgeSize / 2;
8989
const topSquareY = y - edgeSize / 2;
9090

@@ -96,11 +96,11 @@ function calcSquareVectorLength(nodeSize, { x, y }, directionVector, isCustomNod
9696
}
9797

9898
/**
99-
* Calculate a the vector length from the center of a rectangle to it's perimeter.
99+
* Calculates the vector length from the center of a rectangle to the closest edge following a direction.
100100
*
101-
* @param {number} nodeSize The size of the square, when no viewGenerator is specified, else the size of an edge of the viewGenerator square.
102-
* @param {Object.<string, number>} nodeCoords The coords of a the square node.
103-
* @param {Object.<string, number>} directionVector a 2D vector with x and y components.
101+
* @param {number} nodeSize - The size of the square, when no viewGenerator is specified, else the size of an edge of the viewGenerator square.
102+
* @param {Object.<string, number>} nodeCoords - The coords of a the square node.
103+
* @param {Object.<string, number>} directionVector - A 2D vector with x and y components.
104104
* @returns {number} The length of the vector from the center of the symbol to it's closet edge, considering the given direction vector.
105105
*/
106106
function calcRectangleVectorLength(nodeSize, { x, y }, directionVector) {
@@ -122,15 +122,15 @@ function calcRectangleVectorLength(nodeSize, { x, y }, directionVector) {
122122
/**
123123
* Calculate a the vector length of symbol that included in symbols with optimized positioning.
124124
*
125-
* @param {string} symbolType the string that specifies the symbol type (should be one of {@link #node-symbol-type|node.symbolType})
126-
* @param {(number | Object.<string, number>)} nodeSize The size of the square, when no viewGenerator is specified, else the size of an edge of the viewGenerator square.
127-
* @param {Object.<string, number>} nodeCoords The coords of a the square node.
128-
* @param {Object.<string, number>} directionVector a 2D vector with x and y components.
129-
* @param {boolean} isCustomNode is viewGenerator specified.
125+
* @param {string} symbolType - The string that specifies the symbol type (should be one of {@link #node-symbol-type|node.symbolType}).
126+
* @param {(number | Object.<string, number>)} nodeSize - The size of the square, when no viewGenerator is specified, else the size of an edge of the viewGenerator square.
127+
* @param {Object.<string, number>} nodeCoords - The coords of a the square node.
128+
* @param {Object.<string, number>} directionVector - A 2D vector with x and y components.
129+
* @param {boolean} isCustomNode - Is viewGenerator specified.
130130
* @returns {number} The length of the vector from the center of the symbol to it's closet edge, considering the given direction vector.
131131
*/
132132
function calcVectorLength(symbolType, nodeSize, { x, y }, directionVector, isCustomNode) {
133-
if (typeof nodeSize === "object" && nodeSize?.width && nodeSize?.height) {
133+
if (isCustomNode && typeof nodeSize === "object" && nodeSize?.width && nodeSize?.height) {
134134
return calcRectangleVectorLength(nodeSize, { x, y }, directionVector);
135135
}
136136

@@ -155,12 +155,12 @@ function calcVectorLength(symbolType, nodeSize, { x, y }, directionVector, isCus
155155

156156
/**
157157
* When directed graph is specified, we add arrow head to the link.
158-
* In order to add the arrow head we subtract it's size from the last point of the link.
158+
* In order to add the arrow head we subtract its size from the last point of the link.
159159
*
160-
* @param {number} p1 x or y, of the link last point.
161-
* @param {number} p2 x or y, of the link ending point.
162-
* @param {number} pDirectionVector The link direction vector in the x or y axis.
163-
* @param {number} arrowSize The size of the arrow head.
160+
* @param {number} p1 - x or y coordinate, of the link last point.
161+
* @param {number} p2 - x or y coordinate, of the link ending point.
162+
* @param {number} pDirectionVector - The link direction vector in the x or y axis.
163+
* @param {number} arrowSize - The size of the arrow head.
164164
* @returns {number} The amount we should add to the x or y coords, in order to free up space for the arrow head.
165165
*/
166166
function directedGraphCoordsOptimization(p1, p2, pDirectionVector, arrowSize) {
@@ -180,15 +180,15 @@ function directedGraphCoordsOptimization(p1, p2, pDirectionVector, arrowSize) {
180180

181181
/**
182182
* When directed graph is specified, we add arrow head to the link.
183-
* In order to add the arrow head we subtract it's size from the last point of the link.
183+
* In order to add the arrow head we subtract its size from the last point of the link.
184184
*
185-
* @param {Object.<string, number>} optimizedTargetCoords The modified coords of the target node.
186-
* @param {Object.<string, number>} prevCoords The coords of a the last point in the link (last link.breakPoints or the sourceCoords).
187-
* @param {Object.<string, number>} directionVector a 2D vector with x and y components.
188-
* @param {number} arrowSize The size of the arrow head.
189-
* @param {Object.<string, number>} targetCoords The initial coords of the target node.
190-
* @param {(number | Object.<string, number>)} targetNodeSize The target node size.
191-
* @param {boolean} isCustomNode is viewGenerator specified.
185+
* @param {Object.<string, number>} optimizedTargetCoords - The modified coords of the target node.
186+
* @param {Object.<string, number>} prevCoords - The coords of a the last point in the link (last link.breakPoints or the sourceCoords).
187+
* @param {Object.<string, number>} directionVector - A 2D vector with x and y components.
188+
* @param {number} arrowSize - The size of the arrow head.
189+
* @param {Object.<string, number>} targetCoords - The initial coords of the target node.
190+
* @param {(number | Object.<string, number>)} targetNodeSize - The target node size.
191+
* @param {boolean} isCustomNode - Is viewGenerator specified.
192192
* @returns {Object.<string, number>} The amount we should add to the x or y coords, in order to free up space for the arrow head.
193193
*/
194194
function directedGraphOptimization(
@@ -200,21 +200,19 @@ function directedGraphOptimization(
200200
targetNodeSize,
201201
isCustomNode
202202
) {
203-
if (isCustomNode) {
204-
// Check if lastLinkCoord overlaps with the target node.
205-
if (typeof targetNodeSize === "object" && targetNodeSize?.width && targetNodeSize?.height) {
206-
const targetNodeWidth = targetNodeSize.width / 10;
207-
const targetNodeHeight = targetNodeSize.height / 10;
203+
// Check if the last link coord overlaps with the target node.
204+
if (isCustomNode && typeof targetNodeSize === "object" && targetNodeSize?.width && targetNodeSize?.height) {
205+
const targetNodeWidth = targetNodeSize.width / 10;
206+
const targetNodeHeight = targetNodeSize.height / 10;
208207

209-
const leftTargetNodeRectangleX = targetCoords.x - targetNodeWidth / 2;
210-
const xOverlaps = leftTargetNodeRectangleX < prevX && prevX < leftTargetNodeRectangleX + targetNodeWidth;
208+
const leftTargetNodeRectangleX = targetCoords.x - targetNodeWidth / 2;
209+
const xOverlaps = leftTargetNodeRectangleX < prevX && prevX < leftTargetNodeRectangleX + targetNodeWidth;
211210

212-
const topTargetNodeRectangleY = targetCoords.y - targetNodeHeight / 2;
213-
const yOverlaps = topTargetNodeRectangleY < prevY && prevY < topTargetNodeRectangleY + targetNodeHeight;
211+
const topTargetNodeRectangleY = targetCoords.y - targetNodeHeight / 2;
212+
const yOverlaps = topTargetNodeRectangleY < prevY && prevY < topTargetNodeRectangleY + targetNodeHeight;
214213

215-
if (xOverlaps && yOverlaps) {
216-
return targetCoords;
217-
}
214+
if (xOverlaps && yOverlaps) {
215+
return targetCoords;
218216
}
219217
}
220218
const optTrgX = directedGraphCoordsOptimization(prevX, trgX, directionVector.x, arrowSize);
@@ -230,15 +228,15 @@ function directedGraphOptimization(
230228
* Computes new node coordinates to make arrowheads point at nodes.
231229
* Arrow configuration is only available for circles, squares and rectangles.
232230
*
233-
* @param {Object} info - the couple of nodes we need to compute new coordinates
234-
* @param {string} info.sourceId - node source id
235-
* @param {string} info.targetId - node target id
236-
* @param {Object} info.sourceCoords - node source coordinates
237-
* @param {Object} info.targetCoords - node target coordinates
238-
* @param {Object.<string, Object>} nodes - same as {@link #graphrenderer|nodes in renderGraph}.
239-
* @param {Object} config - same as {@link #graphrenderer|config in renderGraph}.
240-
* @param {number} strokeWidth - width of the link stroke
241-
* @param {Array.<Object>} breakPoints - additional set of points that the link will cross
231+
* @param {Object} info - The couple of nodes we need to compute new coordinates.
232+
* @param {string} info.sourceId - Node source id.
233+
* @param {string} info.targetId - Node target id.
234+
* @param {Object} info.sourceCoords - Node source coordinates.
235+
* @param {Object} info.targetCoords - Node target coordinates.
236+
* @param {Object.<string, Object>} nodes - Same as {@link #graphrenderer|nodes in renderGraph}.
237+
* @param {Object} config - Same as {@link #graphrenderer|config in renderGraph}.
238+
* @param {number} strokeWidth - Width of the link stroke.
239+
* @param {Array.<Object>} breakPoints - Additional set of points that the link will cross.
242240
* @returns {Object} new nodes coordinates
243241
* @memberof Graph/helper
244242
*/
@@ -286,9 +284,13 @@ function getNormalizedNodeCoordinates(
286284
const isSourceCustomNode = sourceNode?.viewGenerator || config.node?.viewGenerator;
287285
const sourceNodeSize = sourceNode?.size || config.node?.size;
288286

289-
const sourceVectorLength =
290-
calcVectorLength(sourceSymbolType, sourceNodeSize, { x: srcX, y: srcY }, firstDirectionVector, isSourceCustomNode) *
291-
0.95;
287+
const sourceVectorLength = calcVectorLength(
288+
sourceSymbolType,
289+
sourceNodeSize,
290+
{ x: srcX, y: srcY },
291+
firstDirectionVector,
292+
isSourceCustomNode
293+
);
292294

293295
srcX += sourceVectorLength * firstDirectionVector.x;
294296
srcY += sourceVectorLength * firstDirectionVector.y;
@@ -302,14 +304,13 @@ function getNormalizedNodeCoordinates(
302304
const isTargetCustomNode = targetNode?.viewGenerator || config.node?.viewGenerator;
303305
const targetNodeSize = targetNode?.size || config.node?.size;
304306

305-
const targetVectorLength =
306-
calcVectorLength(
307-
targetSymbolType,
308-
targetNodeSize,
309-
{ x: trgX, y: trgY },
310-
secondDirectionVector,
311-
isTargetCustomNode
312-
) * 0.95;
307+
const targetVectorLength = calcVectorLength(
308+
targetSymbolType,
309+
targetNodeSize,
310+
{ x: trgX, y: trgY },
311+
secondDirectionVector,
312+
isTargetCustomNode
313+
);
313314

314315
const arrowSize = config.directed ? strokeSize : 0;
315316
trgX -= targetVectorLength * secondDirectionVector.x;

0 commit comments

Comments
 (0)