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
@@ -0,0 +1 @@
1
+ export declare function isString(v: any): boolean;
@@ -0,0 +1,4 @@
1
+ export function isString(v) {
2
+ return typeof v === 'string';
3
+ }
4
+ //# sourceMappingURL=is-string.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-string.js","sourceRoot":"","sources":["../../src/helper/is-string.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,QAAQ,CAAC,CAAM;IAC7B,OAAO,OAAO,CAAC,KAAK,QAAQ,CAAC;AAC/B,CAAC","sourcesContent":["export function isString(v: any): boolean {\n return typeof v === 'string';\n}\n"]}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 保留 object 中的部分内容
3
+ * @param obj
4
+ * @param keys
5
+ */
6
+ export declare function pick(obj: Record<string, unknown>, keys: string[]): Record<string, unknown>;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * 保留 object 中的部分内容
3
+ * @param obj
4
+ * @param keys
5
+ */
6
+ export function pick(obj, keys) {
7
+ var r = {};
8
+ keys.forEach(function (key) {
9
+ r[key] = obj[key];
10
+ });
11
+ return r;
12
+ }
13
+ //# sourceMappingURL=pick.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pick.js","sourceRoot":"","sources":["../../src/helper/pick.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,IAAI,CAAC,GAA4B,EAAE,IAAc;IAC/D,IAAM,CAAC,GAAG,EAAE,CAAC;IACb,IAAI,CAAC,OAAO,CAAC,UAAC,GAAG;QACf,CAAC,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,CAAC;AACX,CAAC","sourcesContent":["/**\n * 保留 object 中的部分内容\n * @param obj\n * @param keys\n */\nexport function pick(obj: Record<string, unknown>, keys: string[]): Record<string, unknown> {\n const r = {};\n keys.forEach((key) => {\n r[key] = obj[key];\n });\n return r;\n}\n"]}
package/esm/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { EChartsReactProps, EChartsOption, EChartsInstance } from './types';
2
+ import EChartsReactCore from './core';
3
+ export { EChartsReactProps, EChartsOption, EChartsInstance };
4
+ export default class EChartsReact extends EChartsReactCore {
5
+ constructor(props: EChartsReactProps);
6
+ }
package/esm/index.js ADDED
@@ -0,0 +1,16 @@
1
+ import { __extends } from "tslib";
2
+ import * as echarts from 'echarts';
3
+ import EChartsReactCore from './core';
4
+ // export the Component the echarts Object.
5
+ var EChartsReact = /** @class */ (function (_super) {
6
+ __extends(EChartsReact, _super);
7
+ function EChartsReact(props) {
8
+ var _this = _super.call(this, props) || this;
9
+ // 初始化为 echarts 整个包
10
+ _this.echarts = echarts;
11
+ return _this;
12
+ }
13
+ return EChartsReact;
14
+ }(EChartsReactCore));
15
+ export default EChartsReact;
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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"]}
package/esm/types.d.ts ADDED
@@ -0,0 +1,66 @@
1
+ import { CSSProperties } from 'react';
2
+ export declare type EChartsOption = any;
3
+ export declare type EChartsInstance = any;
4
+ export declare type Opts = {
5
+ readonly devicePixelRatio?: number;
6
+ readonly renderer?: 'canvas' | 'svg';
7
+ readonly width?: number | null | undefined | 'auto';
8
+ readonly height?: number | null | undefined | 'auto';
9
+ readonly locale?: string;
10
+ };
11
+ export declare type EChartsReactProps = {
12
+ /**
13
+ * echarts library entry, use it for import necessary.
14
+ */
15
+ readonly echarts?: any;
16
+ /**
17
+ * `className` for container
18
+ */
19
+ readonly className?: string;
20
+ /**
21
+ * `style` for container
22
+ */
23
+ readonly style?: CSSProperties;
24
+ /**
25
+ * echarts option
26
+ */
27
+ readonly option: EChartsOption;
28
+ /**
29
+ * echarts theme config, can be:
30
+ * 1. theme name string
31
+ * 2. theme object
32
+ */
33
+ readonly theme?: string | Record<string, any>;
34
+ /**
35
+ * notMerge config for echarts, default is `false`
36
+ */
37
+ readonly notMerge?: boolean;
38
+ /**
39
+ * lazyUpdate config for echarts, default is `false`
40
+ */
41
+ readonly lazyUpdate?: boolean;
42
+ /**
43
+ * showLoading config for echarts, default is `false`
44
+ */
45
+ readonly showLoading?: boolean;
46
+ /**
47
+ * loadingOption config for echarts, default is `null`
48
+ */
49
+ readonly loadingOption?: any;
50
+ /**
51
+ * echarts opts config, default is `{}`
52
+ */
53
+ readonly opts?: Opts;
54
+ /**
55
+ * when after chart reander, do the callback widht echarts instance
56
+ */
57
+ readonly onChartReady?: (instance: EChartsInstance) => void;
58
+ /**
59
+ * bind events, default is `{}`
60
+ */
61
+ readonly onEvents?: Record<string, Function>;
62
+ /**
63
+ * should update echarts options
64
+ */
65
+ readonly shouldSetOption?: (prevProps: EChartsReactProps, props: EChartsReactProps) => boolean;
66
+ };
package/esm/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { CSSProperties } from 'react';\n\nexport type EChartsOption = any;\n\nexport type EChartsInstance = any;\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 * 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 reander, do the callback widht 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"]}
package/lib/core.d.ts CHANGED
@@ -1,11 +1,39 @@
1
- /// <reference types="react" />
2
-
3
- import * as React from 'react';
4
- import {ReactEchartsPropsTypes} from './index.d'
5
-
6
- // Core
7
- export interface ReactEchartsCorePropsTypes extends ReactEchartsPropsTypes{
8
- echarts: object;
1
+ import { PureComponent } from 'react';
2
+ import { EChartsReactProps } from './types';
3
+ /**
4
+ * core component for echarts binding
5
+ */
6
+ export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
7
+ /**
8
+ * echarts render container
9
+ */
10
+ ele: HTMLElement;
11
+ /**
12
+ * echarts library entry
13
+ */
14
+ protected echarts: any;
15
+ constructor(props: EChartsReactProps);
16
+ componentDidMount(): void;
17
+ componentDidUpdate(prevProps: EChartsReactProps): void;
18
+ componentWillUnmount(): void;
19
+ /**
20
+ * return the echart object
21
+ * 1. if exist, return the existed instance
22
+ * 2. or new one instance
23
+ */
24
+ getEchartsInstance(): any;
25
+ /**
26
+ * dispose echarts and clear size-sensor
27
+ */
28
+ private dispose;
29
+ /**
30
+ * render a new echarts instance
31
+ */
32
+ private renderNewEcharts;
33
+ private bindEvents;
34
+ /**
35
+ * render the echarts
36
+ */
37
+ private updateEChartsOption;
38
+ render(): JSX.Element;
9
39
  }
