echarts-for-react 2.0.16 → 3.0.1

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.
Files changed (57) hide show
  1. package/LICENSE +3 -3
  2. package/README.md +165 -90
  3. package/esm/core.d.ts +39 -0
  4. package/esm/core.js +157 -0
  5. package/esm/core.js.map +1 -0
  6. package/esm/helper/is-equal.d.ts +2 -0
  7. package/esm/helper/is-equal.js +3 -0
  8. package/esm/helper/is-equal.js.map +1 -0
  9. package/esm/helper/is-function.d.ts +1 -0
  10. package/esm/helper/is-function.js +4 -0
  11. package/esm/helper/is-function.js.map +1 -0
  12. package/esm/helper/is-string.d.ts +1 -0
  13. package/esm/helper/is-string.js +4 -0
  14. package/esm/helper/is-string.js.map +1 -0
  15. package/esm/helper/pick.d.ts +6 -0
  16. package/esm/helper/pick.js +13 -0
  17. package/esm/helper/pick.js.map +1 -0
  18. package/esm/index.d.ts +6 -0
  19. package/esm/index.js +16 -0
  20. package/esm/index.js.map +1 -0
  21. package/esm/types.d.ts +66 -0
  22. package/esm/types.js +2 -0
  23. package/esm/types.js.map +1 -0
  24. package/lib/core.d.ts +38 -10
  25. package/lib/core.js +151 -242
  26. package/lib/core.js.map +1 -0
  27. package/lib/helper/is-equal.d.ts +2 -0
  28. package/lib/helper/is-equal.js +7 -0
  29. package/lib/helper/is-equal.js.map +1 -0
  30. package/lib/helper/is-function.d.ts +1 -0
  31. package/lib/helper/is-function.js +8 -0
  32. package/lib/helper/is-function.js.map +1 -0
  33. package/lib/helper/is-string.d.ts +1 -0
  34. package/lib/helper/is-string.js +8 -0
  35. package/lib/helper/is-string.js.map +1 -0
  36. package/lib/helper/pick.d.ts +6 -0
  37. package/lib/helper/pick.js +17 -0
  38. package/lib/helper/pick.js.map +1 -0
  39. package/lib/index.d.ts +5 -100
  40. package/lib/index.js +17 -39
  41. package/lib/index.js.map +1 -0
  42. package/lib/types.d.ts +66 -0
  43. package/lib/types.js +3 -0
  44. package/lib/types.js.map +1 -0
  45. package/package.json +72 -77
  46. package/src/core.tsx +185 -0
  47. package/src/helper/is-equal.ts +3 -0
  48. package/src/helper/is-function.ts +3 -0
  49. package/src/helper/is-string.ts +3 -0
  50. package/src/helper/pick.ts +12 -0
  51. package/src/index.ts +15 -0
  52. package/src/types.ts +70 -0
  53. package/lib/.DS_Store +0 -0
  54. package/lib/utils.js +0 -14
  55. package/src/core.jsx +0 -194
  56. package/src/index.js +0 -10
  57. package/src/utils.js +0 -10
