UNPKG

3.29 kBTypeScriptView Raw
1import { jsx } from '../../jsx';
2import { isArray } from '@antv/util';
3import Geometry from '../geometry';
4import { LineProps } from './types';
5
6export default (View) => {
7 return class Line extends Geometry<LineProps> {
8 getDefaultCfg() {
9 return {
10 geomType: 'line',
11 sortable: true,
12 };
13 }
14
15 splitPoints(points) {
16 const topPoints = [];
17 const bottomPoints = [];
18 for (let i = 0, len = points.length; i < len; i++) {
19 const point = points[i];
20 const { x, y } = point;
21 topPoints.push({ ...point, x, y: y[1] });
22 bottomPoints.push({ ...point, x, y: y[0] });
23 }
24 return [topPoints, bottomPoints];
25 }
26
27 splitNulls(points, connectNulls) {
28 if (connectNulls) {
29 const tmpPoints = [];
30 for (let i = 0, len = points.length; i < len; i++) {
31 const point = points[i];
32 const { y } = point;
33 if (isArray(y)) {
34 if (isNaN(y[0])) {
35 continue;
36 }
37 tmpPoints.push(point);
38 continue;
39 }
40 if (isNaN(y)) {
41 continue;
42 }
43 tmpPoints.push(point);
44 }
45 if (tmpPoints.length) {
46 return [tmpPoints];
47 }
48 return [];
49 }
50 const result = [];
51 let tmpPoints = [];
52 for (let i = 0, len = points.length; i < len; i++) {
53 const point = points[i];
54 const { y } = point;
55 if (isArray(y)) {
56 if (isNaN(y[0])) {
57 if (tmpPoints.length) {
58 result.push(tmpPoints);
59 tmpPoints = [];
60 }
61 continue;
62 }
63 tmpPoints.push(point);
64 continue;
65 }
66 if (isNaN(y)) {
67 if (tmpPoints.length) {
68 result.push(tmpPoints);
69 tmpPoints = [];
70 }
71 continue;
72 }
73 tmpPoints.push(point);
74 }
75 if (tmpPoints.length) {
76 result.push(tmpPoints);
77 }
78 return result;
79 }
80
81 mapping() {
82 const records = super.mapping();
83 const { props, connectNulls: defaultConnectNulls } = this;
84 const { coord, connectNulls = defaultConnectNulls } = props;
85
86 return records.map((record) => {
87 const { children } = record;
88 // children 有可能为空
89 const { size, color, shape, y } = children[0] || {};
90 // 极坐标时,需加入起点,从而闭合所绘图形
91 const points = coord.isPolar ? [...children, children[0]] : children;
92
93 const splitPoints = this.splitNulls(points, connectNulls);
94
95 const newChildren = splitPoints.map((points) => {
96 const [topPoints, bottomPoints] = isArray(y)
97 ? this.splitPoints(points)
98 : [points, undefined];
99 return {
100 size,
101 color,
102 shape,
103 points: topPoints,
104 bottomPoints,
105 };
106 });
107
108 return {
109 ...record,
110 children: newChildren,
111 };
112 });
113 }
114
115 render() {
116 const { props } = this;
117 const { coord } = props;
118 const records = this.mapping();
119 const clip = this.getClip();
120 return <View {...props} coord={coord} records={records} clip={clip} />;
121 }
122 };
123};