10
-
11
- export default class ReactEchartsCore extends React.Component<ReactEchartsCorePropsTypes, any>{}
package/lib/core.js CHANGED
@@ -1,250 +1,159 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports['default'] = undefined;
7
-
8
- var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
9
-
10
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
11
-
12
- var _react = require('react');
13
-
14
- var _react2 = _interopRequireDefault(_react);
15
-
16
- var _propTypes = require('prop-types');
17
-
18
- var _propTypes2 = _interopRequireDefault(_propTypes);
19
-
20
- var _fastDeepEqual = require('fast-deep-equal');
21
-
22
- var _fastDeepEqual2 = _interopRequireDefault(_fastDeepEqual);
23
-
24
- var _sizeSensor = require('size-sensor');
25
-
26
- var _utils = require('./utils');
27
-
28
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
29
-
30
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
31
-
32
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
33
-
34
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
35
-
36
- var EchartsReactCore = function (_Component) {
37
- _inherits(EchartsReactCore, _Component);
38
-
39
- function EchartsReactCore(props) {
40
- _classCallCheck(this, EchartsReactCore);
41
-
42
- var _this = _possibleConstructorReturn(this, (EchartsReactCore.__proto__ || Object.getPrototypeOf(EchartsReactCore)).call(this, props));
43
-
44
- _this.getEchartsInstance = function () {
45
- return _this.echartsLib.getInstanceByDom(_this.echartsElement) || _this.echartsLib.init(_this.echartsElement, _this.props.theme, _this.props.opts);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var tslib_1 = require("tslib");
4
+ var react_1 = tslib_1.__importStar(require("react"));
5
+ var size_sensor_1 = require("size-sensor");
6
+ var pick_1 = require("./helper/pick");
7
+ var is_function_1 = require("./helper/is-function");
8
+ var is_string_1 = require("./helper/is-string");
9
+ var is_equal_1 = require("./helper/is-equal");
10
+ /**
11
+ * core component for echarts binding
12
+ */
13
+ var EChartsReactCore = /** @class */ (function (_super) {
14
+ tslib_1.__extends(EChartsReactCore, _super);
15
+ function EChartsReactCore(props) {
16
+ var _this = _super.call(this, props) || this;
17
+ _this.echarts = props.echarts;
18
+ _this.ele = null;
19
+ return _this;
20
+ }
21
+ EChartsReactCore.prototype.componentDidMount = function () {
22
+ this.renderNewEcharts();
46
23
  };
47
-
48
- _this.dispose = function () {
49
- if (_this.echartsElement) {
50
- try {
51
- (0, _sizeSensor.clear)(_this.echartsElement);
52
- } catch (e) {
53
- console.warn(e);
24
+ // update
25
+ EChartsReactCore.prototype.componentDidUpdate = function (prevProps) {
26
+ /**
27
+ * if shouldSetOption return false, then return, not update echarts options
28
+ * default is true
29
+ */
30
+ var shouldSetOption = this.props.shouldSetOption;
31
+ if (is_function_1.isFunction(shouldSetOption) && !shouldSetOption(prevProps, this.props)) {
32
+ return;
54
33
  }
55
- // dispose echarts instance
56
- _this.echartsLib.dispose(_this.echartsElement);
57
- }
58
- };
59
-
60
- _this.rerender = function () {
61
- var _this$props = _this.props,
62
- onEvents = _this$props.onEvents,
63
- onChartReady = _this$props.onChartReady;
64
-
65
-
66
- var echartObj = _this.renderEchartDom();
67
- _this.bindEvents(echartObj, onEvents || {});
68
-
69
- // on chart ready
70
- if (typeof onChartReady === 'function') _this.props.onChartReady(echartObj);
71
- // on resize
72
- if (_this.echartsElement) {
73
- (0, _sizeSensor.bind)(_this.echartsElement, function () {
74
- try {
75
- echartObj.resize();
76
- } catch (e) {
77
- console.warn(e);
78
- }
79
- });
80
- }
81
- };
82
-
83
- _this.bindEvents = function (instance, events) {
84
- var _bindEvent = function _bindEvent(eventName, func) {
85
- // ignore the event config which not satisfy
86
- if (typeof eventName === 'string' && typeof func === 'function') {
87
- // binding event
88
- // instance.off(eventName); // 已经 dispose 在重建,所以无需 off 操作
89
- instance.on(eventName, function (param) {
90
- func(param, instance);
91
- });
34
+ // 以下属性修改的时候,需要 dispose 之后再新建
35
+ // 1. 切换 theme 的时候
36
+ // 2. 修改 opts 的时候
37
+ // 3. 修改 onEvents 的时候,这样可以取消所有之前绑定的事件 issue #151
38
+ if (!is_equal_1.isEqual(prevProps.theme, this.props.theme) ||
39
+ !is_equal_1.isEqual(prevProps.opts, this.props.opts) ||
40
+ !is_equal_1.isEqual(prevProps.onEvents, this.props.onEvents)) {
41
+ this.dispose();
42
+ this.renderNewEcharts(); // 重建
43
+ return;
92
44
  }
93
- };
94
-
95
- // loop and bind
96
- for (var eventName in events) {
97
- if (Object.prototype.hasOwnProperty.call(events, eventName)) {
98
- _bindEvent(eventName, events[eventName]);
45
+ // when thoes props isEqual, do not update echarts
46
+ var pickKeys = ['option', 'notMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
47
+ if (is_equal_1.isEqual(pick_1.pick(this.props, pickKeys), pick_1.pick(prevProps, pickKeys))) {
48
+ return;
49
+ }
50
+ var echartsInstance = this.updateEChartsOption();
51
+ /**
52
+ * when style or class name updated, change size.
53
+ */
54
+ if (!is_equal_1.isEqual(prevProps.style, this.props.style) || !is_equal_1.isEqual(prevProps.className, this.props.className)) {
55
+ try {
56
+ echartsInstance.resize();
57
+ }
58
+ catch (e) {
59
+ console.warn(e);
60
+ }
99
61
  }
100
- }
101
- };
102
-
103
- _this.renderEchartDom = function () {
104
- // init the echart object
105
- var echartObj = _this.getEchartsInstance();
106
- // set the echart option
107
- echartObj.setOption(_this.props.option, _this.props.notMerge || false, _this.props.lazyUpdate || false);
108
- // set loading mask
109
- if (_this.props.showLoading) echartObj.showLoading(_this.props.loadingOption || null);else echartObj.hideLoading();
110
-
111
- return echartObj;
112
62
  };
113
-
114
- _this.echartsLib = props.echarts; // the echarts object.
115
- _this.echartsElement = null; // echarts div element
116
- return _this;
117
- }
118
-
119
- // first add
120
-
121
-
122
- _createClass(EchartsReactCore, [{
123
- key: 'componentDidMount',
124
- value: function componentDidMount() {
125
- this.rerender();
126
- }
127
-
128
- // update
129
-
130
- }, {
131
- key: 'componentDidUpdate',
132
- value: function componentDidUpdate(prevProps) {
133
- // 判断是否需要 setOption,由开发者自己来确定。默认为 true
134
- if (typeof this.props.shouldSetOption === 'function' && !this.props.shouldSetOption(prevProps, this.props)) {
135
- return;
136
- }
137
-
138
- // 以下属性修改的时候,需要 dispose 之后再新建
139
- // 1. 切换 theme 的时候
140
- // 2. 修改 opts 的时候
141
- // 3. 修改 onEvents 的时候,这样可以取消所有之前绑定的事件 issue #151
142
- if (!(0, _fastDeepEqual2['default'])(prevProps.theme, this.props.theme) || !(0, _fastDeepEqual2['default'])(prevProps.opts, this.props.opts) || !(0, _fastDeepEqual2['default'])(prevProps.onEvents, this.props.onEvents)) {
63
+ EChartsReactCore.prototype.componentWillUnmount = function () {
143
64
  this.dispose();
144
-
145
- this.rerender(); // 重建
146
- return;
147
- }
148
-
149
- // 当这些属性保持不变的时候,不 setOption
150
- var pickKeys = ['option', 'notMerge', 'lazyUpdate', 'showLoading', 'loadingOption'];
151
- if ((0, _fastDeepEqual2['default'])((0, _utils.pick)(this.props, pickKeys), (0, _utils.pick)(prevProps, pickKeys))) {
152
- return;
153
- }
154
-
155
- var echartObj = this.renderEchartDom();
156
- // 样式修改的时候,可能会导致大小变化,所以触发一下 resize
157
- if (!(0, _fastDeepEqual2['default'])(prevProps.style, this.props.style) || !(0, _fastDeepEqual2['default'])(prevProps.className, this.props.className)) {
158
- try {
159
- echartObj.resize();
160
- } catch (e) {
161
- console.warn(e);
65
+ };
66
+ /**
67
+ * return the echart object
68
+ * 1. if exist, return the existed instance
69
+ * 2. or new one instance
70
+ */
71
+ EChartsReactCore.prototype.getEchartsInstance = function () {
72
+ return this.echarts.getInstanceByDom(this.ele) || this.echarts.init(this.ele, this.props.theme, this.props.opts);
73
+ };
74
+ /**
75
+ * dispose echarts and clear size-sensor
76
+ */
77
+ EChartsReactCore.prototype.dispose = function () {
78
+ if (this.ele) {
79
+ try {
80
+ size_sensor_1.clear(this.ele);
81
+ }
82
+ catch (e) {
83
+ console.warn(e);
84
+ }
85
+ // dispose echarts instance
86
+ this.echarts.dispose(this.ele);
162
87
  }
163
- }
164
- }
165
-
166
- // remove
167
-
168
- }, {
169
- key: 'componentWillUnmount',
170
- value: function componentWillUnmount() {
171
- this.dispose();
172
- }
173
-
174
- // return the echart object
175
-
176
-
177
- // dispose echarts and clear size-sensor
178
-
179
-
88
+ };
89
+ /**
90
+ * render a new echarts instance
91
+ */
92
+ EChartsReactCore.prototype.renderNewEcharts = function () {
93
+ var _a = this.props, onEvents = _a.onEvents, onChartReady = _a.onChartReady;
94
+ // 1. new echarts instance
95
+ var echartsInstance = this.updateEChartsOption();
96
+ // 2. bind events
97
+ this.bindEvents(echartsInstance, onEvents || {});
98
+ // 3. on chart ready
99
+ if (is_function_1.isFunction(onChartReady))
100
+ onChartReady(echartsInstance);
101
+ // 4. on resize
102
+ if (this.ele) {
103
+ size_sensor_1.bind(this.ele, function () {
104
+ try {
105
+ echartsInstance.resize();
106
+ }
107
+ catch (e) {
108
+ console.warn(e);
109
+ }
110
+ });
111
+ }
112
+ };
180
113
  // bind the events
181
-
182
-
183
- // render the dom
184
-
185
- }, {
186
- key: 'render',
187
- value: function render() {
188
- var _this2 = this;
189
-
190
- var _props = this.props,
191
- style = _props.style,
192
- className = _props.className;
193
-
194
- var newStyle = _extends({
195
- height: 300
196
- }, style);
197
- // for render
198
- return _react2['default'].createElement('div', {
199
- ref: function ref(e) {
200
- _this2.echartsElement = e;
201
- },
202
- style: newStyle,
203
- className: 'echarts-for-react ' + className
204
- });
205
- }
206
- }]);
207
-
208
- return EchartsReactCore;
209
- }(_react.Component);
210
-
211
- exports['default'] = EchartsReactCore;
212
-
213
-
214
- EchartsReactCore.propTypes = {
215
- option: _propTypes2['default'].object.isRequired, // eslint-disable-line react/forbid-prop-types
216
- echarts: _propTypes2['default'].object, // eslint-disable-line react/forbid-prop-types
217
- notMerge: _propTypes2['default'].bool,
218
- lazyUpdate: _propTypes2['default'].bool,
219
- style: _propTypes2['default'].object, // eslint-disable-line react/forbid-prop-types
220
- className: _propTypes2['default'].string,
221
- theme: _propTypes2['default'].oneOfType([_propTypes2['default'].string, _propTypes2['default'].object]),
222
- onChartReady: _propTypes2['default'].func,
223
- showLoading: _propTypes2['default'].bool,
224
- loadingOption: _propTypes2['default'].object, // eslint-disable-line react/forbid-prop-types
225
- onEvents: _propTypes2['default'].object, // eslint-disable-line react/forbid-prop-types
226
- opts: _propTypes2['default'].shape({
227
- devicePixelRatio: _propTypes2['default'].number,
228
- renderer: _propTypes2['default'].oneOf(['canvas', 'svg']),
229
- width: _propTypes2['default'].oneOfType([_propTypes2['default'].number, _propTypes2['default'].oneOf([null, undefined, 'auto'])]),
230
- height: _propTypes2['default'].oneOfType([_propTypes2['default'].number, _propTypes2['default'].oneOf([null, undefined, 'auto'])])
231
- }),
232
- shouldSetOption: _propTypes2['default'].func
233
- };
234
-
235
- EchartsReactCore.defaultProps = {
236
- echarts: {},
237
- notMerge: false,
238
- lazyUpdate: false,
239
- style: {},
240
- className: '',
241
- theme: null,
242
- onChartReady: function onChartReady() {},
243
- showLoading: false,
244
- loadingOption: null,
245
- onEvents: {},
246
- opts: {},
247
- shouldSetOption: function shouldSetOption() {
248
- return true;
249
- }
250
- };
114
+ EChartsReactCore.prototype.bindEvents = function (instance, events) {
115
+ function _bindEvent(eventName, func) {
116
+ // ignore the event config which not satisfy
117
+ if (is_string_1.isString(eventName) && is_function_1.isFunction(func)) {
118
+ // binding event
119
+ instance.on(eventName, function (param) {
120
+ func(param, instance);
121
+ });
122
+ }
123
+ }
124
+ // loop and bind
125
+ for (var eventName in events) {
126
+ if (Object.prototype.hasOwnProperty.call(events, eventName)) {
127
+ _bindEvent(eventName, events[eventName]);
128
+ }
129
+ }
130
+ };
131
+ /**
132
+ * render the echarts
133
+ */
134
+ 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 = _c === void 0 ? false : _c, showLoading = _a.showLoading, _d = _a.loadingOption, loadingOption = _d === void 0 ? null : _d;
136
+ // 1. get or initial the echarts object
137
+ var echartInstance = this.getEchartsInstance();
138
+ // 2. set the echarts option
139
+ echartInstance.setOption(option, notMerge, lazyUpdate);
140
+ // 3. set loading mask
141
+ if (showLoading)
142
+ echartInstance.showLoading(loadingOption);
143
+ else
144
+ echartInstance.hideLoading();
145
+ return echartInstance;
146
+ };
147
+ EChartsReactCore.prototype.render = function () {
148
+ var _this = this;
149
+ var _a = this.props, style = _a.style, _b = _a.className, className = _b === void 0 ? '' : _b;
150
+ // default height = 300
151
+ var newStyle = tslib_1.__assign({ height: 300 }, style);
152
+ return (react_1.default.createElement("div", { ref: function (e) {
153
+ _this.ele = e;
154
+ }, style: newStyle, className: "echarts-for-react " + className }));
155
+ };
156
+ return EChartsReactCore;
157
+ }(react_1.PureComponent));
158
+ exports.default = EChartsReactCore;
159
+ //# sourceMappingURL=core.js.map
@@ -0,0 +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,wBA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AAI,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,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"]}
@@ -0,0 +1,2 @@
1
+ import isEqual from 'fast-deep-equal';
2
+ export { isEqual };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isEqual = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var fast_deep_equal_1 = tslib_1.__importDefault(require("fast-deep-equal"));
6
+ exports.isEqual = fast_deep_equal_1.default;
7
+ //# sourceMappingURL=is-equal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-equal.js","sourceRoot":"","sources":["../../src/helper/is-equal.ts"],"names":[],"mappings":";;;;AAAA,4EAAsC;AAE7B,kBAFF,yBAAO,CAEE","sourcesContent":["import isEqual from 'fast-deep-equal';\n\nexport { isEqual };\n"]}
@@ -0,0 +1 @@
1
+ export declare function isFunction(v: any): boolean;