1 | import { __assign } from "tslib";
|
2 | import { each, isEmpty, isEqual, last } from '@antv/util';
|
3 | import { registerShape, registerShapeFactory } from '../base';
|
4 | import { getStyle } from '../util/get-style';
|
5 | function getPath(points) {
|
6 | var flag = points[0];
|
7 | var i = 1;
|
8 | var path = [['M', flag.x, flag.y]];
|
9 | while (i < points.length) {
|
10 | var c = points[i];
|
11 | if (c.x !== points[i - 1].x || c.y !== points[i - 1].y) {
|
12 | path.push(['L', c.x, c.y]);
|
13 | if (c.x === flag.x && c.y === flag.y && i < points.length - 1) {
|
14 | flag = points[i + 1];
|
15 | path.push(['Z']);
|
16 | path.push(['M', flag.x, flag.y]);
|
17 | i++;
|
18 | }
|
19 | }
|
20 | i++;
|
21 | }
|
22 | if (!isEqual(last(path), flag)) {
|
23 | path.push(['L', flag.x, flag.y]);
|
24 | }
|
25 | path.push(['Z']);
|
26 | return path;
|
27 | }
|
28 | var PolygonShapeFactory = registerShapeFactory('polygon', {
|
29 | defaultShapeType: 'polygon',
|
30 | getDefaultPoints: function (pointInfo) {
|
31 | var points = [];
|
32 | each(pointInfo.x, function (subX, index) {
|
33 | var subY = pointInfo.y[index];
|
34 | points.push({
|
35 | x: subX,
|
36 | y: subY,
|
37 | });
|
38 | });
|
39 | return points;
|
40 | },
|
41 | });
|
42 | registerShape('polygon', 'polygon', {
|
43 | draw: function (cfg, container) {
|
44 | if (!isEmpty(cfg.points)) {
|
45 | var shapeAttrs = getStyle(cfg, true, true);
|
46 | var path = this.parsePath(getPath(cfg.points));
|
47 | return container.addShape('path', {
|
48 | attrs: __assign(__assign({}, shapeAttrs), { path: path }),
|
49 | name: 'polygon',
|
50 | });
|
51 | }
|
52 | },
|
53 | getMarker: function (markerCfg) {
|
54 | var color = markerCfg.color;
|
55 | return {
|
56 | symbol: 'square',
|
57 | style: {
|
58 | r: 4,
|
59 | fill: color,
|
60 | },
|
61 | };
|
62 | },
|
63 | });
|
64 | export default PolygonShapeFactory;
|
65 |
|
\ | No newline at end of file |