Echarts 每个柱子设置不同的

本文介绍如何使用ECharts库创建一个柱状图,详细展示了如何配置图表元素、添加数据和响应窗口大小变化。重点在于设置系列颜色和标签格式,并实现图表的实时调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

<div ref="rightChart" style="width: 100%;height:260px;"></div>
rightOptionBar() {
      // 通过id获取获取画布
      const myChart = echarts.init(this.$refs.rightChart,null,{devicePixelRatio: 2});
      // 指定图表的配置项和数据
      let option = {
        grid: {
          left: 30,
          right: 30,
          top: '18%',
          bottom:'10%',
          containLabel: true
        },
        tooltip : {
          formatter: '{a}:{c}%'//a 是series中每条数据name值,c 是data数值
        },
        xAxis : [ //横坐标
          {
            type : 'category',
            data:['控制性工程','特大桥','大桥','特长隧道','长隧道'],
            splitLine: {
              show: false
            },
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis : [ //纵坐标
          {
            type : 'value',
            axisLabel: {
              show:true,
              //Y轴数值可添加百分号
              formatter: '{value}%',
            },
          }
        ],
        series : [
          {
            name: '数量',
            type: 'bar',
            itemStyle: {
              normal: {
                // 设置不同颜色
                color: function(params) {
                  // build a color map as your need.
                  let colorList =['rgba(63,110,160,0.8)','rgba(236,127,102,0.8)','rgba(247,199,58,0.8)','rgba(116,160,250,0.8)','rgba(115,222,180,0.8)']
                  return colorList[params.dataIndex]
                },
                // 显示位置和显示格式
                label: {
                  show: true,
                  position: 'top',
                  formatter: '{c}%'
                }
              }
            },
            //设置柱的宽度
            barWidth:70,
            data: [85,67,90,100,50]
          }
        ]
      };
      myChart.setOption(option);
      window.addEventListener("resize", function () {
        myChart.resize();
      });
      // 使用刚指定的配置项和数据显示图表。画布为 myCharts  true为显示
      if (option && typeof option === "object") {
        myChart .setOption(option, true);
      }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值