@webviz/group-tree-plot 1.3.23 → 1.4.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.
Files changed (37) hide show
  1. package/dist/GroupTreePlot.d.ts +6 -1
  2. package/dist/GroupTreePlot.js +43 -43
  3. package/dist/GroupTreePlot.js.map +1 -1
  4. package/dist/components/PlotErrorOverlay.d.ts +5 -0
  5. package/dist/components/PlotErrorOverlay.js +7 -0
  6. package/dist/components/PlotErrorOverlay.js.map +1 -0
  7. package/dist/components/TreePlotRenderer/TreePlotRenderer.d.ts +11 -0
  8. package/dist/components/TreePlotRenderer/TreePlotRenderer.js +85 -0
  9. package/dist/components/TreePlotRenderer/TreePlotRenderer.js.map +1 -0
  10. package/dist/components/TreePlotRenderer/index.d.ts +1 -0
  11. package/dist/components/TreePlotRenderer/index.js +2 -0
  12. package/dist/components/TreePlotRenderer/index.js.map +1 -0
  13. package/dist/components/TreePlotRenderer/privateComponents/HiddenChildren.d.ts +6 -0
  14. package/dist/components/TreePlotRenderer/privateComponents/HiddenChildren.js +13 -0
  15. package/dist/components/TreePlotRenderer/privateComponents/HiddenChildren.js.map +1 -0
  16. package/dist/components/TreePlotRenderer/privateComponents/TransitionTreeEdge.d.ts +10 -0
  17. package/dist/components/TreePlotRenderer/privateComponents/TransitionTreeEdge.js +30 -0
  18. package/dist/components/TreePlotRenderer/privateComponents/TransitionTreeEdge.js.map +1 -0
  19. package/dist/components/TreePlotRenderer/privateComponents/TransitionTreeNode.d.ts +11 -0
  20. package/dist/components/TreePlotRenderer/privateComponents/TransitionTreeNode.js +37 -0
  21. package/dist/components/TreePlotRenderer/privateComponents/TransitionTreeNode.js.map +1 -0
  22. package/dist/hooks/useCollapseMotionProps.d.ts +15 -0
  23. package/dist/hooks/useCollapseMotionProps.js +46 -0
  24. package/dist/hooks/useCollapseMotionProps.js.map +1 -0
  25. package/dist/types.d.ts +3 -0
  26. package/dist/types.js.map +1 -1
  27. package/dist/utils/DataAssembler.d.ts +74 -0
  28. package/dist/utils/DataAssembler.js +179 -0
  29. package/dist/utils/DataAssembler.js.map +1 -0
  30. package/dist/utils/treePlot.d.ts +36 -0
  31. package/dist/utils/treePlot.js +85 -0
  32. package/dist/utils/treePlot.js.map +1 -0
  33. package/package.json +3 -2
  34. package/dist/GroupTreeAssembler/groupTreeAssembler.d.ts +0 -64
  35. package/dist/GroupTreeAssembler/groupTreeAssembler.js +0 -579
  36. package/dist/GroupTreeAssembler/groupTreeAssembler.js.map +0 -1
  37. /package/dist/{GroupTreeAssembler/group_tree.css → group_tree.css} +0 -0
