echats实现排名柱状图 自定义序号图片+斜边+阴影效果

实现的效果
本图表实现的难点主要在于1、序号自定义图片2、前三行颜色特殊处理3、斜线效果4、底色阴影效果

1、序号自定义图片

本质上就是设置y轴的标签样式。这里采用base64格式的图片,这里可以替换为自己的图片,推荐一个图片转base64格式的网址:链接: 图片转base64格式

let ranking1 = "`data:image/png;base64,......"
let ranking2 = "`data:image/png;base64,......"
let ranking3 = "`data:image/png;base64,......"
Option.yAxis.axisLabel = {
        margin: 75,
        fontSize: 12,
        align: 'left',
        color: '#fff',
        rich: {
          a: {
			color: '#90B2D2',
          },
		  b: {
            padding: [0, 0, 0, 6],
			fontSize: 13
          },
		  c: {
            padding: [0, 0, 0, 3],
			fontSize: 13
          },
		  d: {
			color: '#90B2D2',
			padding: [0, 0, 0, -5]
          },
          a0: {
            backgroundColor: {
              image: ranking1,
            },
            width: 20,
            height: 20,
            align: 'center',
          },
		  a1: {
            backgroundColor: {
              image: ranking2,
            },
            width: 20,
            height: 20,
            align: 'center',
          },
		  a2: {
            backgroundColor: {
              image: ranking3,
            },
            width: 20,
            height: 20,
            align: 'center',
          }
        },
        formatter: function (params,index) {
		  	if(index >= 7){
			  let newIndex = 9 - index
			  return `{a${newIndex}|} {a|${params}}`;
			}else{
			  let newIndex = 9 - index + 1
			  if(newIndex == 10){
			  //为了让序号对齐,单独进行处理
				return `{c|${newIndex}}   {d|${params}}`
			  }
			  return `{b|${newIndex}}   {a|${params}}`
			}
        },
      }

2、前三行颜色特殊处理

这部分是对series中的color进行判断处理

let colorList = ["#C9293D","#FEA636","#854DFC","#3399FF"]
Option.series[0].itemStyle = {
	  normal: {
		barBorderRadius: 0,
		color: (params) => {
		  if(params.dataIndex >= 7){
			let index = 9 - params.dataIndex
			return colorList[index]
		  }
		  else
			return colorList[3]
		}
	  },
	}

3、斜线效果

斜线效果是通过向series中添加一个矢量平行四边形并调整其位置实现的

Option.series.push(
		{
		  type: "pictorialBar",
		  symbol: 'path://M 50,0 35,0 40,100 55,100 Z',
		  symbolPosition: 'end',
		  symbolSize: [4, 7],
		  symbolOffset: [2, 0],
		  emphasis: {
			scale: false
		  },
		  itemStyle: {
			color: "#fff",
			borderWidth: 1,
			opacity: 1
		  },
		  zlevel: 3,
		  data: data,
		  animationDelay: 500,
		  label: {
			normal:{
			  show: false
			},
			emphasis: {
			  show: false
			}
		  }
		}
	  )

4、底色阴影效果

同样向series中加入type为bar的柱状图,颜色通过添加判断来进行区分,数据是基本数据的1.1倍大小,实现了类似进度条的效果

let max = Option.series[0].data[chartOption.series[0].data.length - 1].value * 1.1
Option.series.push(
		{
		  name: '底色',
		  type: 'bar',
		  barCateGoryGap: 20,
		  barGap: '-100%', // 设置外框粗细
		  data: new Array(data.length).fill(max),
		  barWidth: 6,
		  itemStyle: {
			normal: {
			  barBorderRadius: 0,
			  color: function (params) {
				if(params.dataIndex >= 7){
				  let index = 9 - params.dataIndex
				  return colorList[index]
				}
				else
				  return colorList[3]
			  },
			  opacity: 0.3
			},
		  },
		  z: 0,
		  label: {
			normal:{
			  show: false
			},
			emphasis: {
			  show: false
			}
		  }
		}
	  )
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个基于 Vue 和 Echarts 的动态柱状图示例: ```html <template> <div id="myChart" style="width: 600px;height:400px;"></div> </template> <script> import * as echarts from 'echarts'; export default { data() { return { chartData: [], // 存储柱状图的数据 chartTimer: null // 定时器 }; }, mounted() { // 初始化 echarts 实例 this.myChart = echarts.init(document.getElementById('myChart')); // 显示初始的柱状图 this.showChart(); // 每隔 1 秒更新一次柱状图 this.chartTimer = setInterval(() => { this.showChart(); }, 1000); }, methods: { // 显示柱状图 showChart() { // 随机生成一些数据 const data = []; for (let i = 0; i < 5; i++) { data.push(Math.round(Math.random() * 100)); } // 更新柱状图的数据 this.chartData = data; // 配置柱状图的参数 const option = { title: { text: '动态柱状图示例' }, tooltip: {}, xAxis: { data: ['数据1', '数据2', '数据3', '数据4', '数据5'] }, yAxis: {}, series: [{ name: '数据量', type: 'bar', data: this.chartData }] }; // 使用刚指定的配置项和数据显示图表 this.myChart.setOption(option); } }, beforeDestroy() { // 在组件销毁前清除定时器 clearInterval(this.chartTimer); } }; </script> ``` 此示例使用了一个计时器来每隔 1 秒更新一次柱状图的数据,并使用 Echarts 的 API 更新图表。您可以将其复制到一个 Vue 单文件组件中并运行以查看动态柱状图。如果需要更多帮助,请告诉我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值