特征
•輕量,高效,按需綁定事件
•支持按需導(dǎo)入ECharts.js圖表和組件
•支持組件調(diào)整大小事件自動(dòng)更新視圖
一、安裝
npm install --save echarts vue-echarts-v3
二、用法
用 vue-cli 搭建的項(xiàng)目,打開(kāi) build 文件夾中的 webpack.base.conf.js 文件
1、webpack 1.x 修改成如下
{ test: //.js$/, loader: 'babel', include: [ path.join(prjRoot, 'src'), path.join(prjRoot, 'node_modules/vue-echarts-v3/src') ], exclude: /node_modules(?![///]vue-echarts-v3[///]src[///])/ },2、webpack 2.x 修改成如下
{ test: //.js$/, loader: 'babel-loader', include: [resolve('src'), resolve('test'), resolve('node_modules/vue-echarts-v3/src')] }3、導(dǎo)入所有圖表和組件
import IEcharts from 'vue-echarts-v3/src/full.js';
4、手動(dòng)導(dǎo)入ECharts.js模塊以減少捆綁包大小
import IEcharts from 'vue-echarts-v3/src/lite.js';import 'echarts/lib/component/title'; //引入標(biāo)題組件import 'echarts/lib/component/legend'; //引入圖例組件import 'echarts/lib/component/tooltip'; //引入提示框組件import 'echarts/lib/component/toolbox'; //引入工具箱組件// ...(引入自己需要的)import 'echarts/lib/chart/pie'; //以餅圖為例
三、例子
關(guān)于Echarts的API使用參照 Echarts官網(wǎng)
•從接口獲取的動(dòng)態(tài)數(shù)據(jù)可以直接通過(guò)props從父組件傳過(guò)來(lái),替換下面的series[0].data數(shù)組中的數(shù)據(jù)
•也可以從這個(gè)組件中從接口獲取動(dòng)態(tài)數(shù)據(jù)。但option屬性對(duì)應(yīng)的變量pie必須寫(xiě)在computed中return出來(lái),不能再寫(xiě)在data中,否則獲取不到數(shù)據(jù)
<template> <div class="echarts"> <IEcharts :option="pie" @ready="onReady" @click="onClick"></IEcharts> </div></template><script>import IEcharts from 'vue-echarts-v3/src/lite.js';import 'echarts/lib/component/title'; //引入標(biāo)題組件import 'echarts/lib/component/legend'; //引入圖例組件import 'echarts/lib/chart/pie'; export default { components: {IEcharts}, data: () => ({ pie: { title: { text: 'ECharts Demo' }, tooltip: {}, legend:{ type: 'plain', orient: 'vertical', right: 10, top: 20, }, series: [{ type: 'pie', data: [ {name: 'A', value: 1211}, {name: 'B', value: 2323}, {name: 'C', value: 1919} ] }] } }), methods: { onReady(instance) { console.log(instance); }, onClick(event, instance, echarts) { console.log(arguments); } } };</script><style scoped> .echarts{ width: 400px; height: 400px; margin: auto; text-align: center; }</style>總結(jié)
以上所述是小編給大家介紹的在vue中使用vue-echarts-v3的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注