UNPKG

8.42 kBJavaScriptView Raw
1import { __assign, __extends } from "tslib";
2import { jsx, isEqual, Component } from '@antv/f-engine';
3import { deepMix, isFunction, mix, each, clone, isString, isNumber, isArray } from '@antv/util';
4export default (function (View) {
5 return /** @class */function (_super) {
6 __extends(Axis, _super);
7 function Axis(props) {
8 var _this = _super.call(this, props) || this;
9 _this.axisStyle = {};
10 var chart = props.chart,
11 field = props.field;
12 var scaleOption = _this.getScaleOption(props);
13 chart.setScale(field, scaleOption);
14 return _this;
15 }
16 Axis.prototype.willReceiveProps = function (nextProps) {
17 var lastProps = this.props;
18 var chart = nextProps.chart,
19 field = nextProps.field;
20 var nextScaleOption = this.getScaleOption(nextProps);
21 var lastScaleOption = this.getScaleOption(lastProps);
22 if (!isEqual(nextScaleOption, lastScaleOption)) {
23 chart.setScale(field, nextScaleOption);
24 }
25 };
26 Axis.prototype.willMount = function () {
27 this.updateCoord();
28 };
29 Axis.prototype.willUpdate = function () {
30 this.updateCoord();
31 };
32 Axis.prototype.getScaleOption = function (props) {
33 var type = props.type,
34 tickCount = props.tickCount,
35 range = props.range,
36 mask = props.mask,
37 formatter = props.formatter,
38 ticks = props.ticks,
39 min = props.min,
40 max = props.max,
41 nice = props.nice;
42 return {
43 type: type,
44 tickCount: tickCount,
45 range: range,
46 mask: mask,
47 formatter: formatter,
48 min: min,
49 max: max,
50 nice: nice,
51 ticks: ticks
52 };
53 };
54 Axis.prototype._getDimType = function () {
55 var props = this.props;
56 var field = props.field,
57 chart = props.chart;
58 var xScales = chart.getXScales();
59 var scales = xScales.filter(function (scale) {
60 return scale.field === field;
61 });
62 return scales.length > 0 ? 'x' : 'y';
63 };
64 // 获取ticks最大的宽高
65 Axis.prototype.getMaxBBox = function (ticks, style) {
66 var context = this.context;
67 var measureText = context.measureText;
68 var label = style.label,
69 labelOffset = style.labelOffset;
70 var width = 0;
71 var height = 0;
72 ticks.forEach(function (tick) {
73 if (!label) return;
74 var _a = tick.labelStyle,
75 labelStyle = _a === void 0 ? {} : _a,
76 text = tick.text;
77 var bbox = measureText(labelStyle.text || text, __assign(__assign({}, label), labelStyle));
78 width = Math.max(width, bbox.width);
79 height = Math.max(height, bbox.height);
80 });
81 if (!width && !height) {
82 return {
83 width: width,
84 height: height
85 };
86 }
87 var bbox = {
88 width: width + labelOffset,
89 height: height + labelOffset
90 };
91 return bbox;
92 };
93 Axis.prototype._getPosition = function () {
94 var props = this.props;
95 var position = props.position,
96 coord = props.coord;
97 if (position) {
98 return position;
99 }
100 var dimType = this._getDimType();
101 if (coord.transposed) {
102 return dimType === 'x' ? 'left' : 'bottom';
103 }
104 return dimType === 'x' ? 'bottom' : 'left';
105 };
106 Axis.prototype.getTicks = function () {
107 var props = this.props;
108 var field = props.field,
109 chart = props.chart;
110 var scale = chart.getScale(field);
111 var ticks = scale.getTicks();
112 // 设置tick的样式
113 ticks = this._setTicksStyle(ticks);
114 ticks = this._generateGridPoints(ticks);
115 return ticks;
116 };
117 /**
118 * 生成极坐标下网格线的交叉点
119 * @param ticks
120 * @returns
121 */
122 Axis.prototype._generateGridPoints = function (ticks) {
123 var props = this.props;
124 var chart = props.chart,
125 coord = props.coord;
126 if (!coord.isPolar) {
127 return ticks;
128 }
129 var dimType = this._getDimType();
130 // 只需要在 y 的时候生成
131 if (dimType !== 'y') {
132 return ticks;
133 }
134 var xScale = chart.getXScales()[0];
135 var xTicks = xScale.getTicks();
136 ticks.forEach(function (tick) {
137 var gridPoints = xTicks.map(function (xTick) {
138 return coord.convertPoint({
139 x: xTick.value,
140 y: tick.value
141 });
142 });
143 // 添加第 1 个点,形成环状
144 gridPoints.push(gridPoints[0]);
145 tick.gridPoints = gridPoints;
146 });
147 return ticks;
148 };
149 Axis.prototype._setTicksStyle = function (ticks) {
150 var _this = this;
151 var _a = this,
152 props = _a.props,
153 context = _a.context;
154 var theme = context.theme,
155 px2hd = context.px2hd;
156 var _b = props.style,
157 style = _b === void 0 ? {} : _b;
158 var themeAxis = theme.axis;
159 each(themeAxis, function (value, key) {
160 // 关闭tick的样式
161 if (style[key] === null) {
162 return;
163 }
164 var styleValue = isFunction(style[key]) ? undefined : style[key];
165 if (isString(value) || isNumber(value)) {
166 _this.axisStyle[key] = px2hd(styleValue) || value;
167 } else if (isArray(styleValue)) {
168 _this.axisStyle[key] = styleValue.map(function (d) {
169 return px2hd(deepMix(clone(value), d));
170 });
171 } else {
172 _this.axisStyle[key] = px2hd(deepMix(clone(value), styleValue));
173 }
174 });
175 return ticks.map(function (tick, index) {
176 var label = style.label,
177 grid = style.grid;
178 var defaultLabelStyle = themeAxis.label,
179 defaultGridStyle = themeAxis.grid;
180 if (isFunction(label)) {
181 tick.labelStyle = px2hd(mix({}, defaultLabelStyle, label(tick.text, index, ticks)));
182 }
183 if (isFunction(grid)) {
184 tick.gridStyle = px2hd(mix({}, defaultGridStyle, grid(tick.text, index, ticks.length)));
185 }
186 return tick;
187 });
188 };
189 Axis.prototype.convertTicks = function (ticks) {
190 var props = this.props;
191 var coord = props.coord;
192 var dimType = this._getDimType();
193 var otherDim = dimType === 'x' ? 'y' : 'x';
194 return ticks.map(function (tick) {
195 var _a, _b;
196 var start = coord.convertPoint((_a = {}, _a[dimType] = tick.value, _a[otherDim] = 0, _a));
197 var end = coord.convertPoint((_b = {}, _b[dimType] = tick.value, _b[otherDim] = 1, _b));
198 return __assign(__assign({}, tick), {
199 points: [start, end]
200 });
201 });
202 };
203 Axis.prototype.measureLayout = function () {
204 var props = this.props;
205 var visible = props.visible,
206 coord = props.coord;
207 if (visible === false) {
208 return null;
209 }
210 var ticks = this.getTicks();
211 var bbox = this.getMaxBBox(ticks, this.axisStyle);
212 var isPolar = coord.isPolar;
213 var dimType = this._getDimType();
214 var width = bbox.width,
215 height = bbox.height;
216 if (isPolar) {
217 // 机坐标系的 y 不占位置
218 if (dimType === 'y') {
219 return null;
220 }
221 // 4 个方向都需要留空
222 return ['top', 'right', 'bottom', 'left'].map(function (position) {
223 return {
224 position: position,
225 width: width,
226 height: height
227 };
228 });
229 }
230 // 直角坐标系下
231 var position = this._getPosition();
232 return {
233 position: position,
234 width: width,
235 height: height
236 };
237 };
238 // 主要是计算coord的布局
239 Axis.prototype.updateCoord = function () {
240 var props = this.props;
241 var chart = props.chart;
242 var layout = this.measureLayout();
243 chart.updateCoordFor(this, layout);
244 };
245 Axis.prototype.render = function () {
246 var _a = this,
247 props = _a.props,
248 axisStyle = _a.axisStyle;
249 var visible = props.visible,
250 coord = props.coord;
251 if (visible === false) {
252 return null;
253 }
254 var ticks = this.getTicks();
255 var position = this._getPosition();
256 var dimType = this._getDimType();
257 return jsx(View, __assign({}, props, {
258 style: axisStyle,
259 ticks: this.convertTicks(ticks),
260 coord: coord,
261 position: position,
262 dimType: dimType
263 }));
264 };
265 return Axis;
266 }(Component);
267});
\No newline at end of file