UNPKG

5.34 kBJavaScriptView Raw
1import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2import _createClass from "@babel/runtime/helpers/esm/createClass";
3import _get from "@babel/runtime/helpers/esm/get";
4import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
5import _inherits from "@babel/runtime/helpers/esm/inherits";
6import _createSuper from "@babel/runtime/helpers/esm/createSuper";
7import { deepMix, isFunction, isNil, mix } from '@antv/util';
8import { jsx } from '../../jsx';
9import Geometry from '../geometry';
10import * as LabelViews from './label';
11export default (function (Views) {
12 return /*#__PURE__*/function (_Geometry) {
13 _inherits(Interval, _Geometry);
14
15 var _super = _createSuper(Interval);
16
17 function Interval() {
18 _classCallCheck(this, Interval);
19
20 return _super.apply(this, arguments);
21 }
22
23 _createClass(Interval, [{
24 key: "getDefaultCfg",
25 value: function getDefaultCfg() {
26 return {
27 geomType: 'interval',
28 justifyContent: true,
29 startOnZero: true
30 };
31 }
32 }, {
33 key: "getDefaultSize",
34 value: function getDefaultSize() {
35 var attrs = this.attrs,
36 props = this.props,
37 adjust = this.adjust,
38 records = this.records;
39 var coord = props.coord,
40 sizeRatio = props.sizeRatio;
41 var x = attrs.x;
42 var scale = x.scale;
43 var values = scale.values;
44
45 if (sizeRatio) {
46 return 1 / values.length * sizeRatio;
47 }
48
49 var defaultWithRatio = {
50 column: 1 / 2,
51 rose: 0.999999,
52 multiplePie: 3 / 4 // 多饼图
53
54 };
55 var count = values.length;
56 var ratio;
57
58 if (coord.isPolar) {
59 if (coord.transposed && count > 1) {
60 ratio = defaultWithRatio.multiplePie;
61 } else {
62 ratio = defaultWithRatio.rose;
63 }
64 } else {
65 ratio = defaultWithRatio.column;
66 }
67
68 var size = 1 / values.length * ratio; // 分组时size要除以类别个数
69
70 if (adjust && adjust.type === 'dodge') {
71 return size / records.length;
72 }
73
74 return size;
75 }
76 }, {
77 key: "mapping",
78 value: function mapping() {
79 var records = _get(_getPrototypeOf(Interval.prototype), "mapping", this).call(this);
80
81 var props = this.props;
82 var coord = props.coord;
83 var y0 = this.getY0Value();
84 var defaultSize = this.getDefaultSize();
85
86 for (var i = 0, len = records.length; i < len; i++) {
87 var record = records[i];
88 var children = record.children;
89
90 for (var j = 0, _len = children.length; j < _len; j++) {
91 var child = children[j];
92 var normalized = child.normalized,
93 mappedSize = child.size; // 没有指定size,则根据数据来计算默认size
94
95 if (isNil(mappedSize)) {
96 var x = normalized.x,
97 y = normalized.y,
98 _normalized$size = normalized.size,
99 size = _normalized$size === void 0 ? defaultSize : _normalized$size;
100 mix(child, coord.convertRect({
101 x: x,
102 y: y,
103 y0: y0,
104 size: size
105 }));
106 } else {
107 var _x = child.x,
108 _y = child.y;
109 var rect = {
110 size: mappedSize,
111 x: _x,
112 y: _y,
113 y0: y0
114 };
115 mix(child, coord.transformToRect(rect));
116 }
117
118 mix(child.shape, this.getSelectionStyle(child));
119 }
120 }
121
122 return records;
123 } // 获取Y轴坐标零点的画布位置
124
125 }, {
126 key: "getPointY0",
127 value: function getPointY0() {
128 var props = this.props;
129 var coord = props.coord;
130 var y0 = this.getY0Value();
131 var y0Point = coord.convertPoint({
132 y: y0,
133 x: 0
134 });
135 return y0Point === null || y0Point === void 0 ? void 0 : y0Point.y;
136 }
137 }, {
138 key: "render",
139 value: function render() {
140 var props = this.props,
141 state = this.state,
142 container = this.container;
143 var coord = props.coord,
144 _props$shape = props.shape,
145 shape = _props$shape === void 0 ? 'rect' : _props$shape,
146 animation = props.animation,
147 showLabel = props.showLabel,
148 customLabelCfg = props.labelCfg;
149 var View = isFunction(Views) ? Views : Views[shape];
150 var LabelView = LabelViews[shape];
151 var labelCfg = deepMix({
152 label: null,
153 offsetX: 0,
154 offsetY: 0
155 }, customLabelCfg);
156 if (!View) return null;
157 var selected = state.selected;
158 var records = this.mapping();
159 var pointY0 = this.getPointY0();
160 var clip = this.getClip();
161 return jsx(View, {
162 coord: coord,
163 records: records,
164 selected: selected,
165 shape: shape,
166 animation: animation,
167 showLabel: showLabel,
168 labelCfg: labelCfg,
169 LabelView: LabelView,
170 y0: pointY0,
171 clip: clip
172 });
173 }
174 }]);
175
176 return Interval;
177 }(Geometry);
178});
\No newline at end of file