@@ -0,0 +1,179 @@
1
+ import * as d3 from "d3";
2
+ import _ from "lodash";
3
+ import { printTreeValue } from "./treePlot";
4
+ import React from "react";
5
+ /**
6
+ * Utility class to assemble and combine data for one or more dated flow network trees.
7
+ * The instance allows setting an active date, and provides methods to get data and labels pertaining to the given date
8
+ */
9
+ export class DataAssembler {
10
+ /**
11
+ * @param datedTrees A list of dated tree objects. List cannot be empty
12
+ * @param edgeMetadataList A list of labels and units to use when representing values from tree edges
13
+ * @param nodeMetadataList A list of labels and units to use when representing values from tree nodes
14
+ * @throws Throws if dated tree list is empty
15
+ */
16
+ constructor(datedTrees, edgeMetadataList, nodeMetadataList) {
17
+ this._currentTreeIndex = 0;
18
+ this._currentDateIndex = 0;
19
+ // Guard - Require at least one tree to be given.
20
+ if (!datedTrees.length)
21
+ throw new Error("Tree-list is empty");
22
+ this.datedTrees = datedTrees;
23
+ this.edgeMetadataList = edgeMetadataList;
24
+ this.nodeMetadataList = nodeMetadataList;
25
+ this._propertyToLabelMap = new Map();
26
+ [...edgeMetadataList, ...nodeMetadataList].forEach((elm) => {
27
+ this._propertyToLabelMap.set(elm.key, [elm.label, elm.unit]);
28
+ });
29
+ this._propertyMaxVals = new Map();
30
+ this.datedTrees.forEach(({ tree }) => {
31
+ // Utilizing d3 to iterate over each child node from our tree-root
32
+ d3.hierarchy(tree, (d) => d.children).each((node) => {
33
+ const { edge_data } = node.data;
34
+ Object.entries(edge_data).forEach(([key, vals]) => {
35
+ var _a;
36
+ const existingMax = (_a = this._propertyMaxVals.get(key)) !== null && _a !== void 0 ? _a : Number.MIN_VALUE;
37
+ const newMax = Math.max(...vals, existingMax);
38
+ this._propertyMaxVals.set(key, newMax);
39
+ });
40
+ });
41
+ });
42
+ }
43
+ /**
44
+ * Changes the active date for the instance. Raises if date is invalid
45
+ * @param newDate A date string, formatted as YYYY-MM-DD (eg. 2001-01-30)
46
+ * @throws Will throw if the date is invalid, or if no tree uses the given date
47
+ */
48
+ setActiveDate(newDate) {
49
+ const [newTreeIdx, newDateIdx] = findTreeAndDateIndex(newDate, this.datedTrees);
50
+ // I do think these will always both be -1, or not -1, so checking both might be excessive
51
+ if (newTreeIdx === -1 || newDateIdx === -1) {
52
+ throw new Error("Invalid date for data assembler");
53
+ }
54
+ this._currentTreeIndex = newTreeIdx;
55
+ this._currentDateIndex = newDateIdx;
56
+ }
57
+ /**
58
+ * Gets the currently active tree
59
+ * @returns A dated tree
60
+ */
61
+ getActiveTree() {
62
+ return this.datedTrees[this._currentTreeIndex];
63
+ }
64
+ /**
65
+ * Pretty-prints all values in the given object with labels and units added
66
+ * @param data Data from a node or edge
67
+ * @returns A formatted string presentation of the data value
68
+ */
69
+ getTooltip(data) {
70
+ if (this._currentDateIndex === -1)
71
+ return "";
72
+ let text = "";
73
+ for (const propName in data) {
74
+ const [label, unit] = this.getPropertyInfo(propName);
75
+ const value = this.getPropertyValue(data, propName);
76
+ const valueString = printTreeValue(value);
77
+ text += `${label}: ${valueString} ${unit}\n`;
78
+ }
79
+ return text.trimEnd();
80
+ }
81
+ /**
82
+ * Gets a property's value at the currently active date
83
+ * @param data Data from a node or edge
84
+ * @param property A key for a property
85
+ * @returns The value, if found
86
+ */
87
+ getPropertyValue(data, property) {
88
+ var _a;
89
+ if (this._currentDateIndex === -1)
90
+ return null;
91
+ const value = (_a = data[property]) === null || _a === void 0 ? void 0 : _a[this._currentDateIndex];
92
+ return value !== null && value !== void 0 ? value : null;
93
+ }
94
+ /**
95
+ * Gets the label and unit strings for a given property
96
+ * @param propertyKey The key for the property
97
+ * @returns The label and unit for the property. If not found, label defaults to "", and unit defaults to "?"
98
+ */
99
+ getPropertyInfo(propertyKey) {
100
+ var _a;
101
+ const [label, unit] = (_a = this._propertyToLabelMap.get(propertyKey)) !== null && _a !== void 0 ? _a : [];
102
+ const sanitizedLabel = _.upperFirst(label !== null && label !== void 0 ? label : "");
103
+ const sanitizedUnit = unit !== null && unit !== void 0 ? unit : "?";
104
+ return [sanitizedLabel, sanitizedUnit];
105
+ }
106
+ /**
107
+ * Returns a property's value to a normalized value between 2 and 100. A value of 0 will be returned as is.
108
+ * Will also return 0 if no data is found
109
+ * @param data Data object to take data from
110
+ * @param property The property key to take from
111
+ * @returns The normalized value.
112
+ */
113
+ normalizeValue(data, property) {
114
+ var _a;
115
+ const value = (_a = this.getPropertyValue(data, property)) !== null && _a !== void 0 ? _a : 0;
116
+ const maxVal = this._propertyMaxVals.get(property);
117
+ // Don't normalize 0 or invalid properties
118
+ if (!maxVal || value === 0)
119
+ return 0;
120
+ return d3.scaleLinear().domain([0, maxVal]).range([2, 100])(value);
121
+ }
122
+ }
123
+ function findTreeAndDateIndex(targetDate, datedTrees) {
124
+ // Using standard for loop so we can return early when we find a matching tree
125
+ for (let treeIdx = 0; treeIdx < datedTrees.length; treeIdx++) {
126
+ const datedTree = datedTrees[treeIdx];
127
+ const dateIdx = datedTree.dates.findIndex((date) => date === targetDate);
128
+ if (dateIdx !== -1) {
129
+ return [treeIdx, dateIdx];
130
+ }
131
+ }
132
+ // No matching entry found
133
+ return [-1, -1];
134
+ }
135
+ /**
136
+ * Updates the active date in a data-assembler instance.
137
+ * @param dataAssembler An instance of DataAssembler
138
+ * @param targetDate A date string
139
+ * @returns An error message, if the date was invalid
140
+ */
141
+ export function useUpdateAssemblerDate(dataAssembler, targetDate) {
142
+ const [prevDate, setPrevDate] = React.useState(null);
143
+ if (!dataAssembler || targetDate === prevDate)
144
+ return;
145
+ try {
146
+ dataAssembler.setActiveDate(targetDate);
147
+ setPrevDate(targetDate);
148
+ }
149
+ catch (error) {
150
+ return error.message;
151
+ }
152
+ }
153
+ /**
154
+ * Initializes a memoized data-assembler instance. Creates a new instance every time data changes, but returns the last valid instance if the new data is invalid.
155
+ * @param datedTrees A list of dated trees. *Assumes all node labels are unique across all node types*
156
+ * @param edgeMetadataList Dated metadata for each tree edge
157
+ * @param nodeMetadataList Dated metadata for each tree node
158
+ * @returns A data-assembler instance, and an error message (if the instance could not be created)
159
+ */
160
+ export function useDataAssembler(datedTrees, edgeMetadataList, nodeMetadataList) {
161
+ // Store a status message to track if data was valid
162
+ let errorMsg = "";
163
+ // Storing a copy of the last successfully assembled data to render when data becomes invalid
164
+ const lastValidDataAssembler = React.useRef(null);
165
+ const dataAssembler = React.useMemo(() => {
166
+ if (datedTrees.length === 0)
167
+ return null;
168
+ const assembler = new DataAssembler(datedTrees, edgeMetadataList, nodeMetadataList);
169
+ return assembler;
170
+ }, [datedTrees, edgeMetadataList, nodeMetadataList]);
171
+ if (dataAssembler === null) {
172
+ errorMsg = "Invalid data for assembler";
173
+ }
174
+ else if (dataAssembler !== lastValidDataAssembler.current) {
175
+ lastValidDataAssembler.current = dataAssembler;
176
+ }
177
+ return [lastValidDataAssembler.current, errorMsg];
178
+ }
179
+ //# sourceMappingURL=DataAssembler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DataAssembler.js","sourceRoot":"","sources":["../../src/utils/DataAssembler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AASzB,OAAO,CAAC,MAAM,QAAQ,CAAC;AACvB,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;GAGG;AACH,MAAM,OAAO,aAAa;IAWtB;;;;;OAKG;IACH,YACI,UAAuB,EACvB,gBAAgC,EAChC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AAC;IAExE,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE;QACrC,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QAEzC,MAAM,SAAS,GAAG,IAAI,aAAa,CAC/B,UAAU,EACV,gBAAgB,EAChB,gBAAgB,CACnB,CAAC;QAEF,OAAO,SAAS,CAAC;IACrB,CAAC,EAAE,CAAC,UAAU,EAAE,gBAAgB,EAAE,gBAAgB,CAAC,CAAC,CAAC;IAErD,IAAI,aAAa,KAAK,IAAI,EAAE,CAAC;QACzB,QAAQ,GAAG,4BAA4B,CAAC;IAC5C,CAAC;SAAM,IAAI,aAAa,KAAK,sBAAsB,CAAC,OAAO,EAAE,CAAC;QAC1D,sBAAsB,CAAC,OAAO,GAAG,aAAa,CAAC;IACnD,CAAC;IAED,OAAO,CAAC,sBAAsB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AACtD,CAAC"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Utilities for handling the tree nodes generated by d3
3
+ */
4
+ import type { D3TreeEdge, D3TreeNode, RecursiveTreeNode } from "../types";
5
+ import type { HierarchyNode } from "d3";
6
+ /**
7
+ * Computes a unique node identifier that should be unique across the entire tree
8
+ * @param node A node in the tree
9
+ * @returns A unique identifier
10
+ */
11
+ export declare function makeNodeId(node: HierarchyNode<RecursiveTreeNode>): string;
12
+ /**
13
+ * Computes an identifier for a link that should be unique across the entire tree
14
+ * @param link A link in the tree
15
+ * @returns A unique path identifier
16
+ */
17
+ export declare function makeLinkId(link: D3TreeEdge): string;
18
+ /**
19
+ * Formats a value from a node to either N/A, or to a number-string with decimals removed
20
+ * @param value A number or null
21
+ * @returns A formatted string
22
+ */
23
+ export declare function printTreeValue(value: number | null): string;
24
+ /**
25
+ * Given a node in a tree, this method finds that node's closest visible ancestor in another tree-structure.
26
+ * @param targetNode Some tree-node *from the initial tree*.
27
+ * @param newTreeRoot The root *of the new tree structure*
28
+ * @returns A visible node in the new tree that's the closest to the target-node. Defaults to the tree root.
29
+ */
30
+ export declare function findClosestVisibleInNewTree(targetNode: D3TreeNode, newTreeRoot: D3TreeNode): D3TreeNode;
31
+ /**
32
+ * Computes the SVG drawing command for a link in the tree
33
+ * @param link A link in the tree
34
+ * @returns An SVG command string
35
+ */
36
+ export declare function diagonalPath(link: D3TreeEdge): string;
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Utilities for handling the tree nodes generated by d3
3
+ */
4
+ /**
5
+ * Computes a unique node identifier that should be unique across the entire tree
6
+ * @param node A node in the tree
7
+ * @returns A unique identifier
8
+ */
9
+ export function makeNodeId(node) {
10
+ // It's technically possible for a group to have the same name as a well, so it should be enough to simply append the type in front of the label
11
+ const type = node.data.node_type;
12
+ const label = node.data.node_label;
13
+ return `${type}__${label}`;
14
+ }
15
+ /**
16
+ * Computes an identifier for a link that should be unique across the entire tree
17
+ * @param link A link in the tree
18
+ * @returns A unique path identifier
19
+ */
20
+ export function makeLinkId(link) {
21
+ return `path ${makeNodeId(link.target)}`;
22
+ }
23
+ /**
24
+ * Formats a value from a node to either N/A, or to a number-string with decimals removed
25
+ * @param value A number or null
26
+ * @returns A formatted string
27
+ */
28
+ export function printTreeValue(value) {
29
+ if (value === null)
30
+ return "N/A";
31
+ else
32
+ return value.toFixed(0);
33
+ }
34
+ /**
35
+ * Given a node in a tree, this method finds that node's closest visible ancestor in another tree-structure.
36
+ * @param targetNode Some tree-node *from the initial tree*.
37
+ * @param newTreeRoot The root *of the new tree structure*
38
+ * @returns A visible node in the new tree that's the closest to the target-node. Defaults to the tree root.
39
+ */
40
+ export function findClosestVisibleInNewTree(targetNode, newTreeRoot) {
41
+ var _a, _b;
42
+ // Traverse upwards to build the expected path to the original node
43
+ let pathToNode = [targetNode];
44
+ let traversingNode = targetNode;
45
+ while (traversingNode.parent) {
46
+ traversingNode = traversingNode.parent;
47
+ pathToNode = [traversingNode, ...pathToNode];
48
+ }
49
+ // Assume that the tree root will always be an available visible parent
50
+ let visibleParent = newTreeRoot;
51
+ let childrenInTree = (_a = visibleParent.children) !== null && _a !== void 0 ? _a : [];
52
+ // Using the computed path, traverse downwards in the new tree structure as far as possible
53
+ for (let i = 1; i < pathToNode.length; i++) {
54
+ const pathNode = pathToNode[i];
55
+ const nodeId = makeNodeId(pathNode);
56
+ const foundChild = childrenInTree.find((node) => makeNodeId(node) === nodeId);
57
+ // Previous node was the last visible parent for the node
58
+ if (!foundChild) {
59
+ return visibleParent;
60
+ }
61
+ else {
62
+ visibleParent = foundChild;
63
+ childrenInTree = (_b = foundChild.children) !== null && _b !== void 0 ? _b : [];
64
+ }
65
+ }
66
+ return visibleParent;
67
+ }
68
+ /**
69
+ * Computes the SVG drawing command for a link in the tree
70
+ * @param link A link in the tree
71
+ * @returns An SVG command string
72
+ */
73
+ export function diagonalPath(link) {
74
+ const { source, target } = link;
75
+ const avgY = (target.y + source.y) / 2;
76
+ // Svg path drawing commands. Note that x and y is swapped to show the tree sideways
77
+ return `
78
+ M ${source.y} ${source.x} \
79
+ C \
80
+ ${avgY} ${source.x}, \
81
+ ${avgY} ${target.x}, \
82
+ ${target.y} ${target.x}
83
+ `;
84
+ }
85
+ //# sourceMappingURL=treePlot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"treePlot.js","sourceRoot":"","sources":["../../src/utils/treePlot.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,IAAsC;IAC7D,gJAAgJ;IAChJ,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;IACjC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC;IAEnC,OAAO,GAAG,IAAI,KAAK,KAAK,EAAE,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,IAAgB;IACvC,OAAO,QAAQ,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CA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AACzB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,IAAgB;IACzC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAEhC,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACvC,oFAAoF;IACpF,OAAO;YACC,MAAM,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC;;cAElB,IAAI,IAAI,MAAM,CAAC,CAAC;cAChB,IAAI,IAAI,MAAM,CAAC,CAAC;cAChB,MAAM,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC;SACzB,CAAC;AACV,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webviz/group-tree-plot",
3
- "version": "1.3.23",
3
+ "version": "1.4.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -17,7 +17,8 @@
17
17
  "license": "MPL-2.0",
