UNPKG

5.63 kBJavaScriptView Raw
1import { __assign, __extends, __spreadArray } from "tslib";
2import { jsx } from '@antv/f-engine';
3import { isArray, isFunction } from '@antv/util';
4import Geometry from '../geometry';
5export default (function (View) {
6 return /** @class */function (_super) {
7 __extends(Line, _super);
8 function Line() {
9 return _super !== null && _super.apply(this, arguments) || this;
10 }
11 Line.prototype.getDefaultCfg = function () {
12 return {
13 geomType: 'line',
14 sortable: true
15 };
16 };
17 Line.prototype.splitPoints = function (points) {
18 var topPoints = [];
19 var bottomPoints = [];
20 for (var i = 0, len = points.length; i < len; i++) {
21 var point = points[i];
22 var x = point.x,
23 y = point.y;
24 topPoints.push(__assign(__assign({}, point), {
25 x: x,
26 y: y[1]
27 }));
28 bottomPoints.push(__assign(__assign({}, point), {
29 x: x,
30 y: y[0]
31 }));
32 }
33 return [topPoints, bottomPoints];
34 };
35 Line.prototype.splitNulls = function (points, connectNulls) {
36 if (connectNulls) {
37 var tmpPoints_1 = [];
38 for (var i = 0, len = points.length; i < len; i++) {
39 var point = points[i];
40 var x = point.x,
41 y = point.y;
42 // 过滤 x 为 NaN 的点
43 if (isNaN(x)) {
44 continue;
45 }
46 if (isArray(y)) {
47 if (isNaN(y[0])) {
48 continue;
49 }
50 tmpPoints_1.push(point);
51 continue;
52 }
53 if (isNaN(y)) {
54 continue;
55 }
56 tmpPoints_1.push(point);
57 }
58 if (tmpPoints_1.length) {
59 return [tmpPoints_1];
60 }
61 return [];
62 }
63 var result = [];
64 var tmpPoints = [];
65 for (var i = 0, len = points.length; i < len; i++) {
66 var point = points[i];
67 var x = point.x,
68 y = point.y;
69 // 过滤 x 为 NaN 的点
70 if (isNaN(x)) {
71 continue;
72 }
73 if (isArray(y)) {
74 if (isNaN(y[0])) {
75 if (tmpPoints.length) {
76 result.push(tmpPoints);
77 tmpPoints = [];
78 }
79 continue;
80 }
81 tmpPoints.push(point);
82 continue;
83 }
84 if (isNaN(y)) {
85 if (tmpPoints.length) {
86 result.push(tmpPoints);
87 tmpPoints = [];
88 }
89 continue;
90 }
91 tmpPoints.push(point);
92 }
93 if (tmpPoints.length) {
94 result.push(tmpPoints);
95 }
96 return result;
97 };
98 Line.prototype.mapping = function () {
99 var _this = this;
100 var records = _super.prototype.mapping.call(this);
101 var _a = this,
102 props = _a.props,
103 defaultConnectNulls = _a.connectNulls,
104 context = _a.context;
105 var coord = props.coord,
106 _b = props.connectNulls,
107 connectNulls = _b === void 0 ? defaultConnectNulls : _b,
108 sizeZoom = props.sizeZoom;
109 return records.map(function (record) {
110 var _a;
111 var children = record.children;
112 // children 有可能为空
113 var _b = children[0] || {},
114 size = _b.size,
115 color = _b.color,
116 shape = _b.shape,
117 y = _b.y,
118 origin = _b.origin;
119 // 极坐标时,需加入起点,从而闭合所绘图形
120 var points = coord.isPolar ? __spreadArray(__spreadArray([], children, true), [children[0]], false) : children;
121 var sizeZoomRatio = (_a = isFunction(sizeZoom) ? sizeZoom(origin) : sizeZoom) !== null && _a !== void 0 ? _a : 1;
122 var splitPoints = _this.splitNulls(points, connectNulls);
123 var newChildren = splitPoints.map(function (points) {
124 var _a = isArray(y) ? _this.splitPoints(points) : [points, undefined],
125 topPoints = _a[0],
126 bottomPoints = _a[1];
127 return {
128 size: context.px2hd(size || shape.lineWidth) * sizeZoomRatio,
129 color: color,
130 shape: shape,
131 points: [].concat(topPoints),
132 topPoints: topPoints,
133 bottomPoints: bottomPoints
134 };
135 });
136 return __assign(__assign({}, record), {
137 children: newChildren
138 });
139 });
140 };
141 Line.prototype.concatPoints = function (topPoints, bottomPoints) {
142 if (!bottomPoints || !bottomPoints.length) {
143 return topPoints;
144 }
145 var adjust = this.adjust;
146 // 堆叠产生的 bottomPoints 不绘制
147 if (adjust && adjust.type === 'stack') {
148 return topPoints;
149 }
150 // 说明是 y 轴对应字段为数组, 这种情况下首尾默认相连,如果想画 2 根线,在数据里对数组分拆
151 var points = topPoints.concat(bottomPoints.reverse());
152 points.push(topPoints[0]);
153 return points;
154 };
155 Line.prototype.render = function () {
156 var props = this.props;
157 var coord = props.coord;
158 var records = this.mapping();
159 var clip = this.getClip();
160 for (var i = 0, len = records.length; i < len; i++) {
161 var record = records[i];
162 var children = record.children;
163 for (var j = 0, len_1 = children.length; j < len_1; j++) {
164 var child = children[j];
165 var points = child.points,
166 bottomPoints = child.bottomPoints;
167 child.points = this.concatPoints(points, bottomPoints);
168 }
169 }
170 return jsx(View, __assign({}, props, {
171 coord: coord,
172 records: records,
173 clip: clip
174 }));
175 };
176 return Line;
177 }(Geometry);
178});
\No newline at end of file