前端知識 | 淺談在React中使用echarts

方法一:

echarts-for-react 是一個非常簡單的針對於 React 的 Echarts 封裝插件。

和使用所有其他插件一樣,首先,我們需要 install 它:

第一步:

npminstall --save echarts(依賴)

npminstall --save echarts-for-react

第二步:

在我們的項目中導入:

importReactEcharts from 'echarts-for-react'

第三步:

在 render 函數中使用:

  option={this.getOption}

  notMerge={true}

  lazyUpdate={true}

  style={{width: ‘400px’, height: ‘400px’}}

/>

組件基本參數介紹:

option:接收一個對象,該對象爲 echarts 的配置項,詳見:                 http://echarts.baidu.com/option.html#title

notMerge:可選,是否不跟之前設置的 option 進行合併,默認爲 false,即合併。

LazyUpdate:可選,在設置完 option 後是否不立即更新圖表,默認爲 false,即立即更新。

style:echarts 容器 div 大小,默認:{height: ‘300px’}


方法二:

當然,我們也不是真的需要一個 react-echarts 插件,我們可以使用 echarts 提供的模塊化加載方法,按需導入我們需要的圖表:

首先,我們需要在項目中導入 echarts:

importecharts from 'echarts/lib/echarts'    //必須

import'echarts/lib/component/tooltip'        //圖表提示框(按需)

import'echarts/lib/component/grid'      //圖表網格(按需)

import 'echarts/lib/chart/bar'                 //引入柱狀圖(按需)

import'echarts/lib/chart/line’              //引入折線圖(按需)

然後:我們需要在 render 函數中爲圖表放好一個容器:

{this.chartContainer = refs}} style={{width: ‘400px’, height: ‘400px’}}>

最後,我們需要在合適的生命週期中繪製我們的圖表:

letmyChart = echarts.init(this.chartContainer)

letoption = {//echarts配置項}

myChart.setOption(option,true)


好了,echarts 已經成功的在項目中跑起來啦!


-END- 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章