Echarts图表和timeline时间轴的使用

option单独放在一个js文件中

export const barOptionMobiSub = {
  baseOption: {
    timeline: {
      axisType: 'category',
      realtime: true,
      autoPlay: false,
      symbolSize: 8,
      orient: 'vertical',
      inverse: true,
      // z: 2,
      top: 30,
      right: 'auto',
      bottom: 10,
      left: '80%',
      data: [],
      label: {
        interval: 0,
        color: '#1781bf'
      },
      // symbol: 'roundRect',
      symbol: 'pin',
      lineStyle: {
        show: true,
        color: '#1781bf',
        width: 1,
        type: 'dotted'
      },
      controlStyle: {
        showPlayBtn: false,
        showPrevBtn: false,
        showNextBtn: false
      },
      itemStyle: {
        color: '#1781bf'
      },
      checkpointStyle: {
        symbol: 'none'
      },
      tooltip: {
        formatter: []
      },
      grid: {containLabel: true},
      series: [{type: 'bar'}]
    }
  },
  options: [{
    title: {
      text: '',
      left: '30%'
    },
    legend: {
      data: ['累积流量', '累积耗电'],
      orient: 'vertical',
      left: 0
    },
    toolbox: {
      feature: {
        dataView: {}
      }
    },
    tooltip: {},
    xAxis: {
      position: 'top',
      axisLabel: {
        rotate: 45
      }
    },
    yAxis: {
      data: [],
      splitLine: {
        show: false
      },
      axisLabel: {
        interval: 0,
        rotate: -45
      },
      offset: 0,
      gridIndex: 0,
      triggerEvent: true
    },
    grid: {
      left: '20%',
      right: '20%'
      // bottom: 80
    },
    series: [
      {
        name: '累积流量',
        type: 'bar',
        data: [],
        animationDelay: function (idx) {
          return idx * 10
        }
      },
      {
        name: '累积耗电',
        type: 'bar',
        data: [],
        animationDelay: function (idx) {
          return idx * 10 + 100
        }
      }
    ],
    animationEasing: 'elasticOut',
    animationDelayUpdate: function (idx) {
      return idx * 5
    }
  }]
}

在vue文件中导入

import { barOptionMobiSub } from '@/echartOption'
<template>
  <div>
    <div id="myChart" ref="myChart" :class="{hidden: isDisplay}"></div>
    <el-scrollbar class="list">
      <div id="selectArea"
        :class="{hidden: !isDisplay, active: activeName == item.name}"
        v-for="item of districts"
        :key="item.id"
        @click="selectArea(item)"
      >
        <div class="head-title" v-text="item.name" :title="item.name"></div>
      </div>
    </el-scrollbar>
    <div id="myChart2" ref="myChart2" :class="{hidden: !isDisplay}"></div>
    <div
      id="back"
      :class="{hidden: !isDisplay}"
      @click="isDisplay = !isDisplay"
      style="cursor:pointer"
    >返回上一级</div>
  </div>
</template>

