ex1.vue 2.14 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: "300px" }, //图表样式
    };
  },
  mounted() {
    this.initEcharts();
  },
  methods: {
    initEcharts() {
      console.log(Number(this.percentage))
      const option = {
        // tooltip: {
        //   formatter: "{a} <br/>{b} : {c}%",
        // },
        series: [
          {
            type: "gauge",
            min: 0,
            max: 1000,
            splitNumber: 10,
            radius: "85%",
            axisLine: {
              lineStyle: {
                width: 15,
                color: [
                  [0.3, "#67e0e3"],
                  [0.7, "#37a2da"],
                  [1, "#fd666d"],
                ],
              },
            },
            pointer: {
              itemStyle: {
                color: "auto",
              },
            },
            axisTick: {
              distance: -15,
              length: 6,
              lineStyle: {
                color: "#fff",
                width: 2,
              },
            },
            splitLine: {
              distance: -30,
              length: 20,
              lineStyle: {
                color: "#fff",
                width: 4,
              },
            },
            axisLabel: {
              color: "auto",
              distance: 0,
              fontSize: 10,
            },
            detail: {
              valueAnimation: true,
              formatter: "{value}",
              color: "auto",
            },
            data: [
              {
                value: Number(this.percentage),
              },
            ],
          },
        ],
      };
      const myChart = echarts.init(document.getElementById("mychart2"));
      myChart.clear()
      myChart.setOption(option, true)
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
  },
};
</script>