vue+echarts圖表的基本使用步驟總結(jié)
前言
在實(shí)際開(kāi)發(fā)當(dāng)中,我們經(jīng)常需要用到一些圖表來(lái)實(shí)現(xiàn)數(shù)據(jù)可視化,這時(shí)候 echarts 可以幫助我們實(shí)現(xiàn)數(shù)據(jù)的展示。這里將介紹如何使用前端框架vue+echarts來(lái)實(shí)現(xiàn)數(shù)據(jù)可視化。
一、echarts是什么?
長(zhǎng)話短說(shuō),echarts就是一個(gè)幫助數(shù)據(jù)可視化的庫(kù)。
二、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)聽(tīng)瀏覽器縮放,自適應(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)聽(tīng) 父組件剛開(kāi)始沒(méi)有值,只有圖標(biāo)的配置項(xiàng)
// 父組件ajax請(qǐng)求后改變數(shù)據(jù)的值,傳遞過(guò)來(lái),圖標(biāo)已生成,監(jiān)聽(tīng)傳過(guò)來(lái)的值的改變
// deep:true.深度監(jiān)聽(tīng),確保data中子項(xiàng)修改也能監(jiān)聽(tīng)到。寫法參考: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)聽(tīng)
_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中使用jquery滑動(dòng)到頁(yè)面底部的實(shí)現(xiàn)方式
這篇文章主要介紹了vue中使用jquery滑動(dòng)到頁(yè)面底部的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
前端Vue通過(guò)Minio返回的URL下載文件實(shí)現(xiàn)方法
Minio是一個(gè)靈活、高性能、開(kāi)源的對(duì)象存儲(chǔ)解決方案,適用于各種存儲(chǔ)需求,并可以與云計(jì)算、容器化、大數(shù)據(jù)和應(yīng)用程序集成,這篇文章主要給大家介紹了關(guān)于前端Vue通過(guò)Minio返回的URL下載文件實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-07-07
解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題
這篇文章主要介紹了解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
vue中computed下使用箭頭函數(shù)會(huì)報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了vue中computed下使用箭頭函數(shù)會(huì)報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Vue axios 中提交表單數(shù)據(jù)(含上傳文件)
本篇文章主要介紹了Vue axios 中提交表單數(shù)據(jù)(含上傳文件),具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
使用Webpack提高Vue.js應(yīng)用的方式匯總(四種)
Webpack是開(kāi)發(fā)Vue.js單頁(yè)應(yīng)用程序的重要工具。下面通過(guò)四種方式給大家介紹使用Webpack提高Vue.js應(yīng)用,需要的的朋友參考下吧2017-07-07
vue + node如何通過(guò)一個(gè)Txt文件批量生成MP3并壓縮成Zip
這篇文章主要介紹了vue + node如何通過(guò)一個(gè)Txt文件批量生成MP3并壓縮成Zip的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

