apextree 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,7 +4,6 @@ The Apextree is a javascript library built on SVG that helps to create organizat
4
4
 
5
5
  <img width="811" alt="Screenshot 2023-12-17 at 10 28 04 PM" src="https://github.com/apexcharts/tree/assets/17950663/e09212ec-6322-4c68-ac12-9afc524d2abd">
6
6
 
7
-
8
7
  ## Installation
9
8
 
10
9
  To add the Apextree to your project and its dependencies, install the package from npm.
@@ -16,7 +15,7 @@ npm install apextree
16
15
  ## Usage
17
16
 
18
17
  ```js
19
- import ApexTree from 'apextree'
18
+ import ApexTree from 'apextree';
20
19
  ```
21
20
 
22
21
  To create a basic tree with minimal configuration, write as follows:
@@ -47,43 +46,48 @@ To create a basic tree with minimal configuration, write as follows:
47
46
 
48
47
  The layout can be configured by either setting the properties in the table below by passing a second arg to Apextree with these properties set. The latter takes precedence.
49
48
 
50
- Options | Default | Description
51
- --- |------------------------------| ---
52
- width | 400 | The width of graph container
53
- height | 400 | The height of graph container
54
- direction | top | The direction of the tree to start rendering. Possible values: `top`, `bottom`, `left` and `right`
55
- contentKey | name | The key of content in passed data object
56
- siblingSpacing | 50 | The spacing between sibling nodes
57
- childrenSpacing | 50 | The spacing between children and parent
58
- highlightOnHover | true | Enable/disable highlight on hover
59
- containerClassName | root | The class name for the root container
60
- canvasStyle | None | The css styles for canvas root container
61
- enableToolbar | false | Enable/disable graph toolbar
62
- nodeWidth | 50 | The width of graph nodes
63
- nodeHeight | 30 | The height of graph nodes
64
- nodeTemplate | defaultNodeTemplate | The HTML template for nodes
65
- nodeBGColor | `#FFFFFF` | The background color of nodes
66
- nodeBGColorHover | `#FFFFFF` | The background color on hover of nodes
67
- borderWidth | 1 | The border width of the nodes in pixels
68
- borderStyle | solid | The border style of the nodes
69
- borderRadius | 5px | The border radius of the nodes in pixels
70
- borderColor | `#BCBCBC` | The border color of the nodes
71
- borderColorHover | `#5C6BC0` | The border color on hover of the nodes
72
- edgeWidth | `1` | The width for the edges
73
- edgeColor | `#BCBCBC` | The color for the edges
74
- edgeColorHover | `#BCBCBC` | The color for the edges when highlighted
75
- enableTooltip | false | Enable tooltip on hover of nodes
76
- groupLeafNodes | false | For stacking leaf nodes
77
- groupLeafNodesSpacing | 10 | Spacing between stacked leaf nodes
78
- tooltipId | `apextree-tooltip-container` | The tooltip HTML element id
79
- tooltipTemplate | defaultNodeTemplate | The HTML template for tooltip
80
- tooltipMaxWidth | 100 | The max width of the tooltip
81
- tooltipBorderColor | `#BCBCBC` | The border color of tooltip
82
- tooltipBGColor | `#FFFFFF` | The background color of tooltip
83
- fontSize | 14px | The size of font of nodes
84
- fontFamily | None | The font family of nodes
85
- fontWeight | 400 | The font weight of nodes
86
- fontColor | `#000000` | The font color of nodes
49
+ | Options | Default | Description |
50
+ | --- | --- | --- |
51
+ | width | 400 | The width of graph container |
52
+ | height | 400 | The height of graph container |
53
+ | direction | top | The direction of the tree to start rendering. Possible values: `top`, `bottom`, `left` and `right` |
54
+ | contentKey | name | The key of content in passed data object |
55
+ | siblingSpacing | 50 | The spacing between sibling nodes |
56
+ | childrenSpacing | 50 | The spacing between children and parent |
57
+ | highlightOnHover | true | Enable/disable highlight on hover |
58
+ | containerClassName | root | The class name for the root container |
59
+ | canvasStyle | None | The css styles for canvas root container |
60
+ | enableToolbar | false | Enable/disable graph toolbar |
61
+ | nodeWidth | 50 | The width of graph nodes |
62
+ | nodeHeight | 30 | The height of graph nodes |
63
+ | nodeTemplate | defaultNodeTemplate | The HTML template for nodes |
64
+ | nodeBGColor | `#FFFFFF` | The background color of nodes |
65
+ | nodeBGColorHover | `#FFFFFF` | The background color on hover of nodes |
66
+ | borderWidth | 1 | The border width of the nodes in pixels |
67
+ | borderStyle | solid | The border style of the nodes |
68
+ | borderRadius | 5px | The border radius of the nodes in pixels |
69
+ | borderColor | `#BCBCBC` | The border color of the nodes |
70
+ | borderColorHover | `#5C6BC0` | The border color on hover of the nodes |
71
+ | edgeWidth | `1` | The width for the edges |
72
+ | edgeColor | `#BCBCBC` | The color for the edges |
73
+ | edgeColorHover | `#5C6BC0` | The color for the edges when highlighted |
74
+ | enableTooltip | false | Enable tooltip on hover of nodes |
75
+ | groupLeafNodes | false | For stacking leaf nodes |
76
+ | groupLeafNodesSpacing | 10 | Spacing between stacked leaf nodes |
77
+ | tooltipId | `apextree-tooltip-container` | The tooltip HTML element id |
78
+ | tooltipTemplate | defaultNodeTemplate | The HTML template for tooltip |
79
+ | tooltipMaxWidth | 300 | The max width of the tooltip in pixels |
80
+ | tooltipMinWidth | 100 | The min width of the tooltip in pixels |
81
+ | tooltipBorderColor | `#BCBCBC` | The border color of tooltip |
82
+ | tooltipBGColor | `#FFFFFF` | The background color of tooltip |
83
+ | tooltipFontColor | `#000000` | The font color of tooltip text |
84
+ | tooltipFontSize | `12px` | The font size of tooltip text |
85
+ | tooltipPadding | 8 | The padding of tooltip in pixels (0 if custom tooltipTemplate is used) |
86
+ | tooltipOffset | 10 | The offset distance between tooltip and cursor in pixels |
87
+ | fontSize | 14px | The size of font of nodes |
88
+ | fontFamily | None | The font family of nodes |
89
+ | fontWeight | 400 | The font weight of nodes |
90
+ | fontColor | `#000000` | The font color of nodes |
87
91
 
