Blame view

src/components/charts/scatterChart.vue 3.29 KB
db11048f   阿宝   设备状态,学校管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  <template>
    <div class="chart" :id="id"></div>
  </template>
  
  <script>
  export default {
    name: "pieChart",
    props: {
      id: String,
      params: Array,
    },
    watch: {
      params: {
        handler: function (val) {
          if (val.length) {
            this.initData();
          }
        },
        deep: true,
      },
    },
    data() {
      return {
        chart: null,
      };
    },
    created() {},
    mounted() {
      this.initData();
    },
    methods: {
      setOption() {
        const that = this;
        let xData = this.params.map((item) => {
          return item.name;
        });
        const options = {
          // color: this.colors || ["#ff80db", "#c8cc00", "#67c6b5"],
          backgroundColor: "#f8f8f8",
          tooltip: {
            axisPointer: {
              type: "cross",
            },
            formatter: function (params) {
              return (
                params.marker +
                ` 占比:${params.data.value}% <br>数量:${params.data.count}`
              );
            },
          },
          xAxis: {
            type: "category",
            data: xData,
            axisLine: {
              show: true,
              lineStyle: {
                width: 0.5,
                color: "#999",
              },
            },
            axisTick: {
              show: false,
            },
          },
          yAxis: {
            name:"活跃度占比",
            type: "value",
            nameTextStyle:{
              align:"right"
            },
            axisLine: {
              show: true,
              lineStyle: {
                width: 0.5,
                color: "#999",
              },
            },
            splitLine: {
              show: false,
            },
            max: 100,
            axisLabel: {
              show: true,
              interval: "auto",
              formatter: "{value}%",
            },
          },
          series: {
            data: this.params,
            colorBy: "data",
            type: "scatter",
            barMaxWidth: 40, //最大宽度
            barMinHeight: 0, // 最小高度
            symbolSize: function (data) {
              return Math.sqrt(data) * 8 > 10 ? Math.sqrt(data) * 8 : 10;
            },
            itemStyle: {
              shadowBlur: 10,
              shadowColor: "rgba(25, 100, 150, 0.5)",
              shadowOffsetY: 5,
            },
            label: {
              show: true,
              position: "top",
              formatter: function (params) {
                return params.data.count ? params.data.count : "";
              },
            },
            selectedMode: true,
            hoverAnimation: true,
            avoidLabelOverlap: true,
            animationType: "scale",
            animationEasing: "elasticOut",
            animationDelay() {
              return Math.random() * 100;
            },
          },
          grid: {
            top: 40,
            right: "5%",
            bottom: 30,
            left: "10%",
          },
        };
        return options;
      },
      initData() {
        if (!this.chart) {
          const div = document.getElementById(this.id);
          this.chart = this.$echarts.init(div);
        }
        const options = this.setOption();
        this.chart?.clear();
        this.chart.setOption(options, true);
        this.chart.off("click");
        this.chart.on("click", "series", (params) => {
          this.$emit("clickScatterChart", params);
        });
      },
    },
  };
  </script>
  
  <style lang="scss" scoped>
  .chart {
    height: 100%;
  }
  </style>