echart两个年份对比图表

echart下载到本地项目,通过import引入使用

<template>
  <div
    class="com-contain"
    :style="[{ position: 'relative' }, { height:  height + 'px' }]"
    style="overflow-y: auto; overflow-x: hidden; width: 100%;"
  >
    <template v-if="loading">
      <t-loading fix>
        <t-icon type="loading"></t-icon>
        <div>加载中...</div>
      </t-loading>
    </template>
    <template v-else-if="!pagedata.length">
      <div class="hrrc-no-data">暂无数据</div>
    </template>
    <template v-else>
      <div
        class="com-chart"
        ref="seller_ref"
        :style="optionStyle"
        style="min-height: 100%; width: 100%"
      ></div>
    </template>
  </div>
</template>

<script>
import * as echarts  from './echarts.min.js'
export default {
  props: {
    loading: Boolean,
    pagedata: Array,
    height: Number
  },
  data() {
    return {
      chartInstance: null,
      allData: []
    }
  },
  mounted() {
    this.initChart()
    this.updateChart()
  },
  computed: {
    optionStyle() {
      const style = {}
      let arr = []
      this.pagedata.forEach(item => {
        if (item.data && item.data.length) {
          item.data.forEach((subItem) => {
            if (!arr.includes(subItem.technologyTypeName)) {
              arr.push(subItem.technologyTypeName)
            }
          })
        }
      })
      let percentage = arr.length * 55
      style.height = percentage + 'px'
      return style
    }
  },
  methods: {
    // 初始化chartInstance对象
    initChart() {
      this.chartInstance = echarts.init(this.$refs.seller_ref)
    },
    // 更新图表
    updateChart() {
    console.log(this.pagedata)
      let labelData = []
      let yearArr = []
      const json = {}
      this.pagedata.map((item, index) => {
        if (item.data && item.data.length) {
          const arr = []
          const arr1 = []
          const arr2 = []
          item.data.forEach((subItem) => {
            if (!labelData.includes(subItem.technologyTypeName)) {
              labelData.push(subItem.technologyTypeName)
            }
            arr.push({
              name: subItem.technologyTypeName,
              value: subItem.technologyTypeSum ? subItem.technologyTypeSum : 0
            })
            arr1.push({
              name: subItem.technologyTypeName,
              value: subItem.technologyTypeUpSum ? subItem.technologyTypeUpSum : 0
            })
            arr2.push({
              name: subItem.technologyTypeName,
              value: subItem.technologyTypeDownSum ? subItem.technologyTypeDownSum : 0
            })
          })
          json[item.year] = [arr, arr1, arr2]
        }
        console.log(json[item.year])
        yearArr.push(item.year)
      }),
      console.log(json)
      console.log(labelData, 'labelData')
      console.log(yearArr, 'yearArr')
      let xData = labelData
      // let xData = ['IT运维', 'IT运营', '架构/规划', 'IT发', '信息安全']
      // let eventHandling = [11, 38, 23, 139, 66]
      // let eventWarning = [3, 20, 122, 134, 55]
      let legend = ['总人数', '晋级人数', '降级人数']
      this.tyLine = {
        // backgroundColor: "#fff",
        timeline: {
          show: false
        },
        tooltip: {
          // trigger: 'axis', // 悬浮提示 全部展示
          // axisPointer: {
          //   type: 'shadow'
          // },
          backgroundColor: 'rgba(0,0,0,0.5)',
          borderWidth: 0,
          extraCssText: 'box-shadow: 2px 2px 4px 0px rgba(0,0,0,0.3);',
          textStyle: {
            fontSize: 14,
            color: '#fff'
          }
        },
        legend: {
          bottom: '3%',
          // left: '1%',
          // itemWidth: 14,
          // itemHeight: 14,
          // itemGap: 10,
          // icon: 'rect',
          // selectedMode: true,
          // textStyle: {
          //   color: '#fff',
          //   fontSize: 14,
          //   padding: [0, 6]
          // }
          top: 5,
          left: 0,
          textStyle: {
            color: '#fff',
            fontSize: 14
          },
          itemWidth: 8,
          itemHeight: 8,
          icon: 'rect',
          orient: 'horizontal'
        },
        grid: [
          //
          // 左边
          {
            show: false,
            left: '0%',
            top: '15%',
            bottom: '10%',
            containLabel: true,
            width: '38%',
            color: '#fff'
          },
          // 中间
          {
            show: false,
            left: '48%',
            top: '15%',
            bottom: '10%',
            width: '0%'
          },
          // 右边
          {
            show: false,
            right: '0%',
            top: '15%',
            bottom: '10%',
            containLabel: true,
            width: '38%'
          }
        ],
        xAxis: [
          {
            type: 'value',
            inverse: true,
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: false
            },
            splitLine: {
              show: false
            }
          },
          {
            gridIndex: 1,
            show: false
          },
          {
            gridIndex: 2,
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: false
            },
            splitLine: {
              show: false
            }
          }
        ],
        yAxis: [
          {
            type: 'category',
            inverse: true,
            name: yearArr[0], // 图表名
            position: 'right',
            axisLine: {
              show: true,
              lineStyle: {
                color: '#fff'
              }
            },
            axisTick: { // y轴 刻度尺
              show: false
            },
            axisLabel: {
              show: false
            },
            // data: xData
          },
          {
            gridIndex: 1,
            type: 'category',
            inverse: false,
            position: 'right',
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              // 对应中文标签样式
              show: true,
              color: '#fff',
              fontSize: 14,
              align: 'center'
            },
            data: xData.map(function (value) { // 中文标签数据 y轴几个排列
              return {
                value: value,
                textStyle: {
                  align: 'center'
                }
              }
            })
          },
          {
            gridIndex: 2,
            type: 'category',
            name: yearArr[1],
            inverse: true,
            position: 'left',
            axisLine: {
              show: true,
              lineStyle: {
                color: '#fff'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: false
            },
            // data: xData // y轴几个排列
          }
        ],
        series: [
          // 左边
          {
            name: legend[0],
            type: 'bar',
            stack: '1',
            barWidth: 20,
            itemStyle: {
              color: '#2A82FE'
            },
            label: {
              show: false,
              position: 'left',
              color: '#2A82FE'
            },
            data: json[yearArr[0]][0],
            animationEasing: 'elasticOut'
          },
          {
            name: legend[1],
            type: 'bar',
            stack: '1',
            itemStyle: {
              color: '#37D3A4'
            },
            label: {
              show: false,
              position: 'left',
              color: '#37D3A4'
            },
            data: json[yearArr[0]][1],
            animationEasing: 'elasticOut'
          },
          {
            name: legend[2],
            type: 'bar',
            stack: '1',
            itemStyle: {
              color: '#F6BD16'
            },
            label: {
              show: false,
              position: 'left',
              color: '#F6BD16'
            },
            data: json[yearArr[0]][2],
            animationEasing: 'elasticOut'
          }, // 右边
          {
            name: legend[0],
            type: 'bar',
            stack: '2',
            barWidth: 20,
            xAxisIndex: 2,
            yAxisIndex: 2,
            itemStyle: {
              color: '#2A82FE'
            },
            label: {
              show: false,
              position: 'right',
              color: '#2A82FE'
            },
            data: json[yearArr[1]][0],
            animationEasing: 'elasticOut'
          },
          {
            name: legend[1],
            type: 'bar',
            stack: '2',
            xAxisIndex: 2,
            yAxisIndex: 2,
            itemStyle: {
              color: '#37D3A4'
            },
            label: {
              show: false,
              position: 'right',
              color: '#37D3A4'
            },
            data: json[yearArr[1]][1],
            animationEasing: 'elasticOut'
          },
          {
            name: legend[2],
            type: 'bar',
            stack: '2',
            xAxisIndex: 2,
            yAxisIndex: 2,
            itemStyle: {
              color: '#F6BD16'
            },
            label: {
              show: false,
              position: 'right',
              color: '#F6BD16'
            },
            data: json[yearArr[1]][2],
            animationEasing: 'elasticOut'
          }
        ]
      }
      this.chartInstance.setOption(this.tyLine)
    }
  }
}
</script>

