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

方法
因為項目需要展示的數(shù)據(jù)圖表比較多我選擇的是把每一張圖表封裝成一個vue組件來引用。
先上一個完整的代碼,引用時注意在從數(shù)據(jù)庫獲取數(shù)據(jù)是要換成自己的數(shù)據(jù)庫以及要自己定義好你需要的對象加到你設定好的數(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: [], // 當天的 X軸
weekX: [], // 當周的 X軸
monthX: [], // 當月的 X軸
yearX: [], // 當年的 X軸
timeX:[],//任意時間段的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)建一個對象
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: ['當天', '當月', '當年'],
data: ['當周', '當月','當年','所選時間段'],
selectedMode: 'single', // 單選
selected: {
當周: true,
當月: false,
當年: false,
所選時間段: false
}
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross'
},
//自定義顯示標簽
formatter:function(params) {
return params[0].name + '<br>血氧 : '+params[0].data+' %'
}
},
// 工具欄
toolbox: {
feature: {
saveAsImage: {} //可對折線圖進行截圖保存
}
},
grid: {
left: 10, //組件距離容器左邊的距離
right: 10,
top: 30,
bottom: 20,
containLabel: true
},
dataZoom: [ //通過鼠標控制折線圖的放大縮小
{
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軸,那么他會進行對Y值的切換
if (obj.name == '當周'){
options.xAxis[0].data = this.weekX
}else if (obj.name == '當月'){
options.xAxis[0].data = this.monthX
}else if (obj.name == '當年'){
options.xAxis[0].data = this.yearX
}else if (obj.name == '所選時間段'){
options.xAxis[0].data = this.timeX
}
this.myChart.setOption(options, true)
})
// 使用剛指定的配置項和數(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進行賦值,如果這里想一個X軸對應多個Y值那么可以在加一個{}
this.dataY.push({
name: '當月',
type: 'line', // 直線ss
itemStyle: {
normal: {
color: '#2E2E2E',
lineStyle: {
color: '#2E2E2E',
width: 2
}
}
},
data: this.res.map(item => item.monthY)
})
this.dataY.push({ //這邊就可以自定義一個折線顯示的方式和顏色
name: '當周',
type: 'line',
itemStyle: {
normal: {
color: '#FF0000',
lineStyle: {
color: '#FF0000',
width: 2
}
}
},
data: this.res.map(item => item.weekY)
})
this.dataY.push({ //這邊就可以自定義一個折線顯示的方式和顏色
name: '當年', //這個必須和lengen 那邊的保持一致才行
type: 'line',
itemStyle: {
normal: {
color: '#0404B4',
lineStyle: {
color: '#0404B4',
width: 2
}
}
},
data: this.res.map(item => item.yearY)
})
this.dataY.push({ //這邊就可以自定義一個折線顯示的方式和顏色
name: '所選時間段',
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 //窗口大小圖標自適應
})
}, 1000)
}
}
</script>
結(jié)束,完工
到此這篇關于Echarts 如何實現(xiàn)一張圖現(xiàn)切換不同的X軸的文章就介紹到這了,更多相關Echarts 實現(xiàn)一張圖現(xiàn)切換不同的X軸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決Vue.js父組件$on無法監(jiān)聽子組件$emit觸發(fā)事件的問題
今天小編就為大家分享一篇解決Vue.js父組件$on無法監(jiān)聽子組件$emit觸發(fā)事件的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue+Echarts實現(xiàn)繪制動態(tài)折線圖
這篇文章主要為大家詳細介紹了如何利用Vue和Echarts實現(xiàn)繪制動態(tài)折線圖,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-03-03
vue實現(xiàn)后臺管理權限系統(tǒng)及頂欄三級菜單顯示功能
這篇文章主要介紹了vue實現(xiàn)后臺管理權限系統(tǒng)及頂欄三級菜單顯示功能,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06

