【echarts】21、echarts+vue2 - 渐变柱状图

前言

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

实现效果

在这里插入图片描述

代码实现

<template>
  <div class="chart-wrap">
    <ul class="legend-list">
      <li
        v-for="(item, index) in legend"
        :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="chart18" class="chart" />
  </div>
</template>

<script>
import echarts from '@/utils/echarts'

export default {
  name: 'Index',
  data () {
    return {
      chart: null,
      legend: [{ name: '人次', color: 'rgba(6, 200, 140, 1)', selected: true }],
      yData: [25, 20, 15, 38, 40, 34, 22],
      xData: ['8:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00']
    }
  },
  mounted() {
    this.createChartHandler()
  },
  methods: {
    // 创建图表
    createChartHandler () {
      this.chart = this.$echarts.init(document.getElementById('chart18'))
      this.chart.setOption(this.getChartOption(this.yData, this.xData, this.legend))
      window.addEventListener('resize', () => {
        setTimeout(() => {
          this.chart.resize()
        })
      })
    },
    // 获取图表配置项
    getChartOption (yData, xData, legend) {
      return {
        legend: {
          show: false,
          data: legend.map(i => i.name)
        },
        tooltip: {
          trigger: 'item',
          position: 'right',
          extraCssText:
            'color:#fff;background: rgba(0, 38, 118, 0.5);border:none; box-shadow: 0px 0px 8px 1px rgba(0, 145, 255, 0.5);border-radius: 2px;z-index:99',
          formatter: function (params) {
            return `
              <span>${params.seriesName}</span><br>
              ${params.marker}<span>${params.name} </span><span>${params.data}</span>
            `
          }
        },
        textStyle: {
          fontFamily: 'PingFangReg'
        },
        grid: {
          top: '8%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: xData,
          axisTick: {
            show: false
          },
          axisLine: { // 这是x轴线颜色
            lineStyle: {
              color: 'rgba(6, 200, 140, 0.79)'
            }
          },
          axisLabel: {
            textStyle: {
              color: '#fff'
            }
          }
        },
        yAxis: {
          interval: 10,
          type: 'value',
          axisLabel: {
            textStyle: {
              color: '#fff'
            }
          },
          axisLine: { // 这是y轴线颜色
            show: true,
            lineStyle: {
              color: 'rgba(6, 200, 140, 0.79)'
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
              color: 'rgba(6, 200, 140, 0.29)'
            }
          }
        },
        series: [
          {
            name: legend[0].name,
            data: yData,
            type: 'bar',
            label: {
              normal: {
                show: true,
                distance: 4,
                lineHeight: 14,
                formatter: '{c}',
                position: 'top',
                textStyle: {
                  fontFamily: 'PingFangBold',
                  color: '#06C88C',
                  fontSize: 14
                }
              }
            },
            barWidth: 13,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  1,
                  0,
                  0,
                  [
                    { offset: 0, color: 'rgba(6, 200, 140, 1)' },
                    { offset: 1, color: 'rgba(100, 255, 207, 1)' }
                  ],
                  false
                ),
                barBorderRadius: [3, 3, 0, 0],
                label: {
                  show: false,
                  position: 'top',
                  textStyle: {
                    color: '#fff'
                  }
                }
              }
            }
          }
        ]
      }
    },
    // 鼠标移入图例触发
    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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值