echarts-for-react 3.0.1 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -2
- package/esm/core.d.ts +13 -4
- package/esm/core.js +104 -39
- package/esm/core.js.map +1 -1
- package/esm/index.d.ts +1 -1
- package/esm/index.js.map +1 -1
- package/esm/types.d.ts +14 -5
- package/esm/types.js.map +1 -1
- package/lib/core.d.ts +13 -4
- package/lib/core.js +108 -43
- package/lib/core.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js.map +1 -1
- package/lib/types.d.ts +14 -5
- package/lib/types.js.map +1 -1
- package/package.json +4 -4
- package/src/core.tsx +107 -32
- package/src/index.ts +1 -1
- package/src/types.ts +13 -3
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
The simplest, and the best React wrapper for [Apache ECharts](https://github.com/apache/incubator-echarts).
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/echarts-for-react)
|
|
6
|
-
[](https://github.com/hustcc/echarts-for-react/actions/workflows/build.yml)
|
|
7
7
|
[](https://coveralls.io/github/hustcc/echarts-for-react)
|
|
8
8
|
[](https://www.npmjs.com/package/echarts-for-react)
|
|
9
9
|
[](https://www.npmjs.com/package/echarts-for-react)
|
|
@@ -127,7 +127,7 @@ import {
|
|
|
127
127
|
// VisualMapPiecewiseComponent,
|
|
128
128
|
// AriaComponent,
|
|
129
129
|
// TransformComponent,
|
|
130
|
-
|
|
130
|
+
DatasetComponent,
|
|
131
131
|
} from 'echarts/components';
|
|
132
132
|
// Import renderer, note that introducing the CanvasRenderer or SVGRenderer is a required step
|
|
133
133
|
import {
|
|
@@ -218,6 +218,29 @@ import 'echarts/lib/component/title';
|
|
|
218
218
|
/>
|
|
219
219
|
```
|
|
220
220
|
|
|
221
|
+
For **Next.js** user, code transpilation is needed. For Next.js 13.1 or higher, as all `next-transpile-modules` features are natively built-in and the package has been deprecated, so please add `transpilePackages: ['echarts', 'zrender']` into `nextConfig` object:
|
|
222
|
+
|
|
223
|
+
```js
|
|
224
|
+
// next.config.js
|
|
225
|
+
/** @type {import('next').NextConfig} */
|
|
226
|
+
|
|
227
|
+
const nextConfig = {
|
|
228
|
+
// ...existing properties,
|
|
229
|
+
transpilePackages: ['echarts', 'zrender'],
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
module.exports = nextConfig
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
For Next.js with version < 13.1:
|
|
236
|
+
|
|
237
|
+
```js
|
|
238
|
+
// next.config.js
|
|
239
|
+
const withTM = require("next-transpile-modules")(["echarts", "zrender"]);
|
|
240
|
+
|
|
241
|
+
module.exports = withTM({})
|
|
242
|
+
```
|
|
243
|
+
|
|
221
244
|
## Props of Component
|
|
222
245
|
|
|
223
246
|
- **`option`** (required, object)
|
|
@@ -228,6 +251,10 @@ the echarts option config, can see [https://echarts.apache.org/option.html#title
|
|
|
228
251
|
|
|
229
252
|
when `setOption`, not merge the data, default is `false`. See [https://echarts.apache.org/api.html#echartsInstance.setOption](https://echarts.apache.org/api.html#echartsInstance.setOption).
|
|
230
253
|
|
|
254
|
+
- **`replaceMerge`** (optional, string | string[])
|
|
255
|
+
|
|
256
|
+
when `setOption`, default is `null`. See [https://echarts.apache.org/api.html#echartsInstance.setOption](https://echarts.apache.org/api.html#echartsInstance.setOption).
|
|
257
|
+
|
|
231
258
|
- **`lazyUpdate`** (optional, object)
|
|
232
259
|
|
|
233
260
|
when `setOption`, lazy update the data, default is `false`. See [https://echarts.apache.org/api.html#echartsInstance.setOption](https://echarts.apache.org/api.html#echartsInstance.setOption).
|
|
@@ -303,6 +330,10 @@ the `opts` of echarts. `object`, will be used when initial echarts instance by `
|
|
|
303
330
|
/>
|
|
304
331
|
```
|
|
305
332
|
|
|
333
|
+
- **`autoResize`** (optional, boolean)
|
|
334
|
+
|
|
335
|
+
decide whether to trigger `this.resize` when window resize. default is `true`.
|
|
336
|
+
|
|
306
337
|
|
|
307
338
|
## Component API & Echarts API
|
|
308
339
|
|
|
@@ -326,6 +357,25 @@ const echartInstance = this.echartRef.getEchartsInstance();
|
|
|
326
357
|
const base64 = echartInstance.getDataURL();
|
|
327
358
|
```
|
|
328
359
|
|
|
360
|
+
TypeScript and `useRef()` example:
|
|
361
|
+
|
|
362
|
+
```ts
|
|
363
|
+
const getOption = () => {/** */};
|
|
364
|
+
|
|
365
|
+
export default function App() {
|
|
366
|
+
const echartsRef = useRef<InstanceType<typeof ReactEcharts>>(null);
|
|
367
|
+
|
|
368
|
+
useEffect(() => {
|
|
369
|
+
if (echartsRef.current) {
|
|
370
|
+
const echartsInstance = echartsRef.current.getEchartsInstance();
|
|
371
|
+
// do something
|
|
372
|
+
echartsInstance.resize();
|
|
373
|
+
}
|
|
374
|
+
}, []);
|
|
375
|
+
return <ReactEcharts ref={echartsRef} option={getOption()} />;
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
329
379
|
**About API of echarts, can see** [https://echarts.apache.org/api.html#echartsInstance](https://echarts.apache.org/api.html#echartsInstance).
|
|
330
380
|
|
|
331
381
|
You can use the API to do:
|
package/esm/core.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ECharts } from 'echarts';
|
|
1
2
|
import { PureComponent } from 'react';
|
|
2
3
|
import { EChartsReactProps } from './types';
|
|
3
4
|
/**
|
|
@@ -8,6 +9,10 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
8
9
|
* echarts render container
|
|
9
10
|
*/
|
|
10
11
|
ele: HTMLElement;
|
|
12
|
+
/**
|
|
13
|
+
* if this is the first time we are resizing
|
|
14
|
+
*/
|
|
15
|
+
private isInitialResize;
|
|
11
16
|
/**
|
|
12
17
|
* echarts library entry
|
|
13
18
|
*/
|
|
@@ -16,12 +21,11 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
16
21
|
componentDidMount(): void;
|
|
17
22
|
componentDidUpdate(prevProps: EChartsReactProps): void;
|
|
18
23
|
componentWillUnmount(): void;
|
|
24
|
+
initEchartsInstance(): Promise<ECharts>;
|
|
19
25
|
/**
|
|
20
|
-
* return the echart object
|
|
21
|
-
* 1. if exist, return the existed instance
|
|
22
|
-
* 2. or new one instance
|
|
26
|
+
* return the existing echart object
|
|
23
27
|
*/
|
|
24
|
-
getEchartsInstance():
|
|
28
|
+
getEchartsInstance(): ECharts;
|
|
25
29
|
/**
|
|
26
30
|
* dispose echarts and clear size-sensor
|
|
27
31
|
*/
|
|
@@ -31,9 +35,14 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
31
35
|
*/
|
|
32
36
|
private renderNewEcharts;
|
|
33
37
|
private bindEvents;
|
|
38
|
+
private offEvents;
|
|
34
39
|
/**
|
|
35
40
|
* render the echarts
|
|
36
41
|
*/
|
|
37
42
|
private updateEChartsOption;
|
|
43
|
+
/**
|
|
44
|
+
* resize wrapper
|
|
45
|
+
*/
|
|
46
|
+
private resize;
|
|
38
47
|
render(): JSX.Element;
|
|
39
48
|
}
|
package/esm/core.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { __assign, __extends } from "tslib";
|
|
1
|
+
import { __assign, __awaiter, __extends, __generator } from "tslib";
|
|
2
2
|
import React, { PureComponent } from 'react';
|
|
3
3
|
import { bind, clear } from 'size-sensor';
|
|
4
4
|
import { pick } from './helper/pick';
|
|
@@ -14,6 +14,7 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
14
14
|
var _this = _super.call(this, props) || this;
|
|
15
15
|
_this.echarts = props.echarts;
|
|
16
16
|
_this.ele = null;
|
|
17
|
+
_this.isInitialResize = true;
|
|
17
18
|
return _this;
|
|
18
19
|
}
|
|
19
20
|
EChartsReactCore.prototype.componentDidMount = function () {
|
|
@@ -32,42 +33,64 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
32
33
|
// 以下属性修改的时候,需要 dispose 之后再新建
|
|
33
34
|
// 1. 切换 theme 的时候
|
|
34
35
|
// 2. 修改 opts 的时候
|
|
35
|
-
// 3. 修改 onEvents 的时候,这样可以取消所有之前绑定的事件 issue #151
|
|
36
36
|
if (!isEqual(prevProps.theme, this.props.theme) ||
|
|
37
|
-
!isEqual(prevProps.opts, this.props.opts)
|
|
38
|
-
!isEqual(prevProps.onEvents, this.props.onEvents)) {
|
|
37
|
+
!isEqual(prevProps.opts, this.props.opts)) {
|
|
39
38
|
this.dispose();
|
|
40
39
|
this.renderNewEcharts(); // 重建
|
|
41
40
|
return;
|
|
42
41
|
}
|
|
43
|
-
//
|
|
44
|
-
var
|
|
45
|
-
if (isEqual(
|
|
46
|
-
|
|
42
|
+
// 修改 onEvent 的时候先移除历史事件再添加
|
|
43
|
+
var echartsInstance = this.getEchartsInstance();
|
|
44
|
+
if (!isEqual(prevProps.onEvents, this.props.onEvents)) {
|
|
45
|
+
this.offEvents(echartsInstance, prevProps.onEvents);
|
|
46
|
+
this.bindEvents(echartsInstance, this.props.onEvents);
|
|
47
|
+
}
|
|
48
|
+
// when these props are not isEqual, update echarts
|
|
49
|
+
var pickKeys = ['option', 'notMerge', 'replaceMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
|
|
50
|
+
if (!isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {
|
|
51
|
+
this.updateEChartsOption();
|
|
47
52
|
}
|
|
48
|
-
var echartsInstance = this.updateEChartsOption();
|
|
49
53
|
/**
|
|
50
54
|
* when style or class name updated, change size.
|
|
51
55
|
*/
|
|
52
56
|
if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {
|
|
53
|
-
|
|
54
|
-
echartsInstance.resize();
|
|
55
|
-
}
|
|
56
|
-
catch (e) {
|
|
57
|
-
console.warn(e);
|
|
58
|
-
}
|
|
57
|
+
this.resize();
|
|
59
58
|
}
|
|
60
59
|
};
|
|
61
60
|
EChartsReactCore.prototype.componentWillUnmount = function () {
|
|
62
61
|
this.dispose();
|
|
63
62
|
};
|
|
63
|
+
/*
|
|
64
|
+
* initialise an echarts instance
|
|
65
|
+
*/
|
|
66
|
+
EChartsReactCore.prototype.initEchartsInstance = function () {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
68
|
+
var _this = this;
|
|
69
|
+
return __generator(this, function (_a) {
|
|
70
|
+
return [2 /*return*/, new Promise(function (resolve) {
|
|
71
|
+
// create temporary echart instance
|
|
72
|
+
_this.echarts.init(_this.ele, _this.props.theme, _this.props.opts);
|
|
73
|
+
var echartsInstance = _this.getEchartsInstance();
|
|
74
|
+
echartsInstance.on('finished', function () {
|
|
75
|
+
// get final width and height
|
|
76
|
+
var width = _this.ele.clientWidth;
|
|
77
|
+
var height = _this.ele.clientHeight;
|
|
78
|
+
// dispose temporary echart instance
|
|
79
|
+
_this.echarts.dispose(_this.ele);
|
|
80
|
+
// recreate echart instance
|
|
81
|
+
// we use final width and height only if not originally provided as opts
|
|
82
|
+
var opts = __assign({ width: width, height: height }, _this.props.opts);
|
|
83
|
+
resolve(_this.echarts.init(_this.ele, _this.props.theme, opts));
|
|
84
|
+
});
|
|
85
|
+
})];
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
};
|
|
64
89
|
/**
|
|
65
|
-
* return the echart object
|
|
66
|
-
* 1. if exist, return the existed instance
|
|
67
|
-
* 2. or new one instance
|
|
90
|
+
* return the existing echart object
|
|
68
91
|
*/
|
|
69
92
|
EChartsReactCore.prototype.getEchartsInstance = function () {
|
|
70
|
-
return this.echarts.getInstanceByDom(this.ele)
|
|
93
|
+
return this.echarts.getInstanceByDom(this.ele);
|
|
71
94
|
};
|
|
72
95
|
/**
|
|
73
96
|
* dispose echarts and clear size-sensor
|
|
@@ -88,25 +111,34 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
88
111
|
* render a new echarts instance
|
|
89
112
|
*/
|
|
90
113
|
EChartsReactCore.prototype.renderNewEcharts = function () {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
114
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
115
|
+
var _a, onEvents, onChartReady, _b, autoResize, echartsInstance;
|
|
116
|
+
var _this = this;
|
|
117
|
+
return __generator(this, function (_c) {
|
|
118
|
+
switch (_c.label) {
|
|
119
|
+
case 0:
|
|
120
|
+
_a = this.props, onEvents = _a.onEvents, onChartReady = _a.onChartReady, _b = _a.autoResize, autoResize = _b === void 0 ? true : _b;
|
|
121
|
+
// 1. init echarts instance
|
|
122
|
+
return [4 /*yield*/, this.initEchartsInstance()];
|
|
123
|
+
case 1:
|
|
124
|
+
// 1. init echarts instance
|
|
125
|
+
_c.sent();
|
|
126
|
+
echartsInstance = this.updateEChartsOption();
|
|
127
|
+
// 3. bind events
|
|
128
|
+
this.bindEvents(echartsInstance, onEvents || {});
|
|
129
|
+
// 4. on chart ready
|
|
130
|
+
if (isFunction(onChartReady))
|
|
131
|
+
onChartReady(echartsInstance);
|
|
132
|
+
// 5. on resize
|
|
133
|
+
if (this.ele && autoResize) {
|
|
134
|
+
bind(this.ele, function () {
|
|
135
|
+
_this.resize();
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return [2 /*return*/];
|
|
107
139
|
}
|
|
108
140
|
});
|
|
109
|
-
}
|
|
141
|
+
});
|
|
110
142
|
};
|
|
111
143
|
// bind the events
|
|
112
144
|
EChartsReactCore.prototype.bindEvents = function (instance, events) {
|
|
@@ -126,15 +158,26 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
126
158
|
}
|
|
127
159
|
}
|
|
128
160
|
};
|
|
161
|
+
// off the events
|
|
162
|
+
EChartsReactCore.prototype.offEvents = function (instance, events) {
|
|
163
|
+
if (!events)
|
|
164
|
+
return;
|
|
165
|
+
// loop and off
|
|
166
|
+
for (var eventName in events) {
|
|
167
|
+
if (isString(eventName)) {
|
|
168
|
+
instance.off(eventName);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
129
172
|
/**
|
|
130
173
|
* render the echarts
|
|
131
174
|
*/
|
|
132
175
|
EChartsReactCore.prototype.updateEChartsOption = function () {
|
|
133
|
-
var _a = this.props, option = _a.option, _b = _a.notMerge, notMerge = _b === void 0 ? false : _b, _c = _a.lazyUpdate, lazyUpdate =
|
|
176
|
+
var _a = this.props, option = _a.option, _b = _a.notMerge, notMerge = _b === void 0 ? false : _b, _c = _a.replaceMerge, replaceMerge = _c === void 0 ? null : _c, _d = _a.lazyUpdate, lazyUpdate = _d === void 0 ? false : _d, showLoading = _a.showLoading, _e = _a.loadingOption, loadingOption = _e === void 0 ? null : _e;
|
|
134
177
|
// 1. get or initial the echarts object
|
|
135
178
|
var echartInstance = this.getEchartsInstance();
|
|
136
179
|
// 2. set the echarts option
|
|
137
|
-
echartInstance.setOption(option, notMerge, lazyUpdate);
|
|
180
|
+
echartInstance.setOption(option, { notMerge: notMerge, replaceMerge: replaceMerge, lazyUpdate: lazyUpdate });
|
|
138
181
|
// 3. set loading mask
|
|
139
182
|
if (showLoading)
|
|
140
183
|
echartInstance.showLoading(loadingOption);
|
|
@@ -142,6 +185,28 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
142
185
|
echartInstance.hideLoading();
|
|
143
186
|
return echartInstance;
|
|
144
187
|
};
|
|
188
|
+
/**
|
|
189
|
+
* resize wrapper
|
|
190
|
+
*/
|
|
191
|
+
EChartsReactCore.prototype.resize = function () {
|
|
192
|
+
// 1. get the echarts object
|
|
193
|
+
var echartsInstance = this.getEchartsInstance();
|
|
194
|
+
// 2. call echarts instance resize if not the initial resize
|
|
195
|
+
// resize should not happen on first render as it will cancel initial echarts animations
|
|
196
|
+
if (!this.isInitialResize) {
|
|
197
|
+
try {
|
|
198
|
+
echartsInstance.resize({
|
|
199
|
+
width: 'auto',
|
|
200
|
+
height: 'auto',
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
catch (e) {
|
|
204
|
+
console.warn(e);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// 3. update variable for future calls
|
|
208
|
+
this.isInitialResize = false;
|
|
209
|
+
};
|
|
145
210
|
EChartsReactCore.prototype.render = function () {
|
|
146
211
|
var _this = this;
|
|
147
212
|
var _a = this.props, style = _a.style, _b = _a.className, className = _b === void 0 ? '' : _b;
|
|
@@ -149,7 +214,7 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
149
214
|
var newStyle = __assign({ height: 300 }, style);
|
|
150
215
|
return (React.createElement("div", { ref: function (e) {
|
|
151
216
|
_this.ele = e;
|
|
152
|
-
}, style: newStyle, className: "echarts-for-react "
|
|
217
|
+
}, style: newStyle, className: "echarts-for-react ".concat(className) }));
|
|
153
218
|
};
|
|
154
219
|
return EChartsReactCore;
|
|
155
220
|
}(PureComponent));
|
package/esm/core.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAG5C;;GAEG;AACH;IAA8C,oCAAgC;IAW5E,0BAAY,KAAwB;QAApC,YACE,kBAAM,KAAK,CAAC,SAIb;QAFC,KAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;QAC7B,KAAI,CAAC,GAA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AAU,CAAC,CAAC;QACvD,sBAAsB;QACtB,IAAI,WAAW;YAAE,cAAc,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC;;YACtD,cAAc,CAAC,WAAW,EAAE,CAAC;QAElC,OAAO,cAAc,CAAC;IACxB,CAAC;IAED,iCAAM,GAAN;QAAA,iBAcC;QAbO,IAAA,KAA4B,IAAI,CAAC,KAAK,EAApC,KAAK,WAAA,EAAE,iBAAc,EAAd,SAAS,mBAAG,EAAE,KAAe,CAAC;QAC7C,uBAAuB;QACvB,IAAM,QAAQ,cAAK,MAAM,EAAE,GAAG,IAAK,KAAK,CAAE,CAAC;QAE3C,OAAO,CACL,6BACE,GAAG,EAAE,UAAC,CAAc;gBAClB,KAAI,CAAC,GAAG,GAAG,CAAC,CAAC;YACf,CAAC,EACD,KAAK,EAAE,QAAQ,EACf,SAAS,EAAE,uBAAqB,SAAW,GAC3C,CACH,CAAC;IACJ,CAAC;IACH,uBAAC;AAAD,CAAC,AA7KD,CAA8C,aAAa,GA6K1D","sourcesContent":["import React, { PureComponent } from 'react';\nimport { bind, clear } from 'size-sensor';\nimport { pick } from './helper/pick';\nimport { isFunction } from './helper/is-function';\nimport { isString } from './helper/is-string';\nimport { isEqual } from './helper/is-equal';\nimport { EChartsReactProps, EChartsInstance } from './types';\n\n/**\n * core component for echarts binding\n */\nexport default class EChartsReactCore extends PureComponent<EChartsReactProps> {\n /**\n * echarts render container\n */\n public ele: HTMLElement;\n\n /**\n * echarts library entry\n */\n protected echarts: any;\n\n constructor(props: EChartsReactProps) {\n super(props);\n\n this.echarts = props.echarts;\n this.ele = null;\n }\n\n componentDidMount() {\n this.renderNewEcharts();\n }\n\n // update\n componentDidUpdate(prevProps: EChartsReactProps) {\n /**\n * if shouldSetOption return false, then return, not update echarts options\n * default is true\n */\n const { shouldSetOption } = this.props;\n if (isFunction(shouldSetOption) && !shouldSetOption(prevProps, this.props)) {\n return;\n }\n\n // 以下属性修改的时候,需要 dispose 之后再新建\n // 1. 切换 theme 的时候\n // 2. 修改 opts 的时候\n // 3. 修改 onEvents 的时候,这样可以取消所有之前绑定的事件 issue #151\n if (\n !isEqual(prevProps.theme, this.props.theme) ||\n !isEqual(prevProps.opts, this.props.opts) ||\n !isEqual(prevProps.onEvents, this.props.onEvents)\n ) {\n this.dispose();\n\n this.renderNewEcharts(); // 重建\n return;\n }\n\n // when thoes props isEqual, do not update echarts\n const pickKeys = ['option', 'notMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];\n if (isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {\n return;\n }\n\n const echartsInstance = this.updateEChartsOption();\n /**\n * when style or class name updated, change size.\n */\n if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {\n try {\n echartsInstance.resize();\n } catch (e) {\n console.warn(e);\n }\n }\n }\n\n componentWillUnmount() {\n this.dispose();\n }\n\n /**\n * return the echart object\n * 1. if exist, return the existed instance\n * 2. or new one instance\n */\n public getEchartsInstance() {\n return this.echarts.getInstanceByDom(this.ele) || this.echarts.init(this.ele, this.props.theme, this.props.opts);\n }\n\n /**\n * dispose echarts and clear size-sensor\n */\n private dispose() {\n if (this.ele) {\n try {\n clear(this.ele);\n } catch (e) {\n console.warn(e);\n }\n // dispose echarts instance\n this.echarts.dispose(this.ele);\n }\n }\n\n /**\n * render a new echarts instance\n */\n private renderNewEcharts() {\n const { onEvents, onChartReady } = this.props;\n\n // 1. new echarts instance\n const echartsInstance = this.updateEChartsOption();\n\n // 2. bind events\n this.bindEvents(echartsInstance, onEvents || {});\n\n // 3. on chart ready\n if (isFunction(onChartReady)) onChartReady(echartsInstance);\n\n // 4. on resize\n if (this.ele) {\n bind(this.ele, () => {\n try {\n echartsInstance.resize();\n } catch (e) {\n console.warn(e);\n }\n });\n }\n }\n\n // bind the events\n private bindEvents(instance, events: EChartsReactProps['onEvents']) {\n function _bindEvent(eventName: string, func: Function) {\n // ignore the event config which not satisfy\n if (isString(eventName) && isFunction(func)) {\n // binding event\n instance.on(eventName, (param) => {\n func(param, instance);\n });\n }\n }\n\n // loop and bind\n for (const eventName in events) {\n if (Object.prototype.hasOwnProperty.call(events, eventName)) {\n _bindEvent(eventName, events[eventName]);\n }\n }\n }\n\n /**\n * render the echarts\n */\n private updateEChartsOption(): EChartsInstance {\n const { option, notMerge = false, lazyUpdate = false, showLoading, loadingOption = null } = this.props;\n // 1. get or initial the echarts object\n const echartInstance = this.getEchartsInstance();\n // 2. set the echarts option\n echartInstance.setOption(option, notMerge, lazyUpdate);\n // 3. set loading mask\n if (showLoading) echartInstance.showLoading(loadingOption);\n else echartInstance.hideLoading();\n\n return echartInstance;\n }\n\n render(): JSX.Element {\n const { style, className = '' } = this.props;\n // default height = 300\n const newStyle = { height: 300, ...style };\n\n return (\n <div\n ref={(e: HTMLElement) => {\n this.ele = e;\n }}\n style={newStyle}\n className={`echarts-for-react ${className}`}\n />\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.tsx"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAG5C;;GAEG;AACH;IAA8C,oCAAgC;IAgB5E,0BAAY,KAAwB;QAApC,YACE,kBAAM,KAAK,CAAC,SAKb;QAHC,KAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;QAC7B,KAAI,CAAC,GAAG,GAAG,IAAI,CAAC;QAChB,KAAI,CAAC,eAAe,GAAG,IAAI,CAAC;;IAC9B,CAAC;IAED,4CAAiB,GAAjB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AAU,YAAA,EAAE,CAAC,CAAC;QACzE,sBAAsB;QACtB,IAAI,WAAW;YAAE,cAAc,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC;;YACtD,cAAc,CAAC,WAAW,EAAE,CAAC;QAElC,OAAO,cAAc,CAAC;IACxB,CAAC;IAED;;OAEG;IACK,iCAAM,GAAd;QACE,4BAA4B;QAC5B,IAAM,eAAe,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAElD,4DAA4D;QAC5D,wFAAwF;QACxF,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE;YACzB,IAAI;gBACF,eAAe,CAAC,MAAM,CAAC;oBACrB,KAAK,EAAE,MAAM;oBACb,MAAM,EAAE,MAAM;iBACf,CAAC,CAAC;aACJ;YAAC,OAAO,CAAC,EAAE;gBACV,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aACjB;SACF;QAED,sCAAsC;QACtC,IAAI,CAAC,eAAe,GAAG,KAAK,CAAC;IAC/B,CAAC;IAED,iCAAM,GAAN;QAAA,iBAcC;QAbO,IAAA,KAA4B,IAAI,CAAC,KAAK,EAApC,KAAK,WAAA,EAAE,iBAAc,EAAd,SAAS,mBAAG,EAAE,KAAe,CAAC;QAC7C,uBAAuB;QACvB,IAAM,QAAQ,cAAK,MAAM,EAAE,GAAG,IAAK,KAAK,CAAE,CAAC;QAE3C,OAAO,CACL,6BACE,GAAG,EAAE,UAAC,CAAc;gBAClB,KAAI,CAAC,GAAG,GAAG,CAAC,CAAC;YACf,CAAC,EACD,KAAK,EAAE,QAAQ,EACf,SAAS,EAAE,4BAAqB,SAAS,CAAE,GAC3C,CACH,CAAC;IACJ,CAAC;IACH,uBAAC;AAAD,CAAC,AAvPD,CAA8C,aAAa,GAuP1D","sourcesContent":["import type { ECharts } from 'echarts';\nimport React, { PureComponent } from 'react';\nimport { bind, clear } from 'size-sensor';\nimport { pick } from './helper/pick';\nimport { isFunction } from './helper/is-function';\nimport { isString } from './helper/is-string';\nimport { isEqual } from './helper/is-equal';\nimport { EChartsReactProps, EChartsInstance } from './types';\n\n/**\n * core component for echarts binding\n */\nexport default class EChartsReactCore extends PureComponent<EChartsReactProps> {\n /**\n * echarts render container\n */\n public ele: HTMLElement;\n\n /**\n * if this is the first time we are resizing\n */\n private isInitialResize: boolean;\n\n /**\n * echarts library entry\n */\n protected echarts: any;\n\n constructor(props: EChartsReactProps) {\n super(props);\n\n this.echarts = props.echarts;\n this.ele = null;\n this.isInitialResize = true;\n }\n\n componentDidMount() {\n this.renderNewEcharts();\n }\n\n // update\n componentDidUpdate(prevProps: EChartsReactProps) {\n /**\n * if shouldSetOption return false, then return, not update echarts options\n * default is true\n */\n const { shouldSetOption } = this.props;\n if (isFunction(shouldSetOption) && !shouldSetOption(prevProps, this.props)) {\n return;\n }\n\n // 以下属性修改的时候,需要 dispose 之后再新建\n // 1. 切换 theme 的时候\n // 2. 修改 opts 的时候\n if (\n !isEqual(prevProps.theme, this.props.theme) ||\n !isEqual(prevProps.opts, this.props.opts)\n ) {\n this.dispose();\n\n this.renderNewEcharts(); // 重建\n return;\n }\n\n // 修改 onEvent 的时候先移除历史事件再添加\n const echartsInstance = this.getEchartsInstance();\n if (!isEqual(prevProps.onEvents, this.props.onEvents)) {\n this.offEvents(echartsInstance, prevProps.onEvents);\n this.bindEvents(echartsInstance, this.props.onEvents);\n }\n\n // when these props are not isEqual, update echarts\n const pickKeys = ['option', 'notMerge', 'replaceMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];\n if (!isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {\n this.updateEChartsOption();\n }\n\n /**\n * when style or class name updated, change size.\n */\n if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {\n this.resize();\n }\n }\n\n componentWillUnmount() {\n this.dispose();\n }\n\n /*\n * initialise an echarts instance\n */\n public async initEchartsInstance(): Promise<ECharts> {\n return new Promise((resolve) => {\n // create temporary echart instance\n this.echarts.init(this.ele, this.props.theme, this.props.opts);\n const echartsInstance = this.getEchartsInstance();\n\n echartsInstance.on('finished', () => {\n // get final width and height\n const width = this.ele.clientWidth;\n const height = this.ele.clientHeight;\n\n // dispose temporary echart instance\n this.echarts.dispose(this.ele);\n\n // recreate echart instance\n // we use final width and height only if not originally provided as opts\n const opts = {\n width,\n height,\n ...this.props.opts,\n };\n resolve(this.echarts.init(this.ele, this.props.theme, opts));\n });\n });\n }\n\n /**\n * return the existing echart object\n */\n public getEchartsInstance(): ECharts {\n return this.echarts.getInstanceByDom(this.ele);\n }\n\n /**\n * dispose echarts and clear size-sensor\n */\n private dispose() {\n if (this.ele) {\n try {\n clear(this.ele);\n } catch (e) {\n console.warn(e);\n }\n // dispose echarts instance\n this.echarts.dispose(this.ele);\n }\n }\n\n /**\n * render a new echarts instance\n */\n private async renderNewEcharts() {\n const { onEvents, onChartReady, autoResize = true } = this.props;\n\n // 1. init echarts instance\n await this.initEchartsInstance();\n\n // 2. update echarts instance\n const echartsInstance = this.updateEChartsOption();\n\n // 3. bind events\n this.bindEvents(echartsInstance, onEvents || {});\n\n // 4. on chart ready\n if (isFunction(onChartReady)) onChartReady(echartsInstance);\n\n // 5. on resize\n if (this.ele && autoResize) {\n bind(this.ele, () => {\n this.resize();\n });\n }\n }\n\n // bind the events\n private bindEvents(instance, events: EChartsReactProps['onEvents']) {\n function _bindEvent(eventName: string, func: Function) {\n // ignore the event config which not satisfy\n if (isString(eventName) && isFunction(func)) {\n // binding event\n instance.on(eventName, (param) => {\n func(param, instance);\n });\n }\n }\n\n // loop and bind\n for (const eventName in events) {\n if (Object.prototype.hasOwnProperty.call(events, eventName)) {\n _bindEvent(eventName, events[eventName]);\n }\n }\n }\n\n // off the events\n private offEvents(instance, events: EChartsReactProps['onEvents']) {\n if (!events) return;\n // loop and off\n for (const eventName in events) {\n if (isString(eventName)) {\n instance.off(eventName);\n }\n }\n }\n\n /**\n * render the echarts\n */\n private updateEChartsOption(): EChartsInstance {\n const {\n option,\n notMerge = false,\n replaceMerge = null,\n lazyUpdate = false,\n showLoading,\n loadingOption = null,\n } = this.props;\n // 1. get or initial the echarts object\n const echartInstance = this.getEchartsInstance();\n // 2. set the echarts option\n echartInstance.setOption(option, { notMerge, replaceMerge, lazyUpdate });\n // 3. set loading mask\n if (showLoading) echartInstance.showLoading(loadingOption);\n else echartInstance.hideLoading();\n\n return echartInstance;\n }\n\n /**\n * resize wrapper\n */\n private resize() {\n // 1. get the echarts object\n const echartsInstance = this.getEchartsInstance();\n\n // 2. call echarts instance resize if not the initial resize\n // resize should not happen on first render as it will cancel initial echarts animations\n if (!this.isInitialResize) {\n try {\n echartsInstance.resize({\n width: 'auto',\n height: 'auto',\n });\n } catch (e) {\n console.warn(e);\n }\n }\n\n // 3. update variable for future calls\n this.isInitialResize = false;\n }\n\n render(): JSX.Element {\n const { style, className = '' } = this.props;\n // default height = 300\n const newStyle = { height: 300, ...style };\n\n return (\n <div\n ref={(e: HTMLElement) => {\n this.ele = e;\n }}\n style={newStyle}\n className={`echarts-for-react ${className}`}\n />\n );\n }\n}\n"]}
|
package/esm/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EChartsReactProps, EChartsOption, EChartsInstance } from './types';
|
|
2
2
|
import EChartsReactCore from './core';
|
|
3
|
-
export { EChartsReactProps, EChartsOption, EChartsInstance };
|
|
3
|
+
export type { EChartsReactProps, EChartsOption, EChartsInstance };
|
|
4
4
|
export default class EChartsReact extends EChartsReactCore {
|
|
5
5
|
constructor(props: EChartsReactProps);
|
|
6
6
|
}
|
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAEnC,OAAO,gBAAgB,MAAM,QAAQ,CAAC;AAItC,2CAA2C;AAC3C;IAA0C,gCAAgB;IACxD,sBAAY,KAAwB;QAApC,YACE,kBAAM,KAAK,CAAC,SAIb;QAFC,mBAAmB;QACnB,KAAI,CAAC,OAAO,GAAG,OAAO,CAAC;;IACzB,CAAC;IACH,mBAAC;AAAD,CAAC,AAPD,CAA0C,gBAAgB,GAOzD","sourcesContent":["import * as echarts from 'echarts';\nimport { EChartsReactProps, EChartsOption, EChartsInstance } from './types';\nimport EChartsReactCore from './core';\n\nexport { EChartsReactProps, EChartsOption, EChartsInstance };\n\n// export the Component the echarts Object.\nexport default class EChartsReact extends EChartsReactCore {\n constructor(props: EChartsReactProps) {\n super(props);\n\n // 初始化为 echarts 整个包\n this.echarts = echarts;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAEnC,OAAO,gBAAgB,MAAM,QAAQ,CAAC;AAItC,2CAA2C;AAC3C;IAA0C,gCAAgB;IACxD,sBAAY,KAAwB;QAApC,YACE,kBAAM,KAAK,CAAC,SAIb;QAFC,mBAAmB;QACnB,KAAI,CAAC,OAAO,GAAG,OAAO,CAAC;;IACzB,CAAC;IACH,mBAAC;AAAD,CAAC,AAPD,CAA0C,gBAAgB,GAOzD","sourcesContent":["import * as echarts from 'echarts';\nimport { EChartsReactProps, EChartsOption, EChartsInstance } from './types';\nimport EChartsReactCore from './core';\n\nexport type { EChartsReactProps, EChartsOption, EChartsInstance };\n\n// export the Component the echarts Object.\nexport default class EChartsReact extends EChartsReactCore {\n constructor(props: EChartsReactProps) {\n super(props);\n\n // 初始化为 echarts 整个包\n this.echarts = echarts;\n }\n}\n"]}
|
package/esm/types.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { CSSProperties } from 'react';
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
export
|
|
2
|
+
import type { EChartsOption, EChartsType } from 'echarts';
|
|
3
|
+
export type { EChartsOption };
|
|
4
|
+
export type EChartsInstance = EChartsType;
|
|
5
|
+
export type Opts = {
|
|
5
6
|
readonly devicePixelRatio?: number;
|
|
6
7
|
readonly renderer?: 'canvas' | 'svg';
|
|
7
8
|
readonly width?: number | null | undefined | 'auto';
|
|
8
9
|
readonly height?: number | null | undefined | 'auto';
|
|
9
10
|
readonly locale?: string;
|
|
10
11
|
};
|
|
11
|
-
export
|
|
12
|
+
export type EChartsReactProps = {
|
|
12
13
|
/**
|
|
13
14
|
* echarts library entry, use it for import necessary.
|
|
14
15
|
*/
|
|
@@ -35,6 +36,10 @@ export declare type EChartsReactProps = {
|
|
|
35
36
|
* notMerge config for echarts, default is `false`
|
|
36
37
|
*/
|
|
37
38
|
readonly notMerge?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* replaceMerge config for echarts, default is `null`
|
|
41
|
+
*/
|
|
42
|
+
readonly replaceMerge?: string | string[];
|
|
38
43
|
/**
|
|
39
44
|
* lazyUpdate config for echarts, default is `false`
|
|
40
45
|
*/
|
|
@@ -52,7 +57,7 @@ export declare type EChartsReactProps = {
|
|
|
52
57
|
*/
|
|
53
58
|
readonly opts?: Opts;
|
|
54
59
|
/**
|
|
55
|
-
* when after chart
|
|
60
|
+
* when after chart render, do the callback with echarts instance
|
|
56
61
|
*/
|
|
57
62
|
readonly onChartReady?: (instance: EChartsInstance) => void;
|
|
58
63
|
/**
|
|
@@ -63,4 +68,8 @@ export declare type EChartsReactProps = {
|
|
|
63
68
|
* should update echarts options
|
|
64
69
|
*/
|
|
65
70
|
readonly shouldSetOption?: (prevProps: EChartsReactProps, props: EChartsReactProps) => boolean;
|
|
71
|
+
/**
|
|
72
|
+
* should trigger resize when window resize
|
|
73
|
+
*/
|
|
74
|
+
readonly autoResize?: boolean;
|
|
66
75
|
};
|
package/esm/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { CSSProperties } from 'react';\n\nexport type EChartsOption
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { CSSProperties } from 'react';\nimport type { EChartsOption, EChartsType } from 'echarts';\n\nexport type { EChartsOption };\n\nexport type EChartsInstance = EChartsType;\n\nexport type Opts = {\n readonly devicePixelRatio?: number;\n readonly renderer?: 'canvas' | 'svg';\n readonly width?: number | null | undefined | 'auto';\n readonly height?: number | null | undefined | 'auto';\n readonly locale?: string;\n};\n\nexport type EChartsReactProps = {\n /**\n * echarts library entry, use it for import necessary.\n */\n readonly echarts?: any;\n /**\n * `className` for container\n */\n readonly className?: string;\n /**\n * `style` for container\n */\n readonly style?: CSSProperties;\n /**\n * echarts option\n */\n readonly option: EChartsOption;\n /**\n * echarts theme config, can be:\n * 1. theme name string\n * 2. theme object\n */\n readonly theme?: string | Record<string, any>;\n /**\n * notMerge config for echarts, default is `false`\n */\n readonly notMerge?: boolean;\n /**\n * replaceMerge config for echarts, default is `null`\n */\n readonly replaceMerge?: string | string[];\n /**\n * lazyUpdate config for echarts, default is `false`\n */\n readonly lazyUpdate?: boolean;\n /**\n * showLoading config for echarts, default is `false`\n */\n readonly showLoading?: boolean;\n /**\n * loadingOption config for echarts, default is `null`\n */\n readonly loadingOption?: any;\n /**\n * echarts opts config, default is `{}`\n */\n readonly opts?: Opts;\n /**\n * when after chart render, do the callback with echarts instance\n */\n readonly onChartReady?: (instance: EChartsInstance) => void;\n /**\n * bind events, default is `{}`\n */\n readonly onEvents?: Record<string, Function>;\n /**\n * should update echarts options\n */\n readonly shouldSetOption?: (prevProps: EChartsReactProps, props: EChartsReactProps) => boolean;\n\n /**\n * should trigger resize when window resize\n */\n readonly autoResize?: boolean;\n};\n"]}
|
package/lib/core.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ECharts } from 'echarts';
|
|
1
2
|
import { PureComponent } from 'react';
|
|
2
3
|
import { EChartsReactProps } from './types';
|
|
3
4
|
/**
|
|
@@ -8,6 +9,10 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
8
9
|
* echarts render container
|
|
9
10
|
*/
|
|
10
11
|
ele: HTMLElement;
|
|
12
|
+
/**
|
|
13
|
+
* if this is the first time we are resizing
|
|
14
|
+
*/
|
|
15
|
+
private isInitialResize;
|
|
11
16
|
/**
|
|
12
17
|
* echarts library entry
|
|
13
18
|
*/
|
|
@@ -16,12 +21,11 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
16
21
|
componentDidMount(): void;
|
|
17
22
|
componentDidUpdate(prevProps: EChartsReactProps): void;
|
|
18
23
|
componentWillUnmount(): void;
|
|
24
|
+
initEchartsInstance(): Promise<ECharts>;
|
|
19
25
|
/**
|
|
20
|
-
* return the echart object
|
|
21
|
-
* 1. if exist, return the existed instance
|
|
22
|
-
* 2. or new one instance
|
|
26
|
+
* return the existing echart object
|
|
23
27
|
*/
|
|
24
|
-
getEchartsInstance():
|
|
28
|
+
getEchartsInstance(): ECharts;
|
|
25
29
|
/**
|
|
26
30
|
* dispose echarts and clear size-sensor
|
|
27
31
|
*/
|
|
@@ -31,9 +35,14 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
31
35
|
*/
|
|
32
36
|
private renderNewEcharts;
|
|
33
37
|
private bindEvents;
|
|
38
|
+
private offEvents;
|
|
34
39
|
/**
|
|
35
40
|
* render the echarts
|
|
36
41
|
*/
|
|
37
42
|
private updateEChartsOption;
|
|
43
|
+
/**
|
|
44
|
+
* resize wrapper
|
|
45
|
+
*/
|
|
46
|
+
private resize;
|
|
38
47
|
render(): JSX.Element;
|
|
39
48
|
}
|
package/lib/core.js
CHANGED
|
@@ -16,6 +16,7 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
16
16
|
var _this = _super.call(this, props) || this;
|
|
17
17
|
_this.echarts = props.echarts;
|
|
18
18
|
_this.ele = null;
|
|
19
|
+
_this.isInitialResize = true;
|
|
19
20
|
return _this;
|
|
20
21
|
}
|
|
21
22
|
EChartsReactCore.prototype.componentDidMount = function () {
|
|
@@ -28,48 +29,70 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
28
29
|
* default is true
|
|
29
30
|
*/
|
|
30
31
|
var shouldSetOption = this.props.shouldSetOption;
|
|
31
|
-
if (is_function_1.isFunction(shouldSetOption) && !shouldSetOption(prevProps, this.props)) {
|
|
32
|
+
if ((0, is_function_1.isFunction)(shouldSetOption) && !shouldSetOption(prevProps, this.props)) {
|
|
32
33
|
return;
|
|
33
34
|
}
|
|
34
35
|
// 以下属性修改的时候,需要 dispose 之后再新建
|
|
35
36
|
// 1. 切换 theme 的时候
|
|
36
37
|
// 2. 修改 opts 的时候
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
!is_equal_1.isEqual(prevProps.opts, this.props.opts) ||
|
|
40
|
-
!is_equal_1.isEqual(prevProps.onEvents, this.props.onEvents)) {
|
|
38
|
+
if (!(0, is_equal_1.isEqual)(prevProps.theme, this.props.theme) ||
|
|
39
|
+
!(0, is_equal_1.isEqual)(prevProps.opts, this.props.opts)) {
|
|
41
40
|
this.dispose();
|
|
42
41
|
this.renderNewEcharts(); // 重建
|
|
43
42
|
return;
|
|
44
43
|
}
|
|
45
|
-
//
|
|
46
|
-
var
|
|
47
|
-
if (is_equal_1.isEqual(
|
|
48
|
-
|
|
44
|
+
// 修改 onEvent 的时候先移除历史事件再添加
|
|
45
|
+
var echartsInstance = this.getEchartsInstance();
|
|
46
|
+
if (!(0, is_equal_1.isEqual)(prevProps.onEvents, this.props.onEvents)) {
|
|
47
|
+
this.offEvents(echartsInstance, prevProps.onEvents);
|
|
48
|
+
this.bindEvents(echartsInstance, this.props.onEvents);
|
|
49
|
+
}
|
|
50
|
+
// when these props are not isEqual, update echarts
|
|
51
|
+
var pickKeys = ['option', 'notMerge', 'replaceMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
|
|
52
|
+
if (!(0, is_equal_1.isEqual)((0, pick_1.pick)(this.props, pickKeys), (0, pick_1.pick)(prevProps, pickKeys))) {
|
|
53
|
+
this.updateEChartsOption();
|
|
49
54
|
}
|
|
50
|
-
var echartsInstance = this.updateEChartsOption();
|
|
51
55
|
/**
|
|
52
56
|
* when style or class name updated, change size.
|
|
53
57
|
*/
|
|
54
|
-
if (!is_equal_1.isEqual(prevProps.style, this.props.style) || !is_equal_1.isEqual(prevProps.className, this.props.className)) {
|
|
55
|
-
|
|
56
|
-
echartsInstance.resize();
|
|
57
|
-
}
|
|
58
|
-
catch (e) {
|
|
59
|
-
console.warn(e);
|
|
60
|
-
}
|
|
58
|
+
if (!(0, is_equal_1.isEqual)(prevProps.style, this.props.style) || !(0, is_equal_1.isEqual)(prevProps.className, this.props.className)) {
|
|
59
|
+
this.resize();
|
|
61
60
|
}
|
|
62
61
|
};
|
|
63
62
|
EChartsReactCore.prototype.componentWillUnmount = function () {
|
|
64
63
|
this.dispose();
|
|
65
64
|
};
|
|
65
|
+
/*
|
|
66
|
+
* initialise an echarts instance
|
|
67
|
+
*/
|
|
68
|
+
EChartsReactCore.prototype.initEchartsInstance = function () {
|
|
69
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
70
|
+
var _this = this;
|
|
71
|
+
return tslib_1.__generator(this, function (_a) {
|
|
72
|
+
return [2 /*return*/, new Promise(function (resolve) {
|
|
73
|
+
// create temporary echart instance
|
|
74
|
+
_this.echarts.init(_this.ele, _this.props.theme, _this.props.opts);
|
|
75
|
+
var echartsInstance = _this.getEchartsInstance();
|
|
76
|
+
echartsInstance.on('finished', function () {
|
|
77
|
+
// get final width and height
|
|
78
|
+
var width = _this.ele.clientWidth;
|
|
79
|
+
var height = _this.ele.clientHeight;
|
|
80
|
+
// dispose temporary echart instance
|
|
81
|
+
_this.echarts.dispose(_this.ele);
|
|
82
|
+
// recreate echart instance
|
|
83
|
+
// we use final width and height only if not originally provided as opts
|
|
84
|
+
var opts = tslib_1.__assign({ width: width, height: height }, _this.props.opts);
|
|
85
|
+
resolve(_this.echarts.init(_this.ele, _this.props.theme, opts));
|
|
86
|
+
});
|
|
87
|
+
})];
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
};
|
|
66
91
|
/**
|
|
67
|
-
* return the echart object
|
|
68
|
-
* 1. if exist, return the existed instance
|
|
69
|
-
* 2. or new one instance
|
|
92
|
+
* return the existing echart object
|
|
70
93
|
*/
|
|
71
94
|
EChartsReactCore.prototype.getEchartsInstance = function () {
|
|
72
|
-
return this.echarts.getInstanceByDom(this.ele)
|
|
95
|
+
return this.echarts.getInstanceByDom(this.ele);
|
|
73
96
|
};
|
|
74
97
|
/**
|
|
75
98
|
* dispose echarts and clear size-sensor
|
|
@@ -77,7 +100,7 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
77
100
|
EChartsReactCore.prototype.dispose = function () {
|
|
78
101
|
if (this.ele) {
|
|
79
102
|
try {
|
|
80
|
-
size_sensor_1.clear(this.ele);
|
|
103
|
+
(0, size_sensor_1.clear)(this.ele);
|
|
81
104
|
}
|
|
82
105
|
catch (e) {
|
|
83
106
|
console.warn(e);
|
|
@@ -90,31 +113,40 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
90
113
|
* render a new echarts instance
|
|
91
114
|
*/
|
|
92
115
|
EChartsReactCore.prototype.renderNewEcharts = function () {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
116
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
117
|
+
var _a, onEvents, onChartReady, _b, autoResize, echartsInstance;
|
|
118
|
+
var _this = this;
|
|
119
|
+
return tslib_1.__generator(this, function (_c) {
|
|
120
|
+
switch (_c.label) {
|
|
121
|
+
case 0:
|
|
122
|
+
_a = this.props, onEvents = _a.onEvents, onChartReady = _a.onChartReady, _b = _a.autoResize, autoResize = _b === void 0 ? true : _b;
|
|
123
|
+
// 1. init echarts instance
|
|
124
|
+
return [4 /*yield*/, this.initEchartsInstance()];
|
|
125
|
+
case 1:
|
|
126
|
+
// 1. init echarts instance
|
|
127
|
+
_c.sent();
|
|
128
|
+
echartsInstance = this.updateEChartsOption();
|
|
129
|
+
// 3. bind events
|
|
130
|
+
this.bindEvents(echartsInstance, onEvents || {});
|
|
131
|
+
// 4. on chart ready
|
|
132
|
+
if ((0, is_function_1.isFunction)(onChartReady))
|
|
133
|
+
onChartReady(echartsInstance);
|
|
134
|
+
// 5. on resize
|
|
135
|
+
if (this.ele && autoResize) {
|
|
136
|
+
(0, size_sensor_1.bind)(this.ele, function () {
|
|
137
|
+
_this.resize();
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return [2 /*return*/];
|
|
109
141
|
}
|
|
110
142
|
});
|
|
111
|
-
}
|
|
143
|
+
});
|
|
112
144
|
};
|
|
113
145
|
// bind the events
|
|
114
146
|
EChartsReactCore.prototype.bindEvents = function (instance, events) {
|
|
115
147
|
function _bindEvent(eventName, func) {
|
|
116
148
|
// ignore the event config which not satisfy
|
|
117
|
-
if (is_string_1.isString(eventName) && is_function_1.isFunction(func)) {
|
|
149
|
+
if ((0, is_string_1.isString)(eventName) && (0, is_function_1.isFunction)(func)) {
|
|
118
150
|
// binding event
|
|
119
151
|
instance.on(eventName, function (param) {
|
|
120
152
|
func(param, instance);
|
|
@@ -128,15 +160,26 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
128
160
|
}
|
|
129
161
|
}
|
|
130
162
|
};
|
|
163
|
+
// off the events
|
|
164
|
+
EChartsReactCore.prototype.offEvents = function (instance, events) {
|
|
165
|
+
if (!events)
|
|
166
|
+
return;
|
|
167
|
+
// loop and off
|
|
168
|
+
for (var eventName in events) {
|
|
169
|
+
if ((0, is_string_1.isString)(eventName)) {
|
|
170
|
+
instance.off(eventName);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
};
|
|
131
174
|
/**
|
|
132
175
|
* render the echarts
|
|
133
176
|
*/
|
|
134
177
|
EChartsReactCore.prototype.updateEChartsOption = function () {
|
|
135
|
-
var _a = this.props, option = _a.option, _b = _a.notMerge, notMerge = _b === void 0 ? false : _b, _c = _a.lazyUpdate, lazyUpdate =
|
|
178
|
+
var _a = this.props, option = _a.option, _b = _a.notMerge, notMerge = _b === void 0 ? false : _b, _c = _a.replaceMerge, replaceMerge = _c === void 0 ? null : _c, _d = _a.lazyUpdate, lazyUpdate = _d === void 0 ? false : _d, showLoading = _a.showLoading, _e = _a.loadingOption, loadingOption = _e === void 0 ? null : _e;
|
|
136
179
|
// 1. get or initial the echarts object
|
|
137
180
|
var echartInstance = this.getEchartsInstance();
|
|
138
181
|
// 2. set the echarts option
|
|
139
|
-
echartInstance.setOption(option, notMerge, lazyUpdate);
|
|
182
|
+
echartInstance.setOption(option, { notMerge: notMerge, replaceMerge: replaceMerge, lazyUpdate: lazyUpdate });
|
|
140
183
|
// 3. set loading mask
|
|
141
184
|
if (showLoading)
|
|
142
185
|
echartInstance.showLoading(loadingOption);
|
|
@@ -144,6 +187,28 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
144
187
|
echartInstance.hideLoading();
|
|
145
188
|
return echartInstance;
|
|
146
189
|
};
|
|
190
|
+
/**
|
|
191
|
+
* resize wrapper
|
|
192
|
+
*/
|
|
193
|
+
EChartsReactCore.prototype.resize = function () {
|
|
194
|
+
// 1. get the echarts object
|
|
195
|
+
var echartsInstance = this.getEchartsInstance();
|
|
196
|
+
// 2. call echarts instance resize if not the initial resize
|
|
197
|
+
// resize should not happen on first render as it will cancel initial echarts animations
|
|
198
|
+
if (!this.isInitialResize) {
|
|
199
|
+
try {
|
|
200
|
+
echartsInstance.resize({
|
|
201
|
+
width: 'auto',
|
|
202
|
+
height: 'auto',
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
catch (e) {
|
|
206
|
+
console.warn(e);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// 3. update variable for future calls
|
|
210
|
+
this.isInitialResize = false;
|
|
211
|
+
};
|
|
147
212
|
EChartsReactCore.prototype.render = function () {
|
|
148
213
|
var _this = this;
|
|
149
214
|
var _a = this.props, style = _a.style, _b = _a.className, className = _b === void 0 ? '' : _b;
|
|
@@ -151,7 +216,7 @@ var EChartsReactCore = /** @class */ (function (_super) {
|
|
|
151
216
|
var newStyle = tslib_1.__assign({ height: 300 }, style);
|
|
152
217
|
return (react_1.default.createElement("div", { ref: function (e) {
|
|
153
218
|
_this.ele = e;
|
|
154
|
-
}, style: newStyle, className: "echarts-for-react "
|
|
219
|
+
}, style: newStyle, className: "echarts-for-react ".concat(className) }));
|
|
155
220
|
};
|
|
156
221
|
return EChartsReactCore;
|
|
157
222
|
}(react_1.PureComponent));
|
package/lib/core.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.tsx"],"names":[],"mappings":";;;AAAA,qDAA6C;AAC7C,2CAA0C;AAC1C,sCAAqC;AACrC,oDAAkD;AAClD,gDAA8C;AAC9C,8CAA4C;AAG5C;;GAEG;AACH;IAA8C,4CAAgC;IAW5E,0BAAY,KAAwB;QAApC,YACE,kBAAM,KAAK,CAAC,SAIb;QAFC,KAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;QAC7B,KAAI,CAAC,GAAG,GAAG,IAAI,CAAC;;IAClB,CAAC;IAED,4CAAiB,GAAjB;QACE,IAAI,CAAC,gBAAgB,EAAE,CAAC;IAC1B,CAAC;IAED,SAAS;IACT,6CAAkB,GAAlB,UAAmB,SAA4B;QAC7C;;;WAGG;QACK,IAAA,eAAe,GAAK,IAAI,CAAC,KAAK,gBAAf,CAAgB;QACvC,IAAI,wBAAU,CAAC,eAAe,CAAC,IAAI,CAAC,eAAe,CAAC,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,EAA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QAC7C,uBAAuB;QACvB,IAAM,QAAQ,sBAAK,MAAM,EAAE,GAAG,IAAK,KAAK,CAAE,CAAC;QAE3C,OAAO,CACL,uCACE,GAAG,EAAE,UAAC,CAAc;gBAClB,KAAI,CAAC,GAAG,GAAG,CAAC,CAAC;YACf,CAAC,EACD,KAAK,EAAE,QAAQ,EACf,SAAS,EAAE,uBAAqB,SAAW,GAC3C,CACH,CAAC;IACJ,CAAC;IACH,uBAAC;AAAD,CAAC,AA7KD,CAA8C,qBAAa,GA6K1D","sourcesContent":["import React, { PureComponent } from 'react';\nimport { bind, clear } from 'size-sensor';\nimport { pick } from './helper/pick';\nimport { isFunction } from './helper/is-function';\nimport { isString } from './helper/is-string';\nimport { isEqual } from './helper/is-equal';\nimport { EChartsReactProps, EChartsInstance } from './types';\n\n/**\n * core component for echarts binding\n */\nexport default class EChartsReactCore extends PureComponent<EChartsReactProps> {\n /**\n * echarts render container\n */\n public ele: HTMLElement;\n\n /**\n * echarts library entry\n */\n protected echarts: any;\n\n constructor(props: EChartsReactProps) {\n super(props);\n\n this.echarts = props.echarts;\n this.ele = null;\n }\n\n componentDidMount() {\n this.renderNewEcharts();\n }\n\n // update\n componentDidUpdate(prevProps: EChartsReactProps) {\n /**\n * if shouldSetOption return false, then return, not update echarts options\n * default is true\n */\n const { shouldSetOption } = this.props;\n if (isFunction(shouldSetOption) && !shouldSetOption(prevProps, this.props)) {\n return;\n }\n\n // 以下属性修改的时候,需要 dispose 之后再新建\n // 1. 切换 theme 的时候\n // 2. 修改 opts 的时候\n // 3. 修改 onEvents 的时候,这样可以取消所有之前绑定的事件 issue #151\n if (\n !isEqual(prevProps.theme, this.props.theme) ||\n !isEqual(prevProps.opts, this.props.opts) ||\n !isEqual(prevProps.onEvents, this.props.onEvents)\n ) {\n this.dispose();\n\n this.renderNewEcharts(); // 重建\n return;\n }\n\n // when thoes props isEqual, do not update echarts\n const pickKeys = ['option', 'notMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];\n if (isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {\n return;\n }\n\n const echartsInstance = this.updateEChartsOption();\n /**\n * when style or class name updated, change size.\n */\n if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {\n try {\n echartsInstance.resize();\n } catch (e) {\n console.warn(e);\n }\n }\n }\n\n componentWillUnmount() {\n this.dispose();\n }\n\n /**\n * return the echart object\n * 1. if exist, return the existed instance\n * 2. or new one instance\n */\n public getEchartsInstance() {\n return this.echarts.getInstanceByDom(this.ele) || this.echarts.init(this.ele, this.props.theme, this.props.opts);\n }\n\n /**\n * dispose echarts and clear size-sensor\n */\n private dispose() {\n if (this.ele) {\n try {\n clear(this.ele);\n } catch (e) {\n console.warn(e);\n }\n // dispose echarts instance\n this.echarts.dispose(this.ele);\n }\n }\n\n /**\n * render a new echarts instance\n */\n private renderNewEcharts() {\n const { onEvents, onChartReady } = this.props;\n\n // 1. new echarts instance\n const echartsInstance = this.updateEChartsOption();\n\n // 2. bind events\n this.bindEvents(echartsInstance, onEvents || {});\n\n // 3. on chart ready\n if (isFunction(onChartReady)) onChartReady(echartsInstance);\n\n // 4. on resize\n if (this.ele) {\n bind(this.ele, () => {\n try {\n echartsInstance.resize();\n } catch (e) {\n console.warn(e);\n }\n });\n }\n }\n\n // bind the events\n private bindEvents(instance, events: EChartsReactProps['onEvents']) {\n function _bindEvent(eventName: string, func: Function) {\n // ignore the event config which not satisfy\n if (isString(eventName) && isFunction(func)) {\n // binding event\n instance.on(eventName, (param) => {\n func(param, instance);\n });\n }\n }\n\n // loop and bind\n for (const eventName in events) {\n if (Object.prototype.hasOwnProperty.call(events, eventName)) {\n _bindEvent(eventName, events[eventName]);\n }\n }\n }\n\n /**\n * render the echarts\n */\n private updateEChartsOption(): EChartsInstance {\n const { option, notMerge = false, lazyUpdate = false, showLoading, loadingOption = null } = this.props;\n // 1. get or initial the echarts object\n const echartInstance = this.getEchartsInstance();\n // 2. set the echarts option\n echartInstance.setOption(option, notMerge, lazyUpdate);\n // 3. set loading mask\n if (showLoading) echartInstance.showLoading(loadingOption);\n else echartInstance.hideLoading();\n\n return echartInstance;\n }\n\n render(): JSX.Element {\n const { style, className = '' } = this.props;\n // default height = 300\n const newStyle = { height: 300, ...style };\n\n return (\n <div\n ref={(e: HTMLElement) => {\n this.ele = e;\n }}\n style={newStyle}\n className={`echarts-for-react ${className}`}\n />\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.tsx"],"names":[],"mappings":";;;AACA,qDAA6C;AAC7C,2CAA0C;AAC1C,sCAAqC;AACrC,oDAAkD;AAClD,gDAA8C;AAC9C,8CAA4C;AAG5C;;GAEG;AACH;IAA8C,4CAAgC;IAgB5E,0BAAY,KAAwB;QAApC,YACE,kBAAM,KAAK,CAAC,SAKb;QAHC,KAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;QAC7B,KAAI,CAAC,GAAG,GAAG,IAAI,CAAC;QAChB,KAAI,CAAC,eAAe,GAAG,IAAI,CAAC;;IAC9B,CAAC;IAED,4CAAiB,GAAjB;QACE,IAAI,CAAC,gBAAgB,EAAE,CAAC;IAC1B,CAAC;IAED,SAAS;IACT,6CAAkB,GAAlB,UAAmB,SAA4B;QAC7C;;;WAGG;QACK,IAAA,eAAe,GAAK,IAAI,CAAC,KAAK,gBAAf,CAAgB;QACvC,IAAI,IAAA,wBAAU,EAAC,eAAe,CAAC,IAAI,CAAC,eAAe,CAAC,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,EAAE;YAC1E,OAAO;SACR;QAED,6BAA6B;QAC7B,kBAAkB;QAClB,iBAAiB;QACjB,IACE,CAAC,IAAA,kBAAO,EAAC,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC;YAC3C,CAAC,IAAA,kBAAO,EAAC,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EACzC;YACA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AAM,EAAE,MAAM;iBACf,CAAC,CAAC;aACJ;YAAC,OAAO,CAAC,EAAE;gBACV,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aACjB;SACF;QAED,sCAAsC;QACtC,IAAI,CAAC,eAAe,GAAG,KAAK,CAAC;IAC/B,CAAC;IAED,iCAAM,GAAN;QAAA,iBAcC;QAbO,IAAA,KAA4B,IAAI,CAAC,KAAK,EAApC,KAAK,WAAA,EAAE,iBAAc,EAAd,SAAS,mBAAG,EAAE,KAAe,CAAC;QAC7C,uBAAuB;QACvB,IAAM,QAAQ,sBAAK,MAAM,EAAE,GAAG,IAAK,KAAK,CAAE,CAAC;QAE3C,OAAO,CACL,uCACE,GAAG,EAAE,UAAC,CAAc;gBAClB,KAAI,CAAC,GAAG,GAAG,CAAC,CAAC;YACf,CAAC,EACD,KAAK,EAAE,QAAQ,EACf,SAAS,EAAE,4BAAqB,SAAS,CAAE,GAC3C,CACH,CAAC;IACJ,CAAC;IACH,uBAAC;AAAD,CAAC,AAvPD,CAA8C,qBAAa,GAuP1D","sourcesContent":["import type { ECharts } from 'echarts';\nimport React, { PureComponent } from 'react';\nimport { bind, clear } from 'size-sensor';\nimport { pick } from './helper/pick';\nimport { isFunction } from './helper/is-function';\nimport { isString } from './helper/is-string';\nimport { isEqual } from './helper/is-equal';\nimport { EChartsReactProps, EChartsInstance } from './types';\n\n/**\n * core component for echarts binding\n */\nexport default class EChartsReactCore extends PureComponent<EChartsReactProps> {\n /**\n * echarts render container\n */\n public ele: HTMLElement;\n\n /**\n * if this is the first time we are resizing\n */\n private isInitialResize: boolean;\n\n /**\n * echarts library entry\n */\n protected echarts: any;\n\n constructor(props: EChartsReactProps) {\n super(props);\n\n this.echarts = props.echarts;\n this.ele = null;\n this.isInitialResize = true;\n }\n\n componentDidMount() {\n this.renderNewEcharts();\n }\n\n // update\n componentDidUpdate(prevProps: EChartsReactProps) {\n /**\n * if shouldSetOption return false, then return, not update echarts options\n * default is true\n */\n const { shouldSetOption } = this.props;\n if (isFunction(shouldSetOption) && !shouldSetOption(prevProps, this.props)) {\n return;\n }\n\n // 以下属性修改的时候,需要 dispose 之后再新建\n // 1. 切换 theme 的时候\n // 2. 修改 opts 的时候\n if (\n !isEqual(prevProps.theme, this.props.theme) ||\n !isEqual(prevProps.opts, this.props.opts)\n ) {\n this.dispose();\n\n this.renderNewEcharts(); // 重建\n return;\n }\n\n // 修改 onEvent 的时候先移除历史事件再添加\n const echartsInstance = this.getEchartsInstance();\n if (!isEqual(prevProps.onEvents, this.props.onEvents)) {\n this.offEvents(echartsInstance, prevProps.onEvents);\n this.bindEvents(echartsInstance, this.props.onEvents);\n }\n\n // when these props are not isEqual, update echarts\n const pickKeys = ['option', 'notMerge', 'replaceMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];\n if (!isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {\n this.updateEChartsOption();\n }\n\n /**\n * when style or class name updated, change size.\n */\n if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {\n this.resize();\n }\n }\n\n componentWillUnmount() {\n this.dispose();\n }\n\n /*\n * initialise an echarts instance\n */\n public async initEchartsInstance(): Promise<ECharts> {\n return new Promise((resolve) => {\n // create temporary echart instance\n this.echarts.init(this.ele, this.props.theme, this.props.opts);\n const echartsInstance = this.getEchartsInstance();\n\n echartsInstance.on('finished', () => {\n // get final width and height\n const width = this.ele.clientWidth;\n const height = this.ele.clientHeight;\n\n // dispose temporary echart instance\n this.echarts.dispose(this.ele);\n\n // recreate echart instance\n // we use final width and height only if not originally provided as opts\n const opts = {\n width,\n height,\n ...this.props.opts,\n };\n resolve(this.echarts.init(this.ele, this.props.theme, opts));\n });\n });\n }\n\n /**\n * return the existing echart object\n */\n public getEchartsInstance(): ECharts {\n return this.echarts.getInstanceByDom(this.ele);\n }\n\n /**\n * dispose echarts and clear size-sensor\n */\n private dispose() {\n if (this.ele) {\n try {\n clear(this.ele);\n } catch (e) {\n console.warn(e);\n }\n // dispose echarts instance\n this.echarts.dispose(this.ele);\n }\n }\n\n /**\n * render a new echarts instance\n */\n private async renderNewEcharts() {\n const { onEvents, onChartReady, autoResize = true } = this.props;\n\n // 1. init echarts instance\n await this.initEchartsInstance();\n\n // 2. update echarts instance\n const echartsInstance = this.updateEChartsOption();\n\n // 3. bind events\n this.bindEvents(echartsInstance, onEvents || {});\n\n // 4. on chart ready\n if (isFunction(onChartReady)) onChartReady(echartsInstance);\n\n // 5. on resize\n if (this.ele && autoResize) {\n bind(this.ele, () => {\n this.resize();\n });\n }\n }\n\n // bind the events\n private bindEvents(instance, events: EChartsReactProps['onEvents']) {\n function _bindEvent(eventName: string, func: Function) {\n // ignore the event config which not satisfy\n if (isString(eventName) && isFunction(func)) {\n // binding event\n instance.on(eventName, (param) => {\n func(param, instance);\n });\n }\n }\n\n // loop and bind\n for (const eventName in events) {\n if (Object.prototype.hasOwnProperty.call(events, eventName)) {\n _bindEvent(eventName, events[eventName]);\n }\n }\n }\n\n // off the events\n private offEvents(instance, events: EChartsReactProps['onEvents']) {\n if (!events) return;\n // loop and off\n for (const eventName in events) {\n if (isString(eventName)) {\n instance.off(eventName);\n }\n }\n }\n\n /**\n * render the echarts\n */\n private updateEChartsOption(): EChartsInstance {\n const {\n option,\n notMerge = false,\n replaceMerge = null,\n lazyUpdate = false,\n showLoading,\n loadingOption = null,\n } = this.props;\n // 1. get or initial the echarts object\n const echartInstance = this.getEchartsInstance();\n // 2. set the echarts option\n echartInstance.setOption(option, { notMerge, replaceMerge, lazyUpdate });\n // 3. set loading mask\n if (showLoading) echartInstance.showLoading(loadingOption);\n else echartInstance.hideLoading();\n\n return echartInstance;\n }\n\n /**\n * resize wrapper\n */\n private resize() {\n // 1. get the echarts object\n const echartsInstance = this.getEchartsInstance();\n\n // 2. call echarts instance resize if not the initial resize\n // resize should not happen on first render as it will cancel initial echarts animations\n if (!this.isInitialResize) {\n try {\n echartsInstance.resize({\n width: 'auto',\n height: 'auto',\n });\n } catch (e) {\n console.warn(e);\n }\n }\n\n // 3. update variable for future calls\n this.isInitialResize = false;\n }\n\n render(): JSX.Element {\n const { style, className = '' } = this.props;\n // default height = 300\n const newStyle = { height: 300, ...style };\n\n return (\n <div\n ref={(e: HTMLElement) => {\n this.ele = e;\n }}\n style={newStyle}\n className={`echarts-for-react ${className}`}\n />\n );\n }\n}\n"]}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EChartsReactProps, EChartsOption, EChartsInstance } from './types';
|
|
2
2
|
import EChartsReactCore from './core';
|
|
3
|
-
export { EChartsReactProps, EChartsOption, EChartsInstance };
|
|
3
|
+
export type { EChartsReactProps, EChartsOption, EChartsInstance };
|
|
4
4
|
export default class EChartsReact extends EChartsReactCore {
|
|
5
5
|
constructor(props: EChartsReactProps);
|
|
6
6
|
}
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uDAAmC;AAEnC,wDAAsC;AAItC,2CAA2C;AAC3C;IAA0C,wCAAgB;IACxD,sBAAY,KAAwB;QAApC,YACE,kBAAM,KAAK,CAAC,SAIb;QAFC,mBAAmB;QACnB,KAAI,CAAC,OAAO,GAAG,OAAO,CAAC;;IACzB,CAAC;IACH,mBAAC;AAAD,CAAC,AAPD,CAA0C,cAAgB,GAOzD","sourcesContent":["import * as echarts from 'echarts';\nimport { EChartsReactProps, EChartsOption, EChartsInstance } from './types';\nimport EChartsReactCore from './core';\n\nexport { EChartsReactProps, EChartsOption, EChartsInstance };\n\n// export the Component the echarts Object.\nexport default class EChartsReact extends EChartsReactCore {\n constructor(props: EChartsReactProps) {\n super(props);\n\n // 初始化为 echarts 整个包\n this.echarts = echarts;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uDAAmC;AAEnC,wDAAsC;AAItC,2CAA2C;AAC3C;IAA0C,wCAAgB;IACxD,sBAAY,KAAwB;QAApC,YACE,kBAAM,KAAK,CAAC,SAIb;QAFC,mBAAmB;QACnB,KAAI,CAAC,OAAO,GAAG,OAAO,CAAC;;IACzB,CAAC;IACH,mBAAC;AAAD,CAAC,AAPD,CAA0C,cAAgB,GAOzD","sourcesContent":["import * as echarts from 'echarts';\nimport { EChartsReactProps, EChartsOption, EChartsInstance } from './types';\nimport EChartsReactCore from './core';\n\nexport type { EChartsReactProps, EChartsOption, EChartsInstance };\n\n// export the Component the echarts Object.\nexport default class EChartsReact extends EChartsReactCore {\n constructor(props: EChartsReactProps) {\n super(props);\n\n // 初始化为 echarts 整个包\n this.echarts = echarts;\n }\n}\n"]}
|
package/lib/types.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { CSSProperties } from 'react';
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
export
|
|
2
|
+
import type { EChartsOption, EChartsType } from 'echarts';
|
|
3
|
+
export type { EChartsOption };
|
|
4
|
+
export type EChartsInstance = EChartsType;
|
|
5
|
+
export type Opts = {
|
|
5
6
|
readonly devicePixelRatio?: number;
|
|
6
7
|
readonly renderer?: 'canvas' | 'svg';
|
|
7
8
|
readonly width?: number | null | undefined | 'auto';
|
|
8
9
|
readonly height?: number | null | undefined | 'auto';
|
|
9
10
|
readonly locale?: string;
|
|
10
11
|
};
|
|
11
|
-
export
|
|
12
|
+
export type EChartsReactProps = {
|
|
12
13
|
/**
|
|
13
14
|
* echarts library entry, use it for import necessary.
|
|
14
15
|
*/
|
|
@@ -35,6 +36,10 @@ export declare type EChartsReactProps = {
|
|
|
35
36
|
* notMerge config for echarts, default is `false`
|
|
36
37
|
*/
|
|
37
38
|
readonly notMerge?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* replaceMerge config for echarts, default is `null`
|
|
41
|
+
*/
|
|
42
|
+
readonly replaceMerge?: string | string[];
|
|
38
43
|
/**
|
|
39
44
|
* lazyUpdate config for echarts, default is `false`
|
|
40
45
|
*/
|
|
@@ -52,7 +57,7 @@ export declare type EChartsReactProps = {
|
|
|
52
57
|
*/
|
|
53
58
|
readonly opts?: Opts;
|
|
54
59
|
/**
|
|
55
|
-
* when after chart
|
|
60
|
+
* when after chart render, do the callback with echarts instance
|
|
56
61
|
*/
|
|
57
62
|
readonly onChartReady?: (instance: EChartsInstance) => void;
|
|
58
63
|
/**
|
|
@@ -63,4 +68,8 @@ export declare type EChartsReactProps = {
|
|
|
63
68
|
* should update echarts options
|
|
64
69
|
*/
|
|
65
70
|
readonly shouldSetOption?: (prevProps: EChartsReactProps, props: EChartsReactProps) => boolean;
|
|
71
|
+
/**
|
|
72
|
+
* should trigger resize when window resize
|
|
73
|
+
*/
|
|
74
|
+
readonly autoResize?: boolean;
|
|
66
75
|
};
|
package/lib/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { CSSProperties } from 'react';\n\nexport type EChartsOption
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { CSSProperties } from 'react';\nimport type { EChartsOption, EChartsType } from 'echarts';\n\nexport type { EChartsOption };\n\nexport type EChartsInstance = EChartsType;\n\nexport type Opts = {\n readonly devicePixelRatio?: number;\n readonly renderer?: 'canvas' | 'svg';\n readonly width?: number | null | undefined | 'auto';\n readonly height?: number | null | undefined | 'auto';\n readonly locale?: string;\n};\n\nexport type EChartsReactProps = {\n /**\n * echarts library entry, use it for import necessary.\n */\n readonly echarts?: any;\n /**\n * `className` for container\n */\n readonly className?: string;\n /**\n * `style` for container\n */\n readonly style?: CSSProperties;\n /**\n * echarts option\n */\n readonly option: EChartsOption;\n /**\n * echarts theme config, can be:\n * 1. theme name string\n * 2. theme object\n */\n readonly theme?: string | Record<string, any>;\n /**\n * notMerge config for echarts, default is `false`\n */\n readonly notMerge?: boolean;\n /**\n * replaceMerge config for echarts, default is `null`\n */\n readonly replaceMerge?: string | string[];\n /**\n * lazyUpdate config for echarts, default is `false`\n */\n readonly lazyUpdate?: boolean;\n /**\n * showLoading config for echarts, default is `false`\n */\n readonly showLoading?: boolean;\n /**\n * loadingOption config for echarts, default is `null`\n */\n readonly loadingOption?: any;\n /**\n * echarts opts config, default is `{}`\n */\n readonly opts?: Opts;\n /**\n * when after chart render, do the callback with echarts instance\n */\n readonly onChartReady?: (instance: EChartsInstance) => void;\n /**\n * bind events, default is `{}`\n */\n readonly onEvents?: Record<string, Function>;\n /**\n * should update echarts options\n */\n readonly shouldSetOption?: (prevProps: EChartsReactProps, props: EChartsReactProps) => boolean;\n\n /**\n * should trigger resize when window resize\n */\n readonly autoResize?: boolean;\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "echarts-for-react",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.3",
|
|
4
4
|
"description": " Apache Echarts components for React.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@commitlint/cli": "^12.0.0",
|
|
50
50
|
"@commitlint/config-angular": "^12.0.0",
|
|
51
|
-
"@types/jest": "^
|
|
51
|
+
"@types/jest": "^24.0.0",
|
|
52
52
|
"@types/node": "^14.14.31",
|
|
53
53
|
"@types/react": "^17.0.2",
|
|
54
54
|
"@typescript-eslint/eslint-plugin": "^4.15.2",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"eslint-plugin-prettier": "^3.3.1",
|
|
65
65
|
"gh-pages": "^3.1.0",
|
|
66
66
|
"husky": "^5.1.1",
|
|
67
|
-
"jest": "^
|
|
67
|
+
"jest": "^24.0.0",
|
|
68
68
|
"jest-canvas-mock": "^2.3.1",
|
|
69
69
|
"jest-electron": "^0.1.11",
|
|
70
70
|
"lint-md-cli": "^0.1.2",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"npm-run-all": "^4.1.5",
|
|
75
75
|
"prettier": "^2.2.1",
|
|
76
76
|
"rimraf": "^3.0.2",
|
|
77
|
-
"ts-jest": "^
|
|
77
|
+
"ts-jest": "^24.0.2",
|
|
78
78
|
"ts-loader": "^8.0.17",
|
|
79
79
|
"typescript": "^4.2.2"
|
|
80
80
|
},
|
package/src/core.tsx
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ECharts } from 'echarts';
|
|
1
2
|
import React, { PureComponent } from 'react';
|
|
2
3
|
import { bind, clear } from 'size-sensor';
|
|
3
4
|
import { pick } from './helper/pick';
|
|
@@ -15,6 +16,11 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
15
16
|
*/
|
|
16
17
|
public ele: HTMLElement;
|
|
17
18
|
|
|
19
|
+
/**
|
|
20
|
+
* if this is the first time we are resizing
|
|
21
|
+
*/
|
|
22
|
+
private isInitialResize: boolean;
|
|
23
|
+
|
|
18
24
|
/**
|
|
19
25
|
* echarts library entry
|
|
20
26
|
*/
|
|
@@ -25,6 +31,7 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
25
31
|
|
|
26
32
|
this.echarts = props.echarts;
|
|
27
33
|
this.ele = null;
|
|
34
|
+
this.isInitialResize = true;
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
componentDidMount() {
|
|
@@ -45,11 +52,9 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
45
52
|
// 以下属性修改的时候,需要 dispose 之后再新建
|
|
46
53
|
// 1. 切换 theme 的时候
|
|
47
54
|
// 2. 修改 opts 的时候
|
|
48
|
-
// 3. 修改 onEvents 的时候,这样可以取消所有之前绑定的事件 issue #151
|
|
49
55
|
if (
|
|
50
56
|
!isEqual(prevProps.theme, this.props.theme) ||
|
|
51
|
-
!isEqual(prevProps.opts, this.props.opts)
|
|
52
|
-
!isEqual(prevProps.onEvents, this.props.onEvents)
|
|
57
|
+
!isEqual(prevProps.opts, this.props.opts)
|
|
53
58
|
) {
|
|
54
59
|
this.dispose();
|
|
55
60
|
|
|
@@ -57,22 +62,24 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
57
62
|
return;
|
|
58
63
|
}
|
|
59
64
|
|
|
60
|
-
//
|
|
61
|
-
const
|
|
62
|
-
if (isEqual(
|
|
63
|
-
|
|
65
|
+
// 修改 onEvent 的时候先移除历史事件再添加
|
|
66
|
+
const echartsInstance = this.getEchartsInstance();
|
|
67
|
+
if (!isEqual(prevProps.onEvents, this.props.onEvents)) {
|
|
68
|
+
this.offEvents(echartsInstance, prevProps.onEvents);
|
|
69
|
+
this.bindEvents(echartsInstance, this.props.onEvents);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// when these props are not isEqual, update echarts
|
|
73
|
+
const pickKeys = ['option', 'notMerge', 'replaceMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
|
|
74
|
+
if (!isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {
|
|
75
|
+
this.updateEChartsOption();
|
|
64
76
|
}
|
|
65
77
|
|
|
66
|
-
const echartsInstance = this.updateEChartsOption();
|
|
67
78
|
/**
|
|
68
79
|
* when style or class name updated, change size.
|
|
69
80
|
*/
|
|
70
81
|
if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {
|
|
71
|
-
|
|
72
|
-
echartsInstance.resize();
|
|
73
|
-
} catch (e) {
|
|
74
|
-
console.warn(e);
|
|
75
|
-
}
|
|
82
|
+
this.resize();
|
|
76
83
|
}
|
|
77
84
|
}
|
|
78
85
|
|
|
@@ -80,13 +87,40 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
80
87
|
this.dispose();
|
|
81
88
|
}
|
|
82
89
|
|
|
90
|
+
/*
|
|
91
|
+
* initialise an echarts instance
|
|
92
|
+
*/
|
|
93
|
+
public async initEchartsInstance(): Promise<ECharts> {
|
|
94
|
+
return new Promise((resolve) => {
|
|
95
|
+
// create temporary echart instance
|
|
96
|
+
this.echarts.init(this.ele, this.props.theme, this.props.opts);
|
|
97
|
+
const echartsInstance = this.getEchartsInstance();
|
|
98
|
+
|
|
99
|
+
echartsInstance.on('finished', () => {
|
|
100
|
+
// get final width and height
|
|
101
|
+
const width = this.ele.clientWidth;
|
|
102
|
+
const height = this.ele.clientHeight;
|
|
103
|
+
|
|
104
|
+
// dispose temporary echart instance
|
|
105
|
+
this.echarts.dispose(this.ele);
|
|
106
|
+
|
|
107
|
+
// recreate echart instance
|
|
108
|
+
// we use final width and height only if not originally provided as opts
|
|
109
|
+
const opts = {
|
|
110
|
+
width,
|
|
111
|
+
height,
|
|
112
|
+
...this.props.opts,
|
|
113
|
+
};
|
|
114
|
+
resolve(this.echarts.init(this.ele, this.props.theme, opts));
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
83
119
|
/**
|
|
84
|
-
* return the echart object
|
|
85
|
-
* 1. if exist, return the existed instance
|
|
86
|
-
* 2. or new one instance
|
|
120
|
+
* return the existing echart object
|
|
87
121
|
*/
|
|
88
|
-
public getEchartsInstance() {
|
|
89
|
-
return this.echarts.getInstanceByDom(this.ele)
|
|
122
|
+
public getEchartsInstance(): ECharts {
|
|
123
|
+
return this.echarts.getInstanceByDom(this.ele);
|
|
90
124
|
}
|
|
91
125
|
|
|
92
126
|
/**
|
|
@@ -107,26 +141,25 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
107
141
|
/**
|
|
108
142
|
* render a new echarts instance
|
|
109
143
|
*/
|
|
110
|
-
private renderNewEcharts() {
|
|
111
|
-
const { onEvents, onChartReady } = this.props;
|
|
144
|
+
private async renderNewEcharts() {
|
|
145
|
+
const { onEvents, onChartReady, autoResize = true } = this.props;
|
|
146
|
+
|
|
147
|
+
// 1. init echarts instance
|
|
148
|
+
await this.initEchartsInstance();
|
|
112
149
|
|
|
113
|
-
//
|
|
150
|
+
// 2. update echarts instance
|
|
114
151
|
const echartsInstance = this.updateEChartsOption();
|
|
115
152
|
|
|
116
|
-
//
|
|
153
|
+
// 3. bind events
|
|
117
154
|
this.bindEvents(echartsInstance, onEvents || {});
|
|
118
155
|
|
|
119
|
-
//
|
|
156
|
+
// 4. on chart ready
|
|
120
157
|
if (isFunction(onChartReady)) onChartReady(echartsInstance);
|
|
121
158
|
|
|
122
|
-
//
|
|
123
|
-
if (this.ele) {
|
|
159
|
+
// 5. on resize
|
|
160
|
+
if (this.ele && autoResize) {
|
|
124
161
|
bind(this.ele, () => {
|
|
125
|
-
|
|
126
|
-
echartsInstance.resize();
|
|
127
|
-
} catch (e) {
|
|
128
|
-
console.warn(e);
|
|
129
|
-
}
|
|
162
|
+
this.resize();
|
|
130
163
|
});
|
|
131
164
|
}
|
|
132
165
|
}
|
|
@@ -151,15 +184,33 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
151
184
|
}
|
|
152
185
|
}
|
|
153
186
|
|
|
187
|
+
// off the events
|
|
188
|
+
private offEvents(instance, events: EChartsReactProps['onEvents']) {
|
|
189
|
+
if (!events) return;
|
|
190
|
+
// loop and off
|
|
191
|
+
for (const eventName in events) {
|
|
192
|
+
if (isString(eventName)) {
|
|
193
|
+
instance.off(eventName);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
154
198
|
/**
|
|
155
199
|
* render the echarts
|
|
156
200
|
*/
|
|
157
201
|
private updateEChartsOption(): EChartsInstance {
|
|
158
|
-
const {
|
|
202
|
+
const {
|
|
203
|
+
option,
|
|
204
|
+
notMerge = false,
|
|
205
|
+
replaceMerge = null,
|
|
206
|
+
lazyUpdate = false,
|
|
207
|
+
showLoading,
|
|
208
|
+
loadingOption = null,
|
|
209
|
+
} = this.props;
|
|
159
210
|
// 1. get or initial the echarts object
|
|
160
211
|
const echartInstance = this.getEchartsInstance();
|
|
161
212
|
// 2. set the echarts option
|
|
162
|
-
echartInstance.setOption(option, notMerge, lazyUpdate);
|
|
213
|
+
echartInstance.setOption(option, { notMerge, replaceMerge, lazyUpdate });
|
|
163
214
|
// 3. set loading mask
|
|
164
215
|
if (showLoading) echartInstance.showLoading(loadingOption);
|
|
165
216
|
else echartInstance.hideLoading();
|
|
@@ -167,6 +218,30 @@ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
|
|
|
167
218
|
return echartInstance;
|
|
168
219
|
}
|
|
169
220
|
|
|
221
|
+
/**
|
|
222
|
+
* resize wrapper
|
|
223
|
+
*/
|
|
224
|
+
private resize() {
|
|
225
|
+
// 1. get the echarts object
|
|
226
|
+
const echartsInstance = this.getEchartsInstance();
|
|
227
|
+
|
|
228
|
+
// 2. call echarts instance resize if not the initial resize
|
|
229
|
+
// resize should not happen on first render as it will cancel initial echarts animations
|
|
230
|
+
if (!this.isInitialResize) {
|
|
231
|
+
try {
|
|
232
|
+
echartsInstance.resize({
|
|
233
|
+
width: 'auto',
|
|
234
|
+
height: 'auto',
|
|
235
|
+
});
|
|
236
|
+
} catch (e) {
|
|
237
|
+
console.warn(e);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// 3. update variable for future calls
|
|
242
|
+
this.isInitialResize = false;
|
|
243
|
+
}
|
|
244
|
+
|
|
170
245
|
render(): JSX.Element {
|
|
171
246
|
const { style, className = '' } = this.props;
|
|
172
247
|
// default height = 300
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import * as echarts from 'echarts';
|
|
|
2
2
|
import { EChartsReactProps, EChartsOption, EChartsInstance } from './types';
|
|
3
3
|
import EChartsReactCore from './core';
|
|
4
4
|
|
|
5
|
-
export { EChartsReactProps, EChartsOption, EChartsInstance };
|
|
5
|
+
export type { EChartsReactProps, EChartsOption, EChartsInstance };
|
|
6
6
|
|
|
7
7
|
// export the Component the echarts Object.
|
|
8
8
|
export default class EChartsReact extends EChartsReactCore {
|
package/src/types.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { CSSProperties } from 'react';
|
|
2
|
+
import type { EChartsOption, EChartsType } from 'echarts';
|
|
2
3
|
|
|
3
|
-
export type EChartsOption
|
|
4
|
+
export type { EChartsOption };
|
|
4
5
|
|
|
5
|
-
export type EChartsInstance =
|
|
6
|
+
export type EChartsInstance = EChartsType;
|
|
6
7
|
|
|
7
8
|
export type Opts = {
|
|
8
9
|
readonly devicePixelRatio?: number;
|
|
@@ -39,6 +40,10 @@ export type EChartsReactProps = {
|
|
|
39
40
|
* notMerge config for echarts, default is `false`
|
|
40
41
|
*/
|
|
41
42
|
readonly notMerge?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* replaceMerge config for echarts, default is `null`
|
|
45
|
+
*/
|
|
46
|
+
readonly replaceMerge?: string | string[];
|
|
42
47
|
/**
|
|
43
48
|
* lazyUpdate config for echarts, default is `false`
|
|
44
49
|
*/
|
|
@@ -56,7 +61,7 @@ export type EChartsReactProps = {
|
|
|
56
61
|
*/
|
|
57
62
|
readonly opts?: Opts;
|
|
58
63
|
/**
|
|
59
|
-
* when after chart
|
|
64
|
+
* when after chart render, do the callback with echarts instance
|
|
60
65
|
*/
|
|
61
66
|
readonly onChartReady?: (instance: EChartsInstance) => void;
|
|
62
67
|
/**
|
|
@@ -67,4 +72,9 @@ export type EChartsReactProps = {
|
|
|
67
72
|
* should update echarts options
|
|
68
73
|
*/
|
|
69
74
|
readonly shouldSetOption?: (prevProps: EChartsReactProps, props: EChartsReactProps) => boolean;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* should trigger resize when window resize
|
|
78
|
+
*/
|
|
79
|
+
readonly autoResize?: boolean;
|
|
70
80
|
};
|