深夜学姐问我在Vue中Echarts柱状图如何自定义顶部亮点

深夜学姐问我在Vue中Echarts柱状图如何自定义顶部亮点

先看效果图

深夜学姐问我在Vue中Echarts柱状图如何自定义顶部亮点_第1张图片

我们可以看到图中图表不仅有渐变色,同时柱状图顶部位置有一个不相同的顶部亮点(图片)

接下来,我们一起来实现一下这个效果

1. HTML部分

<div id="ParkingLotLoadRanking" ref="ParkingLotLoadRanking" style="height:16.875rem;width:25%;">div>

2. JS部分

	// 获取DOM元素
    let myChart = echarts.init(document.getElementById('ParkingLotLoadRanking'));
    // 左侧名称列表
    let className = ['1号停车场', '2号停车场', '3号停车场', '4号停车场', '5号停车场', '6号停车场', '7号停车场']
    let data = [50, 28, 17, 38, 90, 73, 39]
    let serviceCount = [50, 28, 17, 38, 90, 73, 39]
    // 顶部亮点图列表 在Echarts中如果想使用Base64位图片必须在前边加上 'image://',否则不生效
    let circleList = [
      'image://data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAhCAYAAAC4JqlRAAAFdklEQVRYhY1YTY/bRBh+/BHbiRN7l02bqst+st1dVT0gceXABYk7PfYPwIELv4IfABJ3euuBCxKCKxckBEKi2y1F6Xa3abTZT+fLHxnbaMZje+zE3Y70aiLPzPs888w778xEin/8EO9YJKGbVDEkrvhdWdSKBumGehGJReBVdVZyApsPk/roicSdi4aKb4sUEK38jeLEHIdVRQWOnsgLgORCLWsKzFUDqlmHXEvGRzMCMnEx6XmIgpCDRaU6saMnMTYfRpmMQgzIJdCiNTebqHc6kHUbMYkQBT7ikCReFBWypkNSZUS+A/f0FOOjMSdRtlgglikgypwCK8z0FQPWzgYkpQn/8grT3jFmo0ggjMxhrSWjsWqhcXcP9c4Yo5fH8AYugFDQOSVBx8apAqkzRQBXYd1bhnF7G8HVNZznV4hDhZNWuIkEQm4EkhLC3luGtrwEb9DF8MUV+560R7xmpFXMB1kCYu+1obc3MD7qY9qbAagDqHFTKwhQkBnicIbrgyEaqy6am9uQ5Fdwnp8LfaW0F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MGZt2W06Pd92G3QITY9r1PEHTe/43wHWzfk8OAP8D28XBfaJJ48oAAAAASUVORK5CYII='
    ]
    // 设置每个状图颜色
    var colorList = ['#FCD298', '#24FDE7', '#24FDE7', '#24FDE7', '#24FDE7', '#24FDE7', '#24FDE7'];
    // 设置默认值
    var defaultData = [100, 100, 100, 100, 100, 100, 100]
    let option = {
     
      grid: {
     
        left: '5%',
        right: '5%',
        bottom: '5%',
        top: '10%',
        containLabel: true
      },
      backgroundColor: 'rgb(49,69,81)',
      xAxis: {
     
        show: false,
        type: 'value'
      },
      yAxis: [{
     
        type: 'category',
        inverse: true,
        axisLabel: {
     
          show: true,
          textStyle: {
     
            color: '#B9E4E6'
          },
        },
        splitLine: {
     
          show: false
        },
        axisTick: {
     
          show: false
        },
        axisLine: {
     
          show: false
        },
        data: className
      },
      {
     
        type: 'category',
        inverse: true,
        axisTick: 'none',
        axisLine: 'none',
        show: true,
        axisLabel: {
     
          textStyle: {
     
            color: '#fff',
            fontSize: '12'
          },
          formatter: function (value) {
     
            return value + ' %';
          },
        },
        data: data
      }
      ],
      series: [{
     
        name: '停放车辆数',
        type: 'bar',
        zlevel: 1,
        itemStyle: {
     
          data: serviceCount,
          normal: {
     
            barBorderRadius: 0,
            color: function (params) {
     
              // 大于等于50%的是黄色 反之为蓝色
              var colorList = [
                ['rgba(252, 210, 152, .2)', 'rgba(252, 210, 152, 1)'],
                ['rgba(36, 253, 231, .2)', 'rgba(36, 253, 231, 1)'],
              ];
              var colorItem
              if (params.data >= 50) {
     
                colorItem = colorList[0];
              } else {
     
                colorItem = colorList[1];
              }
              // 设置线条渐变色
              return new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
     
                offset: 0,
                color: colorItem[0]
              },
              {
     
                offset: 1,
                color: colorItem[1]
              }
              ], false);
            }

          },
        },
        barWidth: 4,
        data: data
      },
      {
     
        name: '背景',
        type: 'bar',
        barWidth: 4,
        barGap: '-100%',
        data: defaultData,
        itemStyle: {
     
          normal: {
     
            color: '#1B375E',
            barBorderRadius: 0,
          }
        },
      },
      {
     
        name: 'XXX',
        type: 'pictorialBar',
        symbol: function (params, value) {
     
          // 设置图片
          if (params >= 50) return circleList[0]
          return circleList[1]
        },
        symbolPosition: 'end',
        symbolSize: [30, 30],
        symbolOffset: [10, 0],
        z: 20,
        data: data
      }
      ]
    };
    myChart.setOption(option);
    window.onresize = () => {
     
      myChart.resize();
    };

效果图

深夜学姐问我在Vue中Echarts柱状图如何自定义顶部亮点_第2张图片

你可能感兴趣的