# echarts-for-react
#### 簡介
使用echarts-for-react插件可以在React中調用echarts接口直接渲染出Echarts圖表,只要傳入相關的參數和數據即可。代碼簡介,功能使用。
#### 安裝
~~~
npm install --save echarts-for-react
# `echarts` is the peerDependence of `echarts-for-react`, you can install echarts with your own version.
npm install --save echarts
~~~
#### DEMO演示
~~~
git clone https://github.com/hustcc/echarts-for-react.git
npm install
npm start
~~~

demo效果
demo中可以看到各種不同類型的圖表,在項目開發過程中有需要可以查閱,copy相關的代碼,或者查看圖表的數據格式,了解圖表的相關功能,以便更好地引用及重構圖表。
#### 使用
~~~
import * as React from "react";
import ReactEcharts from "echarts-for-react";
<ReactEcharts
option={this.getOption()}
notMerge={true}
lazyUpdate={true}
theme={"theme_name"}
onChartReady={this.onChartReadyCallback}
onEvents={EventsDict}
opts={} />
~~~
也可手動導入echarts.js模塊以減小包大小
~~~
import React from 'react';
// import the core library.
import ReactEchartsCore from 'echarts-for-react/lib/core';
// then import echarts modules those you have used manually.
import echarts from 'echarts/lib/echarts';
// import 'echarts/lib/chart/line';
import 'echarts/lib/chart/bar';
// import 'echarts/lib/chart/pie';
// import 'echarts/lib/chart/scatter';
// import 'echarts/lib/chart/radar';
// import 'echarts/lib/chart/map';
// import 'echarts/lib/chart/treemap';
// import 'echarts/lib/chart/graph';
// import 'echarts/lib/chart/gauge';
// import 'echarts/lib/chart/funnel';
// import 'echarts/lib/chart/parallel';
// import 'echarts/lib/chart/sankey';
// import 'echarts/lib/chart/boxplot';
// import 'echarts/lib/chart/candlestick';
// import 'echarts/lib/chart/effectScatter';
// import 'echarts/lib/chart/lines';
// import 'echarts/lib/chart/heatmap';
// import 'echarts/lib/component/graphic';
// import 'echarts/lib/component/grid';
// import 'echarts/lib/component/legend';
import 'echarts/lib/component/tooltip';
// import 'echarts/lib/component/polar';
// import 'echarts/lib/component/geo';
// import 'echarts/lib/component/parallel';
// import 'echarts/lib/component/singleAxis';
// import 'echarts/lib/component/brush';
import 'echarts/lib/component/title';
// import 'echarts/lib/component/dataZoom';
// import 'echarts/lib/component/visualMap';
// import 'echarts/lib/component/markPoint';
// import 'echarts/lib/component/markLine';
// import 'echarts/lib/component/markArea';
// import 'echarts/lib/component/timeline';
// import 'echarts/lib/component/toolbox';
// import 'zrender/lib/vml/vml';
<ReactEchartsCore
echarts={echarts}
option={this.getOption()}
notMerge={true}
lazyUpdate={true}
theme={"theme_name"}
onChartReady={this.onChartReadyCallback}
onEvents={EventsDict}
opts={} />
~~~
#### 組件的參數簡介
* **`option`**(required, object)
這個是核心,是必須的,包含echarts圖表的配置項和數據,如標題title、圖例legend、提示框tooltip、x軸xAxis、y軸yAxis、series等,詳見[http://echarts.baidu.com/option.html#title](http://echarts.baidu.com/option.html#title).
* **`notMerge`**(optional, object)
可選,是否不跟之前設置的 option 進行合并,默認為 false,即合并。
* **`lazyUpdate`**(optional, object)
可選,在設置完 option 后是否不立即更新圖表,默認為 false,即立即更新。
* **`style`**(optional, object)
包含echarts圖表的div的樣式,默認是{height: '300px'}.
* **`className`**(optional, string)
包含echarts圖表的div的類名. 可以根據需要自行配置類名,不同類配置不同的css。
* **`theme`**(optional, string)
應用的主題。可以是一個主題的配置對象,也可以是使用已經通過[echarts.registerTheme](https://echarts.baidu.com/api.html#echarts.registerTheme)注冊的主題名稱。
(主題對象的格式樣例:[https://github.com/ecomfe/echarts/blob/master/theme/dark.js](https://github.com/ecomfe/echarts/blob/master/theme/dark.js)).
通過registerTheme注冊主題:
~~~
// import echarts
import echarts from 'echarts';
...
// register theme object
echarts.registerTheme('my_theme', {
backgroundColor: '#f4cccc'
});
...
// render the echarts use option `theme`
<ReactEcharts
option={this.getOption()}
style={{height: '300px', width: '100%'}}
className='echarts-for-echarts'
theme='my_theme' />
~~~
* **`onChartReady`**(optional, function)
當圖表準備好時,將圖表作為參數傳給回調函數
* **`loadingOption`**(optional, object)
* **`showLoading`**(optional, bool, default: false)
是否加載動畫效果
* **`onEvents`**(optional, array(string=>function) )
為圖表綁定事件
~~~
let onEvents = {
'click': this.onChartClick,
'legendselectchanged': this.onChartLegendselectchanged
}
...
<ReactEcharts
option={this.getOption()}
style={{height: '300px', width: '100%'}}
onEvents={onEvents} />
~~~
參見:[http://echarts.baidu.com/api.html#events](http://echarts.baidu.com/api.html#events)
* **`opts`**(optional, object)
附加參數。有下面幾個可選項:
> `devicePixelRatio`
> 設備像素比,默認取瀏覽器的值`window.devicePixelRatio`。
> `renderer`
> 渲染器,支持`'canvas'`或者`'svg'`。參見[使用 Canvas 或者 SVG 渲染](http://echarts.baidu.com/tutorial.html#%E4%BD%BF%E7%94%A8%20Canvas%20%E6%88%96%E8%80%85%20SVG%20%E6%B8%B2%E6%9F%93)。
> `width`
> 可顯式指定實例寬度,單位為像素。如果傳入值為`null`/`undefined`/`'auto'`,則表示自動取`dom`(實例容器)的寬度。
> `height`
> 可顯式指定實例高度,單位為像素。如果傳入值為`null`/`undefined`/`'auto'`,則表示自動取`dom`(實例容器)的高度。
#### 組件API和ECharts API
對于組件來說,只有一個API——getEchartsInstance(),用來獲取Echarts的實例對象。獲取到對象后就可以使用任意的Echarts API。
~~~
// render the echarts component below with rel
<ReactEcharts ref={(e) => { this.echarts_react = e; }}
option={this.getOption()} />
// then get the `ReactEcharts` use this.echarts_react
let echarts_instance = this.echarts_react.getEchartsInstance();
// then you can use any API of echarts.
let base64 = echarts_instance.getDataURL();
~~~
Echarts的API請參見:[Echarts API](https://echarts.baidu.com/api.html#echartsInstance)
使用這些API可以實現以下功能:
* 綁定/解綁事件
* 設置帶有動態數據的動態圖表
* 獲取echarts dom/dataurl/base64,將圖表保存到png。
* 發布圖表
- 第一章 起步
- 第1節 創建react項目
- 第2節 hello world
- 第3節 數據綁定+事件處理
- 3.1 for循環事件處理中的傳參寫法、條件渲染
- 第4章 點擊切換文字
- 第5章 使用html寫react
- 第二章 運用
- 第1節 循環
- 第2節 實現一個簡單的TodoList
- 第2.1節 刪除
- 第3節 父子組件傳參
- 1. 父組件向子組件傳參
- 2. 子組件向父組件傳參
- 第4節 react-router實現一個簡單路由
- 第5節 生命周期
- 第6節 取數據
- 第 7節 獲取dom節點
- 第8節 雙向數據綁定
- 第三章 redux
- 第1節 介紹
- 第2節 安裝redux
- 第3節 使用
- 3.1 action
- 3.2 使用redux實現 todolist
- 第4節封裝redux中的action
- 第5節 redux-thunk中間件
- 5.1介紹
- 5.2使用
- 第四章 ant-design前端ui
- 第一節 安裝
- 第2節 使用
- 2.1 ant-design實現todoList增刪功能
- 第3節 使用整理
- 第五章 vue和react的比較
- 第六章 dva.js輕量級應用框架
- 第1節 介紹
- 第2節 安裝dva
- 第3節 頁面跳轉
- 1. 事件路由跳轉
- 2. 通過路由跳轉
- 第4節 組件之間通信
- 1. 父組件向子組件傳參
- 2. 子組件向父組件傳參
- 第5節 事件處理
- 第6節 發送請求
- 1. 通過路由判斷頁面渲染數據
- 2. 通過事件發送請求
- 第7節 運用
- 1. TodoList
- 1.添加數據
- 1.2輸入框敲回車觸發事件
- 2.刪除數據
- 3. 總結
- 第8節 配合antd使用
- 1. 引入antd
- 2.dva 使用antd注意事項
- 3. 知識點整理
- 第七章 dva后臺項目實戰
- 第1節 登錄加密
- 1.具體實現
- 第2節 知識點
- 第3節 樹結構
- 第八章 react新特性 hooks
- 第1節 hooks介紹
- 第2節 useState的使用
- 第3節 useEffect的使用
- 第4節 dva+antd+hooks企業后臺項目開發流程
- 第 5節 hooks 使用
- 運用
- 第6節 hook整理
- 第7節 react memo
- 第九章 react中使用Echarts
- 知識點
- react中使用mobx
- 知識點
- react中使用rem
- 遞歸實現目錄數
- react使用圖表
- react 同步更新策略
- antd tree默認展開無效
- ts中lint修復篇
- React-query方案
- 高階組件