UNPKG

1.98 kBJavaScriptView Raw
1import { __assign } from "tslib";
2import { each, isEmpty, isEqual, last } from '@antv/util';
3import { registerShape, registerShapeFactory } from '../base';
4import { getStyle } from '../util/get-style';
5function 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}
28var 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});
42registerShape('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});
64export default PolygonShapeFactory;
65//# sourceMappingURL=index.js.map
\No newline at end of file