<style>
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECharts 三个图表切换可以通过 div 显示/隐藏的方式来实现。具体步骤如下: 1. 在页面中添加三个 div,用来存放不同类型的图表。 ```html <div id="chart1" style="display: block;"></div> <div id="chart2" style="display: none;"></div> <div id="chart3" style="display: none;"></div> ``` 2. 在 JS 代码中,使用 ECharts 的实例化对象创建对应的图表,并将其放置在对应的 div 中。 ```javascript // 创建 ECharts 实例对象 var chart1 = echarts.init(document.getElementById('chart1')); var chart2 = echarts.init(document.getElementById('chart2')); var chart3 = echarts.init(document.getElementById('chart3')); // 初始化图表配置项 var option1 = { /* ... */ }; var option2 = { /* ... */ }; var option3 = { /* ... */ }; // 设置图表配置项 chart1.setOption(option1); chart2.setOption(option2); chart3.setOption(option3); ``` 3. 给页面中提供切换图表的元素,如按钮等,绑定点击事件。 ```html <button id="chart1Btn">图表1</button> <button id="chart2Btn">图表2</button> <button id="chart3Btn">图表3</button> ``` ```javascript var chart1Btn = document.getElementById('chart1Btn'); var chart2Btn = document.getElementById('chart2Btn'); var chart3Btn = document.getElementById('chart3Btn'); chart1Btn.onclick = function() { chart1.show(); chart2.hide(); chart3.hide(); } chart2Btn.onclick = function() { chart1.hide(); chart2.show(); chart3.hide(); } chart3Btn.onclick = function() { chart1.hide(); chart2.hide(); chart3.show(); } ``` 以上代码通过按钮的点击事件来切换不同的图表,其中 `show()` 方法将 div 显示出来,`hide()` 方法将 div 隐藏起来。需要注意的是,在页面加载时,第一个图表的 div 应该设置为显示状态,其他两个 div 应该设置为隐藏状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值