ex2.vue 2.87 KB
<template>
  <div class="echart" id="mychart2" :style="myChartStyle"></div>
</template>
     
    <script>
import * as echarts from "echarts";
export default {
  props: {
    percentage: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      myChartStyle: { float: "left", width: "100%", height: "200px" }, //图表样式
    };
  },
  mounted() {
    this.initEcharts();
  },
  methods: {
    initEcharts() {
      console.log(Number(this.percentage));
      const option = {
        series: [
          {
            type: "gauge",
            startAngle: 180,
            radius: "80%",
            endAngle: 0,
            min: 0,
            max: 1000,
            splitNumber: 4,
            axisLine: {
              lineStyle: {
                width: 20,
                color: [
                  [0.25, "#ec5c17"],
                  [0.5, "#fc9b4f"],
                  [0.75, "#fcc14f"],
                  [1, "#00b099"],
                ],
              },
            },
            pointer: {
              itemStyle: {
                color: "auto",
              },
            },
            axisTick: {
              length: 12,
              lineStyle: {
                color: "auto",
                width: 2,
              },
            },
            splitLine: {
              length: 20,
              lineStyle: {
                color: "auto",
                width: 5,
              },
            },
            axisLabel: {
              color: "auto",
              fontSize: 20,
              distance: -60,
              formatter: function (value) {
                if (value === 0.875) {
                  return "A";
                } else if (value === 0.625) {
                  return "B";
                } else if (value === 0.375) {
                  return "C";
                } else if (value === 0.125) {
                  return "D";
                }
                return "";
              },
            },
            title: {
              offsetCenter: [0, "-20%"],
              fontSize: 30,
            },
            detail: {
              fontSize: 20,
              width: "90",
              color: "#fff",
              borderRadius: 100,
              backgroundColor: "auto",
              offsetCenter: ['8', "38%"],
              valueAnimation: true,
              formatter: function (value) {
                return value + "分";
              },
            },
            data: [
              {
                value: Number(this.percentage),
                // name: "Grade Rating",
              },
            ],
          },
        ],
      };
      const myChart = echarts.init(document.getElementById("mychart2"));
      myChart.clear();
      myChart.setOption(option, true);
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
  },
};
</script>