18
18
  "dependencies": {
19
19
  "d3": "^7.8.2",
20
- "lodash": "^4.17.21"
20
+ "lodash": "^4.17.21",
21
+ "motion": "^11.18.0"
21
22
  },
22
23
  "peerDependencies": {
23
24
  "react": "^17 || ^18",
@@ -1,64 +0,0 @@
1
- /**
2
- * Class to assemble Group tree visualization. Creates an _svg, and appends to the
3
- * assigned HTML element. Draws the tree provided in datedTrees with the current flow rate,
4
- * node info and date time.
5
- *
6
- * Provides methods to update selected date time, and change flow rate and node info.
7
- */
8
- export default class GroupTreeAssembler {
9
- /**
10
- * Initialize all trees in the group tree datastructure, once for the entire visualization.
11
- *
12
- */
13
- static initHierarchies(tree_data: any, height: any): any;
14
- /**
15
- *
16
- * @param dom_element_id - id of the HTML element to append the _svg to
17
- * @param datedTrees - List of dated tree data structure containing the trees to visualize
18
- * @param initialFlowRate - key identifying the initial selected flow rate for the tree edges
19
- * @param initialNodeInfo - key identifying the initial selected node info for the tree nodes
20
- * @param currentDateTime - the initial/current date time
21
- * @param edgeMetadataList - List of metadata for the edge keys in the tree data structure
22
- * @param nodeMetadataList - List of metadata for the node keys in the tree data structure
23
- */
24
- constructor(dom_element_id: any, datedTrees: any, initialFlowRate: any, initialNodeInfo: any, currentDateTime: any, edgeMetadataList: any, nodeMetadataList: any);
25
- _propertyToLabelMap: Map<any, any>;
26
- _currentFlowRate: any;
27
- _currentNodeInfo: any;
28
- _currentDateTime: any;
29
- _transitionTime: number;
30
- _path_scale: Map<any, any>;
31
- _rectWidth: any;
32
- _rectHeight: number;
33
- _rectLeftMargin: number;
34
- _rectTopMargin: number;
35
- _treeWidth: number;
36
- _svg: d3.Selection<SVGGElement, any, null, undefined>;
37
- _textpaths: d3.Selection<SVGGElement, any, null, undefined>;
38
- _renderTree: d3.TreeLayout<any>;
39
- _data: any;
40
- _currentTree: {};
41
- /**
42
- * @returns {*} -The initialized hierarchical group tree data structure
43
- */
44
- get data(): any;
45
- /**
46
- * Set the flowrate and update display of all edges accordingly.
47
- *
48
- * @param flowrate - key identifying the flowrate of the incoming edge
49
- */
50
- set flowrate(flowrate: any);
51
- get flowrate(): any;
52
- set nodeinfo(nodeinfo: any);
53
- get nodeinfo(): any;
54
- getEdgeStrokeWidth(key: any, val: any): string;
55
- /**
56
- * Sets the state of the current tree, and updates the tree visualization accordingly.
57
- * The state is changed either due to a branch open/close, or that the tree is entirely changed
58
- * when moving back and fourth in time.
59
- *
60
- * @param root
61
- */
62
- update(newDateTime: any): void;
63
- }
64
- import * as d3 from "d3";