【可视化开发】横向柱状图带排名——配置项及修改样式

在可视化开发中通常会遇到带排名的柱状图,要求前三条数据分别显示不同颜色,其余数据显示同一颜色,如下图所示
在这里插入图片描述

1. 配置柱状图颜色

  1. 定义柱状图渐变颜色数组 barColor
  2. 定义放置数据及样式的数组 studentsBarData
  3. 循环数据数组studentsData.seriesData,将前三位的itemStyle.color设置为渐变颜色数组前三位
  4. 将循环的数据与添加的样式push到空数组studentsBarData中
  5. 设置数据series[0].data : studentsBarData
let barColor = [
	{
		colorStops: [
			{
				offset: 0,
				color: "rgba(254, 205, 77, 0)", // 0% 处的颜色
			},
			{
				offset: 1,
				color: "rgba(254, 205, 77, 1)", // 100% 处的颜色
			},
		],
	},
	{
		colorStops: [
			{
				offset: 0,
				color: "rgba(205, 245, 246, 0)", // 0% 处的颜色
			},
			{
				offset: 1,
				color: "rgba(205, 245, 246, 1)", // 100% 处的颜色
			},
		],
	},
	{
	  colorStops: [
	    {
	      offset: 0,
	      color: "rgba(255, 195, 178, 0)", // 0% 处的颜色
	    },
	    {
	      offset: 1,
	      color: "rgba(255, 195, 178, 1)", // 100% 处的颜色
	    },
	  ],
	},
	{
	  colorStops: [
	    {
	      offset: 0,
	      color: "rgba(32, 196, 255, 0)", // 0% 处的颜色
	    },
	    {
	      offset: 1,
	      color: "rgba(32, 196, 255, 1)", // 100% 处的颜色
	    },
	  ],
	},
];
let studentsBarData = [];
this.studentsData.seriesData.forEach((item, i) => {
	let itemStyle = {
 		color: i > 3 ? barColor [3] : barColor [i],
	};
	studentsBarData.push({
		value: item,
		itemStyle: itemStyle,
	});
});

2. 配置y轴label文字颜色、配置柱状图末尾label文字颜色样式

  1. 定义颜色列表colorList
  2. yAxis[0]配置y轴坐标文字及样式
  3. yAxis[1]配置柱状图后具体数值文字及样式
let colorList = ["#FECD4D", "#CDF5F6", "#FFC3B2", "#20C4FF"];
let options = {
	//...其余配置
	yAxis: [
		{
			type: "category",
			inverse: true,//默认y轴是反转的,使用inverse=true调转
			axisLine: {
				show: false,
			},
			axisTick: {
				show: false,
			},
			axisPointer: {
				label: {
					show: true,
					margin: 30,
				},
			},
			data: this.studentsData.xAxisData,
			axisLabel: {
				margin: 70,
				fontSize: 12,
				align: "left",
				interval: 0,
				color: "#fff",
				rich: {
				//自定义富文本样式
					a1: {
						color: colorList[0],
						width: 20,
						height: 20,
						align: "center",
						fontSize: 14,
						borderRadius: 2,
						borderColor: "rgba(71, 202, 255, 0.50)",
						borderWidth: 2,
						shadowColor: "rgba(71, 202, 255, 0.50)",
						shadowBlur: 10,
					},
					a2: {
						color: colorList[1],
						width: 20,
						height: 20,
						align: "center",
						fontSize: 14,
						borderRadius: 2,
						borderColor: "rgba(71, 202, 255, 0.50)",
						borderWidth: 2,
						shadowColor: "rgba(71, 202, 255, 0.50)",
						shadowBlur: 10,
					},
					a3: {
						color: colorList[2],
						width: 20,
						height: 20,
						align: "center",
						fontSize: 14,
						borderRadius: 2,
						borderColor: "rgba(71, 202, 255, 0.50)",
						borderWidth: 2,
						shadowColor: "rgba(71, 202, 255, 0.50)",
						shadowBlur: 10,
					},
					b: {
						color: colorList[3],
						width: 20,
						height: 20,
						align: "center",
						fontSize: 14,
						lineHeight: 20,
						borderRadius: 2,
						borderColor: "rgba(71, 202, 255, 0.50)",
						borderWidth: 2,
						shadowColor: "rgba(71, 202, 255, 0.50)",
						shadowBlur: 10,
					},
				},
				formatter: (params) => {
					var index = this.studentsData.xAxisData.indexOf(params);
					index = index + 1;
					if (index - 1 < 3) {
						return [
						"{a" + index + "|" + index + "}" + "  " + params,
						].join("\n");
					} else {
						return ["{b|" + index + "}" + "  " + params].join("\n");
					}
				},
			},
		},
		{
			type: "category",
			inverse: true,
			axisTick: "none",
			axisLine: "none",
			show: true,
			data: this.studentsData.seriesData,
			axisLabel: {
			show: true,
			fontSize: 14,
			fontWeight:900,
			color: (value, index) => colorList[index < 3 ? index : 3],//改变柱状图末尾label文字颜色
			formatter: "{value}",
			},
		}
	],
}

3. 初始化排名柱状图echarts代码片段总结

