echarts象形渐变柱状图

一、效果图如下:
在这里插入图片描述
二、代码如下
(1)父组件

<template>
  <div class="page-con">
    <div class="main-con">
      <item />
    </div>
  </div>
</template>

<script>
import item from '../bigdata/components/item.vue'
export default {
  components: {
    item
  }
}
</script>

<style lang="scss" scoped>
.page-con {
  width: 100%;
  height: 100%;
  .main-con {
    width: 35%;
    height: 33%;
  }
}
</style>

(2)子组件

<template>
  <div class="bar">
    <div class="volume" ref="container"></div>
  </div>
</template>

<script>
import echarts from "echarts";
export default {
  data() {
    return {
      myChart: null,
    };
  },
   mounted() {
    //获取数据。
    if (this.$refs.container) {
      this.reloadChart();
      //自适应浏览器。
      window.addEventListener("resize", () => {
        this.reloadChart();
      });
    }
  },
  // 组件销毁。
  beforeDestroy() {
    this.disposeChart();
  },
  methods: {
    drawChart() {
      this.myChart = this.$echarts.init(this.$refs.container);
      let colorList = [
        ["#F76B1C", "#FAD961"],
        ["#4D7CFE", "#51C0F8"],
        ["#0096AF", "#00D6BE"],
      ];
      let option = {
        grid: {
          left: "12%",
          top: "15%",
          bottom: "12%",
          right: "8%",
        },
        tooltip: {
          trigger: "axis",
          formatter: (params) => {
            var result = "";
            params.forEach(function (item) {
              let markerHtml = `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background: linear-gradient(360deg, ${item.color.colorStops[1].color} 0%, ${item.color.colorStops[0].color} 100%);"></span>`;
              result += `${item.axisValue}<br>${markerHtml}${item.data}%`;
            });
            return result;
          },
        },
        xAxis: {
          data: ["2021", "2022", "2023"],
          axisTick: {
            show: false,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "#D1D1D1",
              width: 0.5, //这里是为了突出显示加上的
            },
          },
          axisLabel: {
            textStyle: {
              color: "#8998AC",
              fontSize: this.fontSize(0.35),
            },
          },
        },
        yAxis: [
          {
            name: "个",
            nameTextStyle: {
              color: "#8998AC",
              padding: [0, 25, 0, 0],
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisLabel: {
              textStyle: {
                color: "#999",
                fontSize: this.fontSize(0.35),
              },
            },
          },
        ],
        series: [
          {
            type: "pictorialBar",
            barCategoryGap: "0%",
            symbol:
              "path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z",
            barWidth: "65%",
            itemStyle: {
              color: function (params) {
                let itemColor = colorList[params.dataIndex];
                let curColor = "";
                if (Array.isArray(itemColor)) {
                  curColor = {
                    type: "linear",
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                      {
                        offset: 0,
                        color: itemColor[0], //  0%  处的颜色
                      },
                      {
                        offset: 1,
                        color: itemColor[1] ? itemColor[1] : itemColor[0], //  100%  处的颜色
                      },
                    ],
                    global: false, //  缺省为  false
                  };
                } else {
                  curColor = itemColor;
                }
                return curColor;
              },
            },
            data: [123, 60, 25],
            z: 10,
          },
        ],
      };
      this.myChart.setOption(option);
    },
    // 字体自适应。
    fontSize(res) {
      const clientWidth =
        window.innerWidth ||
        document.documentElement.clientWidth ||
        document.body.clientWidth;
      if (!clientWidth) return;
      const fontSize = 40 * (clientWidth / 1920);
      return res * fontSize;
    },
    // 销毁图表。
    disposeChart() {
      if (this.myChart) {
        this.myChart.dispose();
      }
    },
    // 重新加载图表。
    reloadChart() {
      this.disposeChart();
      this.drawChart();
    },
  },
};
</script>

<style lang="scss" scoped>
.bar {
  width: 100%;
  height: 100%;
  .volume {
    width: 100%;
    height: 100%;
  }
}
</style>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值