@antv/f2
Version:
Charts for mobile visualization.
163 lines (162 loc) • 5.32 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _get from "@babel/runtime/helpers/esm/get";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import { deepMix, isFunction, isNil, mix } from '@antv/util';
import { jsx } from '../../jsx';
import Geometry from '../geometry';
import * as LabelViews from './label';
export default (function (Views) {
return /*#__PURE__*/function (_Geometry) {
_inherits(Interval, _Geometry);
var _super = _createSuper(Interval);
function Interval() {
_classCallCheck(this, Interval);
return _super.apply(this, arguments);
}
_createClass(Interval, [{
key: "getDefaultCfg",
value: function getDefaultCfg() {
return {
geomType: 'interval',
justifyContent: true,
startOnZero: true
};
}
}, {
key: "getDefaultSize",
value: function getDefaultSize() {
var attrs = this.attrs,
props = this.props,
adjust = this.adjust,
records = this.records;
var coord = props.coord,
sizeRatio = props.sizeRatio;
var x = attrs.x;
var scale = x.scale;
var values = scale.values;
if (sizeRatio) {
return 1 / values.length * sizeRatio;
}
var defaultWithRatio = {
column: 1 / 2,
rose: 0.999999,
multiplePie: 3 / 4 // 多饼图
};
var count = values.length;
var ratio;
if (coord.isPolar) {
if (coord.transposed && count > 1) {
ratio = defaultWithRatio.multiplePie;
} else {
ratio = defaultWithRatio.rose;
}
} else {
ratio = defaultWithRatio.column;
}
var size = 1 / values.length * ratio;
// 分组时size要除以类别个数
if (adjust && adjust.type === 'dodge') {
return size / records.length;
}
return size;
}
}, {
key: "mapping",
value: function mapping() {
var records = _get(_getPrototypeOf(Interval.prototype), "mapping", this).call(this);
var props = this.props;
var coord = props.coord;
var y0 = this.getY0Value();
var defaultSize = this.getDefaultSize();
for (var i = 0, len = records.length; i < len; i++) {
var record = records[i];
var children = record.children;
for (var j = 0, _len = children.length; j < _len; j++) {
var child = children[j];
var normalized = child.normalized,
mappedSize = child.size;
// 没有指定size,则根据数据来计算默认size
if (isNil(mappedSize)) {
var x = normalized.x,
y = normalized.y,
_normalized$size = normalized.size,
size = _normalized$size === void 0 ? defaultSize : _normalized$size;
mix(child, coord.convertRect({
x: x,
y: y,
y0: y0,
size: size
}));
} else {
var _x = child.x,
_y = child.y;
var rect = {
size: mappedSize,
x: _x,
y: _y,
y0: y0
};
mix(child, coord.transformToRect(rect));
}
mix(child.shape, this.getSelectionStyle(child));
}
}
return records;
}
// 获取Y轴坐标零点的画布位置
}, {
key: "getPointY0",
value: function getPointY0() {
var props = this.props;
var coord = props.coord;
var y0 = this.getY0Value();
var y0Point = coord.convertPoint({
y: y0,
x: 0
});
return y0Point === null || y0Point === void 0 ? void 0 : y0Point.y;
}
}, {
key: "render",
value: function render() {
var props = this.props,
state = this.state,
container = this.container;
var coord = props.coord,
_props$shape = props.shape,
shape = _props$shape === void 0 ? 'rect' : _props$shape,
animation = props.animation,
showLabel = props.showLabel,
customLabelCfg = props.labelCfg;
var View = isFunction(Views) ? Views : Views[shape];
var LabelView = LabelViews[shape];
var labelCfg = deepMix({
label: null,
offsetX: 0,
offsetY: 0
}, customLabelCfg);
if (!View) return null;
var selected = state.selected;
var records = this.mapping();
var pointY0 = this.getPointY0();
var clip = this.getClip();
return jsx(View, {
coord: coord,
records: records,
selected: selected,
shape: shape,
animation: animation,
showLabel: showLabel,
labelCfg: labelCfg,
LabelView: LabelView,
y0: pointY0,
clip: clip
});
}
}]);
return Interval;
}(Geometry);
});