百分比组件

在这里插入图片描述

//组件
<template>
  <div :class="className" :style="{ height: height, width: width }" style="overflow: hidden;" />
</template>
  
  <script>
export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    dataOption: {
      type: Array,
      default: [],
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
  },
  data() {
    return {
      chart: null,
      dataValue: [],
    };
  },
  mounted() {
    let that = this;
    this.$nextTick(() => {
      setTimeout(function () {
        that.initChart();
      }, 500);
    });
  },
  watch: {
    dataOption: {
      deep: true,
      handler(val) {
        this.initChart();
      },
    },
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el, "macarons");
      this.chart.setOption({
        grid: {
          left: "0px",
          right: "0%",
          containLabel: true,
        },
        xAxis: {
          type: "value",
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          max: 100,
        },
        yAxis: {
          type: "category",
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
        },
        series: [
          {
            name: "XXX",
            type: "pictorialBar",
            yAxisIndex: 0,
            symbolSize: [20, 20],
            symbolOffset: [16, 0],
            z: 12,
            itemStyle: {
              normal: {
                color: "#73b605",
              },
            },
            data: [
              {
                value: this.dataOption[0],
                symbolPosition: "end",
              },
            ],
          },
          {
            name: "XXX",
            type: "pictorialBar",
            yAxisIndex: 0,
            symbolSize: [10, 10],
            symbolOffset: [11, 0],
            z: 13,
            itemStyle: {
              normal: {
                color: "#fff",
              },
            },
            data: [
              {
                value: this.dataOption[0],
                symbolPosition: "end",
              },
            ],
          },
          {
            type: "bar",
            itemStyle: {
              normal: {
                color: "#73b605",
                opacity: 0.7,
                barBorderRadius: 5,
              },
            },
            showBackground: true,
            backgroundStyle: {
              color: "rgba(225, 230, 239)",
              borderRadius: [500, 500, 500, 500],
            },
            barWidth: 10,
            barGap: "-100%", // Make series be overlap
            data: this.dataOption,
          },
        ],
      });
    },
  },
};
</script>
  
//使用组件

1、引入

<scheduleChart :width="'87%'" :height="'30px'" :dataOption="dataOption"></scheduleChart>

dataOption: [80],

你可能感兴趣的:(前端,javascript)