關于echarts-for-react的按需引用

echarts-for-react能讓react項目更好的使用echarts,使用方式也很簡單

首先,安裝

npm install --save echarts-for-react
npm install --save echarts

具體項目中的使用情況,很多同學采用下面這種比較簡單粗暴的方式,全部引用

import ReactEcharts from 'echarts-for-react';
 
// render echarts option.
<ReactEcharts option={this.getOption()} />

但凡這么使用過的同學,應該都會發(fā)現(xiàn),js包非常大,那么,我們該如何優(yōu)化呢,下面我會具體講下如何按需引用。官方給出的例子不夠詳細,我會舉幾個具體例子方便大家參考

案例一,餅圖,按需加載模塊

import React from 'react';
import ReactEcharts from 'echarts-for-react/lib/core';
import echarts from 'echarts/lib/echarts';
import 'echarts/lib/chart/pie';

// render echarts option.
<ReactEcharts option={this.getOption()} />

案例二,柱狀圖,按需加載模塊

import React from 'react';
import ReactEcharts from 'echarts-for-react/lib/core';
import echarts from 'echarts/lib/echarts';
import 'echarts/lib/chart/bar';

// render echarts option.
<ReactEcharts option={this.getOption()} />

案例三,中國地圖,按需加載模塊

import React from 'react';
import ReactEcharts from 'echarts-for-react/lib/core';
import echarts from 'echarts/lib/echarts';
import 'echarts/lib/chart/map';
import 'echarts/map/js/china.js';

// render echarts option.
<ReactEcharts option={this.getOption()} />

注意,如果再地圖中又使用了lines等還需要再引入 import 'echarts/lib/chart/line';

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容