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.js父組件$on無(wú)法監(jiān)聽(tīng)子組件$emit觸發(fā)事件的問(wèn)題
今天小編就為大家分享一篇解決Vue.js父組件$on無(wú)法監(jiān)聽(tīng)子組件$emit觸發(fā)事件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09詳解Vue-cli3.X使用px2rem遇到的問(wèn)題
這篇文章主要介紹了詳解Vue-cli3.X使用px2rem遇到的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Vue+Echarts實(shí)現(xiàn)繪制動(dòng)態(tài)折線圖
這篇文章主要為大家詳細(xì)介紹了如何利用Vue和Echarts實(shí)現(xiàn)繪制動(dòng)態(tài)折線圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03vue控制滾動(dòng)條滑到某個(gè)位置的方法實(shí)例
當(dāng)容器有滾動(dòng)條時(shí),有時(shí)需要將滾動(dòng)條滑到某個(gè)位置,下面這篇文章主要給大家介紹了關(guān)于vue控制滾動(dòng)條滑到某個(gè)位置的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12vue實(shí)現(xiàn)后臺(tái)管理權(quán)限系統(tǒng)及頂欄三級(jí)菜單顯示功能
這篇文章主要介紹了vue實(shí)現(xiàn)后臺(tái)管理權(quán)限系統(tǒng)及頂欄三級(jí)菜單顯示功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06基于Vue3實(shí)現(xiàn)旋轉(zhuǎn)木馬動(dòng)畫效果
這篇文章主要為大家介紹了如何利用Vue3實(shí)現(xiàn)旋轉(zhuǎn)木馬的動(dòng)畫效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,需要的可以參考一下2022-05-05