Element中使用ECharts的項(xiàng)目實(shí)踐
一、引入ECharts
1、直接引入echarts (安裝echarts項(xiàng)目依賴)
npm install echarts --save
2、全局引入 (我們安裝完成之后,可以在main.js中全局引入 echarts)
import echarts from "echarts"; Vue.prototype.$echarts = echarts;
3、我們可以將Echar封裝成組件的形式,方便調(diào)用
封裝在組件中:封裝成 Echarts.vue 文件放在ElementUI前端框架中
<template>
<div ref="chartDom" ></div>
</template>
<script>
import * as echarts from 'echarts';
import debounce from "lodash/debounce";
import { addListener, removeListener} from "resize-detector";
export default {
props: {
option: {
type: Object,
default: ()=> {}
}
},
watch: {
// option(val) {
// this.chart.setOption(val);
// },
option: {
handler(val) {
this.chart.setOption(val);
},
deep: true
}
},
created() {
this.resize = debounce(this.resize, 300);
},
mounted() {
this.renderChart();
addListener(this.$refs.chartDom, this.resize);
},
beforeDestroy() {
removeListener(this.$refs.chartDom, this.resize);
this.chart.dispose();
this.chart = null;
},
methods:{
resize(){
this.chart.resize();
},
renderChart() {
this.chart = echarts.init(this.$refs.chartDom);
this.chart.setOption(this.option);
}
},
}
</script>
<style>
</style>
4、此時(shí)我們可以通過(guò)Echart官網(wǎng)引入我們需要的圖
Echart官網(wǎng) https://echarts.apache.org/zh/index.html
例如:以所選的折柱混合圖為例

引入(需要引入Echarts剛剛封裝好的組件)
從Echart官網(wǎng)獲取對(duì)應(yīng)圖的代碼

引入代碼:
<template>
<div>
<el-row>
<Echarts :option="option" style="height: 400px;width: 630px" />
</el-row>
</div>
</template>
<script>
//引入Echart的包
import Echarts from "../../components/charts/Echarts";
export default {
components:{
Echarts,
},
data(){
return{
option:{
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
crossStyle: {
color: '#999'
}
}
},
toolbox: {
feature: {
dataView: { show: true, readOnly: false },
magicType: { show: true, type: ['line', 'bar'] },
restore: { show: true },
saveAsImage: { show: true }
}
},
legend: {
data: ['Evaporation', 'Precipitation', 'Temperature']
},
xAxis: [
{
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
axisPointer: {
type: 'shadow'
}
}
],
yAxis: [
{
type: 'value',
name: 'Precipitation',
min: 0,
max: 250,
interval: 50,
axisLabel: {
formatter: '{value} ml'
}
},
{
type: 'value',
name: 'Temperature',
min: 0,
max: 25,
interval: 5,
axisLabel: {
formatter: '{value} °C'
}
}
],
series: [
{
name: 'Evaporation',
type: 'bar',
tooltip: {
valueFormatter: function (value) {
return value + ' ml';
}
},
data: [
2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
]
},
{
name: 'Precipitation',
type: 'bar',
tooltip: {
valueFormatter: function (value) {
return value + ' ml';
}
},
data: [
2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
]
},
{
name: 'Temperature',
type: 'line',
yAxisIndex: 1,
tooltip: {
valueFormatter: function (value) {
return value + ' °C';
}
},
data: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
}
]
};
},
created: function () {
},
methods:{
}
}
</script>
<style scoped>
</style>
二、效果展示

到此這篇關(guān)于Element中使用ECharts的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)Element使用ECharts內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue 格式化銀行卡(信用卡)每4位一個(gè)符號(hào)隔斷的問(wèn)題
這篇文章主要介紹了vue 格式化銀行卡(信用卡)每4位一個(gè)符號(hào)隔斷的問(wèn)題,本文給大家分享了解決方法,需要的朋友可以參考下2018-09-09
Vue.js響應(yīng)式數(shù)據(jù)的簡(jiǎn)單實(shí)現(xiàn)方法(一看就會(huì))
Vue最巧妙的特性之一是其響應(yīng)式系統(tǒng),下面這篇文章主要給大家介紹了關(guān)于Vue.js響應(yīng)式數(shù)據(jù)的簡(jiǎn)單實(shí)現(xiàn)方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
詳解keep-alive + vuex 讓緩存的頁(yè)面靈活起來(lái)
這篇文章主要介紹了keep-alive + vuex 讓緩存的頁(yè)面靈活起來(lái),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue打包之后配置統(tǒng)一請(qǐng)求地址步驟詳解
這篇文章主要為大家介紹了vue打包之后配置統(tǒng)一請(qǐng)求地址實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
解決element-ui中下拉菜單子選項(xiàng)click事件不觸發(fā)的問(wèn)題
今天小編就為大家分享一篇解決element-ui中下拉菜單子選項(xiàng)click事件不觸發(fā)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue+animation動(dòng)畫實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了vue+animation動(dòng)畫實(shí)現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
你不知道的Vue技巧之--開發(fā)一個(gè)可以通過(guò)方法調(diào)用的組件(推薦)
這篇文章主要介紹了你不知道的Vue技巧之--開發(fā)一個(gè)可以通過(guò)方法調(diào)用的組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue實(shí)現(xiàn)新聞?wù)故卷?yè)的步驟詳解
最近小編遇到這樣的需求,要實(shí)現(xiàn)一個(gè)新聞?wù)故卷?yè)功能,剛接到這樣的需求還真是一頭霧水,不知從哪入手,今天小編通過(guò)實(shí)例代碼給大家介紹下vue實(shí)現(xiàn)新聞?wù)故卷?yè)的步驟詳解,感興趣的朋友跟隨小編一起看看吧2019-04-04

