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 Element前端應(yīng)用開發(fā)之圖標的維護和使用
在Vue Element前端應(yīng)用中,圖標是必不可少點綴界面的元素,Element界面組件里面提供了很多常見的圖標,因此考慮擴展更多圖標,引入了vue-awesome組件,它利用了Font Awesome的內(nèi)置圖標,實現(xiàn)了更多圖標的整合,可以在項目中使用更多的圖標元素了2021-05-05vue3監(jiān)聽resize窗口事件(離開頁面要銷毀窗口事件)
這篇文章主要給大家介紹了關(guān)于vue3監(jiān)聽resize窗口事件(離開頁面要銷毀窗口事件)的相關(guān)資料,vue是單頁面應(yīng)用,路由切換后,定時器并不會自動關(guān)閉,需要手動清除,當頁面被銷毀時,清除定時器即可,需要的朋友可以參考下2023-11-11vue實現(xiàn)錄音功能js-audio-recorder帶波浪圖效果的示例
這篇文章主要介紹了vue實現(xiàn)錄音功能js-audio-recorder帶波浪圖效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08element-ui 表格數(shù)據(jù)時間格式化的方法
這篇文章主要介紹了element-ui 表格數(shù)據(jù)時間格式化的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08