【echarts】09、echarts+vue2 - 柱状图

前言

基于echarts5.x和vue2实现
记录以便日后查阅

实现效果

在这里插入图片描述

代码实现

<template>
  <div class="chart-wrap">
    <ul class="legend-list">
      <li
        v-for="(item, index) in yData"
        :key="index"
        :class="['legend', item.selected ? '': 'un-active']"
        @mouseenter="enterHandler(item)"
        @mouseleave="leaveHandler(item)"
        @click="clickHandler(item)"
      >
        <i class="rect" :style="{ backgroundColor: item.color }" />
        <span>{{ item.name }}</span>
      </li>
    </ul>
    <div id="chart06" class="chart" />
  </div>
</template>

<script>
export default {
  name: 'Index',
  data () {
    return {
      chart: null,
      yData: [
        { name: '参与人数', value: [280, 180, 30, 230, 240, 50], color: '#24DCF0', selected: true },
        { name: '活动次数', value: [140, 60, 20, 120, 40, 130], color: '#F7B500', selected: true }
      ],
      xData: ['研学活动', '老年大学', '兴趣教育', '技能培训', '公益帮扶', '艺术创作']
    }
  },
  mounted() {
    this.createChartHandler()
  },
  methods: {
    // 创建图表
    createChartHandler () {
      this.chart = this.$echarts.init(document.getElementById('chart06'))
      this.chart.setOption(this.getChartOption(this.yData, this.xData))
      window.addEventListener('resize', () => {
        setTimeout(() => {
          this.chart.resize()
        })
      })
    },
    // 获取图表配置项
    getChartOption (yData, xData) {
      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          extraCssText:
            'fontFamily: PingFangReg;color:#fff;background:rgba(0,0,0,0.6000);border: unset;border-radius: 2px;z-index:99',
          formatter: (e) => {
            const div = document.createElement('div')
            div.innerHTML = `
        <p style="font-size: 12px">技能培训</p>
        ${e
              .map((i) => {
                return `<p style="font-size: 10px;display: flex; align-items: center;"> <span style=\"display:inline-block;margin-right:4px;border-radius:10px;width:4px;height:4px;background-color:${i.color};\"></span> ${i.seriesName}:${i.value}</p>`
              })
              .join('')}
        `
            return div
          }
        },
        legend: {
          show: false,
          data: yData.map((i) => i.name)
        },
        grid: {
          top: '5%',
          left: '8%',
          right: '5%',
          bottom: '15%'
        },
        xAxis: [
          {
            type: 'category',
            data: xData,
            axisLabel: {
              interval: 0,
              fontFamily: 'PingFangReg',
              lineHeight: 17,
              fontSize: 12,
              color: '#fff'
            },
            axisTick: {
              show: true,
              alignWithLabel: true,
              lineStyle: {
                color: '#24DCF0'
              }
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: '#24DCF0'
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            interval: 50,
            axisLabel: {
              fontFamily: 'PingFangReg',
              fontSize: 12,
              color: '#24DCF0'
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: '#24DCF0'
              }
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: '#24DCF0',
                type: 'dashed'
              }
            }
          }
        ],
        series: yData.map((i) => {
          return {
            name: i.name,
            type: 'bar',
            barWidth: 9,
            barGap: 0.2,
            data: i.value,
            itemStyle: {
              color: i.color
            }
          }
        })
      }
    },
    // 鼠标移入图例触发
    enterHandler (item) {
      if (!this.chart) return
      this.chart.dispatchAction({
        type: 'highlight',
        name: item.name
      })
    },
    // 鼠标移出图例触发
    leaveHandler (item) {
      if (!this.chart) return
      this.chart.dispatchAction({
        type: 'downplay',
        name: item.name
      })
    },
    // 选中切换
    clickHandler (item) {
      if (!this.chart) return
      item.selected = !item.selected
      this.chart.dispatchAction({
        type: 'legendToggleSelect',
        name: item.name
      })
    }
  }
}
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值