vue+echarts圖表的基本使用步驟總結(jié)
前言
在實(shí)際開發(fā)當(dāng)中,我們經(jīng)常需要用到一些圖表來實(shí)現(xiàn)數(shù)據(jù)可視化,這時(shí)候 echarts 可以幫助我們實(shí)現(xiàn)數(shù)據(jù)的展示。這里將介紹如何使用前端框架vue+echarts來實(shí)現(xiàn)數(shù)據(jù)可視化。
一、echarts是什么?
長話短說,echarts就是一個(gè)幫助數(shù)據(jù)可視化的庫。
二、Vue+echarts使用步驟
1.安裝 echart
npm install echarts --save
2.在main.js 引入 echarts
import * as echarts from 'echarts' Vue.prototype.$echarts = echarts //將echarts作為全局變量加入Vue
3.一個(gè)vue組件顯示一個(gè)圖表
代碼:直接復(fù)制代碼創(chuàng)建一個(gè)vue組件,到App中引入組件即可
<template> <div id="myChart" :style="{width: '100%', height: '1000px'}" /> </template> <script> export default { mounted() { this.drawLine() }, methods: { drawLine() { // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例 const myChart = this.$echarts.init(document.getElementById('myChart')) myChart.setOption({ title: [ { text: '雷達(dá)圖', left:'center' } ], polar: { radius: [30, '70%'] }, radiusAxis: { max: 4 }, angleAxis: { type: 'category', data: ['a', 'b', 'c', 'd'], startAngle: 75 }, tooltip: {}, series: { type: 'bar', data: [2, 1.2, 2.4, 3.6], coordinateSystem: 'polar', label: { show: true, position: 'middle', formatter: ': {c}' } }, backgroundColor: '#fff', animation: false }) } } } </script>
引入
<template> <div id="app"> <Pie/> </div> </template> <script> import Pie from './components/Pie' export default { name: 'App', components: { Pie } }
效果
4. 一個(gè)組件顯示多個(gè)echarts圖表
以下是將兩個(gè)圖表放在一個(gè)組件中展示的方式 使用了 props 屬性 watch 深度監(jiān)視
props用于父組件向子組件傳遞數(shù)據(jù)
創(chuàng)建組件1 :柱狀圖
<template> <!-- 創(chuàng)建承載圖標(biāo)的容器 --> <div :id=id :data=data></div> </template> <script> export default { mounted() { this.drawLine(this.id,this.data) }, data(){ return { chartGrapgh:null } }, props:["id","data"], watch:{ data:{ handler(newVal,oldVal){ this.drawLine(this.id,newVal) }, deep:true } }, methods: { drawLine(id,data) { // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例 let _this = this let myChart = document.getElementById(id) this.chartGrapgh = this.$echarts.init(myChart) this.chartGrapgh.setOption(data) this.$echarts.init(document.getElementById(id)).setOption(data); window.addEventListener("resize",function () { _this.chartGrapgh.resize(); //監(jiān)聽瀏覽器縮放,自適應(yīng)瀏覽器大小 }) } }, beforeDestroy(){ if(this.chartGrapgh){ this.chartGrapgh.clear() //組件銷毀之前清空?qǐng)D表 } } } </script>
創(chuàng)建組件2:折線圖
<template> <!-- 創(chuàng)建承載圖標(biāo)的容器 --> <div :id=id :data=data></div> </template> <script> export default { data(){ return{ newPatagrapgh:null //讓每個(gè)組件都有一塊自己的區(qū)域顯示圖表,不再是一整塊 } }, props:["id","data"], // 深度監(jiān)聽 父組件剛開始沒有值,只有圖標(biāo)的配置項(xiàng) // 父組件ajax請(qǐng)求后改變數(shù)據(jù)的值,傳遞過來,圖標(biāo)已生成,監(jiān)聽傳過來的值的改變 // deep:true.深度監(jiān)聽,確保data中子項(xiàng)修改也能監(jiān)聽到。寫法參考:https://cn.vuejs.org/v2/api/#watch watch:{ data:{ handler(newVal,oldVal){ this.drawLine(this.id,newVal) }, deep:true } }, mounted() { this.drawLine(this.id,this.data) }, methods: { drawLine(id,data) { // 創(chuàng)建屬于組件本身的圖形區(qū)域,不是全局 $echarts let _this = this let mychart = document.getElementById(id) this.newPatagrapgh = this.$echarts.init(mychart) this.newPatagrapgh.setOption(data) window.addEventListener("resize",function () { //可選,瀏覽器縮放監(jiān)聽 _this.newPatagrapgh.resize(); }) } }, } </script>
父組件App.vue
<template> <div id="app"> <!-- <Zhe :id="'myChart'" :data="optionZhe" style="width:75%;height:350px;"/> --> <hr/> <!-- <Zhu :id="'myChart2'" :data="optionZhu" style="width:75%;height:350px;"/> --> <Pie/> </div> </template> <script> import Zhe from './components/Zhe' import Zhu from './components/Zhu' export default { name: 'App', components: { Zhe, Zhu }, data(){ //在App中傳入子組件要渲染的數(shù)據(jù): return { optionZhe:{ //折線圖的配置 title:{ text:"折線圖", left:'center' }, xAxis:{ type: 'category', data: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'] }, yAxis:{ type: 'value' }, series: [ { data:[5, 20, 36, 10, 10, 20], type:'line' } ] }, optionZhu:{ //柱狀圖的配置 title: { text: '柱狀圖', subtext: '2', }, tooltip: { trigger: 'axis' }, legend: { data: ['Rainfall', 'Evaporation'] }, toolbox: { show: true, feature: { dataView: { show: true, readOnly: false }, magicType: { show: true, type: ['line', 'bar'] }, restore: { show: true }, saveAsImage: { show: true } } }, calculable: true, xAxis: [ { type: 'category', // prettier-ignore data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] } ], yAxis: [ { type: 'value' } ], series: [ { name: 'Rainfall', type: 'bar', 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 ], markPoint: { data: [ { type: 'max', name: 'Max' }, { type: 'min', name: 'Min' } ] }, markLine: { data: [{ type: 'average', name: 'Avg' }] } }, { name: 'Evaporation', type: 'bar', 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 ], markPoint: { data: [ { name: 'Max', value: 182.2, xAxis: 7, yAxis: 183 }, { name: 'Min', value: 2.3, xAxis: 11, yAxis: 3 } ] }, markLine: { data: [{ type: 'average', name: 'Avg' }] } } ] } } } } </script>
效果:
三、總結(jié)
以上就是vue+echarts的基本使用:包含了在echarts在vue中的基本使用、單個(gè)組件顯示單個(gè)圖表以及單個(gè)組件顯示多個(gè)圖表。
到此這篇關(guān)于vue+echarts圖表的基本使用步驟的文章就介紹到這了,更多相關(guān)vue+echarts圖表使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue截圖轉(zhuǎn)base64轉(zhuǎn)文件File異步獲取方式
這篇文章主要介紹了vue截圖轉(zhuǎn)base64轉(zhuǎn)文件File異步獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Vue中el-tree樹全部展開或收起的實(shí)現(xiàn)示例
本文主要介紹了Vue中el-tree樹全部展開或收起的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07vue中實(shí)現(xiàn)頁面刷新以及局部刷新的方法
這篇文章主要給大家介紹了關(guān)于vue中實(shí)現(xiàn)頁面刷新以及局部刷新的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01詳解vue父子組件關(guān)于模態(tài)框狀態(tài)的綁定方案
這篇文章主要介紹了詳解vue父子組件關(guān)于模態(tài)框狀態(tài)的綁定方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06vue遞歸組件實(shí)現(xiàn)樹形結(jié)構(gòu)
這篇文章主要為大家詳細(xì)介紹了vue遞歸組件實(shí)現(xiàn)樹形結(jié)構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Vue2中如何使用全局事件總線實(shí)現(xiàn)任意組件間通信
全局事件總線就是一種組件間通信的方式,適用于任意組件間通信,下面這篇文章主要給大家介紹了關(guān)于Vue2中如何使用全局事件總線實(shí)現(xiàn)任意組件間通信的相關(guān)資料,需要的朋友可以參考下2022-12-12Vue3中數(shù)據(jù)響應(yīng)式原理與高效數(shù)據(jù)操作全解析
這篇文章主要為大家詳細(xì)介紹了Vue3中數(shù)據(jù)響應(yīng)式原理與高效數(shù)據(jù)操作的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02淺析vue 函數(shù)配置項(xiàng)watch及函數(shù) $watch 源碼分享
這篇文章主要介紹了vue 函數(shù)配置項(xiàng)watch及函數(shù) $watch 源碼分享 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11vue2項(xiàng)目中封裝echarts地圖的優(yōu)雅方法
這篇文章主要給大家介紹了關(guān)于vue2項(xiàng)目中封裝echarts地圖的優(yōu)雅方法,需要的朋友可以參考下2022-03-03