@performant-software/visualize 0.6.0-beta.1
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/LICENSE +21 -0
- package/README.md +0 -0
- package/build/index.js +2 -0
- package/build/index.js.map +1 -0
- package/build/main.css +43 -0
- package/index.js +1 -0
- package/package.json +32 -0
- package/src/components/TreeGraph.css +13 -0
- package/src/components/TreeGraph.js +443 -0
- package/src/hooks/Zoom.css +34 -0
- package/src/hooks/Zoom.js +139 -0
- package/src/i18n/en.json +11 -0
- package/src/i18n/i18n.js +24 -0
- package/src/index.js +7 -0
- package/types/components/TreeGraph.js.flow +443 -0
- package/types/hooks/Zoom.js.flow +139 -0
- package/types/i18n/i18n.js.flow +24 -0
- package/types/index.js.flow +7 -0
- package/webpack.config.js +3 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
.zoom .btn {
|
|
2
|
+
margin: 0;
|
|
3
|
+
text-align: center;
|
|
4
|
+
border: none;
|
|
5
|
+
background: #2f2f2f;
|
|
6
|
+
color: #d3d3d3;
|
|
7
|
+
padding: 0 4px;
|
|
8
|
+
border-top: 1px solid #0a0a0a;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.zoom .btn-lg {
|
|
12
|
+
font-size: 1em;
|
|
13
|
+
line-height: 1;
|
|
14
|
+
padding: 7px;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.zoom .btn-zoom {
|
|
18
|
+
width: 30px;
|
|
19
|
+
font-size: 2em;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.zoom .btn-bottom {
|
|
23
|
+
font-size: 2em;
|
|
24
|
+
margin-bottom: 1rem;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.zoom .controls {
|
|
28
|
+
position: absolute;
|
|
29
|
+
top: 15px;
|
|
30
|
+
right: 15px;
|
|
31
|
+
display: flex;
|
|
32
|
+
flex-direction: column;
|
|
33
|
+
align-items: flex-end;
|
|
34
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import React, { useCallback, type ComponentType, type Element } from 'react';
|
|
4
|
+
import { localPoint } from '@visx/event';
|
|
5
|
+
import { Zoom } from '@visx/zoom';
|
|
6
|
+
import i18n from '../i18n/i18n';
|
|
7
|
+
import './Zoom.css';
|
|
8
|
+
|
|
9
|
+
type Props = {
|
|
10
|
+
initialTransform?: {
|
|
11
|
+
skewX: number,
|
|
12
|
+
skewY: number,
|
|
13
|
+
scaleX: number,
|
|
14
|
+
scaleY: number,
|
|
15
|
+
translateX: number,
|
|
16
|
+
translateY: number
|
|
17
|
+
},
|
|
18
|
+
parentHeight: number,
|
|
19
|
+
parentWidth: number,
|
|
20
|
+
scaleXMax?: number,
|
|
21
|
+
scaleXMin?: number,
|
|
22
|
+
scaleYMax?: number,
|
|
23
|
+
scaleYMin?: number
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type WithZoomType = (WrappedComponent: ComponentType<any>) => (props: Props) => Element<any>;
|
|
27
|
+
|
|
28
|
+
const DEFAULT_INITIAL_TRANSFORM = {
|
|
29
|
+
scaleX: 1,
|
|
30
|
+
scaleY: 1,
|
|
31
|
+
translateX: 0,
|
|
32
|
+
translateY: 0,
|
|
33
|
+
skewX: 0,
|
|
34
|
+
skewY: 0,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const withZoom: WithZoomType = (WrappedComponent: ComponentType<any>) => (props: Props) => {
|
|
38
|
+
const { parentWidth: width, parentHeight: height } = props;
|
|
39
|
+
const { scaleXMin = 1 / 2, scaleXMax = 4 } = props;
|
|
40
|
+
const { scaleYMin = 1 / 2, scaleYMax = 4 } = props;
|
|
41
|
+
const { initialTransform = DEFAULT_INITIAL_TRANSFORM } = props;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Renders the container with zoom controls.
|
|
45
|
+
*
|
|
46
|
+
* @type {unknown}
|
|
47
|
+
*/
|
|
48
|
+
const renderZoomContainer = useCallback((zoom) => (
|
|
49
|
+
<rect
|
|
50
|
+
width={width}
|
|
51
|
+
height={height}
|
|
52
|
+
rx={14}
|
|
53
|
+
fill='transparent'
|
|
54
|
+
onTouchStart={zoom.dragStart}
|
|
55
|
+
onTouchMove={zoom.dragMove}
|
|
56
|
+
onTouchEnd={zoom.dragEnd}
|
|
57
|
+
onMouseDown={zoom.dragStart}
|
|
58
|
+
onMouseMove={zoom.dragMove}
|
|
59
|
+
onMouseUp={zoom.dragEnd}
|
|
60
|
+
onMouseLeave={() => {
|
|
61
|
+
if (zoom.isDragging) zoom.dragEnd();
|
|
62
|
+
}}
|
|
63
|
+
onDoubleClick={(event) => {
|
|
64
|
+
const point = localPoint(event) || { x: 0, y: 0 };
|
|
65
|
+
zoom.scale({ scaleX: 1.1, scaleY: 1.1, point });
|
|
66
|
+
}}
|
|
67
|
+
/>
|
|
68
|
+
), [width, height]);
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<Zoom
|
|
72
|
+
width={width}
|
|
73
|
+
height={height}
|
|
74
|
+
scaleXMin={scaleXMin}
|
|
75
|
+
scaleXMax={scaleXMax}
|
|
76
|
+
scaleYMin={scaleYMin}
|
|
77
|
+
scaleYMax={scaleYMax}
|
|
78
|
+
initialTransformMatrix={initialTransform}
|
|
79
|
+
>
|
|
80
|
+
{ (zoom) => (
|
|
81
|
+
<div
|
|
82
|
+
className='zoom'
|
|
83
|
+
>
|
|
84
|
+
<WrappedComponent
|
|
85
|
+
{...props}
|
|
86
|
+
renderZoomContainer={renderZoomContainer.bind(this, zoom)}
|
|
87
|
+
zoom={zoom}
|
|
88
|
+
/>
|
|
89
|
+
<div
|
|
90
|
+
className='controls'
|
|
91
|
+
>
|
|
92
|
+
<button
|
|
93
|
+
type='button'
|
|
94
|
+
className='btn btn-zoom'
|
|
95
|
+
onClick={() => zoom.scale({ scaleX: 1.2, scaleY: 1.2 })}
|
|
96
|
+
>
|
|
97
|
+
{ i18n.t('Zoom.buttons.zoomIn') }
|
|
98
|
+
</button>
|
|
99
|
+
<button
|
|
100
|
+
type='button'
|
|
101
|
+
className='btn btn-zoom btn-bottom'
|
|
102
|
+
onClick={() => zoom.scale({ scaleX: 0.8, scaleY: 0.8 })}
|
|
103
|
+
>
|
|
104
|
+
{ i18n.t('Zoom.buttons.zoomOut') }
|
|
105
|
+
</button>
|
|
106
|
+
<button
|
|
107
|
+
type='button'
|
|
108
|
+
className='btn btn-lg'
|
|
109
|
+
onClick={zoom.center}
|
|
110
|
+
>
|
|
111
|
+
{ i18n.t('Zoom.buttons.center') }
|
|
112
|
+
</button>
|
|
113
|
+
<button
|
|
114
|
+
type='button'
|
|
115
|
+
className='btn btn-lg'
|
|
116
|
+
onClick={zoom.reset}
|
|
117
|
+
>
|
|
118
|
+
{ i18n.t('Zoom.buttons.reset') }
|
|
119
|
+
</button>
|
|
120
|
+
<button
|
|
121
|
+
type='button'
|
|
122
|
+
className='btn btn-lg'
|
|
123
|
+
onClick={zoom.clear}
|
|
124
|
+
>
|
|
125
|
+
{ i18n.t('Zoom.buttons.clear') }
|
|
126
|
+
</button>
|
|
127
|
+
</div>
|
|
128
|
+
</div>
|
|
129
|
+
)}
|
|
130
|
+
</Zoom>
|
|
131
|
+
);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export default withZoom;
|
|
135
|
+
|
|
136
|
+
export type ZoomProps = {
|
|
137
|
+
renderZoomContainer: () => Element<any>,
|
|
138
|
+
zoom: typeof Zoom
|
|
139
|
+
};
|
package/src/i18n/en.json
ADDED
package/src/i18n/i18n.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import i18next from 'i18next';
|
|
2
|
+
|
|
3
|
+
import en from './en.json';
|
|
4
|
+
|
|
5
|
+
const resources = {
|
|
6
|
+
en: {
|
|
7
|
+
translation: en
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const i18n = i18next.createInstance();
|
|
12
|
+
|
|
13
|
+
i18n
|
|
14
|
+
.init({
|
|
15
|
+
debug: true,
|
|
16
|
+
fallbackLng: 'en',
|
|
17
|
+
lng: 'en',
|
|
18
|
+
interpolation: {
|
|
19
|
+
escapeValue: false,
|
|
20
|
+
},
|
|
21
|
+
resources
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export default i18n;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import { Group } from '@visx/group';
|
|
4
|
+
import { hierarchy, Tree } from '@visx/hierarchy';
|
|
5
|
+
import { withParentSize } from '@visx/responsive';
|
|
6
|
+
import {
|
|
7
|
+
LinkHorizontal,
|
|
8
|
+
LinkHorizontalCurve,
|
|
9
|
+
LinkHorizontalLine,
|
|
10
|
+
LinkHorizontalStep,
|
|
11
|
+
LinkRadial,
|
|
12
|
+
LinkRadialCurve,
|
|
13
|
+
LinkRadialLine,
|
|
14
|
+
LinkRadialStep,
|
|
15
|
+
LinkVertical,
|
|
16
|
+
LinkVerticalCurve,
|
|
17
|
+
LinkVerticalLine,
|
|
18
|
+
LinkVerticalStep
|
|
19
|
+
} from '@visx/shape';
|
|
20
|
+
import { pointRadial } from 'd3-shape';
|
|
21
|
+
import React, {
|
|
22
|
+
useCallback,
|
|
23
|
+
useEffect,
|
|
24
|
+
useMemo,
|
|
25
|
+
useRef,
|
|
26
|
+
type ComponentType,
|
|
27
|
+
type Element
|
|
28
|
+
} from 'react';
|
|
29
|
+
import _ from 'underscore';
|
|
30
|
+
import withZoom, { type ZoomProps } from '../hooks/Zoom';
|
|
31
|
+
import './TreeGraph.css';
|
|
32
|
+
|
|
33
|
+
type Props = ZoomProps & {
|
|
34
|
+
data: any,
|
|
35
|
+
fontSize: number,
|
|
36
|
+
layout: string,
|
|
37
|
+
linkType: string,
|
|
38
|
+
offset: number,
|
|
39
|
+
orientation: string,
|
|
40
|
+
parentHeight: number,
|
|
41
|
+
parentWidth: number,
|
|
42
|
+
linkColor: string,
|
|
43
|
+
linkWidth: number,
|
|
44
|
+
margin: {
|
|
45
|
+
top: number,
|
|
46
|
+
right: number,
|
|
47
|
+
bottom: number,
|
|
48
|
+
left: number
|
|
49
|
+
},
|
|
50
|
+
nodeWidth: number,
|
|
51
|
+
onClick?: (data: any) => void,
|
|
52
|
+
renderNode?: (data: any) => Element<any>,
|
|
53
|
+
renderText?: (data: any) => Element<any>,
|
|
54
|
+
stepPercent: number,
|
|
55
|
+
textColor: string
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const Layout = {
|
|
59
|
+
cartesian: 'cartesian',
|
|
60
|
+
polar: 'polar'
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const LinkType = {
|
|
64
|
+
curve: 'curve',
|
|
65
|
+
line: 'line',
|
|
66
|
+
step: 'step'
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const Orientation = {
|
|
70
|
+
horizontal: 'horizontal',
|
|
71
|
+
vertical: 'vertical'
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const Circle: ComponentType<any> = (props: any) => (
|
|
75
|
+
<circle
|
|
76
|
+
r={props.radius}
|
|
77
|
+
{...props}
|
|
78
|
+
/>
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const Rectangle: ComponentType<any> = (props: any) => (
|
|
82
|
+
<rect
|
|
83
|
+
{...props}
|
|
84
|
+
/>
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const TreeGraph = (props: Props) => {
|
|
88
|
+
const ref = useRef();
|
|
89
|
+
|
|
90
|
+
const innerWidth = props.parentWidth - props.margin.left - props.margin.right;
|
|
91
|
+
const innerHeight = props.parentHeight - props.margin.top - props.margin.bottom;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Sets the link component based on the layout, linkType, and orientation props.
|
|
95
|
+
*
|
|
96
|
+
* @type {*}
|
|
97
|
+
*/
|
|
98
|
+
const LinkComponent = useMemo(() => {
|
|
99
|
+
let value;
|
|
100
|
+
|
|
101
|
+
if (props.layout === Layout.polar) {
|
|
102
|
+
if (props.linkType === LinkType.step) {
|
|
103
|
+
value = LinkRadialStep;
|
|
104
|
+
} else if (props.linkType === LinkType.curve) {
|
|
105
|
+
value = LinkRadialCurve;
|
|
106
|
+
} else if (props.linkType === LinkType.line) {
|
|
107
|
+
value = LinkRadialLine;
|
|
108
|
+
} else {
|
|
109
|
+
value = LinkRadial;
|
|
110
|
+
}
|
|
111
|
+
} else if (props.orientation === Orientation.vertical) {
|
|
112
|
+
if (props.linkType === LinkType.step) {
|
|
113
|
+
value = LinkVerticalStep;
|
|
114
|
+
} else if (props.linkType === LinkType.curve) {
|
|
115
|
+
value = LinkVerticalCurve;
|
|
116
|
+
} else if (props.linkType === LinkType.line) {
|
|
117
|
+
value = LinkVerticalLine;
|
|
118
|
+
} else {
|
|
119
|
+
value = LinkVertical;
|
|
120
|
+
}
|
|
121
|
+
} else if (props.linkType === LinkType.step) {
|
|
122
|
+
value = LinkHorizontalStep;
|
|
123
|
+
} else if (props.linkType === LinkType.curve) {
|
|
124
|
+
value = LinkHorizontalCurve;
|
|
125
|
+
} else if (props.linkType === LinkType.line) {
|
|
126
|
+
value = LinkHorizontalLine;
|
|
127
|
+
} else {
|
|
128
|
+
value = LinkHorizontal;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return value;
|
|
132
|
+
}, [props.layout, props.linkType, props.orientation]);
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Returns the depth of the last expanded node.
|
|
136
|
+
*
|
|
137
|
+
* @type {function(*, *): *}
|
|
138
|
+
*/
|
|
139
|
+
const getMaxDepth = useCallback((node, depth) => {
|
|
140
|
+
let max = depth;
|
|
141
|
+
|
|
142
|
+
if (node.isExpanded) {
|
|
143
|
+
max = _.max(_.map(node.children, (child) => getMaxDepth(child, depth + 1)));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return max;
|
|
147
|
+
}, []);
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Returns the node position for the passed coordinates.
|
|
151
|
+
*
|
|
152
|
+
* @type {function(*, *): {top: *, left: *}}
|
|
153
|
+
*/
|
|
154
|
+
const getNodePosition = useCallback((x, y) => {
|
|
155
|
+
let top;
|
|
156
|
+
let left;
|
|
157
|
+
|
|
158
|
+
if (props.layout === Layout.polar) {
|
|
159
|
+
const [radialX, radialY] = pointRadial(x, y);
|
|
160
|
+
top = radialY;
|
|
161
|
+
left = radialX;
|
|
162
|
+
} else if (props.orientation === Orientation.vertical) {
|
|
163
|
+
top = y;
|
|
164
|
+
left = x;
|
|
165
|
+
} else {
|
|
166
|
+
top = x;
|
|
167
|
+
left = y;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
top,
|
|
172
|
+
left
|
|
173
|
+
};
|
|
174
|
+
}, [props.layout, props.orientation]);
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Returns the position and size of the root node.
|
|
178
|
+
*
|
|
179
|
+
* @type {{sizeWidth: *, origin: *, sizeHeight: *}}
|
|
180
|
+
*/
|
|
181
|
+
const root = useMemo(() => {
|
|
182
|
+
let sizeWidth;
|
|
183
|
+
let sizeHeight;
|
|
184
|
+
let origin;
|
|
185
|
+
|
|
186
|
+
const depth = getMaxDepth(props.data, 1);
|
|
187
|
+
|
|
188
|
+
if (props.layout === Layout.polar) {
|
|
189
|
+
origin = {
|
|
190
|
+
x: innerWidth / 2,
|
|
191
|
+
y: innerHeight / 2,
|
|
192
|
+
};
|
|
193
|
+
sizeWidth = 2 * Math.PI;
|
|
194
|
+
sizeHeight = (Math.min(innerWidth, innerHeight) / 2) * (depth / 2);
|
|
195
|
+
} else {
|
|
196
|
+
origin = { x: 0, y: 0 };
|
|
197
|
+
if (props.orientation === Orientation.vertical) {
|
|
198
|
+
sizeWidth = innerWidth;
|
|
199
|
+
sizeHeight = innerHeight;
|
|
200
|
+
} else {
|
|
201
|
+
sizeWidth = innerHeight;
|
|
202
|
+
sizeHeight = innerWidth;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
sizeWidth,
|
|
208
|
+
sizeHeight,
|
|
209
|
+
origin
|
|
210
|
+
};
|
|
211
|
+
}, [getMaxDepth, innerHeight, innerWidth, props.data, props.layout, props.orientation]);
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Renders the passed node. If provided, only the "renderNode" prop is called.
|
|
215
|
+
*
|
|
216
|
+
* @type {(function(*): (*))|*}
|
|
217
|
+
*/
|
|
218
|
+
const renderNode = useCallback((node) => {
|
|
219
|
+
if (props.renderNode) {
|
|
220
|
+
return props.renderNode(node.data);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (node.depth === 0) {
|
|
224
|
+
return (
|
|
225
|
+
<Circle
|
|
226
|
+
fill='#FF4A4A'
|
|
227
|
+
onClick={props.onClick && props.onClick.bind(this, node.data)}
|
|
228
|
+
/>
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return (
|
|
233
|
+
<Rectangle
|
|
234
|
+
fill='#272b4d'
|
|
235
|
+
stroke={node.data.children ? '#03c0dc' : '#26deb0'}
|
|
236
|
+
strokeWidth={1}
|
|
237
|
+
strokeDasharray={node.data.children ? '0' : '2,2'}
|
|
238
|
+
strokeOpacity={node.data.children ? 1 : 0.6}
|
|
239
|
+
rx={node.data.children ? 0 : 10}
|
|
240
|
+
onClick={props.onClick && props.onClick.bind(this, node.data)}
|
|
241
|
+
/>
|
|
242
|
+
);
|
|
243
|
+
}, [props.onClick, props.renderNode]);
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Renders the text for the passed node.
|
|
247
|
+
*
|
|
248
|
+
* @type {(function(*): (*))|*}
|
|
249
|
+
*/
|
|
250
|
+
const renderText = useCallback((node) => {
|
|
251
|
+
if (props.renderText) {
|
|
252
|
+
return props.renderText(node.data);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return (
|
|
256
|
+
<text
|
|
257
|
+
dy='0.33em'
|
|
258
|
+
fontSize={props.fontSize}
|
|
259
|
+
textAnchor='middle'
|
|
260
|
+
fill={props.textColor}
|
|
261
|
+
>
|
|
262
|
+
{ node.data.name }
|
|
263
|
+
</text>
|
|
264
|
+
);
|
|
265
|
+
}, [props.renderText]);
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Renders the group element for the passed node.
|
|
269
|
+
*
|
|
270
|
+
* @type {unknown}
|
|
271
|
+
*/
|
|
272
|
+
const renderGroup = useCallback((node, key) => {
|
|
273
|
+
const { top, left } = getNodePosition(node.x, node.y);
|
|
274
|
+
|
|
275
|
+
return (
|
|
276
|
+
<Group
|
|
277
|
+
top={top}
|
|
278
|
+
left={left}
|
|
279
|
+
key={key}
|
|
280
|
+
>
|
|
281
|
+
{ renderNode(node) }
|
|
282
|
+
{ renderText(node) }
|
|
283
|
+
</Group>
|
|
284
|
+
);
|
|
285
|
+
}, [renderNode, renderText, props.layout, props.orientation]);
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Resizes the "circle" and "rect" elements based on the text contained in the group. This effect will also
|
|
289
|
+
* convert text into multiple lines.
|
|
290
|
+
*/
|
|
291
|
+
useEffect(() => {
|
|
292
|
+
const { current } = ref;
|
|
293
|
+
if (current) {
|
|
294
|
+
const padding = 15;
|
|
295
|
+
const groups = current.getElementsByTagName('g');
|
|
296
|
+
_.each(groups, (group) => {
|
|
297
|
+
const circle = _.first(group.getElementsByTagName('circle'));
|
|
298
|
+
const rect = _.first(group.getElementsByTagName('rect'));
|
|
299
|
+
|
|
300
|
+
const text = _.first(group.getElementsByTagName('text'));
|
|
301
|
+
const { height } = text.getBBox();
|
|
302
|
+
|
|
303
|
+
if (!text.getElementsByTagName('tspan').length) {
|
|
304
|
+
const words = text.innerHTML.split(/\s+/);
|
|
305
|
+
|
|
306
|
+
const lines = [];
|
|
307
|
+
let lineIndex = 0;
|
|
308
|
+
|
|
309
|
+
for (let i = 0; i < words.length; i += 1) {
|
|
310
|
+
// Initialize the line array for the current line
|
|
311
|
+
if (!lines[lineIndex]) {
|
|
312
|
+
lines[lineIndex] = [];
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Add the current word to the current line
|
|
316
|
+
lines[lineIndex].push(words[i]);
|
|
317
|
+
|
|
318
|
+
// Look ahead to the next word and increment the line index if necessary
|
|
319
|
+
if (i < words.length - 1) {
|
|
320
|
+
const testElement = text.cloneNode();
|
|
321
|
+
testElement.innerHTML = [...lines[lineIndex], words[i + 1]].join(' ');
|
|
322
|
+
group.append(testElement);
|
|
323
|
+
|
|
324
|
+
if (testElement.getBBox().width > props.nodeWidth) {
|
|
325
|
+
lineIndex += 1;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
testElement.remove();
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Append the lines to the text element
|
|
333
|
+
_.each(lines, (line, index) => {
|
|
334
|
+
const tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
|
|
335
|
+
tspan.setAttribute('x', '0');
|
|
336
|
+
if (index > 0) {
|
|
337
|
+
tspan.setAttribute('dy', `${height}px`);
|
|
338
|
+
}
|
|
339
|
+
tspan.appendChild(document.createTextNode(line.join(' ')));
|
|
340
|
+
|
|
341
|
+
if (index === 0) {
|
|
342
|
+
text.replaceChildren(tspan);
|
|
343
|
+
} else {
|
|
344
|
+
text.appendChild(tspan);
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
// Set the shape attributes based on the text size
|
|
349
|
+
const bbox = text.getBBox();
|
|
350
|
+
if (circle && text) {
|
|
351
|
+
circle.setAttribute('x', bbox.x - padding);
|
|
352
|
+
circle.setAttribute('y', bbox.y - padding);
|
|
353
|
+
circle.setAttribute('r', (bbox.width / 2) + padding);
|
|
354
|
+
} else if (rect && text) {
|
|
355
|
+
rect.setAttribute('x', bbox.x - padding);
|
|
356
|
+
rect.setAttribute('y', bbox.y - padding);
|
|
357
|
+
rect.setAttribute('width', bbox.width + 2 * padding);
|
|
358
|
+
rect.setAttribute('height', bbox.height + 2 * padding);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
}, [props.data]);
|
|
364
|
+
|
|
365
|
+
return (
|
|
366
|
+
<div
|
|
367
|
+
className='tree-graph'
|
|
368
|
+
ref={ref}
|
|
369
|
+
style={{
|
|
370
|
+
display: 'flex',
|
|
371
|
+
flexGrow: '1'
|
|
372
|
+
}}
|
|
373
|
+
>
|
|
374
|
+
<svg
|
|
375
|
+
width={props.parentWidth}
|
|
376
|
+
height={props.parentHeight - props.offset}
|
|
377
|
+
ref={props.zoom.containerRef}
|
|
378
|
+
>
|
|
379
|
+
{ props.renderZoomContainer() }
|
|
380
|
+
<Group
|
|
381
|
+
top={props.margin.top}
|
|
382
|
+
left={props.margin.left}
|
|
383
|
+
transform={props.zoom.toString()}
|
|
384
|
+
>
|
|
385
|
+
<Tree
|
|
386
|
+
root={hierarchy(props.data, (d) => (d.isExpanded ? d.children : null))}
|
|
387
|
+
size={[root.sizeWidth, root.sizeHeight]}
|
|
388
|
+
separation={(a, b) => (a.parent === b.parent ? 1 : 2) / a.depth}
|
|
389
|
+
>
|
|
390
|
+
{ (tree) => (
|
|
391
|
+
<Group
|
|
392
|
+
top={root.origin.y}
|
|
393
|
+
left={root.origin.x}
|
|
394
|
+
>
|
|
395
|
+
{ tree.links().map((link, i) => (
|
|
396
|
+
<LinkComponent
|
|
397
|
+
key={i}
|
|
398
|
+
data={link}
|
|
399
|
+
percent={props.stepPercent}
|
|
400
|
+
stroke={props.linkColor}
|
|
401
|
+
strokeWidth={props.linkWidth}
|
|
402
|
+
fill='none'
|
|
403
|
+
/>
|
|
404
|
+
))}
|
|
405
|
+
{ _.map(tree.descendants(), renderGroup) }
|
|
406
|
+
</Group>
|
|
407
|
+
)}
|
|
408
|
+
</Tree>
|
|
409
|
+
</Group>
|
|
410
|
+
</svg>
|
|
411
|
+
</div>
|
|
412
|
+
);
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
TreeGraph.defaultProps = {
|
|
416
|
+
fontSize: 12,
|
|
417
|
+
layout: Layout.cartesian,
|
|
418
|
+
linkColor: '#B2B09B',
|
|
419
|
+
linkType: LinkType.line,
|
|
420
|
+
linkWidth: 1,
|
|
421
|
+
margin: {
|
|
422
|
+
top: 30,
|
|
423
|
+
left: 30,
|
|
424
|
+
right: 30,
|
|
425
|
+
bottom: 70
|
|
426
|
+
},
|
|
427
|
+
nodeWidth: 75,
|
|
428
|
+
offset: 0,
|
|
429
|
+
orientation: Orientation.horizontal,
|
|
430
|
+
stepPercent: 0.5,
|
|
431
|
+
textColor: '#FFFFFF'
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
type TreeGraphType = ComponentType<any> & {
|
|
435
|
+
Circle: typeof Circle,
|
|
436
|
+
Rectangle: typeof Rectangle
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
const TreeGraphComponent: TreeGraphType = withParentSize(withZoom(TreeGraph));
|
|
440
|
+
TreeGraphComponent.Circle = Circle;
|
|
441
|
+
TreeGraphComponent.Rectangle = Rectangle;
|
|
442
|
+
|
|
443
|
+
export default TreeGraphComponent;
|