package/src/core.tsx ADDED
@@ -0,0 +1,185 @@
1
+ import React, { PureComponent } from 'react';
2
+ import { bind, clear } from 'size-sensor';
3
+ import { pick } from './helper/pick';
4
+ import { isFunction } from './helper/is-function';
5
+ import { isString } from './helper/is-string';
6
+ import { isEqual } from './helper/is-equal';
7
+ import { EChartsReactProps, EChartsInstance } from './types';
8
+
9
+ /**
10
+ * core component for echarts binding
11
+ */
12
+ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
13
+ /**
14
+ * echarts render container
15
+ */
16
+ public ele: HTMLElement;
17
+
18
+ /**
19
+ * echarts library entry
20
+ */
21
+ protected echarts: any;
22
+
23
+ constructor(props: EChartsReactProps) {
24
+ super(props);
25
+
26
+ this.echarts = props.echarts;
27
+ this.ele = null;
28
+ }
29
+
30
+ componentDidMount() {
31
+ this.renderNewEcharts();
32
+ }
33
+
34
+ // update
35
+ componentDidUpdate(prevProps: EChartsReactProps) {
36
+ /**
37
+ * if shouldSetOption return false, then return, not update echarts options
38
+ * default is true
39
+ */
40
+ const { shouldSetOption } = this.props;
41
+ if (isFunction(shouldSetOption) && !shouldSetOption(prevProps, this.props)) {
42
+ return;
43
+ }
44
+
45
+ // 以下属性修改的时候,需要 dispose 之后再新建
46
+ // 1. 切换 theme 的时候
47
+ // 2. 修改 opts 的时候
48
+ // 3. 修改 onEvents 的时候,这样可以取消所有之前绑定的事件 issue #151
49
+ if (
50
+ !isEqual(prevProps.theme, this.props.theme) ||
51
+ !isEqual(prevProps.opts, this.props.opts) ||
52
+ !isEqual(prevProps.onEvents, this.props.onEvents)
53
+ ) {
54
+ this.dispose();
55
+
56
+ this.renderNewEcharts(); // 重建
57
+ return;
58
+ }
59
+
60
+ // when thoes props isEqual, do not update echarts
61
+ const pickKeys = ['option', 'notMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
62
+ if (isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {
63
+ return;
64
+ }
65
+
66
+ const echartsInstance = this.updateEChartsOption();
67
+ /**
68
+ * when style or class name updated, change size.
69
+ */
70
+ if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {
71
+ try {
72
+ echartsInstance.resize();
73
+ } catch (e) {
74
+ console.warn(e);
75
+ }
76
+ }
77
+ }
78
+
79
+ componentWillUnmount() {
80
+ this.dispose();
81
+ }
82
+
83
+ /**
84
+ * return the echart object
85
+ * 1. if exist, return the existed instance
86
+ * 2. or new one instance
87
+ */
88
+ public getEchartsInstance() {
89
+ return this.echarts.getInstanceByDom(this.ele) || this.echarts.init(this.ele, this.props.theme, this.props.opts);
90
+ }
91
+
92
+ /**
93
+ * dispose echarts and clear size-sensor
94
+ */
95
+ private dispose() {
96
+ if (this.ele) {
97
+ try {
98
+ clear(this.ele);
99
+ } catch (e) {
100
+ console.warn(e);
101
+ }
102
+ // dispose echarts instance
103
+ this.echarts.dispose(this.ele);
104
+ }
105
+ }
106
+
107
+ /**
108
+ * render a new echarts instance
109
+ */
110
+ private renderNewEcharts() {
111
+ const { onEvents, onChartReady } = this.props;
112
+
113
+ // 1. new echarts instance
114
+ const echartsInstance = this.updateEChartsOption();
115
+
116
+ // 2. bind events
117
+ this.bindEvents(echartsInstance, onEvents || {});
118
+
119
+ // 3. on chart ready
120
+ if (isFunction(onChartReady)) onChartReady(echartsInstance);
121
+
122
+ // 4. on resize
123
+ if (this.ele) {
124
+ bind(this.ele, () => {
125
+ try {
126
+ echartsInstance.resize();
127
+ } catch (e) {
128
+ console.warn(e);
129
+ }
130
+ });
131
+ }
132
+ }
133
+
134
+ // bind the events
135
+ private bindEvents(instance, events: EChartsReactProps['onEvents']) {
136
+ function _bindEvent(eventName: string, func: Function) {
137
+ // ignore the event config which not satisfy
138
+ if (isString(eventName) && isFunction(func)) {
139
+ // binding event
140
+ instance.on(eventName, (param) => {
141
+ func(param, instance);
142
+ });
143
+ }
144
+ }
145
+
146
+ // loop and bind
147
+ for (const eventName in events) {
148
+ if (Object.prototype.hasOwnProperty.call(events, eventName)) {
149
+ _bindEvent(eventName, events[eventName]);
150
+ }
151
+ }
152
+ }
153
+
154
+ /**
155
+ * render the echarts
156
+ */
157
+ private updateEChartsOption(): EChartsInstance {
158
+ const { option, notMerge = false, lazyUpdate = false, showLoading, loadingOption = null } = this.props;
159
+ // 1. get or initial the echarts object
160
+ const echartInstance = this.getEchartsInstance();
161
+ // 2. set the echarts option
162
+ echartInstance.setOption(option, notMerge, lazyUpdate);
163
+ // 3. set loading mask
164
+ if (showLoading) echartInstance.showLoading(loadingOption);
165
+ else echartInstance.hideLoading();
166
+
167
+ return echartInstance;
168
+ }
169
+
170
+ render(): JSX.Element {
171
+ const { style, className = '' } = this.props;
172
+ // default height = 300
173
+ const newStyle = { height: 300, ...style };
174
+
175
+ return (
176
+ <div
177
+ ref={(e: HTMLElement) => {
178
+ this.ele = e;
179
+ }}
180
+ style={newStyle}
181
+ className={`echarts-for-react ${className}`}
182
+ />
183
+ );
184
+ }
185
+ }
@@ -0,0 +1,3 @@
1
+ import isEqual from 'fast-deep-equal';
2
+
3
+ export { isEqual };
@@ -0,0 +1,3 @@
1
+ export function isFunction(v: any): boolean {
2
+ return typeof v === 'function';
3
+ }
@@ -0,0 +1,3 @@
1
+ export function isString(v: any): boolean {
2
+ return typeof v === 'string';
3
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 保留 object 中的部分内容
3
+ * @param obj
4
+ * @param keys
5
+ */
6
+ export function pick(obj: Record<string, unknown>, keys: string[]): Record<string, unknown> {
7
+ const r = {};
8
+ keys.forEach((key) => {
9
+ r[key] = obj[key];
10
+ });
11
+ return r;
12
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ import * as echarts from 'echarts';
2
+ import { EChartsReactProps, EChartsOption, EChartsInstance } from './types';
3
+ import EChartsReactCore from './core';
4
+
5
+ export { EChartsReactProps, EChartsOption, EChartsInstance };
6
+
7
+ // export the Component the echarts Object.
8
+ export default class EChartsReact extends EChartsReactCore {
9
+ constructor(props: EChartsReactProps) {
10
+ super(props);
11
+
12
+ // 初始化为 echarts 整个包
13
+ this.echarts = echarts;
14
+ }
15
+ }
package/src/types.ts ADDED
@@ -0,0 +1,70 @@
1
+ import { CSSProperties } from 'react';
2
+
3
+ export type EChartsOption = any;
4
+
5
+ export type EChartsInstance = any;
6
+
7
+ export type Opts = {
8
+ readonly devicePixelRatio?: number;
9
+ readonly renderer?: 'canvas' | 'svg';
10
+ readonly width?: number | null | undefined | 'auto';
11
+ readonly height?: number | null | undefined | 'auto';
12
+ readonly locale?: string;
13
+ };
14
+
15
+ export type EChartsReactProps = {
16
+ /**
17
+ * echarts library entry, use it for import necessary.
18
+ */
19
+ readonly echarts?: any;
20
+ /**
21
+ * `className` for container
22
+ */
23
+ readonly className?: string;
24
+ /**
25
+ * `style` for container
26
+ */
27
+ readonly style?: CSSProperties;
28
+ /**
29
+ * echarts option
30
+ */
31
+ readonly option: EChartsOption;
32
+ /**
33
+ * echarts theme config, can be:
34
+ * 1. theme name string
35
+ * 2. theme object
36
+ */
37
+ readonly theme?: string | Record<string, any>;
38
+ /**
39
+ * notMerge config for echarts, default is `false`
40
+ */
41
+ readonly notMerge?: boolean;
42
+ /**
43
+ * lazyUpdate config for echarts, default is `false`
44
+ */
45
+ readonly lazyUpdate?: boolean;
46
+ /**
47
+ * showLoading config for echarts, default is `false`
48
+ */
49
+ readonly showLoading?: boolean;
50
+ /**
51
+ * loadingOption config for echarts, default is `null`
52
+ */
53
+ readonly loadingOption?: any;
54
+ /**
55
+ * echarts opts config, default is `{}`
56
+ */
57
+ readonly opts?: Opts;
58
+ /**
59
+ * when after chart reander, do the callback widht echarts instance
60
+ */
61
+ readonly onChartReady?: (instance: EChartsInstance) => void;
62
+ /**
63
+ * bind events, default is `{}`
64
+ */
65
+ readonly onEvents?: Record<string, Function>;
66
+ /**
67
+ * should update echarts options
68
+ */
69
+ readonly shouldSetOption?: (prevProps: EChartsReactProps, props: EChartsReactProps) => boolean;
70
+ };
package/lib/.DS_Store DELETED
Binary file
package/lib/utils.js DELETED
@@ -1,14 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- /* eslint-disable import/prefer-default-export */
7
-
8
- var pick = exports.pick = function pick(obj, keys) {
9
- var r = {};
10
- keys.forEach(function (key) {
11
- r[key] = obj[key];
12
- });
13
- return r;
14
- };
package/src/core.jsx DELETED
@@ -1,194 +0,0 @@
1
- import React, { Component } from 'react';
2
- import PropTypes from 'prop-types';
3
- import isEqual from 'fast-deep-equal';
4
- import { bind, clear } from 'size-sensor';
5
- import { pick } from './utils';
6
-
7
- export default class EchartsReactCore extends Component {
8
- constructor(props) {
9
- super(props);
10
- this.echartsLib = props.echarts; // the echarts object.
11
- this.echartsElement = null; // echarts div element
12
- }
13
-
14
- // first add
15
- componentDidMount() {
16
- this.rerender();
17
- }
18
-
19
- // update
20
- componentDidUpdate(prevProps) {
21
- // 判断是否需要 setOption,由开发者自己来确定。默认为 true
22
- if (typeof this.props.shouldSetOption === 'function' && !this.props.shouldSetOption(prevProps, this.props)) {
23
- return;
24
- }
25
-
26
- // 以下属性修改的时候,需要 dispose 之后再新建
27
- // 1. 切换 theme 的时候
28
- // 2. 修改 opts 的时候
29
- // 3. 修改 onEvents 的时候,这样可以取消所有之前绑定的事件 issue #151
30
- if (
31
- !isEqual(prevProps.theme, this.props.theme) ||
32
- !isEqual(prevProps.opts, this.props.opts) ||
33
- !isEqual(prevProps.onEvents, this.props.onEvents)
34
- ) {
35
- this.dispose();
36
-
37
- this.rerender(); // 重建
38
- return;
39
- }
40
-
41
- // 当这些属性保持不变的时候,不 setOption
42
- const pickKeys = ['option', 'notMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
43
- if (isEqual(pick(this.props, pickKeys), pick(prevProps, pickKeys))) {
44
- return;
45
- }
46
-
47
- const echartObj = this.renderEchartDom();
48
- // 样式修改的时候,可能会导致大小变化,所以触发一下 resize
49
- if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {
50
- try {
51
- echartObj.resize();
52
- } catch (e) {
53
- console.warn(e);
54
- }
55
- }
56
- }
57
-
58
- // remove
59
- componentWillUnmount() {
60
- this.dispose();
61
- }
62
-
63
- // return the echart object
64
- getEchartsInstance = () => this.echartsLib.getInstanceByDom(this.echartsElement) ||
65
- this.echartsLib.init(this.echartsElement, this.props.theme, this.props.opts);
66
-
67
- // dispose echarts and clear size-sensor
68
- dispose = () => {
69
- if (this.echartsElement) {
70
- try {
71
- clear(this.echartsElement);
72
- } catch (e) {
73
- console.warn(e);
74
- }
75
- // dispose echarts instance
76
- this.echartsLib.dispose(this.echartsElement);
77
- }
78
- };
79
-
80
- rerender = () => {
81
- const { onEvents, onChartReady } = this.props;
82
-
83
- const echartObj = this.renderEchartDom();
84
- this.bindEvents(echartObj, onEvents || {});
85
-
86
- // on chart ready
87
- if (typeof onChartReady === 'function') this.props.onChartReady(echartObj);
88
- // on resize
89
- if (this.echartsElement) {
90
- bind(this.echartsElement, () => {
91
- try {
92
- echartObj.resize();
93
- } catch (e) {
94
- console.warn(e);
95
- }
96
- });
97
- }
98
- };
99
-
100
- // bind the events
101
- bindEvents = (instance, events) => {
102
- const _bindEvent = (eventName, func) => {
103
- // ignore the event config which not satisfy
104
- if (typeof eventName === 'string' && typeof func === 'function') {
105
- // binding event
106
- // instance.off(eventName); // 已经 dispose 在重建,所以无需 off 操作
107
- instance.on(eventName, (param) => {
108
- func(param, instance);
109
- });
110
- }
111
- };
112
-
113
- // loop and bind
114
- for (const eventName in events) {
115
- if (Object.prototype.hasOwnProperty.call(events, eventName)) {
116
- _bindEvent(eventName, events[eventName]);
117
- }
118
- }
119
- };
120
-
121
- // render the dom
122
- renderEchartDom = () => {
123
- // init the echart object
124
- const echartObj = this.getEchartsInstance();
125
- // set the echart option
126
- echartObj.setOption(this.props.option, this.props.notMerge || false, this.props.lazyUpdate || false);
127
- // set loading mask
128
- if (this.props.showLoading) echartObj.showLoading(this.props.loadingOption || null);
129
- else echartObj.hideLoading();
130
-
131
- return echartObj;
132
- };
133
-
134
- render() {
135
- const { style, className } = this.props;
136
- const newStyle = {
137
- height: 300,
138
- ...style,
139
- };
140
- // for render
141
- return (
142
- <div
143
- ref={(e) => { this.echartsElement = e; }}
144
- style={newStyle}
145
- className={`echarts-for-react ${className}`}
146
- />
147
- );
148
- }
149
- }
150
-
151
- EchartsReactCore.propTypes = {
152
- option: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
153
- echarts: PropTypes.object, // eslint-disable-line react/forbid-prop-types
154
- notMerge: PropTypes.bool,
155
- lazyUpdate: PropTypes.bool,
156
- style: PropTypes.object, // eslint-disable-line react/forbid-prop-types
157
- className: PropTypes.string,
158
- theme: PropTypes.oneOfType([
159
- PropTypes.string,
160
- PropTypes.object
161
- ]),
162
- onChartReady: PropTypes.func,
163
- showLoading: PropTypes.bool,
164
- loadingOption: PropTypes.object, // eslint-disable-line react/forbid-prop-types
165
- onEvents: PropTypes.object, // eslint-disable-line react/forbid-prop-types
166
- opts: PropTypes.shape({
167
- devicePixelRatio: PropTypes.number,
168
- renderer: PropTypes.oneOf(['canvas', 'svg']),
169
- width: PropTypes.oneOfType([
170
- PropTypes.number,
171
- PropTypes.oneOf([null, undefined, 'auto'])
172
- ]),
173
- height: PropTypes.oneOfType([
174
- PropTypes.number,
175
- PropTypes.oneOf([null, undefined, 'auto'])
176
- ]),
177
- }),
178
- shouldSetOption: PropTypes.func,
179
- };
180
-
181
- EchartsReactCore.defaultProps = {
182
- echarts: {},
183
- notMerge: false,
184
- lazyUpdate: false,
185
- style: {},
186
- className: '',
187
- theme: null,
188
- onChartReady: () => {},
189
- showLoading: false,
190
- loadingOption: null,
191
- onEvents: {},
192
- opts: {},
193
- shouldSetOption: () => true,
194
- };
package/src/index.js DELETED
@@ -1,10 +0,0 @@
1
- import echarts from 'echarts';
2
- import EchartsReactCore from './core';
3
-
4
- // export the Component the echarts Object.
5
- export default class EchartsReact extends EchartsReactCore {
6
- constructor(props) {
7
- super(props);
8
- this.echartsLib = echarts;
9
- }
10
- }
package/src/utils.js DELETED
@@ -1,10 +0,0 @@
1
- /* eslint-disable import/prefer-default-export */
2
-
3
-
4
- export const pick = (obj, keys) => {
5
- const r = {};
6
- keys.forEach((key) => {
7
- r[key] = obj[key];
8
- });
9
- return r;
10
- };