<script>
import echarts from 'echarts'
import { mapGetters } from 'vuex'
import { getGroupedReport } from '@/api/report'
// import { getProjectDistricts } from '@/api/project'
import { barOption, barOptionMobi, barOptionSub, barOptionMobiSub } from '@/echartOption'
import { isMobileDevice } from '@/utils/common'
export default {
  data() {
    return {
      listQuery: {
        keyword: undefined,
        district: undefined
      },
      myChart: null,
      myChart2: null,
      isDisplay: false,
      transId: '',
      districtVariables: {},
      activeName: ''
    }
  },
  computed: {
    ...mapGetters(['districts']),
    // 子图表渲染时获取时间轴上乡镇id
    districtIds() {
      return this.districts.map(item => item.id)
    }
  },
  async mounted() {
    // 按各乡镇总体情况统计
    const resp = await getGroupedReport({ groupby: 'district_variables' })
    this.districtVariables = resp.results
    this.draw()
  },

  methods: {
    draw() {
      // 实例化echarts对象
      var _this = this
      _this.myChart = echarts.init(this.$refs.myChart)
      _this.myChart2 = echarts.init(this.$refs.myChart2)

      var myChart = _this.myChart
      // var myChart2 = _this.myChart2
      if (isMobileDevice()) {
        myChart.setOption(barOptionMobi)
      } else {
        myChart.setOption(barOption)
      }
      // 按乡镇统计
      if (isMobileDevice()) {
        myChart.setOption({
          yAxis: {
            data: _this.districtVariables.district
          },
          series: [
            { data: _this.districtVariables.variable1 },
            { data: [2300, 2490] }
          ]
        })
        // 父图表下y轴上的点击事件
        myChart.on('click', function(params) {
          if (params.componentType === 'yAxis') {
            for (var i in _this.districts) {
              if (_this.districts[i].name === params.value) {
                _this.isDisplay = !_this.isDisplay
                _this.transId = _this.districts[i].id
                // 根据乡镇id展示该乡镇下所有项目的图表,并同时渲染时间轴
                _this.handleChartEvent(_this.transId, _this.districts[i].name, 'isMobi')
              }
            }
          }
        })
        // 手机上时间轴上乡镇列表的点击事件
        // myChart2.on('timelinechanged', function(timeLineIndex) {
        //   // 获取乡镇id
        //   const transId = _this.districtIds[timeLineIndex.currentIndex]
        //   // 点击时间轴上乡镇列表切换项目展示
        //   this.handleChartEvent(transId, _this.districts[transId - 1].name, 'isMobi')
        // })
      } else {
        myChart.setOption({
          xAxis: {
            data: _this.districtVariables.district
          },
          series: [
            { data: _this.districtVariables.variable1 },
            { data: [2300, 2490] }
          ]
        })
        // 父图表下x轴上的点击事件
        myChart.on('click', function(params) {
          if (params.componentType === 'xAxis') {
            for (var i in _this.districts) {
              if (_this.districts[i].name === params.value) {
                _this.isDisplay = !_this.isDisplay
                _this.transId = _this.districts[i].id
                // 根据乡镇id展示该乡镇下所有项目的图表,并同时渲染时间轴
                _this.handleChartEvent(_this.transId, _this.districts[i].name)
              }
            }
          }
        })

        // 时间轴上乡镇列表的点击事件
        // myChart2.on('timelinechanged', function(timeLineIndex) {
        //   // 获取乡镇id
        //   const transId = _this.districtIds[timeLineIndex.currentIndex]
        //   // 点击时间轴上乡镇列表切换项目展示
        //   this.handleChartEvent(transId, _this.districts[transId - 1].name)
        // }
      }
    },

    selectArea(param) {
      var _this = this
      if (isMobileDevice()) {
        this.handleChartEvent(param.id, this.districts[param.id - 1].name, 'isMobi')
      } else {
        this.handleChartEvent(param.id, this.districts[param.id - 1].name)
        _this.activeName = param.name
      }
    },
    handleChartEvent(transId, transName, isMobi) {
      var _this = this
      var myChart2 = _this.myChart2
      getGroupedReport({district_id: transId, groupby: 'project_variables'}).then(res => {
        // barOptionSub.baseOption.timeline.currentIndex = transId - 1
        if (isMobi) {
          // barOptionMobiSub.baseOption.timeline.data = _this.districts.map(item => item.name)
          // barOptionMobiSub.baseOption.timeline.tooltip.formatter = _this.districts.map(item => item.name)
          myChart2.setOption(barOptionMobiSub, true)
          myChart2.resize()
          myChart2.setOption({
            options: [
              {
                title: {
                  text: transName + '下属站点统计'
                },
                yAxis: {
                  data: res.results.project
                },
                series: [{data: res.results.variable1}]
              }
            ]
          })
        } else {
          // barOptionSub.baseOption.timeline.data = _this.districts.map(item => item.name)
          // barOptionSub.baseOption.timeline.tooltip.formatter = _this.districts.map(item => item.name)
          myChart2.setOption(barOptionSub, true)
          myChart2.resize()
          myChart2.setOption({
            // options: [
            // {
            title: {
              text: transName + '下属站点统计'
            },
            xAxis: {
              data: res.results.project
            },
            series: [{data: res.results.variable1}]
            // }
            // ]
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
#myChart {
  position: relative;
  width: 98%;
  height: calc(100vh - 106px);
  //margin: 5px auto;
  margin: 10px;
  border: 1px solid #ccc;
}
.list {
  position: relative;
  float: left;
  width: 70px;
  left: 5px;
  height: calc(100vh - 136px);
  border: 1px solid #ccc;
  overflow: hidden;
  .el-scrollbar__wrap {
    overflow-x: hidden !important;
  }
  #selectArea {
    float: left;
    cursor: pointer;
    .head-title {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      font-size: 12px;
      color: #1781bf;
      font-weight: 400;
      padding-left: 10px;
      height: 30px;
      line-height: 30px;
    }
  }
  /*#selectArea.active {*/
  /*  background-color: #cfddf3;*/
  /*  width: 78px;*/
  /*}*/
}

#myChart2 {
  position: relative;
  margin-top: 10px;
  width: calc(100vw - 100px);
  left: 80px;
  right: 5px;
  // height: 450px;
  height: calc(100vh - 136px);
  // margin: 5px auto;
  border: 1px solid #ccc;
}
#back {
  margin-top: 10px;
  margin-left: 10px;
  font-size: 15px;
  color: #606266;
  height: 30px;
  width: fit-content;
}
.hidden {
  display: none !important;
}
.active {
  background-color: #cfddf3;
  width: 78px;
}
</style>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计的实现,以及它们在MATLAB环境的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
你可以使用 ECharts 的 `timeline` 组件来实现轴的时间线效果。下面是一个简单的示例代码,演示了如何使用 `timeline` 组件: ```javascript // 创建一个图表实例 var myChart = echarts.init(document.getElementById('chart')); // 定义图表的配置项和数据 var option = { baseOption: { timeline: { data: ['2010', '2011', '2012', '2013', '2014'], // 设置时间线的数据 axisType: 'category', autoPlay: true, // 自动播放 playInterval: 1000, // 播放间隔时间,单位为毫秒 loop: true, // 循环播放 bottom: 20 // 时间线的位置 }, xAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'], // 设置 x 轴数据 }, yAxis: { type: 'value' }, series: { type: 'bar', data: [10, 20, 30, 40, 50] // 设置 y 轴数据 } }, options: [ { // 时间线第一个年份的配置 series: { data: [10, 20, 30, 40, 50] } }, { // 时间线第二个年份的配置 series: { data: [20, 30, 40, 50, 60] } }, // ... { // 时间线最后一个年份的配置 series: { data: [50, 60, 70, 80, 90] } } ] }; // 使用配置项和数据显示图表 myChart.setOption(option); ``` 上述代码,首先创建了一个 ECharts 实例并指定了要渲染的 DOM 元素。然后定义了图表的配置项和数据,包括`timeline`组件、`xAxis`和 `yAxis`,以及 `series` 的数据。 在 `timeline` 的配置,你可以通过 `data` 属性设置时间线的数据。然后通过 `axisType`、`autoPlay`、`playInterval` 和 `loop` 等属性来设置播放效果。在示例,时间线从 2010 年播放到 2014 年。 在 `options` 数组,可以通过多个元素来设置不同年份的图表配置。每个元素的 `series` 属性可以设置对应年份的数据。示例,通过多个元素来设置了不同年份的柱状图数据。 最后,使用 `setOption` 方法将配置项和数据应用到图表,即可显示出带有时间线效果的图表。 请注意,上述代码只是一个简单示例,你可以根据自己的需求进行更复杂的配置和数据设定。同时,确保在页面引入了 ECharts 的脚本文件,并在 HTML 有一个具有指定 id 的 DOM 元素用于渲染图表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值