欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Echarts实现一张图现切换不同的X轴(实例代码)

程序员文章站 2022-06-17 23:08:41
效果图如果大家想实现如下图的效果那么久继续往下看吧,直接上动图!方法因为项目需要展示的数据图表比较多我选择的是把每一张图表封装成一个vue组件来引用。先上一个完整的代码,引用时注意在从数据库获取数据是...

效果图

如果大家想实现如下图的效果那么久继续往下看吧,直接上动图!

Echarts实现一张图现切换不同的X轴(实例代码)

方法

因为项目需要展示的数据图表比较多我选择的是把每一张图表封装成一个vue组件来引用。
先上一个完整的代码,引用时注意在从数据库获取数据是要换成自己的数据库以及要自己定义好你需要的对象加到你设定好的数组中:

<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: {
//获取数据库中的数据
    fetchdata() {
    this.axios({
          method: 'get',
          url: 'http://localhost:8080/xxxx/xxxx' }).then(function(resp) {
          console.log("oxygen ===>",resp.data)
          let num = resp.data.length //获取数组的长度
          for (let i = 0; i <num; i++) {
            //创建一个对象
            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)
      })

      // 使用刚指定的配置项和数据显示图表。
      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) //初始化的数据显示
        window.onresize = this.mychart.resize //窗口大小图标自适应
      })
    }, 1000)
  }
}
</script>

结束,完工

到此这篇关于echarts 如何实现一张图现切换不同的x轴的文章就介绍到这了,更多相关echarts 实现一张图现切换不同的x轴内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!