echarts象形柱状图

一般常用的是柱状图可能大部分人很少关注到还有象形柱状图的存在,甚至都没听说过这个名字。象形柱状图type: pictorialBar,可以自定义条纹的颜色、高度以及每个条纹的间隔。

1、封装图表组件

<template>
  <div ref="split" :style="{height: height}"></div>
</template>

<script>
  import {
    formatNumber
  } from "@/utils/common";
  export default {
    props: {
      // 数据列表
      dataList: {  
        type: Array,
        default: () => []
      },
      // x轴坐标数据
      xData: {  
        type: Array,
        default: () => []
      },
      // y轴坐标单位
      unit: {  
        type: String,
        default: "条"
      },
      //图表的高度
      height: {
        type: String,
        default: "246px"
      }
    },
    mounted() {
      // 初始化图表
      this.initChart();
    },
    methods: {
      initChart() {
        let dom = this.$refs["split"];
        var myChart = echarts.init(dom);
        var option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow',
              shadowStyle: {
                color: 'rgba(42,43,107,0.5)'
              }
            },
            formatter: `{b}:{${formatNumber('c',0)}}${this.unit}`
          },
          grid: {
            bottom: 24,
            top: 30,
            left: 0,
            right: 0,
            containLabel: true
          },
          dataZoom: [{
            type: "inside",
            start: 0,
            end: 100
          }],
          xAxis: {
            type: 'category',
            data: this.xData,
            axisLine: {
              lineStyle: {
                color: '#31305A'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                fontSize: 12,
                fontWeight: 400,
                color: "#5E5E81"
              }
            }
          },
          yAxis: {
            type: 'value',
            axisLine: {
              lineStyle: {
                color: '#31305A'
              }
            },
            axisLabel: {
              textStyle: {
                fontSize: 12,
                fontWeight: 400,
                color: "#5E5E81"
              }
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: '#31305A',
                type: 'solid',
              }
            }
          },
          series: [{
            data: this.dataList,
            type: 'pictorialBar',
            barWidth: 30,
            symbolRepeat: true,
            itemStyle: {
              normal: {
                color: "#342bf5"
              }
            },
            symbolMargin: 2,
            symbol: 'rect',
            symbolSize: ['100%', 5],
            symbolClip: true
          }],
        };
        option && myChart.setOption(option);
        window.addEventListener("resize", function () {
          myChart.resize();  //缩放屏幕时图表自适应
        });
      }
    },
  }
</script>

2、引用组件

<split-bar-chart :xData="xData" :dataList="TrendList" unit="条" height="220px" v-if="TrendList.length"></split-bar-chart>

3、图表数据结构

xData: x轴的坐标数据,这里存储的是年龄,数据是动态的通过调后端接口获取

['30', '30-40', '40-50', '50-60', '60']

dataList: y轴的数据,和年份一一对应

[220, 806, 613, 426, 501]

4、formatNumber是自定义n位分隔符函数

// num:数字 len:保留小数点位数
export function formatNumber(num, len) {
  num = num + "";
  if (!num.includes(".")) {
    num += ".";
  }
  if (len) {
    num = Number(num).toFixed(len).toString();
  }
  let val = num
    .replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
      return $1 + ",";
    })
    .replace(/\.$/, "");
  return val;
}

效果展示

拓展 —— 象形柱状图颜色设置渐变

看了官网配置项手册以及网上参考资料发现不能直接给象形柱状图设置渐变颜色,但是可以先画渐变的柱状图,然后把象形柱状图重叠在柱状图之上来实现

  series: [
    {
      type: 'bar',
      itemStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: '#e2f6f7' },
          { offset: 0.5, color: '#70e3e9' },
          { offset: 1, color: '#188df0' }
        ])
      },
      barWidth: 20,
      data: [68, 95, 146, 80, 117, 102, 76]
    },
    {
      type: 'pictorialBar', //设置类型为象形柱状图
      symbol: 'rect', //图形类型,带圆角的矩形
      symbolMargin: '2', //图形垂直间隔
      symbolRepeat: true, //图形是否重复
      // 分隔
      itemStyle: {
        normal: {
          color: '#fff'
        }
      },
      symbolClip: true,
      symbolSize: [20, 2],
      symbolPosition: 'start',
      // symbolOffset: [-12, -1],
      data: [68, 95, 146, 80, 117, 102, 76],
      z: 1,
      zlevel: 2 //重点,层级必须设置大于柱状图
    }
  ]

 这世界很喧嚣,做你自己就好

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值