@antv/f2
Version:
Charts for mobile visualization.
178 lines • 5.63 kB
JavaScript
import { __assign, __extends, __spreadArray } from "tslib";
import { jsx } from '@antv/f-engine';
import { isArray, isFunction } from '@antv/util';
import Geometry from '../geometry';
export default (function (View) {
return /** @class */function (_super) {
__extends(Line, _super);
function Line() {
return _super !== null && _super.apply(this, arguments) || this;
}
Line.prototype.getDefaultCfg = function () {
return {
geomType: 'line',
sortable: true
};
};
Line.prototype.splitPoints = function (points) {
var topPoints = [];
var bottomPoints = [];
for (var i = 0, len = points.length; i < len; i++) {
var point = points[i];
var x = point.x,
y = point.y;
topPoints.push(__assign(__assign({}, point), {
x: x,
y: y[1]
}));
bottomPoints.push(__assign(__assign({}, point), {
x: x,
y: y[0]
}));
}
return [topPoints, bottomPoints];
};
Line.prototype.splitNulls = function (points, connectNulls) {
if (connectNulls) {
var tmpPoints_1 = [];
for (var i = 0, len = points.length; i < len; i++) {
var point = points[i];
var x = point.x,
y = point.y;
// 过滤 x 为 NaN 的点
if (isNaN(x)) {
continue;
}
if (isArray(y)) {
if (isNaN(y[0])) {
continue;
}
tmpPoints_1.push(point);
continue;
}
if (isNaN(y)) {
continue;
}
tmpPoints_1.push(point);
}
if (tmpPoints_1.length) {
return [tmpPoints_1];
}
return [];
}
var result = [];
var tmpPoints = [];
for (var i = 0, len = points.length; i < len; i++) {
var point = points[i];
var x = point.x,
y = point.y;
// 过滤 x 为 NaN 的点
if (isNaN(x)) {
continue;
}
if (isArray(y)) {
if (isNaN(y[0])) {
if (tmpPoints.length) {
result.push(tmpPoints);
tmpPoints = [];
}
continue;
}
tmpPoints.push(point);
continue;
}
if (isNaN(y)) {
if (tmpPoints.length) {
result.push(tmpPoints);
tmpPoints = [];
}
continue;
}
tmpPoints.push(point);
}
if (tmpPoints.length) {
result.push(tmpPoints);
}
return result;
};
Line.prototype.mapping = function () {
var _this = this;
var records = _super.prototype.mapping.call(this);
var _a = this,
props = _a.props,
defaultConnectNulls = _a.connectNulls,
context = _a.context;
var coord = props.coord,
_b = props.connectNulls,
connectNulls = _b === void 0 ? defaultConnectNulls : _b,
sizeZoom = props.sizeZoom;
return records.map(function (record) {
var _a;
var children = record.children;
// children 有可能为空
var _b = children[0] || {},
size = _b.size,
color = _b.color,
shape = _b.shape,
y = _b.y,
origin = _b.origin;
// 极坐标时,需加入起点,从而闭合所绘图形
var points = coord.isPolar ? __spreadArray(__spreadArray([], children, true), [children[0]], false) : children;
var sizeZoomRatio = (_a = isFunction(sizeZoom) ? sizeZoom(origin) : sizeZoom) !== null && _a !== void 0 ? _a : 1;
var splitPoints = _this.splitNulls(points, connectNulls);
var newChildren = splitPoints.map(function (points) {
var _a = isArray(y) ? _this.splitPoints(points) : [points, undefined],
topPoints = _a[0],
bottomPoints = _a[1];
return {
size: context.px2hd(size || shape.lineWidth) * sizeZoomRatio,
color: color,
shape: shape,
points: [].concat(topPoints),
topPoints: topPoints,
bottomPoints: bottomPoints
};
});
return __assign(__assign({}, record), {
children: newChildren
});
});
};
Line.prototype.concatPoints = function (topPoints, bottomPoints) {
if (!bottomPoints || !bottomPoints.length) {
return topPoints;
}
var adjust = this.adjust;
// 堆叠产生的 bottomPoints 不绘制
if (adjust && adjust.type === 'stack') {
return topPoints;
}
// 说明是 y 轴对应字段为数组, 这种情况下首尾默认相连,如果想画 2 根线,在数据里对数组分拆
var points = topPoints.concat(bottomPoints.reverse());
points.push(topPoints[0]);
return points;
};
Line.prototype.render = function () {
var props = this.props;
var coord = props.coord;
var records = this.mapping();
var clip = this.getClip();
for (var i = 0, len = records.length; i < len; i++) {
var record = records[i];
var children = record.children;
for (var j = 0, len_1 = children.length; j < len_1; j++) {
var child = children[j];
var points = child.points,
bottomPoints = child.bottomPoints;
child.points = this.concatPoints(points, bottomPoints);
}
}
return jsx(View, __assign({}, props, {
coord: coord,
records: records,
clip: clip
}));
};
return Line;
}(Geometry);
});