vue中如何使用echarts動態(tài)渲染數(shù)據(jù)
一、業(yè)務(wù)場景:
最近在vue中使用echarts時 引入的時候怎么也引不上,后面發(fā)現(xiàn)需要綁定在原型上就可以完美解決(也可以直接在需要引入的頁面用ES5中的require引入require(‘echarts’))
為了避免大家走彎路,下面整合了一下echarts 在vue框架中的使用步驟
二、具體實現(xiàn)步驟:
1、先在終端安裝echarts
npm install echarts --save
2、在main.js中引入(這里分5.0以上和以下兩個版本來安裝)
5.0以上版本
import * as echarts from 'echarts'
5.0以下版本
import echarts from 'echarts'
注冊在原型上 `
vue.prototype.$echarts = echarts
3、在html部分留一個div容器來承載畫布
<div id="main" style="width: 500px;height:400px;"></div>
4、把要實現(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)
// 基于準備好的dom,初始化echarts實例
var myChart = this.$echarts.init(
document.getElementById('main')
)
// 配置option選項
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選項用js放進dom節(jié)點
myChart.setOption(option)
}
}).catch((err) => {
console.log(err)
})
},
5、頁面加載的時候調(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)
// 基于準備好的dom,初始化echarts實例
var myChart = this.$echarts.init(
document.getElementById('main')
)
// 配置option選項
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選項用js放進dom節(jié)點
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數(shù)組根據(jù)對象屬性去重和數(shù)組對象去重實現(xiàn)方式
文章介紹了兩種去重方法:一種是根據(jù)對象的`id`屬性去重,另一種是根據(jù)`id`和`name`屬性去重,通過`map()`和`filter()`方法結(jié)合`indexOf()`實現(xiàn)去重2025-12-12
Vue router的addRoute方法實現(xiàn)控制權(quán)限方法詳解
這篇文章主要介紹了vue動態(tài)路由添加,vue-router的addRoute方法實現(xiàn)權(quán)限控制流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
Vue配合Vant使用時area省市區(qū)選擇器的使用方式
這篇文章主要介紹了Vue配合Vant使用時area省市區(qū)選擇器的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Vue實現(xiàn)動態(tài)路由設(shè)置的兩大方法詳解
這篇文章主要為大家詳細介紹了Vue中兩種動態(tài)路由設(shè)置方案,主要是前端控制方案和后端控制方案,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2026-04-04

