UNPKG

3.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var util_1 = require("@antv/util");
4var base_1 = require("../base");
5var get_path_points_1 = require("../util/get-path-points");
6var get_style_1 = require("../util/get-style");
7var path_1 = require("../util/path");
8var split_points_1 = require("../util/split-points");
9var util_2 = require("./util");
10function getShapeAttrs(cfg, smooth, constraint) {
11 var isStack = cfg.isStack, connectNulls = cfg.connectNulls, isInCircle = cfg.isInCircle, showSinglePoint = cfg.showSinglePoint;
12 var shapeAttrs = get_style_1.getStyle(cfg, true, false, 'lineWidth');
13 var points = get_path_points_1.getPathPoints(cfg.points, connectNulls, showSinglePoint); // 根据 connectNulls 值处理 points
14 var path = [];
15 for (var i = 0, len = points.length; i < len; i++) {
16 var eachLinePoints = points[i];
17 path = path.concat(getPath(eachLinePoints, isInCircle, isStack, smooth, constraint, shapeAttrs));
18 }
19 shapeAttrs.path = path;
20 return shapeAttrs;
21}
22// 单条 path
23function getSinglePath(points, isInCircle, smooth, constraint, style) {
24 if (points.length === 1) {
25 // 只有一个点时
26 return [
27 ['M', points[0].x, points[0].y - style.lineWidth / 2],
28 ['L', points[0].x, points[0].y],
29 ['L', points[0].x, points[0].y + style.lineWidth / 2],
30 ];
31 }
32 var path;
33 if (!smooth) {
34 path = path_1.getLinePath(points, false);
35 if (isInCircle) {
36 path.push(['Z']);
37 }
38 }
39 else {
40 // 直角坐标系下绘制曲线时限制最大值、最小值
41 if (isInCircle && points.length) {
42 points.push({ x: points[0].x, y: points[0].y });
43 }
44 path = path_1.getSplinePath(points, false, constraint);
45 }
46 return path;
47}
48function getRangePath(points, isInCircle, isStack, smooth, constraint, style) {
49 var topPoints = [];
50 var bottomPoints = [];
51 util_1.each(points, function (point) {
52 var result = split_points_1.splitPoints(point);
53 topPoints.push(result[1]); // 上边
54 bottomPoints.push(result[0]); // 底边
55 });
56 var topPath = getSinglePath(topPoints, isInCircle, smooth, constraint, style);
57 var bottomPath = getSinglePath(bottomPoints, isInCircle, smooth, constraint, style);
58 if (isStack) {
59 return topPath;
60 }
61 return topPath.concat(bottomPath);
62}
63function getPath(points, isInCircle, isStack, smooth, constraint, style) {
64 if (points.length) {
65 var first = points[0];
66 return util_1.isArray(first.y)
67 ? getRangePath(points, isInCircle, isStack, smooth, constraint, style)
68 : getSinglePath(points, isInCircle, smooth, constraint, style);
69 }
70 return [];
71}
72var LineShapeFactory = base_1.registerShapeFactory('line', {
73 defaultShapeType: 'line',
74});
75// 这里因为代码公用,所以直接全部注册
76// 'line' 默认折线;'dot' 点线 ···;'dash' 断线 - - -
77util_1.each(['line', 'dot', 'dash', 'smooth'], function (shapeType) {
78 base_1.registerShape('line', shapeType, {
79 draw: function (cfg, container) {
80 var smooth = shapeType === 'smooth';
81 var constraint;
82 if (smooth) {
83 var _a = this.coordinate, start = _a.start, end = _a.end;
84 constraint = [
85 [start.x, end.y],
86 [end.x, start.y],
87 ];
88 }
89 var attrs = getShapeAttrs(cfg, smooth, constraint);
90 var shape = container.addShape({
91 type: 'path',
92 attrs: attrs,
93 name: 'line',
94 capture: !smooth,
95 });
96 return shape;
97 },
98 getMarker: function (markerCfg) {
99 return util_2.getLineMarker(markerCfg, shapeType);
100 },
101 });
102});
103exports.default = LineShapeFactory;
104//# sourceMappingURL=index.js.map
\No newline at end of file