Vue中的echarts圖表如何實現(xiàn)loading效果
echarts圖表實現(xiàn)loading效果
main.js 中配置Vue屬性ecahrts
// 引入echarts import echarts from 'echarts' Vue.prototype.$echarts = echarts?
data()初始化數(shù)據(jù)調用數(shù)據(jù)mounted()周期函數(shù)內獲取畫布節(jié)點,并且調用加載loading和圖表渲染computed計算屬性內定義echarts渲染內容以及數(shù)據(jù)請求
當服務器返回數(shù)據(jù) hideLoading()
注意:loading方法要定義在計算屬性的get方法中,set可以不做任何定義。這樣圖表于loading樣式在畫布上不會沖突
?? ?<template> ?? ? ? ?<div> ?? ? ? ? ? ?<div class="echarts-wrap"> ?? ? ? ? ? ? ? ?<div id="dev-echarts"></div> ?? ? ? ? ? ?</div> ?? ? ? ?</div> ?? ?</template>
?? ?<script>
?? ? ? ?export default {
?? ? ? ? ? ?name: "echarts",
?? ? ? ? ? ?data() {
?? ? ? ? ? ? ? ?return {
?? ? ? ? ? ? ? ? ? ?one: [],
?? ? ? ? ? ? ? ? ? ?two: [],
?? ? ? ? ? ? ? ? ? ?three: [],
?? ? ? ? ? ? ? ? ? ?four: []
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ?},
?? ? ? ? ? ?mounted() {
?? ? ? ? ? ? ? ?this.echartsG = this.$echarts.init(document.getElementById('dev-echarts'), 'dark');
?? ? ? ? ? ? ? ?this.loading
?? ? ? ? ? ? ? ?this.initDrawDevEchart
?? ? ? ? ? ?},
?? ? ? ? ? ?computed: {
?? ? ? ? ? ? ? ?initDrawDevEchart() {
?? ? ? ? ? ? ? ? ? ?this.$axios.get("getEchartsUrl", {
?? ? ? ? ? ? ? ? ? ? ? ?params: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ?id: 1
?? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ?}).then((response) => {
?? ? ? ? ? ? ? ? ? ? ? ?this.one= response.data.one
?? ? ? ? ? ? ? ? ? ? ? ?this.two= response.data.two
?? ? ? ? ? ? ? ? ? ? ? ?this.three= response.data.three
?? ? ? ? ? ? ? ? ? ? ? ?this.xAxis= response.data.xaxis
?? ?
?? ? ? ? ? ? ? ? ? ? ? ?this.echartsG.hideLoading()
?? ? ? ? ? ? ? ? ? ? ? ?let optionG = {
?? ? ? ? ? ? ? ? ? ? ? ? ? ?backgroundColor: 'rgba(128, 128, 128, 0)',
?? ? ? ? ? ? ? ? ? ? ? ? ? ?title: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?text: 'loading效果演示',
?? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ?dataZoom: {},
?? ? ? ? ? ? ? ? ? ? ? ? ? ?tooltip: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?trigger: 'axis'
?? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ?legend: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data: ['一', '二', '三']
?? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ?xAxis: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?type: 'category',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 調整柱狀圖位置
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?boundaryGap: true,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data: this.xAxis
?? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ?yAxis: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?type: 'value',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?axisLabel: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?formatter: '{value}'
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ?series: [
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?name: '一',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?type: 'bar',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data: this.one,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?name: '二',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?type: 'bar',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data: this.two
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?name: '三',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?type: 'bar',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data: this.three
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ? ? ? ? ?]
?? ? ? ? ? ? ? ? ? ? ? ?};
?? ? ? ? ? ? ? ? ? ? ? ?this.echartsG.setOption(optionG);
?? ? ? ? ? ? ? ? ? ?}).catch((err => {
?? ? ? ? ? ? ? ? ? ? ? ?console.log(err)
?? ? ? ? ? ? ? ? ? ?}))
?? ?
?? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ?loading: {
?? ? ? ? ? ? ? ? ? ?get: function () {
?? ? ? ? ? ? ? ? ? ? ? ?this.echartsG.setOption({
?? ? ? ? ? ? ? ? ? ? ? ? ? ?backgroundColor: 'rgba(128, 128, 128, 0)',
?? ? ? ? ? ? ? ? ? ? ? ? ? ?legend: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data: ['一', '二', '三']
?? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ?xAxis: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?type: 'category',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?boundaryGap: true,
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data: []
?? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ?yAxis: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?type: 'value',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?axisLabel: {
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?formatter: '{value}'
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ?series: [
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?name: '一',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?type: 'bar',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data: []
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?name: '二',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?type: 'bar',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data: []
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?},
?? ?
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?name: '三',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?type: 'bar',
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data: []
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ? ? ? ? ?]
?? ?
?? ? ? ? ? ? ? ? ? ? ? ?});
?? ? ? ? ? ? ? ? ? ? ? ?this.echartsG.showLoading({
?? ? ? ? ? ? ? ? ? ? ? ? ? ?text: '統(tǒng)計中,請稍候...'
?? ? ? ? ? ? ? ? ? ? ? ? ? ?, maskColor: 'rgba(3,3,8,0.5)',
?? ? ? ? ? ? ? ? ? ? ? ? ? ?textColor: '#fff600'
?? ? ? ? ? ? ? ? ? ? ? ?});
?? ? ? ? ? ? ? ? ? ?},
?? ? ? ? ? ? ? ? ? ?set(e) {
?? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ?}
?? ? ? ?}
</script>?? ?Vue使用echarts圖表總結
今天在寫后臺項目的時候,使用echarts來繪制圖表。下面來說說怎么使用echarts。
echarts地址:https://echarts.apache.org/zh/index.html
效果:

代碼:
在vue項目中使用echarts圖表的步驟:
安裝echarts依賴
npm install echarts -S
或者使用淘寶的鏡像
npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm install echarts -S
創(chuàng)建圖表
一、全局引入
在main.js中
// 引入echarts import echarts from 'echarts' Vue.prototype.$echarts = echarts
二、局部引入(在需要的頁面中引入)
import echarts from "echarts";
在頁面中的使用(在這里我用的局部引入)
完整的代碼:
<template>
<div>
<!-- 面包屑 -->
<BreadCrumb level1="數(shù)據(jù)統(tǒng)計" level2="數(shù)據(jù)報表"></BreadCrumb>
<!-- 內容 -->
<el-card style="margin-top:20px;">
<!-- 為Echarts準備一個Dom -->
<div id="main" style="width: 750px;height:400px;"></div>
</el-card>
</div>
</template>
<script>
import { getReports } from "../../http/api";
import echarts from "echarts";
import _ from "lodash";
export default {
data() {
return {
// 需要合并的數(shù)據(jù)
options: {
title: {
text: "用戶來源"
},
tooltip: {
trigger: "axis",
axisPointer: {
type: "cross",
label: {
backgroundColor: "#E9EEF3"
}
}
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true
},
xAxis: [
{
boundaryGap: false
}
],
yAxis: [
{
type: "value"
}
]
}
};
},
mounted() {
this.reports();
},
methods: {
async reports() {
var myChart = echarts.init(document.getElementById("main"));
const res = await getReports();
console.log(res);
const resultJieg = _.merge(res.result, this.options);
// 展示數(shù)據(jù)
myChart.setOption(resultJieg);
}
}
};
</script>
<style></style>解釋:
1、需要在頁面上給一個掛載點
<!-- 為Echarts準備一個Dom -->
<div id="main" style="width: 750px;height:400px;"></div>
2、在data里面定義一下
// 需要合并的數(shù)據(jù)
options: {
title: {
text: "用戶來源"
},
tooltip: {
trigger: "axis",
axisPointer: {
type: "cross",
label: {
backgroundColor: "#E9EEF3"
}
}
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true
},
xAxis: [
{
boundaryGap: false
}
],
yAxis: [
{
type: "value"
}
]
}
3、初始化數(shù)據(jù)
mounted() {
this.reports();
},
methods: {
async reports() {
//獲取在頁面中掛載的數(shù)據(jù)
var myChart = echarts.init(document.getElementById("main"));
//調用接口(即后臺返回的數(shù)據(jù))
const res = await getReports();
console.log(res);
//使用lodash來合并數(shù)據(jù)
const resultJieg = _.merge(res.result, this.options);
// 展示數(shù)據(jù)
myChart.setOption(resultJieg);
}
}
總結一下:
在vue中使用echarts圖表,分為二步:
1.在頁面中給圖表一個掛載的元素。
2.在mounted的函數(shù)里初始化數(shù)據(jù)。
- 通過echarts.init來拿到頁面中掛載的節(jié)點。
- 調用數(shù)據(jù)
- 最后通過setOption來展示數(shù)據(jù)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決axios post 后端無法接收數(shù)據(jù)的問題
今天小編就為大家分享一篇解決axios post 后端無法接收數(shù)據(jù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Vue+elementui 實現(xiàn)復雜表頭和動態(tài)增加列的二維表格功能
這篇文章主要介紹了Vue+elementui 實現(xiàn)復雜表頭和動態(tài)增加列的二維表格功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
npm?ERR!?code?E404在vscode安裝插件時報錯的兩種解決方案
這篇文章主要給大家介紹了關于npm?ERR!?code?E404在vscode安裝插件時報錯的兩種解決方案,關于這個問題,通常是由于插件名稱輸入錯誤、網(wǎng)絡問題或插件已被刪除引起的,文中將兩種解決方法都介紹的非常詳細,需要的朋友可以參考下2023-04-04
解決VUE3 keep-alive頁面切換報錯parentComponent.ctx.deactivate
這篇文章主要介紹了解決VUE3 keep-alive頁面切換報錯parentComponent.ctx.deactivate is not a function的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

