# 气泡图

查看代码详情
<template>
  <div ref="effectScatter" style="height: 100%"></div>
</template>
  
  <script>
const labelRight = {
  position: "right",
};
export default {
  name: "WebEffectScatter",
  props: {
    config: {
      type: Function,
    },
    data: {
      type: [Array, Object],
      default: () => [],
    },
    styles: {
      type: String,
      default: "height: 100%;width:800px;",
    },
    title: {
      type: [Array, Object],
      default: () => ({
        text: "统计数据",
        subtext: "单位:个",
      }),
    },
    tooltip: {
      type: [Array, Object],
      default: () => [
        {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
      ],
    },
    grid: {
      type: [Array, Object],
      default: () => [
        {
          top: "250",
          bottom: 30,
        },
      ],
    },
    legend: {
      type: [Array, Object],
      default: () => [
        {
          orient: "vertical",
          left: "0%",
          top: "0%",
          bottom: "center",
          data: ["<10w", "10w-50w", "50w-100w", "100w-500w", ">500w"],
        },
      ],
    },
    xAxis: {
      type: [Array, Object],
      default: () => [
        {
          type: "value",
          position: "top",
          splitLine: {
            lineStyle: {
              type: "dashed",
            },
          },
        },
      ],
    },
    yAxis: {
      type: [Array, Object],
      default: () => [
        {
          type: "category",
          axisLine: { show: false },
          axisLabel: { show: false },
          axisTick: { show: false },
          splitLine: { show: false },
          data: [
            "ten",
            "nine",
            "eight",
            "seven",
            "six",
            "five",
            "four",
            "three",
            "two",
            "one",
          ],
        },
      ],
    },
    series: {
      type: [Array, Object],
      default: () => [
        {
          name: "Cost",
          type: "effectScatter",
          stack: "Total",
          label: {
            show: true,
            formatter: "{b}",
          },
          data: [
            { value: -0.07, label: labelRight },
            { value: -0.09, label: labelRight },
            0.2,
            0.44,
            { value: -0.23, label: labelRight },
            0.08,
            { value: -0.17, label: labelRight },
            0.47,
            { value: -0.36, label: labelRight },
            0.18,
          ],
        },
      ],
    },
  },
  data() {
    return {
      option: {
        title: this.title ? this.titleTransform(this.title) : [],
        tooltip: this.tooltip,
        grid: this.grid,
        legend: this.legend,
        xAxis: this.xAxis,
        yAxis: this.yAxis,
        series: this.series,
      },
      charts: null,
    };
  },
  methods: {
    titleTransform({ text, subtext, ...others }) {
      let arr = [];
      let target = {};
      if (text) {
        target = {
          text: "{style1|}{style2|}{style3|}" + text,
          textStyle: {
            fontWeight: "800",
            color: "#333",
            fontSize: 18,
            rich: {
              style1: {
                height: 20,
                width: 4,
                backgroundColor: "#2d65f2",
              },
              style2: {
                height: 20,
                width: 4,
                backgroundColor: "#b2c2ff",
              },
              style3: {
                width: 10,
              },
            },
          },
          left: 0,
          top: 0,
          ...others,
        };
        arr.push(target);
      }
      if (subtext) {
        target = {
          subtext: "{style1|}" + subtext,
          subtextStyle: {
            align: "right",
            verticalAlign: "top",
            color: "#666",
            fontSize: "18",
            rich: {
              style1: {},
            },
          },
          right: 0,
          top: -10,
          ...others,
        };
      }
      arr.push(target);
      return arr;
    },
  },
  mounted() {
    this.charts = this.$echarts.init(this.$refs.effectScatter);
    if (!this.config) {
      this.charts.setOption(this.option);
    }
    this.$watch(
      "data",
      (newVal, oldVal) => {
        let { title, ...option } = this.config(newVal);
        this.charts.setOption({
          title: title ? this.titleTransform(title) : [],
          ...option,
        });
      },
      { deep: true, immediate: true }
    );
  },
};
</script>
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

# 1.单个柱状图 + 多个饼图

查看代码详情
<template>
  <WebEffectScatter :config="getOptions" :data="data"></WebEffectScatter>
</template>

<script>
export default {
  data() {
    return {
      data: [],
    };
  },
  async created() {
    let res = await this.$api.getEffectScatter(1);
    if (res.data) {
      this.data = res.data.map((item) => ({ ...item, value: item.descript }));
    }
  },
  methods: {
    getOptions(data) {
      return {
        xAxis: {},
        yAxis: {
          scale: true,
        },
        series: [
          {
            name: "1990",
            type: "effectScatter",
            showEffectOn: "emphasis",
            rippleEffect: {
              period: 1.5, //波纹秒数
              color: "rgba(245,50,11,1)",
              brushType: "stroke", //stroke(涟漪)和fill(扩散),两种效果
              scale: 3, //波纹范围
            },
            hoverAnimation: true,
            ...{
              // 普通样式
              label: {
                show: true,
                distance: 18,
                position: "top",
                formatter: function ({ data }) {
                  if (data.time) {
                    return "{text|" + data.name + "|" + data.time + "}";
                  } else {
                    return "{text|" + data.name + "}";
                  }
                },
                textStyle: {
                  rich: {
                    text: {
                      color: "#fff",
                      backgroundColor: [
                        ["#79d1b7", "#13a46d"],
                        ["#fc755a", "#f5320b"],
                      ].map((c) => ({
                        type: "linear",
                        x: 0,
                        y: 0,
                        x2: 1,
                        y2: 0,
                        colorStops: [
                          { offset: 0, color: c[0] },
                          { offset: 1, color: c[1] },
                        ],
                      }))[0],
                      borderWidth: 1,
                      borderType: "solid",
                      borderColor: ["#b4ebde", "#fbc3b6"][0],
                      boxShadow: [
                        "0px 1px 4px #68bea0",
                        "0px 1px 4px #8d9bd1",
                      ][0],
                      fontSize: 18, //字体大小
                      padding: [6, 8, 6, 8],
                    },
                  },
                },
              },
              itemStyle: {
                color: "rgba(245,50,11,0.5)", //字体和点颜色
              },
            },
            emphasis: {
              // hover样式
              label: {
                show: true,
                distance: 18,
                position: "top",
                formatter: function ({ data }) {
                  if (data.time) {
                    return "{text|" + data.name + "|" + data.time + "}";
                  } else {
                    return "{text|" + data.name + "}";
                  }
                },
                textStyle: {
                  rich: {
                    rich: {
                      text: {
                        color: "#fff",
                        backgroundColor: [
                          ["#79d1b7", "#13a46d"],
                          ["#fc755a", "#f5320b"],
                        ].map((c) => ({
                          type: "linear",
                          x: 0,
                          y: 0,
                          x2: 1,
                          y2: 0,
                          colorStops: [
                            { offset: 0, color: c[0] },
                            { offset: 1, color: c[1] },
                          ],
                        }))[1],
                        borderWidth: 1,
                        borderType: "solid",
                        borderColor: ["#b4ebde", "#fbc3b6"][1],
                        boxShadow: [
                          "0px 1px 4px #68bea0",
                          "0px 1px 4px #8d9bd1",
                        ][1],
                        fontSize: 18, //字体大小
                        padding: [6, 8, 6, 8],
                      },
                    },
                  },
                },
              },
              itemStyle: {
                color: "rgba(245,50,11,0.8)", //字体和点颜色
              },
            },
            ...{
              // 选中样式
              selectedMode: "single",
              select: {
                label: {
                  show: true,
                  distance: 18,
                  position: "top",
                  formatter: function ({ data }) {
                    if (data.time) {
                      return "{text|" + data.name + "|" + data.time + "}";
                    } else {
                      return "{text|" + data.name + "}";
                    }
                  },
                  textStyle: {
                    rich: {
                      rich: {
                        text: {
                          color: "#fff",
                          backgroundColor: [
                            ["#79d1b7", "#13a46d"],
                            ["#fc755a", "#f5320b"],
                          ].map((c) => ({
                            type: "linear",
                            x: 0,
                            y: 0,
                            x2: 1,
                            y2: 0,
                            colorStops: [
                              { offset: 0, color: c[0] },
                              { offset: 1, color: c[1] },
                            ],
                          }))[1],
                          borderWidth: 1,
                          borderType: "solid",
                          borderColor: ["#b4ebde", "#fbc3b6"][1],
                          boxShadow: [
                            "0px 1px 4px #68bea0",
                            "0px 1px 4px #8d9bd1",
                          ][1],
                          fontSize: 18, //字体大小
                          padding: [6, 8, 6, 8],
                        },
                      },
                    },
                  },
                },
                itemStyle: {
                  color: "rgba(245,50,11,1)", //字体和点颜色
                },
              },
            },
            data,
          },
        ],
      };
    },
  },
};
</script>
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195