-
Notifications
You must be signed in to change notification settings - Fork 19.8k
Expand file tree
/
Copy pathgraphic.ts
More file actions
986 lines (909 loc) · 30 KB
/
Copy pathgraphic.ts
File metadata and controls
986 lines (909 loc) · 30 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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 * as pathTool from 'zrender/src/tool/path';
import * as matrix from 'zrender/src/core/matrix';
import * as vector from 'zrender/src/core/vector';
import Path, { PathProps } from 'zrender/src/graphic/Path';
import Transformable, { copyTransform } from 'zrender/src/core/Transformable';
import ZRImage, { ImageStyleProps } from 'zrender/src/graphic/Image';
import Group from 'zrender/src/graphic/Group';
import ZRText from 'zrender/src/graphic/Text';
import Circle from 'zrender/src/graphic/shape/Circle';
import Ellipse from 'zrender/src/graphic/shape/Ellipse';
import Sector from 'zrender/src/graphic/shape/Sector';
import Ring from 'zrender/src/graphic/shape/Ring';
import Polygon from 'zrender/src/graphic/shape/Polygon';
import Polyline from 'zrender/src/graphic/shape/Polyline';
import Rect from 'zrender/src/graphic/shape/Rect';
import Line from 'zrender/src/graphic/shape/Line';
import BezierCurve from 'zrender/src/graphic/shape/BezierCurve';
import Arc from 'zrender/src/graphic/shape/Arc';
import CompoundPath from 'zrender/src/graphic/CompoundPath';
import LinearGradient from 'zrender/src/graphic/LinearGradient';
import RadialGradient from 'zrender/src/graphic/RadialGradient';
import BoundingRect, { RectLike } from 'zrender/src/core/BoundingRect';
import OrientedBoundingRect from 'zrender/src/core/OrientedBoundingRect';
import Point from 'zrender/src/core/Point';
import IncrementalDisplayable from 'zrender/src/graphic/IncrementalDisplayable';
import * as subPixelOptimizeUtil from 'zrender/src/graphic/helper/subPixelOptimize';
import { Dictionary } from 'zrender/src/core/types';
import Displayable, { DisplayableProps } from 'zrender/src/graphic/Displayable';
import Element from 'zrender/src/Element';
import Model from '../model/Model';
import {
AnimationOptionMixin,
ZRRectLike,
ZRStyleProps,
CommonTooltipOption,
ComponentItemTooltipLabelFormatterParams,
NullUndefined,
ComponentOption,
Payload
} from './types';
import {
extend,
isArrayLike,
map,
defaults,
isString,
keys,
each,
hasOwn,
isArray,
isNumber,
clone,
assert,
} from 'zrender/src/core/util';
import { getECData } from './innerStore';
import ComponentModel from '../model/Component';
import {
updateProps,
initProps,
removeElement,
removeElementWithFadeOut,
isElementRemoved
} from '../animation/basicTransition';
import { ExtendedElement } from '../core/ExtendedElement';
import { mathMin, mathMax, mathAbs } from './number';
import type ExtensionAPI from '../core/ExtensionAPI';
import type CanvasPainter from 'zrender/src/canvas/Painter';
/**
* @deprecated export for compatitable reason
*/
export {updateProps, initProps, removeElement, removeElementWithFadeOut, isElementRemoved};
const _customShapeMap: Dictionary<{ new(): Path }> = {};
type ExtendShapeOpt = Parameters<typeof Path.extend>[0];
type ExtendShapeReturn = ReturnType<typeof Path.extend>;
export const XY = ['x', 'y'] as const;
export const WH = ['width', 'height'] as const;
/**
* NOTICE: Only canvas renderer can set these hoverLayer flags.
* @see ElementCommonState['hoverLayer']
*/
export const HOVER_LAYER_NO = 0;
export const HOVER_LAYER_FROM_THRESHOLD = 1;
export const HOVER_LAYER_FOR_INCREMENTAL = 2;
/**
* Extend shape with parameters
*/
export function extendShape(opts: ExtendShapeOpt): ExtendShapeReturn {
return Path.extend(opts);
}
const extendPathFromString = pathTool.extendFromString;
type SVGPathOption = Parameters<typeof extendPathFromString>[1];
type SVGPathCtor = ReturnType<typeof extendPathFromString>;
type SVGPath = InstanceType<SVGPathCtor>;
/**
* Extend path
*/
export function extendPath(pathData: string, opts: SVGPathOption): SVGPathCtor {
return extendPathFromString(pathData, opts);
}
/**
* Register a user defined shape.
* The shape class can be fetched by `getShapeClass`
* This method will overwrite the registered shapes, including
* the registered built-in shapes, if using the same `name`.
* The shape can be used in `custom series` and
* `graphic component` by declaring `{type: name}`.
*
* @param name
* @param ShapeClass Can be generated by `extendShape`.
*/
export function registerShape(name: string, ShapeClass: {new(): Path}) {
_customShapeMap[name] = ShapeClass;
}
/**
* Find shape class registered by `registerShape`. Usually used in
* fetching user defined shape.
*
* [Caution]:
* (1) This method **MUST NOT be used inside echarts !!!**, unless it is prepared
* to use user registered shapes.
* Because the built-in shape (see `getBuiltInShape`) will be registered by
* `registerShape` by default. That enables users to get both built-in
* shapes as well as the shapes belonging to themsleves. But users can overwrite
* the built-in shapes by using names like 'circle', 'rect' via calling
* `registerShape`. So the echarts inner featrues should not fetch shapes from here
* in case that it is overwritten by users, except that some features, like
* `custom series`, `graphic component`, do it deliberately.
*
* (2) In the features like `custom series`, `graphic component`, the user input
* `{tpye: 'xxx'}` does not only specify shapes but also specify other graphic
* elements like `'group'`, `'text'`, `'image'` or event `'path'`. Those names
* are reserved names, that is, if some user registers a shape named `'image'`,
* the shape will not be used. If we intending to add some more reserved names
* in feature, that might bring break changes (disable some existing user shape
* names). But that case probably rarely happens. So we don't make more mechanism
* to resolve this issue here.
*
* @param name
* @return The shape class. If not found, return nothing.
*/
export function getShapeClass(name: string): {new(): Path} {
if (_customShapeMap.hasOwnProperty(name)) {
return _customShapeMap[name];
}
}
/**
* Create a path element from path data string
* @param pathData
* @param opts
* @param rect
* @param layout 'center' or 'cover' default to be cover
*/
export function makePath(
pathData: string,
opts: SVGPathOption,
rect: ZRRectLike,
layout?: 'center' | 'cover'
): SVGPath {
const path = pathTool.createFromString(pathData, opts);
if (rect) {
if (layout === 'center') {
rect = centerGraphic(rect, path.getBoundingRect());
}
resizePath(path, rect);
}
return path;
}
/**
* Create a image element from image url
* @param imageUrl image url
* @param opts options
* @param rect constrain rect
* @param layout 'center' or 'cover'. Default to be 'cover'
*/
export function makeImage(
imageUrl: string,
rect: ZRRectLike,
layout?: 'center' | 'cover'
) {
const zrImg = new ZRImage({
style: {
image: imageUrl,
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height
},
onload(img) {
if (layout === 'center') {
const boundingRect = {
width: img.width,
height: img.height
};
zrImg.setStyle(centerGraphic(rect, boundingRect));
}
}
});
return zrImg;
}
/**
* Get position of centered element in bounding box.
*
* @param rect element local bounding box
* @param boundingRect constraint bounding box
* @return element position containing x, y, width, and height
*/
function centerGraphic(rect: ZRRectLike, boundingRect: {
width: number
height: number
}): ZRRectLike {
// Set rect to center, keep width / height ratio.
const aspect = boundingRect.width / boundingRect.height;
let width = rect.height * aspect;
let height;
if (width <= rect.width) {
height = rect.height;
}
else {
width = rect.width;
height = width / aspect;
}
const cx = rect.x + rect.width / 2;
const cy = rect.y + rect.height / 2;
return {
x: cx - width / 2,
y: cy - height / 2,
width: width,
height: height
};
}
export const mergePath = pathTool.mergePath;
/**
* Resize a path to fit the rect
* @param path
* @param rect
*/
export function resizePath(path: SVGPath, rect: ZRRectLike): void {
if (!path.applyTransform) {
return;
}
const pathRect = path.getBoundingRect();
const m = pathRect.calculateTransform(rect);
path.applyTransform(m);
}
/**
* Sub pixel optimize line for canvas
*/
export function subPixelOptimizeLine(
shape: {
x1: number, y1: number, x2: number, y2: number
},
lineWidth: number
) {
subPixelOptimizeUtil.subPixelOptimizeLine(shape, shape, {lineWidth});
return shape;
}
/**
* Sub pixel optimize rect for canvas
*/
export function subPixelOptimizeRect(
shape: {
x: number, y: number, width: number, height: number
},
style: {
lineWidth?: number
}
) {
subPixelOptimizeUtil.subPixelOptimizeRect(shape, shape, style);
return shape;
}
/**
* Sub pixel optimize for canvas
*
* @param position Coordinate, such as x, y
* @param lineWidth Should be nonnegative integer.
* @param positiveOrNegative Default false (negative).
* @return Optimized position.
*/
export const subPixelOptimize = subPixelOptimizeUtil.subPixelOptimize;
/**
* Get transform matrix of target (param target),
* in coordinate of its ancestor (param ancestor)
*
* @param target
* @param [ancestor]
*/
export function getTransform(target: Transformable, ancestor?: Transformable): matrix.MatrixArray {
const mat = matrix.identity([]);
while (target && target !== ancestor) {
matrix.mul(mat, target.getLocalTransform(), mat);
target = target.parent;
}
return mat;
}
/**
* Apply transform to an vertex.
* @param target [x, y]
* @param transform Can be:
* + Transform matrix: like [1, 0, 0, 1, 0, 0]
* + {position, rotation, scale}, the same as `zrender/Transformable`.
* @param invert Whether use invert matrix.
* @return [x, y]
*/
export function applyTransform(
target: vector.VectorArray,
transform: Transformable | matrix.MatrixArray,
invert?: boolean
): number[] {
if (transform && !isArrayLike(transform)) {
transform = Transformable.getLocalTransform(transform);
}
if (invert) {
transform = matrix.invert([], transform as matrix.MatrixArray);
}
return vector.applyTransform([], target, transform as matrix.MatrixArray);
}
/**
* @param direction 'left' 'right' 'top' 'bottom'
* @param transform Transform matrix: like [1, 0, 0, 1, 0, 0]
* @param invert Whether use invert matrix.
* @return Transformed direction. 'left' 'right' 'top' 'bottom'
*/
export function transformDirection(
direction: 'left' | 'right' | 'top' | 'bottom',
transform: matrix.MatrixArray,
invert?: boolean
): 'left' | 'right' | 'top' | 'bottom' {
// Pick a base, ensure that transform result will not be (0, 0).
const hBase = (transform[4] === 0 || transform[5] === 0 || transform[0] === 0)
? 1 : mathAbs(2 * transform[4] / transform[0]);
const vBase = (transform[4] === 0 || transform[5] === 0 || transform[2] === 0)
? 1 : mathAbs(2 * transform[4] / transform[2]);
let vertex: vector.VectorArray = [
direction === 'left' ? -hBase : direction === 'right' ? hBase : 0,
direction === 'top' ? -vBase : direction === 'bottom' ? vBase : 0
];
vertex = applyTransform(vertex, transform, invert);
return mathAbs(vertex[0]) > mathAbs(vertex[1])
? (vertex[0] > 0 ? 'right' : 'left')
: (vertex[1] > 0 ? 'bottom' : 'top');
}
function isNotGroup(el: Element): el is Displayable {
return !el.isGroup;
}
function isPath(el: Displayable): el is Path {
return (el as Path).shape != null;
}
/**
* Apply group transition animation from g1 to g2.
* If no animatableModel, no animation.
*/
export function groupTransition(
g1: Group,
g2: Group,
animatableModel: Model<AnimationOptionMixin>
) {
if (!g1 || !g2) {
return;
}
function getElMap(g: Group) {
const elMap: Dictionary<Displayable> = {};
g.traverse(function (el: Element) {
if (isNotGroup(el) && el.anid) {
elMap[el.anid] = el;
}
});
return elMap;
}
function getAnimatableProps(el: Displayable) {
const obj: PathProps = {
x: el.x,
y: el.y,
rotation: el.rotation
};
if (isPath(el)) {
obj.shape = clone(el.shape);
}
return obj;
}
const elMap1 = getElMap(g1);
g2.traverse(function (el) {
if (isNotGroup(el) && el.anid) {
const oldEl = elMap1[el.anid];
if (oldEl) {
const newProp = getAnimatableProps(el);
el.attr(getAnimatableProps(oldEl));
updateProps(el, newProp, animatableModel, getECData(el).dataIndex);
}
}
});
}
export function clipPointsByRect(points: vector.VectorArray[], rect: ZRRectLike): number[][] {
// FIXME: This way might be incorrect when graphic clipped by a corner
// and when element has a border.
return map(points, function (point) {
let x = point[0];
x = mathMax(x, rect.x);
x = mathMin(x, rect.x + rect.width);
let y = point[1];
y = mathMax(y, rect.y);
y = mathMin(y, rect.y + rect.height);
return [x, y];
});
}
/**
* Return a new clipped rect. If rect size are negative, return undefined.
*/
export function clipRectByRect(targetRect: ZRRectLike, rect: ZRRectLike): ZRRectLike | undefined {
const x = mathMax(targetRect.x, rect.x);
const x2 = mathMin(targetRect.x + targetRect.width, rect.x + rect.width);
const y = mathMax(targetRect.y, rect.y);
const y2 = mathMin(targetRect.y + targetRect.height, rect.y + rect.height);
// If the total rect is cliped, nothing, including the border,
// should be painted. So return undefined.
if (x2 >= x && y2 >= y) {
return {
x: x,
y: y,
width: x2 - x,
height: y2 - y
};
}
}
export function createIcon(
iconStr: string, // Support 'image://' or 'path://' or direct svg path.
opt?: Omit<DisplayableProps, 'style'>,
rect?: ZRRectLike
): SVGPath | ZRImage {
const innerOpts: DisplayableProps = extend({rectHover: true}, opt);
const style: ZRStyleProps = innerOpts.style = {strokeNoScale: true};
rect = rect || {x: -1, y: -1, width: 2, height: 2};
if (iconStr) {
return iconStr.indexOf('image://') === 0
? (
(style as ImageStyleProps).image = iconStr.slice(8),
defaults(style, rect),
new ZRImage(innerOpts)
)
: (
makePath(
iconStr.replace('path://', ''),
innerOpts,
rect,
'center'
)
);
}
}
/**
* Return `true` if the given line (line `a`) and the given polygon
* are intersect.
* Note that we do not count colinear as intersect here because no
* requirement for that. We could do that if required in future.
*/
export function linePolygonIntersect(
a1x: number, a1y: number, a2x: number, a2y: number,
points: vector.VectorArray[]
): boolean {
for (let i = 0, p2 = points[points.length - 1]; i < points.length; i++) {
const p = points[i];
if (lineLineIntersect(a1x, a1y, a2x, a2y, p[0], p[1], p2[0], p2[1])) {
return true;
}
p2 = p;
}
}
/**
* Return `true` if the given two lines (line `a` and line `b`)
* are intersect.
* Note that we do not count colinear as intersect here because no
* requirement for that. We could do that if required in future.
*/
export function lineLineIntersect(
a1x: number, a1y: number, a2x: number, a2y: number,
b1x: number, b1y: number, b2x: number, b2y: number
): boolean {
// let `vec_m` to be `vec_a2 - vec_a1` and `vec_n` to be `vec_b2 - vec_b1`.
const mx = a2x - a1x;
const my = a2y - a1y;
const nx = b2x - b1x;
const ny = b2y - b1y;
// `vec_m` and `vec_n` are parallel iff
// existing `k` such that `vec_m = k · vec_n`, equivalent to `vec_m X vec_n = 0`.
const nmCrossProduct = crossProduct2d(nx, ny, mx, my);
if (nearZero(nmCrossProduct)) {
return false;
}
// `vec_m` and `vec_n` are intersect iff
// existing `p` and `q` in [0, 1] such that `vec_a1 + p * vec_m = vec_b1 + q * vec_n`,
// such that `q = ((vec_a1 - vec_b1) X vec_m) / (vec_n X vec_m)`
// and `p = ((vec_a1 - vec_b1) X vec_n) / (vec_n X vec_m)`.
const b1a1x = a1x - b1x;
const b1a1y = a1y - b1y;
const q = crossProduct2d(b1a1x, b1a1y, mx, my) / nmCrossProduct;
if (q < 0 || q > 1) {
return false;
}
const p = crossProduct2d(b1a1x, b1a1y, nx, ny) / nmCrossProduct;
if (p < 0 || p > 1) {
return false;
}
return true;
}
/**
* Cross product of 2-dimension vector.
*/
function crossProduct2d(x1: number, y1: number, x2: number, y2: number) {
return x1 * y2 - x2 * y1;
}
function nearZero(val: number) {
return val <= (1e-6) && val >= -(1e-6);
}
/**
* NOTE:
* A negative-width/height rect (due to negative margins) is not supported;
* it will be clampped to zero width/height.
* Although negative-width/height rects can be defined reasonably following the
* similar sense in CSS, but they are rarely used, hard to understand and complicated.
*
* @param rect Assume its width/height >= 0 if existing.
* x/y/width/height is allowed to be NaN,
* for the case that only x/width or y/height is intended to be computed.
* @param delta
* If be `number[]`, should be `[top, right, bottom, left]`,
* which can be used in padding or margin case.
* @see `normalizeCssArray` in `util/format.ts`
* If be `number`, it means [delta, delta, delta, delta],
* which can be used in lineWidth (borderWith) case,
* [NOTICE]: commonly pass lineWidth / 2, following the convention that border is
* half inside half outside of the rect.
* @param shrinkOrExpand
* `true` - shrink if `delta[i]` is positive, commmonly used in `padding` case.
* `false` - expand if `delta[i]` is positive, commmonly used in `margin` case. (default)
* @param noNegative
* `true` - negative `delta[i]` will be clampped to 0.
* `false` - No clamp to `delta`. (default).
* @return The input `rect`.
*/
export function expandOrShrinkRect<TRect extends RectLike>(
rect: TRect,
delta: number[] | number | NullUndefined,
shrinkOrExpand: boolean,
noNegative: boolean,
minSize?: number[] // by default [0, 0].
): TRect {
if (delta == null) {
return rect;
}
else if (isNumber(delta)) {
_tmpExpandRectDelta[0] = _tmpExpandRectDelta[1] = _tmpExpandRectDelta[2] = _tmpExpandRectDelta[3] = delta;
}
else {
if (__DEV__) {
assert(delta.length === 4);
}
_tmpExpandRectDelta[0] = delta[0];
_tmpExpandRectDelta[1] = delta[1];
_tmpExpandRectDelta[2] = delta[2];
_tmpExpandRectDelta[3] = delta[3];
}
if (noNegative) {
_tmpExpandRectDelta[0] = mathMax(0, _tmpExpandRectDelta[0]);
_tmpExpandRectDelta[1] = mathMax(0, _tmpExpandRectDelta[1]);
_tmpExpandRectDelta[2] = mathMax(0, _tmpExpandRectDelta[2]);
_tmpExpandRectDelta[3] = mathMax(0, _tmpExpandRectDelta[3]);
}
if (shrinkOrExpand) {
_tmpExpandRectDelta[0] = -_tmpExpandRectDelta[0];
_tmpExpandRectDelta[1] = -_tmpExpandRectDelta[1];
_tmpExpandRectDelta[2] = -_tmpExpandRectDelta[2];
_tmpExpandRectDelta[3] = -_tmpExpandRectDelta[3];
}
expandRectOnOneDimension(rect, _tmpExpandRectDelta, 'x', 'width', 3, 1, minSize && minSize[0] || 0);
expandRectOnOneDimension(rect, _tmpExpandRectDelta, 'y', 'height', 0, 2, minSize && minSize[1] || 0);
return rect;
}
const _tmpExpandRectDelta = [0, 0, 0, 0];
function expandRectOnOneDimension(
rect: RectLike,
delta: number[],
xy: 'x' | 'y',
wh: 'width' | 'height',
ltIdx: 3 | 0, rbIdx: 1 | 2,
minSize: number
): void {
const deltaSum = delta[rbIdx] + delta[ltIdx];
const oldSize = rect[wh];
rect[wh] += deltaSum;
minSize = mathMax(0, mathMin(minSize, oldSize));
if (rect[wh] < minSize) {
rect[wh] = minSize;
// Try to make the position of the zero rect reasonable in most visual cases.
rect[xy] += (
delta[ltIdx] >= 0 ? -delta[ltIdx]
: delta[rbIdx] >= 0 ? oldSize + delta[rbIdx]
: mathAbs(deltaSum) > 1e-8 ? (oldSize - minSize) * delta[ltIdx] / deltaSum
: 0
);
}
else {
rect[xy] -= delta[ltIdx];
}
}
export function setTooltipConfig(opt: {
el: Element,
componentModel: ComponentModel,
itemName: string,
itemTooltipOption?: string | CommonTooltipOption<unknown>
formatterParamsExtra?: Dictionary<unknown>
}): void {
const itemTooltipOption = opt.itemTooltipOption;
const componentModel = opt.componentModel;
const itemName = opt.itemName;
const itemTooltipOptionObj = isString(itemTooltipOption)
? { formatter: itemTooltipOption }
: itemTooltipOption;
const mainType = componentModel.mainType;
const componentIndex = componentModel.componentIndex;
const formatterParams = {
componentType: mainType,
name: itemName,
$vars: ['name']
} as ComponentItemTooltipLabelFormatterParams;
(formatterParams as any)[mainType + 'Index'] = componentIndex;
const formatterParamsExtra = opt.formatterParamsExtra;
if (formatterParamsExtra) {
each(keys(formatterParamsExtra), key => {
if (!hasOwn(formatterParams, key)) {
formatterParams[key] = formatterParamsExtra[key];
formatterParams.$vars.push(key);
}
});
}
const ecData = getECData(opt.el);
ecData.componentMainType = mainType;
ecData.componentIndex = componentIndex;
ecData.tooltipConfig = {
name: itemName,
option: defaults({
content: itemName,
encodeHTMLContent: true,
formatterParams: formatterParams
}, itemTooltipOptionObj)
};
}
function traverseElement(el: Element, cb: (el: Element) => boolean | void) {
let stopped;
// TODO
// Polyfill for fixing zrender group traverse don't visit it's root issue.
if (el.isGroup) {
stopped = cb(el);
}
if (!stopped) {
el.traverse(cb);
}
}
export function traverseElements(els: Element | Element[] | undefined | null, cb: (el: Element) => boolean | void) {
if (els) {
if (isArray(els)) {
for (let i = 0; i < els.length; i++) {
traverseElement(els[i], cb);
}
}
else {
traverseElement(els, cb);
}
}
}
/**
* After a boundingRect applying a `transform`, whether to be still parallel screen X and Y.
*/
export function isBoundingRectAxisAligned(transform: matrix.MatrixArray | NullUndefined): boolean {
return !transform
|| (mathAbs(transform[1]) < AXIS_ALIGN_EPSILON && mathAbs(transform[2]) < AXIS_ALIGN_EPSILON)
|| (mathAbs(transform[0]) < AXIS_ALIGN_EPSILON && mathAbs(transform[3]) < AXIS_ALIGN_EPSILON);
}
const AXIS_ALIGN_EPSILON = 1e-5;
/**
* Create or copy to the existing bounding rect to avoid modifying `source`.
*
* @usage
* out.rect = ensureCopyRect(out.rect, sourceRect);
*/
export function ensureCopyRect(
target: BoundingRect | NullUndefined,
source: BoundingRect
): BoundingRect {
return target ? BoundingRect.copy(target, source) : source.clone();
}
/**
* Create or copy to the existing transform to avoid modifying `source`.
*
* [CAUTION]: transform is `NullUndefined` if no transform, following convention of zrender,
* and enable to bypass some unnecessary calculation, since in most cases there is no transform.
*
* @usage
* out.transform = ensureCopyTransform(out.transform, sourceTransform);
*/
export function ensureCopyTransform(
target: matrix.MatrixArray | NullUndefined,
source: matrix.MatrixArray | NullUndefined
): matrix.MatrixArray | NullUndefined {
return source ? matrix.copy(target || matrix.create(), source) : undefined;
}
export function retrieveZInfo(
model: Model<Partial<Pick<ComponentOption, 'z' | 'zlevel'>>>,
): {
z: ComponentOption['z']
zlevel: ComponentOption['zlevel']
} {
return {
z: model.get('z') || 0,
zlevel: model.get('zlevel') || 0,
};
}
/**
* Assume all of the elements has the same `z` and `zlevel`.
*/
export function calcZ2Range(el: Element): {
min: number
max: number
} {
let max = -Infinity;
let min = Infinity;
traverseElement(el, el => {
visitEl(el);
visitEl(el.getTextContent());
visitEl(el.getTextGuideLine());
});
function visitEl(el: Element): void {
if (!el || el.isGroup) {
return;
}
const currentStates = el.currentStates;
if (currentStates.length) {
for (let idx = 0; idx < currentStates.length; idx++) {
calcZ2(el.states[currentStates[idx]] as Displayable);
}
}
calcZ2(el as Displayable);
}
function calcZ2(entity: Pick<Displayable, 'z2'>): void {
if (entity) {
const z2 = entity.z2;
// Consider z2 may be NullUndefined
if (z2 > max) {
max = z2;
}
if (z2 < min) {
min = z2;
}
}
}
if (min > max) {
min = max = 0;
}
return {min, max};
}
export function traverseUpdateZ(
el: Element,
z: number,
zlevel: number,
modelZ2?: number,
): void {
doUpdateZ(el, z, zlevel, modelZ2 != null ? modelZ2 : -Infinity);
}
function doUpdateZ(
el: Element,
z: number,
zlevel: number,
// FIXME: Ideally all the labels should be above all the glyphs by default,
// e.g. in graph, edge labels should be above node elements.
// Currently impl does not guarantee that.
maxZ2: number,
): number {
// `ignoreModelZ` is used to intentionally lift elements to cover other elements,
// where maxZ2 (for label.z2) should also not be counted for its parents.
if ((el as ExtendedElement).ignoreModelZ) {
return maxZ2;
}
// Group may also have textContent
const label = el.getTextContent();
const labelLine = el.getTextGuideLine();
const isGroup = el.isGroup;
if (isGroup) {
// set z & zlevel of children elements of Group
const children = (el as Group).childrenRef();
for (let i = 0; i < children.length; i++) {
maxZ2 = mathMax(
doUpdateZ(
children[i],
z,
zlevel,
maxZ2
),
maxZ2
);
}
}
else {
// not Group
(el as Displayable).z = z;
(el as Displayable).zlevel = zlevel;
maxZ2 = mathMax((el as Displayable).z2 || 0, maxZ2);
}
// NOTICE: Do not call any method that can set REDRAW_BIT,
// otherwise progressive rendering is broken.
// always set z and zlevel if label/labelLine exists
if (label) {
label.z = z;
label.zlevel = zlevel;
// lift z2 of text content
// TODO if el.emphasis.z2 is spcefied, what about textContent.
isFinite(maxZ2) && (label.z2 = maxZ2 + 2);
}
if (labelLine) {
const textGuideLineConfig = el.textGuideLineConfig;
labelLine.z = z;
labelLine.zlevel = zlevel;
isFinite(maxZ2)
&& (labelLine.z2 = maxZ2 + (textGuideLineConfig && textGuideLineConfig.showAbove ? 1 : -1));
}
return maxZ2;
}
export function payloadDisableAnimation<TPayload extends Payload>(
payload: TPayload
): TPayload {
// Disable animation in `updateProps` of `graphic.ts`.
payload.animation = {duration: 0};
return payload;
}
/**
* Decompose an affine matrix to
* x/y/scaleX/scaleY/rotation/skewX/skewY
*/
export function decomposeTransform(
out: Transformable,
mt: matrix.MatrixArray | NullUndefined
): Transformable {
mt
? matrix.copy(tmpDTR.transform, mt)
: matrix.identity(tmpDTR.transform);
// Use a tmp transformable to avoid effects from parent.
tmpDTR.decomposeTransform();
copyTransform(out, tmpDTR);
return out;
}
const tmpDTR = new Transformable();
tmpDTR.transform = matrix.create();
/**
* If not canvas painter, return null/undefined.
*/
export function getCurrentCanvasPainter(api: ExtensionAPI): CanvasPainter | NullUndefined {
const painter = api.getZr().painter;
return painter.getType() === 'canvas' ? (painter as CanvasPainter) : null;
}
// Register built-in shapes. These shapes might be overwritten
// by users, although we do not recommend that.
registerShape('circle', Circle);
registerShape('ellipse', Ellipse);
registerShape('sector', Sector);
registerShape('ring', Ring);
registerShape('polygon', Polygon);
registerShape('polyline', Polyline);
registerShape('rect', Rect);
registerShape('line', Line);
registerShape('bezierCurve', BezierCurve);
registerShape('arc', Arc);
export {
Group,
ZRImage as Image,
ZRText as Text,
Circle,
Ellipse,
Sector,
Ring,
Polygon,
Polyline,
Rect,
Line,
BezierCurve,
Arc,
IncrementalDisplayable,
CompoundPath,
LinearGradient,
RadialGradient,
BoundingRect,
OrientedBoundingRect,
Point,
Path
};