echarts-for-react 2.0.4 → 2.0.8

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 CHANGED
@@ -1,16 +1,28 @@
1
1
  # echarts-for-react
2
2
 
3
- A very simple echarts(v3.0) wrapper for react.
3
+ A very simple echarts(v3.0 & v4.0) wrapper for react.
4
4
 
5
- [![Build Status](https://travis-ci.org/hustcc/echarts-for-react.svg?branch=master)](https://travis-ci.org/hustcc/echarts-for-react) [![Coverage Status](https://coveralls.io/repos/github/hustcc/echarts-for-react/badge.svg?branch=master)](https://coveralls.io/github/hustcc/echarts-for-react?branch=master) [![npm](https://img.shields.io/npm/v/echarts-for-react.svg)](https://www.npmjs.com/package/echarts-for-react) [![npm](https://img.shields.io/npm/dt/echarts-for-react.svg)](https://www.npmjs.com/package/echarts-for-react) [![npm](https://img.shields.io/npm/l/echarts-for-react.svg)](https://www.npmjs.com/package/echarts-for-react) ![echarts supported](https://img.shields.io/badge/echarts-%5E3.0.0-blue.svg) ![react supported](https://img.shields.io/badge/React-%3E%3D0.13.2%20%7C%7C%20%5E0.14%20%7C%7C%20%5E15.0.0%20%7C%7C%20%3E%3D16.0.0-blue.svg)
5
+ [![Build Status](https://travis-ci.org/hustcc/echarts-for-react.svg?branch=master)](https://travis-ci.org/hustcc/echarts-for-react) [![Coverage Status](https://coveralls.io/repos/github/hustcc/echarts-for-react/badge.svg?branch=master)](https://coveralls.io/github/hustcc/echarts-for-react?branch=master) [![npm](https://img.shields.io/npm/v/echarts-for-react.svg)](https://www.npmjs.com/package/echarts-for-react) [![npm](https://img.shields.io/npm/dm/echarts-for-react.svg)](https://www.npmjs.com/package/echarts-for-react) [![npm](https://img.shields.io/npm/l/echarts-for-react.svg)](https://www.npmjs.com/package/echarts-for-react) ![echarts supported](https://img.shields.io/badge/echarts-%5E3.0.0%20%7C%7C%20%5E4.0.0-blue.svg) ![react supported](https://img.shields.io/badge/React-%3E%3D0.13.2%20%7C%7C%20%5E0.14%20%7C%7C%20%5E15.0.0%20%7C%7C%20%3E%3D16.0.0-blue.svg)
6
6
 
7
7
  # 1. install
8
8
 
9
9
  ```sh
10
10
  npm install --save echarts-for-react
11
+
12
+ # `echarts` is the peerDependence of `echarts-for-react`, you can install echarts with your own version.
13
+ npm install --save echarts
14
+ ```
15
+
16
+ Then use it.
17
+
18
+ ```js
19
+ import ReactEcharts from 'echarts-for-react';
20
+
21
+ // render echarts option.
22
+ <ReactEcharts option={this.getOption()} />
11
23
  ```
12
24
 
13
- How to run the demo:
25
+ You can run demo by:
14
26
 
15
27
  ```sh
16
28
  git clone git@github.com:hustcc/echarts-for-react.git
@@ -39,7 +51,8 @@ import ReactEcharts from 'echarts-for-react'; // or var ReactEcharts = require(
39
51
  lazyUpdate={true}
40
52
  theme={"theme_name"}
41
53
  onChartReady={this.onChartReadyCallback}
42
- onEvents={EventsDict} />
54
+ onEvents={EventsDict}
55
+ opts={} />
43
56
  ```
44
57
 
45
58
  ### typescript
@@ -49,12 +62,13 @@ import * as React from "react";
49
62
  import ReactEcharts from "echarts-for-react";
50
63
 
51
64
  <ReactEcharts
52
- option={this.getOption()}
53
- notMerge={true}
54
- lazyUpdate={true}
55
- theme={"theme_name"}
56
- onChartReady={this.onChartReadyCallback}
57
- onEvents={EventsDict} />
65
+ option={this.getOption()}
66
+ notMerge={true}
67
+ lazyUpdate={true}
68
+ theme={"theme_name"}
69
+ onChartReady={this.onChartReadyCallback}
70
+ onEvents={EventsDict}
71
+ opts={} />
58
72
  ```
59
73
 
60
74
  If you have only used bar chart (or others), you can import echarts modules manually to reduce bundle file size, e.g.
@@ -77,7 +91,8 @@ import 'echarts/lib/component/tooltip';
77
91
  lazyUpdate={true}
78
92
  theme={"theme_name"}
79
93
  onChartReady={this.onChartReadyCallback}
80
- onEvents={EventsDict} />
94
+ onEvents={EventsDict}
95
+ opts={} />
81
96
  ```
82
97
 
83
98
 
@@ -87,11 +102,11 @@ import 'echarts/lib/component/tooltip';
87
102
 
88
103
  the echarts option config, can see [http://echarts.baidu.com/option.html#title](http://echarts.baidu.com/option.html#title).
89
104
 
90
- - **`notMerge`** (required, object)
105
+ - **`notMerge`** (optional, object)
91
106
 
92
107
  when `setOption`, not merge the data, default is `false`. See [http://echarts.baidu.com/api.html#echartsInstance.setOption](http://echarts.baidu.com/api.html#echartsInstance.setOption).
93
108
 
94
- - **`lazyUpdate`** (required, object)
109
+ - **`lazyUpdate`** (optional, object)
95
110
 
96
111
  when `setOption`, lazy update the data, default is `false`. See [http://echarts.baidu.com/api.html#echartsInstance.setOption](http://echarts.baidu.com/api.html#echartsInstance.setOption).
97
112
 
@@ -107,7 +122,6 @@ the `class` of echarts div. you can setting the css style of charts by class nam
107
122
 
108
123
  the `theme` of echarts. `string`, should `registerTheme` before use it (theme object format: [https://github.com/ecomfe/echarts/blob/master/theme/dark.js](https://github.com/ecomfe/echarts/blob/master/theme/dark.js)). e.g.
109
124
 
110
-
111
125
  ```js
112
126
  // import echarts
113
127
  import echarts from 'echarts';
@@ -154,6 +168,18 @@ let onEvents = {
154
168
  ```
155
169
  for more event key name, see: [http://echarts.baidu.com/api.html#events](http://echarts.baidu.com/api.html#events)
156
170
 
171
+ - **`opts`** (optional, object)
172
+
173
+ the `opts` of echarts. `object`, will be used when initial echarts instance by `echarts.init`. Document [here](http://echarts.baidu.com/api.html#echarts.init).
174
+
175
+ ```js
176
+ <ReactEcharts
177
+ option={this.getOption()}
178
+ style={{height: '300px'}}
179
+ opts={{renderer: 'svg'}} // use svg to render the chart.
180
+ />
181
+ ```
182
+
157
183
 
158
184
  # 4. Component API & Echarts API
159
185
 
@@ -185,7 +211,23 @@ You can use the API to do:
185
211
  4. `release` the charts.
186
212
 
187
213
 
188
- # 5. LICENSE
214
+ # 5. Q & A
215
+
216
+ - How to render the chart with svg when using echarts 4.x
217
+
218
+ Use the props `opts` of component with `renderer = 'svg'`. For example:
219
+
220
+
221
+ ```js
222
+ <ReactEcharts
223
+ option={this.getOption()}
224
+ style={{height: '300px'}}
225
+ opts={{renderer: 'svg'}} // use svg to render the chart.
226
+ />
227
+ ```
228
+
229
+
230
+ # 6. LICENSE
189
231
 
190
232
  MIT@[hustcc](https://github.com/hustcc).
191
233
 
package/lib/core.js CHANGED
@@ -42,7 +42,7 @@ var EchartsReactCore = function (_Component) {
42
42
  var _this = _possibleConstructorReturn(this, (EchartsReactCore.__proto__ || Object.getPrototypeOf(EchartsReactCore)).call(this, props));
43
43
 
44
44
  _this.getEchartsInstance = function () {
45
- return _this.echartsInstance.getInstanceByDom(_this.echartsElement) || _this.echartsInstance.init(_this.echartsElement, _this.props.theme);
45
+ return _this.echartsLib.getInstanceByDom(_this.echartsElement) || _this.echartsLib.init(_this.echartsElement, _this.props.theme, _this.props.opts);
46
46
  };
47
47
 
48
48
  _this.dispose = function () {
@@ -51,7 +51,8 @@ var EchartsReactCore = function (_Component) {
51
51
  try {
52
52
  _elementResizeEvent2['default'].unbind(_this.echartsElement);
53
53
  } catch (_) {}
54
- _this.echartsInstance.dispose(_this.echartsElement);
54
+ // dispose echarts instance
55
+ _this.echartsLib.dispose(_this.echartsElement);
55
56
  }
56
57
  };
57
58
 
@@ -63,6 +64,12 @@ var EchartsReactCore = function (_Component) {
63
64
 
64
65
  var echartObj = _this.renderEchartDom();
65
66
  _this.bindEvents(echartObj, onEvents || {});
67
+
68
+ // fix bug of 100px width * height.
69
+ try {
70
+ echartObj.resize();
71
+ } catch (_) {}
72
+
66
73
  // on chart ready
67
74
  if (typeof onChartReady === 'function') _this.props.onChartReady(echartObj);
68
75
  // on resize
@@ -74,20 +81,21 @@ var EchartsReactCore = function (_Component) {
74
81
  };
75
82
 
76
83
  _this.bindEvents = function (instance, events) {
77
- var _loopEvent = function _loopEvent(eventName) {
84
+ var _bindEvent = function _bindEvent(eventName, func) {
78
85
  // ignore the event config which not satisfy
79
- if (typeof eventName === 'string' && typeof events[eventName] === 'function') {
86
+ if (typeof eventName === 'string' && typeof func === 'function') {
80
87
  // binding event
81
- instance.off(eventName);
88
+ // instance.off(eventName); // 已经 dispose 在重建,所以无需 off 操作
82
89
  instance.on(eventName, function (param) {
83
- events[eventName](param, instance);
90
+ func(param, instance);
84
91
  });
85
92
  }
86
93
  };
87
94
 
95
+ // loop and bind
88
96
  for (var eventName in events) {
89
97
  if (Object.prototype.hasOwnProperty.call(events, eventName)) {
90
- _loopEvent(eventName);
98
+ _bindEvent(eventName, events[eventName]);
91
99
  }
92
100
  }
93
101
  };
@@ -103,7 +111,7 @@ var EchartsReactCore = function (_Component) {
103
111
  return echartObj;
104
112
  };
105
113
 
106
- _this.echartsInstance = _this.props.echarts; // the echarts object.
114
+ _this.echartsLib = _this.props.echarts; // the echarts object.
107
115
  _this.echartsElement = null; // echarts div element
108
116
  return _this;
109
117
  }
@@ -123,16 +131,19 @@ var EchartsReactCore = function (_Component) {
123
131
  key: 'componentDidUpdate',
124
132
  value: function componentDidUpdate(prevProps) {
125
133
  var echartObj = this.renderEchartDom();
126
- this.bindEvents(echartObj, this.props.onEvents || {});
127
134
 
128
- // 切换 theme 的时候,需要 dispost 之后才新建
129
- if (this.props.theme !== prevProps.theme) {
135
+ // 以下属性修改的时候,需要 dispose 之后再新建
136
+ // 1. 切换 theme 的时候
137
+ // 2. 修改 opts 的时候
138
+ // 3. 修改 onEvents 的时候,这样可以取消所以之前绑定的事件 issue #151
139
+ if (prevProps.theme !== this.props.theme || !isEqual(prevProps.opts, this.props.opts) || !isEqual(Object.keys(prevProps.onEvents || {}).sort(), Object.keys(this.props.onEvents || {}).sort())) {
130
140
  this.dispose();
131
141
 
132
142
  this.rerender(); // 重建
133
143
  return;
134
144
  }
135
145
 
146
+ // 样式修改的时候,可能会导致大小变化,所以触发一下 resize
136
147
  if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {
137
148
  try {
138
149
  echartObj.resize();
@@ -200,7 +211,13 @@ EchartsReactCore.propTypes = {
200
211
  onChartReady: _propTypes2['default'].func,
201
212
  showLoading: _propTypes2['default'].bool,
202
213
  loadingOption: _propTypes2['default'].object, // eslint-disable-line react/forbid-prop-types
203
- onEvents: _propTypes2['default'].object // eslint-disable-line react/forbid-prop-types
214
+ onEvents: _propTypes2['default'].object, // eslint-disable-line react/forbid-prop-types
215
+ opts: _propTypes2['default'].shape({
216
+ devicePixelRatio: _propTypes2['default'].number,
217
+ renderer: _propTypes2['default'].oneOf(['canvas', 'svg']),
218
+ width: _propTypes2['default'].oneOfType([_propTypes2['default'].number, _propTypes2['default'].oneOf([null, undefined, 'auto'])]),
219
+ height: _propTypes2['default'].oneOfType([_propTypes2['default'].number, _propTypes2['default'].oneOf([null, undefined, 'auto'])])
220
+ })
204
221
  };
205
222
 
206
223
  EchartsReactCore.defaultProps = {
@@ -213,5 +230,6 @@ EchartsReactCore.defaultProps = {
213
230
  onChartReady: function onChartReady() {},
214
231
  showLoading: false,
215
232
  loadingOption: null,
216
- onEvents: {}
233
+ onEvents: {},
234
+ opts: {}
217
235
  };
package/lib/index.d.ts CHANGED
@@ -12,6 +12,13 @@ interface ObjectMap {
12
12
  [key: string]: any,
13
13
  }
14
14
 
15
+ interface optsMap {
16
+ devicePixelRatio?: number,
17
+ renderer?: 'canvas' | 'svg',
18
+ width?: number | null | undefined | 'auto',
19
+ height?: number | null | undefined | 'auto',
20
+ }
21
+
15
22
  // Index
16
23
  export interface ReactEchartsPropsTypes {
17
24
  option: ObjectMap;
@@ -25,6 +32,7 @@ export interface ReactEchartsPropsTypes {
25
32
  loadingOption?: ObjectMap;
26
33
  onEvents?: EventMap;
27
34
  echarts?: object;
35
+ opts?: optsMap;
28
36
  }
29
37
 
30
38
  export default class ReactEcharts extends React.Component<ReactEchartsPropsTypes, any>{}
package/lib/index.js CHANGED
@@ -30,7 +30,7 @@ var EchartsReact = function (_EchartsReactCore) {
30
30
 
31
31
  var _this = _possibleConstructorReturn(this, (EchartsReact.__proto__ || Object.getPrototypeOf(EchartsReact)).call(this, props));
32
32
 
33
- _this.echartsInstance = _echarts2['default'];
33
+ _this.echartsLib = _echarts2['default'];
34
34
  return _this;
35
35
  }
36
36
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "echarts-for-react",
3
- "version": "2.0.4",
4
- "description": "baidu Echarts(v3.x) components for react",
3
+ "version": "2.0.8",
4
+ "description": "baidu Echarts(v3.x & v4.x) components for react.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "files": [
@@ -15,8 +15,7 @@
15
15
  "test": "npm run lint && npm run cover",
16
16
  "demo": "webpack",
17
17
  "dev": "webpack --watch",
18
- "build": "npm run clean && cross-env NODE_ENV=production babel src -d lib && npm run demo",
19
- "prepublish": "npm run build",
18
+ "build": "npm run test && npm run clean && cross-env NODE_ENV=production babel src -d lib && npm run demo",
20
19
  "cover": "jest --coverage",
21
20
  "coveralls": "cat ./coverage/lcov.info | coveralls"
22
21
  },
@@ -42,7 +41,7 @@
42
41
  },
43
42
  "homepage": "https://github.com/hustcc/echarts-for-react",
44
43
  "devDependencies": {
45
- "76": "^0.0.3",
44
+ "76": "^0.0.4",
46
45
  "babel-cli": "^6.26.0",
47
46
  "babel-core": "^6.26.0",
48
47
  "babel-eslint": "^8.0.3",
@@ -60,7 +59,8 @@
60
59
  "coveralls": "^3.0.0",
61
60
  "cross-env": "^5.1.3",
62
61
  "css-loader": "^0.28.7",
63
- "echarts": "^3.8.5",
62
+ "echarts": "^4.0.0",
63
+ "echarts-gl": "^1.0.0-beta.6",
64
64
  "enzyme": "^3.2.0",
65
65
  "enzyme-adapter-react-15": "^1.0.5",
66
66
  "eslint": "^4.13.1",
@@ -72,7 +72,7 @@
72
72
  "eslint-plugin-react": "^7.5.1",
73
73
  "file-loader": "^1.1.6",
74
74
  "jest": "^21.2.1",
75
- "jest-canvas-mock": "^0.0.2",
75
+ "jest-canvas-mock": "^1.0.0",
76
76
  "jsx-loader": "^0.13.2",
77
77
  "react": "^15.5.0",
78
78
  "react-adsense": "^0.0.3",
@@ -85,12 +85,12 @@
85
85
  "webpack-dev-server": "^2.9.7"
86
86
  },
87
87
  "dependencies": {
88
- "element-resize-event": "2.0.8"
88
+ "element-resize-event": "2.0.9"
89
89
  },
90
90
  "peerDependencies": {
91
91
  "react": ">=0.13.2 || ^0.14 || ^15.0.0 || >=16.0.0",
92
92
  "prop-types": "^15.5.7",
93
- "echarts": "^3.0.0"
93
+ "echarts": "^3.0.0 || ^4.0.0"
94
94
  },
95
95
  "jest": {
96
96
  "testRegex": "(/__tests__/.*\\.spec)\\.(js|jsx)$",
package/src/core.jsx CHANGED
@@ -7,7 +7,7 @@ const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
7
7
  export default class EchartsReactCore extends Component {
8
8
  constructor(props) {
9
9
  super(props);
10
- this.echartsInstance = this.props.echarts; // the echarts object.
10
+ this.echartsLib = this.props.echarts; // the echarts object.
11
11
  this.echartsElement = null; // echarts div element
12
12
  }
13
13
 
@@ -19,16 +19,23 @@ export default class EchartsReactCore extends Component {
19
19
  // update
20
20
  componentDidUpdate(prevProps) {
21
21
  const echartObj = this.renderEchartDom();
22
- this.bindEvents(echartObj, this.props.onEvents || {});
23
22
 
24
- // 切换 theme 的时候,需要 dispost 之后才新建
25
- if (this.props.theme !== prevProps.theme) {
23
+ // 以下属性修改的时候,需要 dispose 之后再新建
24
+ // 1. 切换 theme 的时候
25
+ // 2. 修改 opts 的时候
26
+ // 3. 修改 onEvents 的时候,这样可以取消所以之前绑定的事件 issue #151
27
+ if (
28
+ prevProps.theme !== this.props.theme ||
29
+ !isEqual(prevProps.opts, this.props.opts) ||
30
+ !isEqual(Object.keys(prevProps.onEvents || {}).sort(), Object.keys(this.props.onEvents || {}).sort())
31
+ ) {
26
32
  this.dispose();
27
33
 
28
34
  this.rerender(); // 重建
29
35
  return;
30
36
  }
31
37
 
38
+ // 样式修改的时候,可能会导致大小变化,所以触发一下 resize
32
39
  if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {
33
40
  try {
34
41
  echartObj.resize();
@@ -42,8 +49,8 @@ export default class EchartsReactCore extends Component {
42
49
  }
43
50
 
44
51
  // return the echart object
45
- getEchartsInstance = () => this.echartsInstance.getInstanceByDom(this.echartsElement) ||
46
- this.echartsInstance.init(this.echartsElement, this.props.theme);
52
+ getEchartsInstance = () => this.echartsLib.getInstanceByDom(this.echartsElement) ||
53
+ this.echartsLib.init(this.echartsElement, this.props.theme, this.props.opts);
47
54
 
48
55
  // dispose echarts and element-resize-event
49
56
  dispose = () => {
@@ -52,7 +59,8 @@ export default class EchartsReactCore extends Component {
52
59
  try {
53
60
  elementResizeEvent.unbind(this.echartsElement);
54
61
  } catch (_) {}
55
- this.echartsInstance.dispose(this.echartsElement);
62
+ // dispose echarts instance
63
+ this.echartsLib.dispose(this.echartsElement);
56
64
  }
57
65
  };
58
66
 
@@ -61,6 +69,12 @@ export default class EchartsReactCore extends Component {
61
69
 
62
70
  const echartObj = this.renderEchartDom();
63
71
  this.bindEvents(echartObj, onEvents || {});
72
+
73
+ // fix bug of 100px width * height.
74
+ try {
75
+ echartObj.resize();
76
+ } catch (_) {}
77
+
64
78
  // on chart ready
65
79
  if (typeof onChartReady === 'function') this.props.onChartReady(echartObj);
66
80
  // on resize
@@ -73,20 +87,21 @@ export default class EchartsReactCore extends Component {
73
87
 
74
88
  // bind the events
75
89
  bindEvents = (instance, events) => {
76
- const _loopEvent = (eventName) => {
90
+ const _bindEvent = (eventName, func) => {
77
91
  // ignore the event config which not satisfy
78
- if (typeof eventName === 'string' && typeof events[eventName] === 'function') {
92
+ if (typeof eventName === 'string' && typeof func === 'function') {
79
93
  // binding event
80
- instance.off(eventName);
94
+ // instance.off(eventName); // 已经 dispose 在重建,所以无需 off 操作
81
95
  instance.on(eventName, (param) => {
82
- events[eventName](param, instance);
96
+ func(param, instance);
83
97
  });
84
98
  }
85
99
  };
86
100
 
101
+ // loop and bind
87
102
  for (const eventName in events) {
88
103
  if (Object.prototype.hasOwnProperty.call(events, eventName)) {
89
- _loopEvent(eventName);
104
+ _bindEvent(eventName, events[eventName]);
90
105
  }
91
106
  }
92
107
  };
@@ -132,7 +147,19 @@ EchartsReactCore.propTypes = {
132
147
  onChartReady: PropTypes.func,
133
148
  showLoading: PropTypes.bool,
134
149
  loadingOption: PropTypes.object, // eslint-disable-line react/forbid-prop-types
135
- onEvents: PropTypes.object // eslint-disable-line react/forbid-prop-types
150
+ onEvents: PropTypes.object, // eslint-disable-line react/forbid-prop-types
151
+ opts: PropTypes.shape({
152
+ devicePixelRatio: PropTypes.number,
153
+ renderer: PropTypes.oneOf(['canvas', 'svg']),
154
+ width: PropTypes.oneOfType([
155
+ PropTypes.number,
156
+ PropTypes.oneOf([null, undefined, 'auto'])
157
+ ]),
158
+ height: PropTypes.oneOfType([
159
+ PropTypes.number,
160
+ PropTypes.oneOf([null, undefined, 'auto'])
161
+ ]),
162
+ })
136
163
  };
137
164
 
138
165
  EchartsReactCore.defaultProps = {
@@ -146,4 +173,5 @@ EchartsReactCore.defaultProps = {
146
173
  showLoading: false,
147
174
  loadingOption: null,
148
175
  onEvents: {},
176
+ opts: {},
149
177
  };
package/src/index.js CHANGED
@@ -5,6 +5,6 @@ import EchartsReactCore from './core';
5
5
  export default class EchartsReact extends EchartsReactCore {
6
6
  constructor(props) {
7
7
  super(props);
8
- this.echartsInstance = echarts;
8
+ this.echartsLib = echarts;
9
9
  }
10
10
  }