vue中如何使用echarts動態(tài)渲染數(shù)據(jù)
一、業(yè)務(wù)場景:
最近在vue中使用echarts時(shí) 引入的時(shí)候怎么也引不上,后面發(fā)現(xiàn)需要綁定在原型上就可以完美解決(也可以直接在需要引入的頁面用ES5中的require引入require(‘echarts’))
為了避免大家走彎路,下面整合了一下echarts 在vue框架中的使用步驟
二、具體實(shí)現(xiàn)步驟:
1、先在終端安裝echarts
npm install echarts --save
2、在main.js中引入(這里分5.0以上和以下兩個(gè)版本來安裝)
5.0以上版本
import * as echarts from 'echarts'
5.0以下版本
import echarts from 'echarts'
注冊在原型上 `
vue.prototype.$echarts = echarts
3、在html部分留一個(gè)div容器來承載畫布
<div id="main" style="width: 500px;height:400px;"></div>
4、把要實(shí)現(xiàn)的代碼放入函數(shù)中
init() {
//調(diào)接口
quShiPic({})
.then(res => {
console.log(res)
const { data, count, code, msg } = res
if (msg == 'success') {
this.quLineLists = data
console.log(this.quLineLists)
console.log(this.quLineLists[0].data)
console.log(this.quLineLists[1].data)
console.log(this.quLineLists[2].data)
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
var myChart = this.$echarts.init(
document.getElementById('main')
)
// 配置option選項(xiàng)
var option = {
title: {
text: '熱力變化曲線'
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['全部', '人', '物']
},
grid: {
left: '3%',
right: '2%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']
},
yAxis: {
type: 'value'
},
series:
[
{
name: '全部',
type: 'line',
stack: 'Total',
smooth: true,
// data: [120, 132,0, 101, 134, 90,0, 230, 210]
data: this.quLineLists[0].data
},
{
name: '人',
type: 'line',
smooth: true,
stack: 'Total',
// data: [220, 182, 191, 234, 290, 330, 310]
data: this.quLineLists[1].data
},
{
name: '物',
type: 'line',
stack: 'Total',
smooth: true,
// data: [150, 232, 201, 154, 190, 330, 410]
data: this.quLineLists[2].data
}
]
}
// 把配置option選項(xiàng)用js放進(jìn)dom節(jié)點(diǎn)
myChart.setOption(option)
}
}).catch((err) => {
console.log(err)
})
},
5、頁面加載的時(shí)候調(diào)用功能函數(shù)(mounted生命周期里)
mounted() {
this.init()
},
三、完整代碼:
<template>
<div>
<div id="main" style="width: 600px;height:400px;"></div>
</div>
</template>
<script>
export default {
name: 'WhiteName',
data() {
return {}
},
mounted() {
this.init()
},
methods: {
getLine() {
quShiPic({})
.then(res => {
console.log(res)
const { data, count, code, msg } = res
if (msg == 'success') {
this.quLineLists = data
console.log(this.quLineLists)
console.log(this.quLineLists[0].data)
console.log(this.quLineLists[1].data)
console.log(this.quLineLists[2].data)
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
var myChart = this.$echarts.init(
document.getElementById('main')
)
// 配置option選項(xiàng)
var option = {
title: {
text: '熱力變化曲線'
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['全部', '人', '物']
},
grid: {
left: '3%',
right: '2%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']
},
yAxis: {
type: 'value'
},
series:
[
{
name: '全部',
type: 'line',
stack: 'Total',
smooth: true,
// data: [120, 132,0, 101, 134, 90,0, 230, 210]
data: this.quLineLists[0].data
},
{
name: '人',
type: 'line',
smooth: true,
stack: 'Total',
// data: [220, 182, 191, 234, 290, 330, 310]
data: this.quLineLists[1].data
},
{
name: '物',
type: 'line',
stack: 'Total',
smooth: true,
// data: [150, 232, 201, 154, 190, 330, 410]
data: this.quLineLists[2].data
}
]
}
// 把配置option選項(xiàng)用js放進(jìn)dom節(jié)點(diǎn)
myChart.setOption(option)
}
}).catch((err) => {
console.log(err)
})
},
}
}
}
</script>
<style scoped>
</style>
四、效果展示:

總結(jié)
到此這篇關(guān)于vue中如何使用echarts動態(tài)渲染數(shù)據(jù)的文章就介紹到這了,更多相關(guān)vue echarts動態(tài)渲染數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue Element前端應(yīng)用開發(fā)之圖標(biāo)的維護(hù)和使用
在Vue Element前端應(yīng)用中,圖標(biāo)是必不可少點(diǎn)綴界面的元素,Element界面組件里面提供了很多常見的圖標(biāo),因此考慮擴(kuò)展更多圖標(biāo),引入了vue-awesome組件,它利用了Font Awesome的內(nèi)置圖標(biāo),實(shí)現(xiàn)了更多圖標(biāo)的整合,可以在項(xiàng)目中使用更多的圖標(biāo)元素了2021-05-05
vue獲取路由詳細(xì)內(nèi)容信息方法實(shí)例
獲取路由詳細(xì)內(nèi)容信息是我們?nèi)粘i_發(fā)中經(jīng)常會遇到的需求,下面這篇文章主要給大家介紹了關(guān)于vue獲取路由詳細(xì)內(nèi)容信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
vue3監(jiān)聽resize窗口事件(離開頁面要銷毀窗口事件)
這篇文章主要給大家介紹了關(guān)于vue3監(jiān)聽resize窗口事件(離開頁面要銷毀窗口事件)的相關(guān)資料,vue是單頁面應(yīng)用,路由切換后,定時(shí)器并不會自動關(guān)閉,需要手動清除,當(dāng)頁面被銷毀時(shí),清除定時(shí)器即可,需要的朋友可以參考下2023-11-11
vue實(shí)現(xiàn)錄音功能js-audio-recorder帶波浪圖效果的示例
這篇文章主要介紹了vue實(shí)現(xiàn)錄音功能js-audio-recorder帶波浪圖效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
element-ui 表格數(shù)據(jù)時(shí)間格式化的方法
這篇文章主要介紹了element-ui 表格數(shù)據(jù)時(shí)間格式化的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
vue項(xiàng)目中引入noVNC遠(yuǎn)程桌面的方法
下面小編就為大家分享一篇vue項(xiàng)目中引入noVNC遠(yuǎn)程桌面的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03

