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

方法
因?yàn)轫?xiàng)目需要展示的數(shù)據(jù)圖表比較多我選擇的是把每一張圖表封裝成一個(gè)vue組件來(lái)引用。
先上一個(gè)完整的代碼,引用時(shí)注意在從數(shù)據(jù)庫(kù)獲取數(shù)據(jù)是要換成自己的數(shù)據(jù)庫(kù)以及要自己定義好你需要的對(duì)象加到你設(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ù)庫(kù)中的數(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ù)組的長(zhǎng)度
for (let i = 0; i <num; i++) {
//創(chuàng)建一個(gè)對(duì)象
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: {} //可對(duì)折線圖進(jìn)行截圖保存
}
},
grid: {
left: 10, //組件距離容器左邊的距離
right: 10,
top: 30,
bottom: 20,
containLabel: true
},
dataZoom: [ //通過(guò)鼠標(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: "血氧趨勢(shì)圖",
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軸,那么他會(huì)進(jìn)行對(duì)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) //過(guò)濾掉undefined、NaN、null、空串
this.weekX = (this.res.map(item => item.weekX)).filter(Boolean) //過(guò)濾掉undefined、NaN、null、空串
this.yearX = (this.res.map(item => item.yearX)).filter(Boolean) //過(guò)濾掉undefined、NaN、null、空串
this.timeX = (this.ans.map(item => item.timeX)).filter(Boolean) //過(guò)濾掉undefined、NaN、null、空串
//對(duì)dataY進(jìn)行賦值,如果這里想一個(gè)X軸對(duì)應(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中的基礎(chǔ)過(guò)渡動(dòng)畫(huà)及實(shí)現(xiàn)原理解析
這篇文章主要介紹了Vue中的基礎(chǔ)過(guò)渡動(dòng)畫(huà)原理解析,需要的朋友可以參考下2018-12-12
運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決
剛剛創(chuàng)建好vue項(xiàng)目的時(shí)候,運(yùn)行 npm run dev 會(huì)報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決方法,需要的朋友可以參考下2022-10-10
VUE2 前端實(shí)現(xiàn) 靜態(tài)二級(jí)省市聯(lián)動(dòng)選擇select的示例
下面小編就為大家分享一篇VUE2 前端實(shí)現(xiàn) 靜態(tài)二級(jí)省市聯(lián)動(dòng)選擇select的示例。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
vue 頁(yè)面跳轉(zhuǎn)的實(shí)現(xiàn)方式
這篇文章主要介紹了vue 頁(yè)面跳轉(zhuǎn)的實(shí)現(xiàn)方式,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2021-01-01
vue 動(dòng)態(tài)添加的路由頁(yè)面刷新時(shí)失效的原因及解決方案
這篇文章主要介紹了vue動(dòng)態(tài)添加的路由頁(yè)面刷新時(shí)失效的原因及解決方案,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下2021-02-02
vue-router history模式服務(wù)器端配置過(guò)程記錄
vue路由有hash和history兩種模式,這篇文章主要給大家介紹了關(guān)于vue-router history模式服務(wù)器端配置的相關(guān)資料,需要的朋友可以參考下2021-06-06
Vue應(yīng)用qs插件實(shí)現(xiàn)參數(shù)格式化示例詳解
這篇文章主要為大家介紹了Vue應(yīng)用qs插件實(shí)現(xiàn)參數(shù)格式化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

