Echarts實(shí)現(xiàn)一張圖現(xiàn)切換不同的X軸(實(shí)例代碼)
效果圖
如果大家想實(shí)現(xiàn)如下圖的效果那么久繼續(xù)往下看吧,直接上動圖!

方法
因?yàn)轫?xiàng)目需要展示的數(shù)據(jù)圖表比較多我選擇的是把每一張圖表封裝成一個(gè)vue組件來引用。
先上一個(gè)完整的代碼,引用時(shí)注意在從數(shù)據(jù)庫獲取數(shù)據(jù)是要換成自己的數(shù)據(jù)庫以及要自己定義好你需要的對象加到你設(shè)定好的數(shù)組中:
<template>
<div>
<div id="main" style="height:350px;width:100%"></div>
</div>
</template>
<script>
import echarts from 'echarts'
export default {
data() {
return {
ans:[],
// dayX: [], // 當(dāng)天的 X軸
weekX: [], // 當(dāng)周的 X軸
monthX: [], // 當(dāng)月的 X軸
yearX: [], // 當(dāng)年的 X軸
timeX:[],//任意時(shí)間段的X軸
dataY: [] // Y 軸
}
},
created() {
this.fetchData()
},
methods: {
//獲取數(shù)據(jù)庫中的數(shù)據(jù)
fetchData() {
this.axios({
method: 'GET',
url: 'http://localhost:8080/xxxx/xxxx' }).then(function(resp) {
console.log("oxygen ===>",resp.data)
let num = resp.data.length //獲取數(shù)組的長度
for (let i = 0; i <num; i++) {
//創(chuàng)建一個(gè)對象
let arr = {}
arr.timeX = resp.data[i].chkDate.slice(5, 10)
arr.timeY = resp.data[i].oxgnSaturation
vm.ans.push(arr)
}
})
},
init(dataX, dataY) {
this.myChart = echarts.init(document.getElementById('main'))
let option = {
legend: {
icon: 'stack',
// data: ['當(dāng)天', '當(dāng)月', '當(dāng)年'],
data: ['當(dāng)周', '當(dāng)月','當(dāng)年','所選時(shí)間段'],
selectedMode: 'single', // 單選
selected: {
當(dāng)周: true,
當(dāng)月: false,
當(dāng)年: false,
所選時(shí)間段: false
}
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross'
},
//自定義顯示標(biāo)簽
formatter:function(params) {
return params[0].name + '<br>血氧 : '+params[0].data+' %'
}
},
// 工具欄
toolbox: {
feature: {
saveAsImage: {} //可對折線圖進(jìn)行截圖保存
}
},
grid: {
left: 10, //組件距離容器左邊的距離
right: 10,
top: 30,
bottom: 20,
containLabel: true
},
dataZoom: [ //通過鼠標(biāo)控制折線圖的放大縮小
{
show: true,
type: 'inside',
filterMode: 'none',
xAxisIndex: [0]
},
{
show: true,
type: 'inside',
filterMode: 'none',
yAxisIndex: [0]
}
],
xAxis: {
type: 'category',
miniInterval: 3,
boundaryGap: false,
axisTick: {
show: false
},
splitLine: {
// X 軸分隔線樣式
show: true,
lineStyle: {
color: ['#f3f0f0'],
width: 1,
type: 'solid'
}
},
data: dataX
},
yAxis: [
{
name: "血氧趨勢圖",
type: 'value',
splitLine: {
// Y 軸分隔線樣式
show: true,
lineStyle: {
color: ['#f3f0f0'],
width: 1,
type: 'solid'
}
}
}
],
series: dataY
}
this.myChart.on('legendselectchanged', obj => {
var options = this.myChart.getOption()
//這里是選擇切換什么樣的x軸,那么他會進(jìn)行對Y值的切換
if (obj.name == '當(dāng)周'){
options.xAxis[0].data = this.weekX
}else if (obj.name == '當(dāng)月'){
options.xAxis[0].data = this.monthX
}else if (obj.name == '當(dāng)年'){
options.xAxis[0].data = this.yearX
}else if (obj.name == '所選時(shí)間段'){
options.xAxis[0].data = this.timeX
}
this.myChart.setOption(options, true)
})
// 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。
this.myChart.setOption(option)
},
mounted() {
setTimeout(() => {
this.$nextTick(() => {
this.monthX = (this.res.map(item => item.monthX)).filter(Boolean) //過濾掉undefined、NaN、null、空串
this.weekX = (this.res.map(item => item.weekX)).filter(Boolean) //過濾掉undefined、NaN、null、空串
this.yearX = (this.res.map(item => item.yearX)).filter(Boolean) //過濾掉undefined、NaN、null、空串
this.timeX = (this.ans.map(item => item.timeX)).filter(Boolean) //過濾掉undefined、NaN、null、空串
//對dataY進(jìn)行賦值,如果這里想一個(gè)X軸對應(yīng)多個(gè)Y值那么可以在加一個(gè){}
this.dataY.push({
name: '當(dāng)月',
type: 'line', // 直線ss
itemStyle: {
normal: {
color: '#2E2E2E',
lineStyle: {
color: '#2E2E2E',
width: 2
}
}
},
data: this.res.map(item => item.monthY)
})
this.dataY.push({ //這邊就可以自定義一個(gè)折線顯示的方式和顏色
name: '當(dāng)周',
type: 'line',
itemStyle: {
normal: {
color: '#FF0000',
lineStyle: {
color: '#FF0000',
width: 2
}
}
},
data: this.res.map(item => item.weekY)
})
this.dataY.push({ //這邊就可以自定義一個(gè)折線顯示的方式和顏色
name: '當(dāng)年', //這個(gè)必須和lengen 那邊的保持一致才行
type: 'line',
itemStyle: {
normal: {
color: '#0404B4',
lineStyle: {
color: '#0404B4',
width: 2
}
}
},
data: this.res.map(item => item.yearY)
})
this.dataY.push({ //這邊就可以自定義一個(gè)折線顯示的方式和顏色
name: '所選時(shí)間段',
type: 'line',
itemStyle: {
normal: {
color: '#DF01D7',
lineStyle: {
color: '#DF01D7',
width: 2
}
}
},
data: this.ans.map(item => item.timeY)
})
this.init(this.weekX, this.dataY) //初始化的數(shù)據(jù)顯示
window.onresize = this.myChart.resize //窗口大小圖標(biāo)自適應(yīng)
})
}, 1000)
}
}
</script>
結(jié)束,完工
到此這篇關(guān)于Echarts 如何實(shí)現(xiàn)一張圖現(xiàn)切換不同的X軸的文章就介紹到這了,更多相關(guān)Echarts 實(shí)現(xiàn)一張圖現(xiàn)切換不同的X軸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Vue.js父組件$on無法監(jiān)聽子組件$emit觸發(fā)事件的問題
今天小編就為大家分享一篇解決Vue.js父組件$on無法監(jiān)聽子組件$emit觸發(fā)事件的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue+Echarts實(shí)現(xiàn)繪制動態(tài)折線圖
這篇文章主要為大家詳細(xì)介紹了如何利用Vue和Echarts實(shí)現(xiàn)繪制動態(tài)折線圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
vue實(shí)現(xiàn)后臺管理權(quán)限系統(tǒng)及頂欄三級菜單顯示功能
這篇文章主要介紹了vue實(shí)現(xiàn)后臺管理權(quán)限系統(tǒng)及頂欄三級菜單顯示功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
基于Vue3實(shí)現(xiàn)旋轉(zhuǎn)木馬動畫效果
這篇文章主要為大家介紹了如何利用Vue3實(shí)現(xiàn)旋轉(zhuǎn)木馬的動畫效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Vue有一定的幫助,需要的可以參考一下2022-05-05

