echarts象形渐变柱状图

一、效果图如下:
echarts象形渐变柱状图_第1张图片
二、代码如下
(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>

你可能感兴趣的:(echarts,angular.js,前端)