This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
d3-utils.js
294 lines (275 loc) · 6.96 KB
/
d3-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import _ from 'lodash';
import log from 'log';
/**
* Adding the generic util functions for rendering basic shapes
*/
/**
* Check whether the parent is undefined or not.
* @param parent
* @throws error message
*/
const logParentUndefined = function (parent) {
if (_.isUndefined(parent)) {
const errMsg = 'Parent Undefined';
log.warn(errMsg);
throw errMsg;
}
};
/**
* Draws a basic svg rectangle
* @param x
* @param y
* @param width
* @param height
* @param rx
* @param ry
* @param parent
* @returns basic svg rectangle
*/
const rect = function (x, y, width, height, rx, ry, parent) {
logParentUndefined(parent);
rx = rx || 0;
ry = ry || 0;
return parent.append('rect')
.attr('x', x)
.attr('y', y)
.attr('width', width)
.attr('height', height)
.attr('rx', rx)
.attr('ry', ry);
};
/**
* Draws a basic svg circle
* @param x
* @param y
* @param radius
* @param parent
* @returns basic svg circle
*/
const circle = function (x, y, radius, parent) {
logParentUndefined(parent);
const svgCircle = parent.append('circle')
.attr('cx', x)
.attr('cy', y)
.attr('r', radius);
return svgCircle;
};
/**
* Draws a basic centered svg rectangle
* @param center
* @param width
* @param height
* @param rx
* @param ry
* @param parent
* @returns centered rectangle
*/
const centeredRect = function (center, width, height, rx, ry, parent) {
logParentUndefined(parent);
rx = rx || 0;
ry = ry || 0;
return this.rect(center.x() - (width / 2), center.y() - (height / 2), width, height, rx, ry, parent);
};
/**
* Draw a generic svg line
* @param x1
* @param y1
* @param x2
* @param y2
* @param parent
* @returns Generic svg line
*/
const line = function (x1, y1, x2, y2, parent) {
logParentUndefined(parent);
return parent.append('line')
.attr('x1', x1)
.attr('y1', y1)
.attr('x2', x2)
.attr('y2', y2);
};
/**
* Draw a generic svg line
* @param start {Point}
* @param end {Point}
* @param parent
* @returns Generic svg line
*/
const lineFromPoints = function (start, end, parent) {
logParentUndefined(parent);
return parent.append('line')
.attr('x1', start.x())
.attr('y1', start.y())
.attr('x2', end.x())
.attr('y2', end.y());
};
/**
* Draw a generic svg text Element
* @param x
* @param y
* @param textContent
* @param parent
* @returns svg text element
*/
const textElement = function (x, y, textContent, parent) {
logParentUndefined(parent);
return parent.append('text')
.attr('x', x)
.attr('y', y)
.text(() => textContent);
};
/**
* Draw a generic svg input Element
* @param x
* @param y
* @param textContent
* @param parent
* @returns svg text element
*/
const inputElement = function (x, y, textContent, parent) {
logParentUndefined(parent);
return parent.append('input')
.attr('x', x)
.attr('y', y)
.text(() => textContent);
};
/**
* Draw a centered svg text Element on a given point
*
* @param center {Point}
* @param text {String}
* @param parent {SVGElement}
* @returns svg text element
*/
const centeredText = function (center, text, parent) {
logParentUndefined(parent);
return parent.append('text')
.attr('x', center.x())
.attr('y', center.y())
.attr('text-anchor', 'middle')
.attr('alignment-baseline', 'central')
.attr('dominant-baseline', 'central') // alignment-baseline doesn't work in firefox https://goo.gl/jJ6RAB
.text(() => text);
};
/**
* Append a svg group to the given parent
* @param parent
* @returns svg group
*/
const group = function (parent) {
logParentUndefined(parent);
return parent.append('g');
};
/**
* Append a generic svg to the provided parent element
* @param opts
* @param parent
* @returns svg
*/
const svg = function (opts, parent) {
logParentUndefined(parent);
return parent.append('svg')
.attr('height', opts.height)
.attr('width', opts.width)
.attr('class', opts.class);
};
/**
* Draw a right pointed arrow head
* @param x
* @param y
* @param parent
* @returns polyline generating an arrow head
*/
const inputTriangle = function (x, y, parent) {
logParentUndefined(parent);
const points = `${x},${y - 5} ${x + 5},${y} ${x},${y + 5}`;
return parent.append('polyline')
.attr('points', points);
};
/**
* Draw a left pointed arrow head
* @param x
* @param y
* @param parent
* @returns polyline generating an arrow head
*/
const outputTriangle = function (x, y, parent) {
logParentUndefined(parent);
const points = `${x},${y} ${x + 5},${y - 5} ${x + 5},${y + 5}`;
return parent.append('polyline')
.attr('points', points);
};
/**
* Draw a dashed line
* @param x1
* @param y1
* @param x2
* @param y2
* @param color
* @param parent
* @returns generic line which is dashed
*/
const dashedLine = function (x1, y1, x2, y2, color, parent) {
logParentUndefined(parent);
return parent.append('line')
.attr('x1', x1)
.attr('y1', y1)
.attr('x2', x2)
.attr('y2', y2)
.attr('stroke-dasharray', '4, 3');
};
/**
* Draw a generic polygon
* @param points
* @param parent
* @returns polygon generated by the given points
*/
const polygon = function (points, parent) {
logParentUndefined(parent);
return parent.append('polygon')
.attr('points', points);
};
/**
* Draw a generic polyline
* @param points
* @param parent
* @returns polyline generated by the given points
*/
const polyline = function (points, parent) {
logParentUndefined(parent);
return parent.append('polyline')
.attr('points', points);
};
const draw = {};
draw.centeredRect = centeredRect;
draw.rect = rect;
draw.line = line;
draw.lineFromPoints = lineFromPoints;
draw.textElement = textElement;
draw.inputElement = inputElement;
draw.centeredText = centeredText;
draw.circle = circle;
draw.group = group;
draw.svg = svg;
draw.inputTriangle = inputTriangle;
draw.outputTriangle = outputTriangle;
draw.dashedLine = dashedLine;
draw.polygon = polygon;
draw.polyline = polyline;
export default draw;