initStudentsCharts() {
      this.students = this.$echarts.init(this.$refs.students);
      let colorList = ["#FECD4D", "#CDF5F6", "#FFC3B2", "#20C4FF"];
      let data1 = []//设置背景柱状图数据
      for (var i = this.studentsData.seriesData.length - 1; i >= 0; i--) {
          data1.push(Math.max.apply(null,this.studentsData.seriesData) * 1.2);   
      };
      let barColor = [
        {
          colorStops: [
            {
              offset: 0,
              color: "rgba(254, 205, 77, 0)", // 0% 处的颜色
            },
            {
              offset: 1,
              color: "rgba(254, 205, 77, 1)", // 100% 处的颜色
            },
          ],
        },
        {
          colorStops: [
            {
              offset: 0,
              color: "rgba(205, 245, 246, 0)", // 0% 处的颜色
            },
            {
              offset: 1,
              color: "rgba(205, 245, 246, 1)", // 100% 处的颜色
            },
          ],
        },
        {
          colorStops: [
            {
              offset: 0,
              color: "rgba(255, 195, 178, 0)", // 0% 处的颜色
            },
            {
              offset: 1,
              color: "rgba(255, 195, 178, 1)", // 100% 处的颜色
            },
          ],
        },
        {
          colorStops: [
            {
              offset: 0,
              color: "rgba(32, 196, 255, 0)", // 0% 处的颜色
            },
            {
              offset: 1,
              color: "rgba(32, 196, 255, 1)", // 100% 处的颜色
            },
          ],
        },
      ];
      let studentsBarData = [];
      this.studentsData.seriesData.forEach((item, i) => {
        let itemStyle = {
          color: i > 3 ? barColor[3] : barColor[i],
        };
        studentsBarData.push({
          value: item,
          itemStyle: itemStyle,
        });
      });
      let options = {
        legend: {
          show: false,
        },
        grid: {
          left: "-10%",
          right: "3%",
          bottom: "0%",
          top: "0%",
          containLabel: true,
        },
        xAxis: {
          show: false,
          type: "value",
        },
        yAxis: [
          {
            type: "category",
            inverse: true,
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisPointer: {
              label: {
                show: true,
                margin: 30,
              },
            },
            data: this.studentsData.xAxisData,
            axisLabel: {
              margin: 70,
              fontSize: 12,
              align: "left",
              interval: 0,
              color: "#fff",
              rich: {
                a1: {
                  color: colorList[0],
                  width: 20,
                  height: 20,
                  align: "center",
                  fontSize: 14,
                  borderRadius: 2,
                  borderColor: "rgba(71, 202, 255, 0.50)",
                  borderWidth: 2,
                  shadowColor: "rgba(71, 202, 255, 0.50)",
                  shadowBlur: 10,
                },
                a2: {
                  color: colorList[1],
                  width: 20,
                  height: 20,
                  align: "center",
                  fontSize: 14,
                  borderRadius: 2,
                  borderColor: "rgba(71, 202, 255, 0.50)",
                  borderWidth: 2,
                  shadowColor: "rgba(71, 202, 255, 0.50)",
                  shadowBlur: 10,
                },
                a3: {
                  color: colorList[2],
                  width: 20,
                  height: 20,
                  align: "center",
                  fontSize: 14,
                  borderRadius: 2,
                  borderColor: "rgba(71, 202, 255, 0.50)",
                  borderWidth: 2,
                  shadowColor: "rgba(71, 202, 255, 0.50)",
                  shadowBlur: 10,
                },
                b: {
                  color: colorList[3],
                  width: 20,
                  height: 20,
                  align: "center",
                  fontSize: 14,
                  lineHeight: 20,
                  borderRadius: 2,
                  borderColor: "rgba(71, 202, 255, 0.50)",
                  borderWidth: 2,
                  shadowColor: "rgba(71, 202, 255, 0.50)",
                  shadowBlur: 10,
                },
              },
              formatter: (params) => {
                var index = this.studentsData.xAxisData.indexOf(params);
                index = index + 1;
                if (index - 1 < 3) {
                  return [
                    "{a" + index + "|" + index + "}" + "  " + params,
                  ].join("\n");
                } else {
                  return ["{b|" + index + "}" + "  " + params].join("\n");
                }
              },
            },
          },
          {
            type: "category",
            inverse: true,
            axisTick: "none",
            axisLine: "none",
            show: true,
            data: this.studentsData.seriesData,
            axisLabel: {
              show: true,
              fontSize: 14,
              fontWeight:900,
              color: (value, index) => colorList[index < 3 ? index : 3],//改变柱状图末尾label文字颜色
              formatter: "{value}",
            },
          },
        ],
        series: [
          {
            z: 2,
            name: "value",
            type: "bar",
            barWidth: 10,
            zlevel: 1,
            data: studentsBarData,
            label: {
              show: false,
              position: "right",
              color: "#fff",
              fontSize: 14,
              offset: [10, 0],
            },
          },
          {
            name: "背景",
            type: "bar",
            barWidth: 10,
            barGap: "-100%",
            itemStyle: {
              normal: {
                color: "rgba(21, 132, 185, 0.18)",
              },
            },
            data: data1,
          },
        ],
      };
      this.students.setOption(options);
      // 当浏览器宽度改变的时候,图表的大小也跟着改变
      window.onresize = () => {
        this.students.resize();
      };
    },
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值