echarts实现柱状图加一条横线

1.示例图

2.实现

先写一个柱状图,然后在每个柱状图上加一条横线,最后在统一实现柱状图的渐变背景

复制代码
<template>
  <div class="abnormal-sg">
    <div
      ref="discountChart"
      class="chart-box__canvas"
    />
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'Abnormal',
  data () {
    return {
      chartInstance: null,
      xData: ['苹果', '香蕉', '橙子'],
      yData: [20, 100, 50]
    }
  },
  mounted () {
    this.initChart()
    // 图表自适应
    window.addEventListener('resize', this.handleResize)
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.handleResize)
    if (this.chartInstance) {
      this.chartInstance.dispose()
      this.chartInstance = null
    }
  },
  methods: {
    initChart () {
      this.initDiscountChart(this.$refs.discountChart)
    },
    handleResize () {
      if (this.chartInstance) {
        this.chartInstance.resize()
      }
    },
    // 初始化图表
    initDiscountChart (el) {
      if (!el) return null
      if (this.chartInstance) {
        this.chartInstance.dispose()
      }

      const chart = echarts.init(el)
      this.chartInstance = chart

      chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            lineStyle: {
              type: 'dashed' // dashed虚线 solid实线 dotted点线
            }
          }
        },
        grid: {
          left: 60,
          right: 20,
          bottom: 30,
          top: 40
        },
        xAxis: {
          type: 'category',
          data: this.xData,
          axisLabel: {
            fontSize: 16
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          }
        },
        yAxis: {
          type: 'value',
          name: '单位:个',
          nameTextStyle: {
            fontSize: 16
          },
          splitLine: {
            lineStyle: {
              color: '#e2e8f6',
              type: 'dashed'
            }
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            fontSize: 16
          }
        },
        series: [
          {
            type: 'bar',
            barWidth: '100%',
            barCategoryGap: '0%',
            stack: 'topLineStack',
            data: this.yData,
            itemStyle: {
              color: '#c2e0fe'
            }
          },
          // ----------------------横线----------------------------
          {
            type: 'custom',
            coordinateSystem: 'cartesian2d',
            data: this.yData.map((value, index) => [
              this.xData[index],
              value
            ]),
            renderItem: (params, api) => {
              const x = api.coord([api.value(0), api.value(1)])[0]
              const y = api.coord([api.value(0), api.value(1)])[1]
              // 获取当前分类的宽度
              const bandWidth = api.size([1, 0])[0]
              // 横线宽度为分类宽度
              return {
                type: 'line',
                shape: {
                  x1: x - bandWidth / 2,
                  y1: y,
                  x2: x + bandWidth / 2,
                  y2: y
                },
                style: {
                  stroke: 'green',
                  lineWidth: 2
                },
                silent: true
              }
            },
            silent: true,
            tooltip: {
              show: false
            },
            z: 10
          }
        ]
      })

      // -----------------------渐变颜色-------------------------
      // 获取实际绘图区的位置和高度
      const gridRect = chart
        .getModel()
        .getComponent('grid')
        .coordinateSystem
        .getRect()
      // 创建整个绘图区共用的垂直渐变
      const gradient = new echarts.graphic.LinearGradient(
        0,
        gridRect.y,
        0,
        gridRect.y + gridRect.height,
        [
          {
            offset: 0,
            color: 'rgba(103, 194, 58, 0.8)'
          },
          {
            offset: 1,
            color: 'rgba(230, 241, 255, 0.7)'
          }
        ],
        true
      )
      // 应用统一渐变
      chart.setOption({
        series: [
          {
            itemStyle: {
              color: gradient
            }
          }
        ]
      })

      return chart
    }
  }
}
</script>
<style scoped lang="scss">
.abnormal-sg {
  width: 100%;
  height: 200px;
}
.chart-box__canvas {
  width: 100%;
  height: 100%;
}
</style>

一般图表可参考:echarts图表集

相关推荐
FRing031 小时前
vue2干货总结12(loading效果、全局前置导航守卫、搜索历史管理)
开发语言·前端·javascript·vue.js·vue2
YIAN1 小时前
我踩完 DeepAgents 记忆模块的坑,才搞懂长期记忆和摘要不是一回事
前端·javascript·agent
map_3d_vis4 小时前
百度地图 JSAPI 4.0 是什么:版本说明与能力概览
前端·javascript·webgl·地图·百度地图
码云之上4 小时前
微信小程序设备指纹实践,删了重装还能认出这台手机吗?
前端·安全·微信小程序
JavaGuide4 小时前
刚刚,GLM-5.4 正式发布,实测来了:看图用 Blender 建凉亭、做技术视频,再画一辆真能传动的自行车
前端·后端
乌鲁木齐德润恒圣信息科技有限公司6 小时前
GEO生成式引擎优化:技术原理与43引擎自动测试系统
大数据·前端·人工智能
泯泷6 小时前
第一章:Lit简介 - 现代Web组件标准
前端·javascript·web components
Moment7 小时前
AI 写代码越来越快,程序员为什么反而更累了
前端·后端·面试
apeman7 小时前
git-cherry合并代码工具
前端·后端
SGAMERrain8 小时前
前端小游戏有 25000+ 答案,如何避免一次性加载全部数据?
前端