UNPKG

7.88 kBJavaScriptView Raw
1"use strict";
2var __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})();
15var __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};
26var __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}));
33var __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});
38var __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};
45var __importDefault = (this && this.__importDefault) || function (mod) {
46 return (mod && mod.__esModule) ? mod : { "default": mod };
47};
48Object.defineProperty(exports, "__esModule", { value: true });
49exports.Chart = void 0;
50var react_1 = __importDefault(require("react"));
51var debounce_1 = __importDefault(require("@antv/util/lib/debounce"));
52var dom_1 = require("@antv/g2/lib/util/dom");
53var resize_observer_1 = require("@juggle/resize-observer");
54var ErrorBoundary_1 = __importStar(require("../../boundary/ErrorBoundary"));
55var withContainer_1 = __importDefault(require("../../boundary/withContainer"));
56var root_1 = __importDefault(require("../../context/root"));
57var view_1 = __importDefault(require("../../context/view"));
58var group_1 = __importDefault(require("../../context/group"));
59var chartHelper_1 = __importDefault(require("./chartHelper"));
60var util_1 = require("@antv/util");
61var Chart = /** @class */ (function (_super) {
62 __extends(Chart, _super);
63 function Chart(props) {
64 var _this = _super.call(this, props) || this;
65 _this.isRootView = true;
66 _this.resize = debounce_1.default(function () {
67 var chart = _this.chartHelper.chart;
68 if (_this.props.autoFit && _this.chartHelper.chart) {
69 var _a = dom_1.getChartSize(_this.props.container, _this.props.autoFit, chart.width, chart.height), width = _a.width, height = _a.height;
70 if (chart.width !== width || chart.height !== height) {
71 chart.changeSize(width, height);
72 chart.emit('resize');
73 }
74 }
75 }, 300);
76 // 监听容器发生resize
77 _this.resizeObserver = new resize_observer_1.ResizeObserver(_this.resize);
78 _this.resizeObserver.observe(props.container);
79 _this.chartHelper = new chartHelper_1.default();
80 return _this;
81 }
82 Chart.prototype.componentDidMount = function () {
83 if (this.isError) {
84 this.chartHelper.destory();
85 }
86 else {
87 this.chartHelper.render();
88 }
89 };
90 Chart.prototype.componentDidUpdate = function () {
91 if (this.isError) {
92 this.chartHelper.destory();
93 return;
94 }
95 // 更新图表大小
96 var _a = this.props, width = _a.width, height = _a.height, autoFit = _a.autoFit;
97 // 已经自适应就不更新大小了
98 if (!autoFit && this.chartHelper.chart) {
99 if ((width >= 0 && width !== this.chartHelper.chart.width) ||
100 (height >= 0 && height !== this.chartHelper.chart.height)) {
101 var nextWidth = width || this.chartHelper.chart.width;
102 var nextHeight = height || this.chartHelper.chart.height;
103 // changeSize方法内部有调用render, 自动更新无需
104 this.chartHelper.chart.changeSize(nextWidth, nextHeight);
105 this.chartHelper.chart.emit('resize');
106 }
107 else {
108 this.chartHelper.render();
109 }
110 }
111 else {
112 this.chartHelper.render();
113 }
114 };
115 Chart.prototype.componentWillUnmount = function () {
116 this.chartHelper.destory();
117 this.resizeObserver.unobserve(this.props.container);
118 };
119 // 外部通过ref调用获取实例
120 Chart.prototype.getG2Instance = function () {
121 return this.chartHelper.chart;
122 };
123 Chart.prototype.render = function () {
124 var _this = this;
125 var _a = this.props, placeholder = _a.placeholder, data = _a.data, errorContent = _a.errorContent;
126 var ErrorBoundaryProps = this.props.ErrorBoundaryProps;
127 if ((data === undefined || data.length === 0) && placeholder) {
128 this.chartHelper.destory();
129 var pl = placeholder === true ? react_1.default.createElement("div", { style: { position: 'relative', top: '48%', color: '#aaa', textAlign: 'center' } }, "\u6682\u65E0\u6570\u636E") : placeholder;
130 return react_1.default.createElement(ErrorBoundary_1.default, __assign({}, ErrorBoundaryProps), pl);
131 }
132 this.chartHelper.update(this.props);
133 if (errorContent) {
134 // 兼容 4.0 的用法
135 ErrorBoundaryProps = __assign({ fallback: errorContent }, ErrorBoundaryProps);
136 }
137 else {
138 // react-ErrorBoundary
139 ErrorBoundaryProps = {
140 FallbackComponent: ErrorBoundary_1.ErrorFallback
141 };
142 }
143 return (react_1.default.createElement(ErrorBoundary_1.default, __assign({}, ErrorBoundaryProps, { key: this.chartHelper.key, onError: function () {
144 var args = [];
145 for (var _i = 0; _i < arguments.length; _i++) {
146 args[_i] = arguments[_i];
147 }
148 _this.isError = true;
149 util_1.isFunction(ErrorBoundaryProps.onError) && ErrorBoundaryProps.onError.apply(ErrorBoundaryProps, args);
150 }, onReset: function () {
151 var args = [];
152 for (var _i = 0; _i < arguments.length; _i++) {
153 args[_i] = arguments[_i];
154 }
155 _this.isError = false;
156 util_1.isFunction(ErrorBoundaryProps.onReset) && ErrorBoundaryProps.onReset.apply(ErrorBoundaryProps, args);
157 }, resetKeys: [this.chartHelper.key], fallback: errorContent }),
158 react_1.default.createElement(root_1.default.Provider, { value: this.chartHelper },
159 react_1.default.createElement(view_1.default.Provider, { value: this.chartHelper.chart },
160 react_1.default.createElement(group_1.default.Provider, { value: this.chartHelper.extendGroup }, this.props.children)))));
161 };
162 Chart.defaultProps = {
163 placeholder: false,
164 visible: true,
165 interactions: [],
166 filter: [],
167 };
168 return Chart;
169}(react_1.default.Component));
170exports.Chart = Chart;
171exports.default = withContainer_1.default(Chart);
172//# sourceMappingURL=index.js.map
\No newline at end of file