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í)我們可以通過Echart官網(wǎng)引入我們需要的圖
Echart官網(wǎng) https://echarts.apache.org/zh/index.html
例如:以所選的折柱混合圖為例
引入(需要引入Echarts剛剛封裝好的組件)
從Echart官網(wǎng)獲取對應(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue 格式化銀行卡(信用卡)每4位一個(gè)符號隔斷的問題
這篇文章主要介紹了vue 格式化銀行卡(信用卡)每4位一個(gè)符號隔斷的問題,本文給大家分享了解決方法,需要的朋友可以參考下2018-09-09Vue.js響應(yīng)式數(shù)據(jù)的簡單實(shí)現(xiàn)方法(一看就會)
Vue最巧妙的特性之一是其響應(yīng)式系統(tǒng),下面這篇文章主要給大家介紹了關(guān)于Vue.js響應(yīng)式數(shù)據(jù)的簡單實(shí)現(xiàn)方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03詳解keep-alive + vuex 讓緩存的頁面靈活起來
這篇文章主要介紹了keep-alive + vuex 讓緩存的頁面靈活起來,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04解決element-ui中下拉菜單子選項(xiàng)click事件不觸發(fā)的問題
今天小編就為大家分享一篇解決element-ui中下拉菜單子選項(xiàng)click事件不觸發(fā)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08vue+animation動(dòng)畫實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了vue+animation動(dòng)畫實(shí)現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04你不知道的Vue技巧之--開發(fā)一個(gè)可以通過方法調(diào)用的組件(推薦)
這篇文章主要介紹了你不知道的Vue技巧之--開發(fā)一個(gè)可以通過方法調(diào)用的組件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04vue實(shí)現(xiàn)新聞?wù)故卷摰牟襟E詳解
最近小編遇到這樣的需求,要實(shí)現(xiàn)一個(gè)新聞?wù)故卷摴δ?,剛接到這樣的需求還真是一頭霧水,不知從哪入手,今天小編通過實(shí)例代碼給大家介紹下vue實(shí)現(xiàn)新聞?wù)故卷摰牟襟E詳解,感興趣的朋友跟隨小編一起看看吧2019-04-04