88
92
  Default node template
89
93
 
@@ -92,43 +96,46 @@ const defaultNodeTemplate = (content: string) => {
92
96
  return `<div style='display: flex;justify-content: center;align-items: center; text-align: center; height: 100%;'>${content}</div>`;
93
97
  };
94
98
  ```
99
+
95
100
  ### Expected data format
96
101
 
97
102
  ```json
98
103
  {
99
- "id": "1",
104
+ "id": "1",
100
105
  "name": "A",
101
106
  "children": []
102
107
  }
103
108
  ```
109
+
104
110
  Passed data object should contain id, name and children.
105
111
 
106
- For *id* key, value of id can be unique otherwise edge highlight won't work as expected.
112
+ For _id_ key, value of id can be unique otherwise edge highlight won't work as expected.
107
113
 
108
- For *name* key, if using other than *name* then specify key name in contentKey option
114
+ For _name_ key, if using other than _name_ then specify key name in contentKey option
109
115
 
110
- For *children* key, it contains list of child objects
116
+ For _children_ key, it contains list of child objects
111
117
 
112
118
  **Example**
119
+
113
120
  ```js
114
121
  const data = {
115
- "id": "1",
116
- "name": "A",
117
- "children": [
118
- {
119
- "id": "2",
120
- "name": "B",
121
- "children": [
122
- {
123
- "id": "3",
124
- "name": "C"
125
- },
126
- {
127
- "id": "4",
128
- "name": "D"
129
- }
130
- ]
131
- }
132
- ]
133
- }
122
+ id: '1',
123
+ name: 'A',
124
+ children: [
125
+ {
126
+ id: '2',
127
+ name: 'B',
128
+ children: [
129
+ {
130
+ id: '3',
131
+ name: 'C',
132
+ },
133
+ {
134
+ id: '4',
135
+ name: 'D',
136
+ },
137
+ ],
138
+ },
139
+ ],
140
+ };
134
141
  ```
@@ -6613,21 +6613,23 @@ const getTooltipStyles = ({
6613
6613
  fontFamily,
6614
6614
  fontSize,
6615
6615
  fontWeight,
6616
- maxWidth = 300,
6616
+ maxWidth,
6617
6617
  padding,
6618
6618
  placement = "top",
6619
6619
  x: x2,
6620
6620
  y: y2
6621
6621
  }) => {
6622
6622
  const styles = ["position: absolute;", "z-index: 1000;", "pointer-events: none;"];
6623
- styles.push(`width: ${maxWidth}px;`);
6624
- styles.push(`max-width: ${maxWidth}px;`);
6625
6623
  styles.push("box-sizing: border-box;");
6626
6624
  styles.push("word-wrap: break-word;");
6627
6625
  styles.push("overflow-wrap: break-word;");
6628
6626
  styles.push(`left: ${x2}px;`);
6629
6627
  styles.push(`top: ${y2}px;`);
6630
6628
  styles.push(`data-placement: ${placement};`);
6629
+ if (maxWidth) {
6630
+ styles.push(`width: ${maxWidth}px;`);
6631
+ styles.push(`max-width: ${maxWidth}px;`);
6632
+ }
6631
6633
  if (borderColor) {
6632
6634
  styles.push(`border: 1px solid ${borderColor};`);
6633
6635
  }
@@ -9992,8 +9994,13 @@ const DefaultOptions = {
9992
9994
  siblingSpacing: 50,
9993
9995
  tooltipBGColor: "#FFFFFF",
9994
9996
  tooltipBorderColor: "#BCBCBC",
9997
+ tooltipFontColor: "#000000",
9998
+ tooltipFontSize: "12px",
9995
9999
  tooltipId: "apextree-tooltip-container",
9996
- tooltipMaxWidth: 100,
10000
+ tooltipMaxWidth: void 0,
10001
+ tooltipMinWidth: 100,
10002
+ tooltipPadding: 8,
10003
+ tooltipOffset: 10,
9997
10004
  viewPortHeight: 600,
9998
10005
  viewPortWidth: 800,
9999
10006
  width: "100%"
@@ -10147,6 +10154,77 @@ function findParentsWithOnlyLeafNodes(tree) {
10147
10154
  dfs2(tree);
10148
10155
  return result;
10149
10156
  }
10157
+ function calculateTooltipPosition(mouseX, mouseY, tooltipWidth, tooltipHeight, offset) {
10158
+ const viewportWidth = window.innerWidth;
10159
+ const viewportHeight = window.innerHeight;
10160
+ const scrollX = window.scrollX || document.documentElement.scrollLeft;
10161
+ const scrollY = window.scrollY || document.documentElement.scrollTop;
10162
+ const padding = 10;
10163
+ let placement = "top";
10164
+ let x2 = mouseX;
10165
+ let y2 = mouseY - tooltipHeight - offset;
10166
+ const viewportY = mouseY - scrollY;
10167
+ if (viewportY - tooltipHeight - offset < padding) {
10168
+ placement = "bottom";
10169
+ y2 = mouseY + offset;
10170
+ if (viewportY + offset + tooltipHeight > viewportHeight - padding) {
10171
+ placement = "top";
10172
+ y2 = scrollY + padding;
10173
+ }
10174
+ }
10175
+ const viewportX = mouseX - scrollX;
10176
+ if (viewportX + tooltipWidth > viewportWidth - padding) {
10177
+ x2 = scrollX + viewportWidth - tooltipWidth - padding;
10178
+ }
10179
+ if (viewportX < padding) {
10180
+ x2 = scrollX + padding;
10181
+ }
10182
+ if (placement === "top" || placement === "bottom") {
10183
+ const centeredX = mouseX - tooltipWidth / 2;
10184
+ const centeredViewportX = centeredX - scrollX;
10185
+ if (centeredViewportX >= padding && centeredViewportX + tooltipWidth <= viewportWidth - padding) {
10186
+ x2 = centeredX;
10187
+ }
10188
+ }
10189
+ return { placement, x: x2, y: y2 };
10190
+ }
10191
+ function showTooltip(chartContext, config, content, mouseX, mouseY, containerElement) {
10192
+ const { bgColor, borderColor, fontColor, fontSize, fontFamily, maxWidth, minWidth, offset, padding, tooltipId } = config;
10193
+ const tempDiv = document.createElement("div");
10194
+ tempDiv.style.position = "absolute";
10195
+ tempDiv.style.visibility = "hidden";
10196
+ tempDiv.style.width = `${maxWidth}px`;
10197
+ tempDiv.style.minWidth = `${minWidth}px`;
10198
+ tempDiv.style.padding = `${padding}px`;
10199
+ tempDiv.style.fontSize = fontSize;
10200
+ tempDiv.style.fontFamily = fontFamily;
10201
+ tempDiv.style.wordWrap = "break-word";
10202
+ tempDiv.style.whiteSpace = "normal";
10203
+ tempDiv.innerHTML = content;
10204
+ document.body.appendChild(tempDiv);
10205
+ const tooltipHeight = tempDiv.offsetHeight;
10206
+ const tooltipWidth = tempDiv.offsetWidth;
10207
+ document.body.removeChild(tempDiv);
10208
+ const position2 = calculateTooltipPosition(mouseX, mouseY, tooltipWidth, tooltipHeight, offset);
10209
+ const styles = getTooltipStyles({
10210
+ bgColor,
10211
+ borderColor,
10212
+ fontColor,
10213
+ fontFamily,
10214
+ fontSize,
10215
+ fontWeight: "400",
10216
+ maxWidth,
10217
+ padding,
10218
+ placement: position2.placement,
10219
+ x: position2.x,
10220
+ y: position2.y
10221
+ });
10222
+ const enhancedStyles = `${styles} min-width: ${minWidth}px; word-wrap: break-word; white-space: normal; overflow-wrap: break-word; line-height: 1.4;`;
10223
+ updateTooltip(chartContext, tooltipId, enhancedStyles, content);
10224
+ }
10225
+ function hideTooltip(chartContext, tooltipId) {
10226
+ updateTooltip(chartContext, tooltipId);
10227
+ }
10150
10228
  const methods$1 = {};
10151
10229
  const names = [];
10152
10230
  function registerMethods(name, m) {
@@ -15916,6 +15994,7 @@ class Paper {
15916
15994
  this.chartContext = chartContext;
15917
15995
  this.width = width2;
15918
15996
  this.height = height2;
15997
+ this.containerElement = element;
15919
15998
  element.innerHTML = "";
15920
15999
  this.canvas = SVG().addTo(element).size("100%", "100%").viewbox(`0 0 ${width2} ${height2}`).panZoom({ zoomFactor: 0.2, zoomMin: 0.1 }).attr({ style: canvasStyle });
15921
16000
  }
@@ -16002,6 +16081,9 @@ class Paper {
16002
16081
  this.canvas.zoom(newZoomVal);
16003
16082
  }
16004
16083
  }
16084
+ getContainerElement() {
16085
+ return this.containerElement;
16086
+ }
16005
16087
  }
16006
16088
  const graphOptions = {
16007
16089
  marginx: 100,
@@ -16216,7 +16298,6 @@ class Graph2 extends Paper {
16216
16298
  const node = this.nodeMap[nodeId];
16217
16299
  const { options } = this;
16218
16300
  const {
16219
- borderRadius,
16220
16301
  contentKey,
16221
16302
  direction,
16222
16303
  enableTooltip,
@@ -16228,6 +16309,7 @@ class Graph2 extends Paper {
16228
16309
  const {
16229
16310
  borderColor,
16230
16311
  borderStyle,
16312
+ borderRadius,
16231
16313
  borderWidth,
16232
16314
  fontColor,
16233
16315
  fontFamily,
@@ -16240,8 +16322,13 @@ class Graph2 extends Paper {
16240
16322
  nodeWidth,
16241
16323
  tooltipBGColor,
16242
16324
  tooltipBorderColor,
16325
+ tooltipFontColor,
16326
+ tooltipFontSize,
16243
16327
  tooltipId,
16244
- tooltipMaxWidth
16328
+ tooltipMaxWidth,
16329
+ tooltipMinWidth,
16330
+ tooltipOffset,
16331
+ tooltipPadding
16245
16332
  } = { ...options, ...node.options };
16246
16333
  const x2 = DirectionConfig[direction].leafX({
16247
16334
  childrenSpacing: groupLeafNodesSpacing,
@@ -16293,19 +16380,30 @@ class Graph2 extends Paper {
16293
16380
  if (enableTooltip) {
16294
16381
  const tooltipContent = tooltipTemplate ? tooltipTemplate(node[contentKey]) : nodeContent;
16295
16382
  group.on("mousemove", (e) => {
16296
- const styles2 = getTooltipStyles({
16297
- bgColor: tooltipBGColor,
16298
- borderColor: tooltipBorderColor,
16299
- maxWidth: tooltipMaxWidth,
16300
- padding: !tooltipTemplate ? 10 : 0,
16301
- x: e.pageX,
16302
- y: e.pageY
16303
- });
16304
- updateTooltip(graphInstance.chartContext, tooltipId, styles2, tooltipContent);
16383
+ showTooltip(
16384
+ graphInstance.chartContext,
16385
+ {
16386
+ bgColor: tooltipBGColor,
16387
+ borderColor: tooltipBorderColor,
16388
+ fontColor: tooltipFontColor,
16389
+ fontSize: tooltipFontSize,
16390
+ fontFamily,
16391
+ maxWidth: tooltipMaxWidth,
16392
+ minWidth: tooltipMinWidth,
16393
+ offset: tooltipOffset,
16394
+ padding: tooltipTemplate ? 0 : tooltipPadding,
16395
+ tooltipId
16396
+ },
16397
+ tooltipContent,
16398
+ e.pageX,
16399
+ e.pageY,
16400
+ graphInstance.getContainerElement()
16401
+ );
16305
16402
  });
16306
16403
  group.on("mouseout", (e) => {
16307
- if (e.relatedTarget.tagName === "svg") {
16308
- updateTooltip(graphInstance.chartContext, tooltipId);
16404
+ var _a;
16405
+ if (((_a = e.relatedTarget) == null ? void 0 : _a.tagName) === "svg") {
16406
+ hideTooltip(graphInstance.chartContext, tooltipId);
16309
16407
  }
16310
16408
  });
16311
16409
  }
@@ -16346,7 +16444,12 @@ class Graph2 extends Paper {
16346
16444
  tooltipBGColor,
16347
16445
  tooltipBorderColor,
16348
16446
  tooltipId,
16349
- tooltipMaxWidth
16447
+ tooltipFontColor,
16448
+ tooltipFontSize,
16449
+ tooltipMaxWidth,
16450
+ tooltipMinWidth,
16451
+ tooltipOffset,
16452
+ tooltipPadding
16350
16453
  } = { ...options, ...node.options };
16351
16454
  const { height: height2, width: width2, x: x2, y: y2 } = node;
16352
16455
  const graphInstance = this;
@@ -16392,19 +16495,30 @@ class Graph2 extends Paper {
16392
16495
  if (enableTooltip) {
16393
16496
  const tooltipContent = tooltipTemplate ? tooltipTemplate(node[contentKey]) : nodeContent;
16394
16497
  group.on("mousemove", (e) => {
16395
- const styles = getTooltipStyles({
16396
- bgColor: tooltipBGColor,
16397
- borderColor: tooltipBorderColor,
16398
- maxWidth: tooltipMaxWidth,
16399
- padding: !tooltipTemplate ? 10 : 0,
16400
- x: e.pageX,
16401
- y: e.pageY
16402
- });
16403
- updateTooltip(graphInstance.chartContext, tooltipId, styles, tooltipContent);
16498
+ showTooltip(
16499
+ graphInstance.chartContext,
16500
+ {
16501
+ bgColor: tooltipBGColor,
16502
+ borderColor: tooltipBorderColor,
16503
+ fontColor: tooltipFontColor,
16504
+ fontSize: tooltipFontSize,
16505
+ fontFamily,
16506
+ maxWidth: tooltipMaxWidth,
16507
+ minWidth: tooltipMinWidth,
16508
+ offset: tooltipOffset,
16509
+ padding: tooltipTemplate ? 0 : tooltipPadding,
16510
+ tooltipId
16511
+ },
16512
+ tooltipContent,
16513
+ e.pageX,
16514
+ e.pageY,
16515
+ graphInstance.getContainerElement()
16516
+ );
16404
16517
  });
16405
16518
  group.on("mouseout", (e) => {
16406
- if (e.relatedTarget.tagName === "svg") {
16407
- updateTooltip(graphInstance.chartContext, tooltipId);
16519
+ var _a2;
16520
+ if (((_a2 = e.relatedTarget) == null ? void 0 : _a2.tagName) === "svg") {
16521
+ hideTooltip(graphInstance.chartContext, tooltipId);
16408
16522
  }
16409
16523
  });
16410
16524
  }