1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __assign = (this && this.__assign) || function () {
|
16 | __assign = Object.assign || function(t) {
|
17 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
18 | s = arguments[i];
|
19 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
20 | t[p] = s[p];
|
21 | }
|
22 | return t;
|
23 | };
|
24 | return __assign.apply(this, arguments);
|
25 | };
|
26 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
27 | if (k2 === undefined) k2 = k;
|
28 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
29 | }) : (function(o, m, k, k2) {
|
30 | if (k2 === undefined) k2 = k;
|
31 | o[k2] = m[k];
|
32 | }));
|
33 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
34 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
35 | }) : function(o, v) {
|
36 | o["default"] = v;
|
37 | });
|
38 | var __importStar = (this && this.__importStar) || function (mod) {
|
39 | if (mod && mod.__esModule) return mod;
|
40 | var result = {};
|
41 | if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
42 | __setModuleDefault(result, mod);
|
43 | return result;
|
44 | };
|
45 | var __rest = (this && this.__rest) || function (s, e) {
|
46 | var t = {};
|
47 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
48 | t[p] = s[p];
|
49 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
50 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
51 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
52 | t[p[i]] = s[p[i]];
|
53 | }
|
54 | return t;
|
55 | };
|
56 | var __spreadArrays = (this && this.__spreadArrays) || function () {
|
57 | for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
58 | for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
59 | for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
60 | r[k] = a[j];
|
61 | return r;
|
62 | };
|
63 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
64 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
65 | };
|
66 | Object.defineProperty(exports, "__esModule", { value: true });
|
67 | var react_1 = __importStar(require("react"));
|
68 | var unique_id_1 = __importDefault(require("@antv/util/lib/unique-id"));
|
69 | var is_function_1 = __importDefault(require("@antv/util/lib/is-function"));
|
70 | var withContainer_1 = __importDefault(require("./boundary/withContainer"));
|
71 | var ErrorBoundary_1 = __importStar(require("./boundary/ErrorBoundary"));
|
72 | var root_1 = __importDefault(require("./context/root"));
|
73 | var view_1 = __importDefault(require("./context/view"));
|
74 | var plotTools_1 = require("./utils/plotTools");
|
75 | var shallowEqual_1 = __importDefault(require("./utils/shallowEqual"));
|
76 | var pickWithout_1 = __importDefault(require("./utils/pickWithout"));
|
77 | var cloneDeep_1 = __importDefault(require("./utils/cloneDeep"));
|
78 | var constant_1 = require("./utils/constant");
|
79 | var resize_observer_1 = require("@juggle/resize-observer");
|
80 | var getElementSize_1 = __importDefault(require("./utils/getElementSize"));
|
81 | var polyfill_1 = require("./plots/core/polyfill");
|
82 | var util_1 = require("@antv/util");
|
83 | var warning_1 = __importDefault(require("warning"));
|
84 |
|
85 | var locale_1 = require("@antv/g2plot/lib/core/locale");
|
86 | var en_US_1 = require("@antv/g2plot/lib/locales/en_US");
|
87 | var zh_CN_1 = require("@antv/g2plot/lib/locales/zh_CN");
|
88 |
|
89 | locale_1.registerLocale('en-US', en_US_1.EN_US_LOCALE);
|
90 | locale_1.registerLocale('zh-CN', zh_CN_1.ZH_CN_LOCALE);
|
91 | var DEFAULT_PLACEHOLDER = (react_1.default.createElement("div", { style: { position: 'absolute', top: '48%', left: '50%', color: '#aaa', textAlign: 'center' } }, "\u6682\u65E0\u6570\u636E"));
|
92 | var DESCRIPTION_STYLE = {
|
93 | padding: '8px 24px 10px 10px',
|
94 | fontFamily: 'PingFang SC',
|
95 | fontSize: 12,
|
96 | color: 'grey',
|
97 | textAlign: 'left',
|
98 | lineHeight: '16px',
|
99 | };
|
100 | var TITLE_STYLE = {
|
101 | padding: '10px 0 0 10px',
|
102 | fontFamily: 'PingFang SC',
|
103 | fontSize: 18,
|
104 | color: 'black',
|
105 | textAlign: 'left',
|
106 | lineHeight: '20px',
|
107 | };
|
108 | var BasePlot = (function (_super) {
|
109 | __extends(BasePlot, _super);
|
110 | function BasePlot() {
|
111 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
112 | _this._context = { chart: null };
|
113 | return _this;
|
114 | }
|
115 | BasePlot.prototype.componentDidMount = function () {
|
116 | if (this.props.children && this.g2Instance.chart) {
|
117 | this.g2Instance.chart.render();
|
118 | }
|
119 | polyfill_1.polyfillEvents(this.g2Instance, {}, this.props);
|
120 | this.g2Instance.data = this.props.data;
|
121 | this.preConfig = pickWithout_1.default(this.props, __spreadArrays(constant_1.REACT_PIVATE_PROPS, [
|
122 | 'container',
|
123 | 'PlotClass',
|
124 | 'onGetG2Instance',
|
125 | 'data',
|
126 | ]));
|
127 | };
|
128 | BasePlot.prototype.componentDidUpdate = function (prevProps) {
|
129 | if (this.props.children && this.g2Instance.chart) {
|
130 | this.g2Instance.chart.render();
|
131 | }
|
132 |
|
133 | polyfill_1.polyfillEvents(this.g2Instance, prevProps, this.props);
|
134 | };
|
135 | BasePlot.prototype.componentWillUnmount = function () {
|
136 | var _this = this;
|
137 | if (this.g2Instance) {
|
138 | setTimeout(function () {
|
139 | _this.g2Instance.destroy();
|
140 | _this.g2Instance = null;
|
141 | _this._context.chart = null;
|
142 | }, 0);
|
143 | }
|
144 | };
|
145 | BasePlot.prototype.getG2Instance = function () {
|
146 | return this.g2Instance;
|
147 | };
|
148 | BasePlot.prototype.getChartView = function () {
|
149 | return this.g2Instance.chart;
|
150 | };
|
151 | BasePlot.prototype.checkInstanceReady = function () {
|
152 | if (!this.g2Instance) {
|
153 | this.initInstance();
|
154 | this.g2Instance.render();
|
155 | }
|
156 | else if (this.shouldReCreate()) {
|
157 |
|
158 | this.g2Instance.destroy();
|
159 | this.initInstance();
|
160 | this.g2Instance.render();
|
161 | }
|
162 | else if (this.diffConfig()) {
|
163 | var options = pickWithout_1.default(this.props, [
|
164 | 'container',
|
165 | 'PlotClass',
|
166 | 'onGetG2Instance',
|
167 | 'children',
|
168 | ]);
|
169 | this.g2Instance.update(options);
|
170 | }
|
171 | else if (this.diffData()) {
|
172 | this.g2Instance.changeData(this.props.data);
|
173 | }
|
174 |
|
175 | var currentConfig = pickWithout_1.default(this.props, __spreadArrays(constant_1.REACT_PIVATE_PROPS, [
|
176 | 'container',
|
177 | 'PlotClass',
|
178 | 'onGetG2Instance',
|
179 | 'data',
|
180 | ]));
|
181 | this.preConfig = cloneDeep_1.default(currentConfig);
|
182 | this.g2Instance.data = this.props.data;
|
183 | };
|
184 | BasePlot.prototype.initInstance = function () {
|
185 | var _a = this.props, container = _a.container, PlotClass = _a.PlotClass, onGetG2Instance = _a.onGetG2Instance, children = _a.children, options = __rest(_a, ["container", "PlotClass", "onGetG2Instance", "children"]);
|
186 | this.g2Instance = new PlotClass(container, options);
|
187 | this._context.chart = this.g2Instance;
|
188 | if (is_function_1.default(onGetG2Instance)) {
|
189 | onGetG2Instance(this.g2Instance);
|
190 | }
|
191 | };
|
192 | BasePlot.prototype.diffConfig = function () {
|
193 |
|
194 | var preConfig = this.preConfig || {};
|
195 | var currentConfig = pickWithout_1.default(this.props, __spreadArrays(constant_1.REACT_PIVATE_PROPS, [
|
196 | 'container',
|
197 | 'PlotClass',
|
198 | 'onGetG2Instance',
|
199 | 'data',
|
200 | ]));
|
201 | return !shallowEqual_1.default(preConfig, currentConfig);
|
202 | };
|
203 | BasePlot.prototype.diffData = function () {
|
204 |
|
205 | var preData = this.g2Instance.data;
|
206 | var data = this.props.data;
|
207 | if (!util_1.isArray(preData) || !util_1.isArray(data)) {
|
208 |
|
209 | return !preData === data;
|
210 | }
|
211 | if (preData.length !== data.length) {
|
212 | return true;
|
213 | }
|
214 | var isEqual = true;
|
215 | preData.forEach(function (element, index) {
|
216 | if (!shallowEqual_1.default(element, data[index])) {
|
217 | isEqual = false;
|
218 | }
|
219 | });
|
220 | return !isEqual;
|
221 | };
|
222 | BasePlot.prototype.shouldReCreate = function () {
|
223 | var forceUpdate = this.props.forceUpdate;
|
224 | if (forceUpdate) {
|
225 | return true;
|
226 | }
|
227 | return false;
|
228 | };
|
229 | BasePlot.prototype.render = function () {
|
230 | this.checkInstanceReady();
|
231 | var chartView = this.getChartView();
|
232 | return (react_1.default.createElement(root_1.default.Provider, { value: this._context },
|
233 | react_1.default.createElement(view_1.default.Provider, { value: chartView },
|
234 | react_1.default.createElement("div", { key: unique_id_1.default('plot-chart') }, this.props.children))));
|
235 | };
|
236 | return BasePlot;
|
237 | }(react_1.default.Component));
|
238 | var BxPlot = withContainer_1.default(BasePlot);
|
239 | function createPlot(PlotClass, name, transCfg) {
|
240 | if (transCfg === void 0) { transCfg = function (cfg) { return cfg; }; }
|
241 | var Com = react_1.default.forwardRef(function (props, ref) {
|
242 |
|
243 | var title = props.title, description = props.description, _a = props.autoFit, autoFit = _a === void 0 ? true : _a, forceFit = props.forceFit, _b = props.errorContent, errorContent = _b === void 0 ? ErrorBoundary_1.ErrorFallback : _b, containerStyle = props.containerStyle, containerProps = props.containerProps, placeholder = props.placeholder, ErrorBoundaryProps = props.ErrorBoundaryProps, isMaterial = props.isMaterial, cfg = __rest(props, ["title", "description", "autoFit", "forceFit", "errorContent", "containerStyle", "containerProps", "placeholder", "ErrorBoundaryProps", "isMaterial"]);
|
244 | var realCfg = transCfg(cfg);
|
245 | var container = react_1.useRef();
|
246 | var titleDom = react_1.useRef();
|
247 | var descDom = react_1.useRef();
|
248 | var _c = react_1.useState(0), chartHeight = _c[0], setChartHeight = _c[1];
|
249 | var resizeObserver = react_1.useRef();
|
250 | var resizeFn = react_1.useCallback(function () {
|
251 | if (!container.current) {
|
252 | return;
|
253 | }
|
254 | var containerSize = getElementSize_1.default(container.current, props);
|
255 | var titleSize = titleDom.current ? getElementSize_1.default(titleDom.current) : { width: 0, height: 0 };
|
256 | var descSize = descDom.current ? getElementSize_1.default(descDom.current) : { width: 0, height: 0 };
|
257 | var ch = (containerSize.height - titleSize.height - descSize.height);
|
258 | if (ch === 0) {
|
259 |
|
260 | ch = 350;
|
261 | }
|
262 | if (ch < 20) {
|
263 |
|
264 | ch = 20;
|
265 | }
|
266 |
|
267 | if (Math.abs(chartHeight - ch) > 1) {
|
268 | setChartHeight(ch);
|
269 | }
|
270 | }, [container.current, titleDom.current, chartHeight, descDom.current]);
|
271 | var resize = react_1.useCallback(util_1.debounce(resizeFn, 500), [resizeFn]);
|
272 | var FallbackComponent = react_1.default.isValidElement(errorContent) ? function () { return errorContent; } : errorContent;
|
273 |
|
274 | if (placeholder && !realCfg.data) {
|
275 | var pl = placeholder === true ? DEFAULT_PLACEHOLDER : placeholder;
|
276 |
|
277 | return react_1.default.createElement(ErrorBoundary_1.default, __assign({ FallbackComponent: FallbackComponent }, ErrorBoundaryProps),
|
278 | react_1.default.createElement("div", { style: { width: props.width || '100%', height: props.height || 400, textAlign: 'center', position: 'relative' } }, pl));
|
279 | }
|
280 | var titleCfg = plotTools_1.visibleHelper(title, false);
|
281 | var descriptionCfg = plotTools_1.visibleHelper(description, false);
|
282 | var titleStyle = __assign(__assign({}, TITLE_STYLE), titleCfg.style);
|
283 | var descStyle = __assign(__assign(__assign({}, DESCRIPTION_STYLE), descriptionCfg.style), { top: titleStyle.height });
|
284 | var isAutoFit = (forceFit !== undefined) ? forceFit : autoFit;
|
285 | if (!util_1.isNil(forceFit)) {
|
286 | warning_1.default(false, '请使用autoFit替代forceFit');
|
287 | }
|
288 | ;
|
289 | react_1.useEffect(function () {
|
290 | if (!isAutoFit) {
|
291 | if (container.current) {
|
292 | resizeFn();
|
293 | resizeObserver.current && resizeObserver.current.unobserve(container.current);
|
294 | }
|
295 | }
|
296 | else {
|
297 | if (container.current) {
|
298 | resizeFn();
|
299 | resizeObserver.current = new resize_observer_1.ResizeObserver(resize);
|
300 | resizeObserver.current.observe(container.current);
|
301 | }
|
302 | else {
|
303 | setChartHeight(0);
|
304 | }
|
305 | }
|
306 | return function () {
|
307 | resizeObserver.current && container.current && resizeObserver.current.unobserve(container.current);
|
308 | };
|
309 | }, [container.current, isAutoFit]);
|
310 | return react_1.default.createElement(ErrorBoundary_1.default, __assign({ FallbackComponent: FallbackComponent }, ErrorBoundaryProps),
|
311 | react_1.default.createElement("div", __assign({ ref: function (el) {
|
312 | container.current = el;
|
313 |
|
314 | if (isMaterial) {
|
315 | if (util_1.isFunction(ref)) {
|
316 | ref(el);
|
317 | }
|
318 | else if (ref) {
|
319 | ref.current = el;
|
320 | }
|
321 | }
|
322 | }, className: "bizcharts-plot" }, containerProps, { style: { position: 'relative', height: props.height || '100%', width: props.width || '100%' } }),
|
323 | titleCfg.visible && react_1.default.createElement("div", __assign({ ref: titleDom }, polyfill_1.polyfillTitleEvent(realCfg), { className: "bizcharts-plot-title", style: titleStyle }), titleCfg.text),
|
324 | descriptionCfg.visible && react_1.default.createElement("div", __assign({ ref: descDom }, polyfill_1.polyfillDescriptionEvent(realCfg), { className: "bizcharts-plot-description", style: descStyle }), descriptionCfg.text),
|
325 | !!chartHeight && react_1.default.createElement(BxPlot
|
326 |
|
327 | , __assign({
|
328 |
|
329 | appendPadding: [10, 5, 10, 10], autoFit: isAutoFit,
|
330 |
|
331 | ref: isMaterial ? undefined : ref }, realCfg, { PlotClass: PlotClass, containerStyle: __assign(__assign({}, containerStyle), { height: chartHeight }) }))));
|
332 | });
|
333 | Com.displayName = name || PlotClass.name;
|
334 | return Com;
|
335 | }
|
336 | exports.default = createPlot;
|
337 |
|
\ | No newline at end of file |