@antv/f2
Version:
Charts for mobile visualization.
309 lines (308 loc) • 10.7 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var _util = require("@antv/util");
var _jsx = require("../../jsx");
var _equal = _interopRequireDefault(require("../../base/equal"));
var _component = _interopRequireDefault(require("../../base/component"));
var _default = function _default(View) {
return /*#__PURE__*/function (_Component) {
(0, _inherits2.default)(Axis, _Component);
var _super = (0, _createSuper2.default)(Axis);
function Axis(props) {
var _this;
(0, _classCallCheck2.default)(this, Axis);
_this = _super.call(this, props);
_this.style = {};
var _this$props = _this.props,
chart = _this$props.chart,
field = _this$props.field;
var scaleOption = _this.getScaleOption(props);
chart.setScale(field, scaleOption);
return _this;
}
(0, _createClass2.default)(Axis, [{
key: "willReceiveProps",
value: function willReceiveProps(nextProps) {
var lastProps = this.props;
var chart = nextProps.chart,
field = nextProps.field;
var nextScaleOption = this.getScaleOption(nextProps);
var lastScaleOption = this.getScaleOption(lastProps);
if (!(0, _equal.default)(nextScaleOption, lastScaleOption)) {
chart.setScale(field, nextScaleOption);
}
}
}, {
key: "willMount",
value: function willMount() {
this.updateCoord();
}
}, {
key: "willUpdate",
value: function willUpdate() {
this.updateCoord();
}
}, {
key: "getScaleOption",
value: function getScaleOption(props) {
var type = props.type,
tickCount = props.tickCount,
range = props.range,
mask = props.mask,
formatter = props.formatter,
ticks = props.ticks,
min = props.min,
max = props.max,
nice = props.nice;
return {
type: type,
tickCount: tickCount,
range: range,
mask: mask,
formatter: formatter,
min: min,
max: max,
nice: nice,
ticks: ticks
};
}
}, {
key: "_getDimType",
value: function _getDimType() {
var props = this.props;
var field = props.field,
chart = props.chart;
var xScales = chart.getXScales();
var scales = xScales.filter(function (scale) {
return scale.field === field;
});
return scales.length > 0 ? 'x' : 'y';
}
// 获取ticks最大的宽高
}, {
key: "getMaxBBox",
value: function getMaxBBox(ticks, style) {
var context = this.context;
var measureText = context.measureText;
var label = style.label,
labelOffset = style.labelOffset;
var width = 0;
var height = 0;
ticks.forEach(function (tick) {
if (!label) return;
var _tick$labelStyle = tick.labelStyle,
labelStyle = _tick$labelStyle === void 0 ? {} : _tick$labelStyle,
text = tick.text;
var bbox = measureText(labelStyle.text || text, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, label), labelStyle));
width = Math.max(width, bbox.width);
height = Math.max(height, bbox.height);
});
if (!width && !height) {
return {
width: width,
height: height
};
}
var bbox = {
width: width + labelOffset,
height: height + labelOffset
};
return bbox;
}
}, {
key: "_getPosition",
value: function _getPosition() {
var props = this.props;
var position = props.position,
coord = props.coord;
if (position) {
return position;
}
var dimType = this._getDimType();
if (coord.transposed) {
return dimType === 'x' ? 'left' : 'bottom';
}
return dimType === 'x' ? 'bottom' : 'left';
}
}, {
key: "getTicks",
value: function getTicks() {
var props = this.props;
var field = props.field,
chart = props.chart;
var scale = chart.getScale(field);
var ticks = scale.getTicks();
// 设置tick的样式
ticks = this._setTicksStyle(ticks);
ticks = this._generateGridPoints(ticks);
return ticks;
}
/**
* 生成极坐标下网格线的交叉点
* @param ticks
* @returns
*/
}, {
key: "_generateGridPoints",
value: function _generateGridPoints(ticks) {
var props = this.props;
var chart = props.chart,
coord = props.coord;
if (!coord.isPolar) {
return ticks;
}
var dimType = this._getDimType();
// 只需要在 y 的时候生成
if (dimType !== 'y') {
return ticks;
}
var xScale = chart.getXScales()[0];
var xTicks = xScale.getTicks();
ticks.forEach(function (tick) {
var gridPoints = xTicks.map(function (xTick) {
return coord.convertPoint({
x: xTick.value,
y: tick.value
});
});
// 添加第 1 个点,形成环状
gridPoints.push(gridPoints[0]);
tick.gridPoints = gridPoints;
});
return ticks;
}
}, {
key: "_setTicksStyle",
value: function _setTicksStyle(ticks) {
var _this2 = this;
var props = this.props,
context = this.context;
var theme = context.theme,
px2hd = context.px2hd;
var _props$style = props.style,
style = _props$style === void 0 ? {} : _props$style;
var themeAxis = theme.axis;
(0, _util.each)(themeAxis, function (value, key) {
// 关闭tick的样式
if (style[key] === null) {
return;
}
var styleValue = (0, _util.isFunction)(style[key]) ? undefined : style[key];
if ((0, _util.isString)(value) || (0, _util.isNumber)(value)) {
_this2.style[key] = px2hd(styleValue) || value;
} else {
_this2.style[key] = px2hd((0, _util.deepMix)((0, _util.clone)(value), styleValue));
}
});
return ticks.map(function (tick, index) {
var label = style.label,
grid = style.grid;
var defaultLabelStyle = themeAxis.label,
defaultGridStyle = themeAxis.grid;
if ((0, _util.isFunction)(label)) {
tick.labelStyle = px2hd((0, _util.mix)({}, defaultLabelStyle, label(tick.text, index, ticks)));
}
if ((0, _util.isFunction)(grid)) {
tick.gridStyle = px2hd((0, _util.mix)({}, defaultGridStyle, grid(tick.text, index, ticks.length)));
}
return tick;
});
}
}, {
key: "convertTicks",
value: function convertTicks(ticks) {
var props = this.props;
var coord = props.coord;
var dimType = this._getDimType();
var otherDim = dimType === 'x' ? 'y' : 'x';
return ticks.map(function (tick) {
var _coord$convertPoint, _coord$convertPoint2;
var start = coord.convertPoint((_coord$convertPoint = {}, (0, _defineProperty2.default)(_coord$convertPoint, dimType, tick.value), (0, _defineProperty2.default)(_coord$convertPoint, otherDim, 0), _coord$convertPoint));
var end = coord.convertPoint((_coord$convertPoint2 = {}, (0, _defineProperty2.default)(_coord$convertPoint2, dimType, tick.value), (0, _defineProperty2.default)(_coord$convertPoint2, otherDim, 1), _coord$convertPoint2));
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, tick), {}, {
points: [start, end]
});
});
}
}, {
key: "measureLayout",
value: function measureLayout() {
var props = this.props;
var visible = props.visible,
coord = props.coord;
if (visible === false) {
return null;
}
var ticks = this.getTicks();
var bbox = this.getMaxBBox(ticks, this.style);
var isPolar = coord.isPolar;
var dimType = this._getDimType();
var width = bbox.width,
height = bbox.height;
if (isPolar) {
// 机坐标系的 y 不占位置
if (dimType === 'y') {
return null;
}
// 4 个方向都需要留空
return ['top', 'right', 'bottom', 'left'].map(function (position) {
return {
position: position,
width: width,
height: height
};
});
}
// 直角坐标系下
var position = this._getPosition();
return {
position: position,
width: width,
height: height
};
}
// 主要是计算coord的布局
}, {
key: "updateCoord",
value: function updateCoord() {
var props = this.props;
var chart = props.chart;
var layout = this.measureLayout();
chart.updateCoordFor(this, layout);
}
}, {
key: "render",
value: function render() {
var props = this.props,
style = this.style;
var visible = props.visible,
coord = props.coord;
if (visible === false) {
return null;
}
var ticks = this.getTicks();
var position = this._getPosition();
var dimType = this._getDimType();
return (0, _jsx.jsx)(View, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
style: style,
ticks: this.convertTicks(ticks),
coord: coord,
position: position,
dimType: dimType
}));
}
}]);
return Axis;
}(_component.default);
};
exports.default = _default;