# 漏斗图

查看代码详情
<template>
  <div ref="bar" style="height: 100%"></div>
</template>
  
  <script>
const labelRight = {
  position: "right",
};
export default {
  name: "WebBar",
  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: "bar",
          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.bar);
    if (!this.config) {
      this.charts.setOption(this.option);
    }
  },
  watch: {
    data: {
      handler(val) {
        let { title, ...option } = this.config(val);
        this.charts.setOption({
          title: title ? this.titleTransform(title) : [],
          ...option,
        });
      },
      deep: 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
213

# 1.多个漏斗

查看代码详情
<template>
  <div ref="pie" style="height: 100%"></div>
</template>

<script>
export default {
  data() {
    return {
      option: {
        title: {
          text: "漏斗图",
        },
        legend: {
          data: ["Show", "Click", "Visit", "Inquiry", "Order"],
        },
        series: [
          {
            name: "Expected",
            type: "funnel",
            left: "10%",
            width: "80%",
            maxSize: "80%",
            minSize: "60%",
            gap: 20,
            labelLine: {
              show: false,
            },
            itemStyle: {
              color: ({ data: { color } }) => {
                return new this.$echarts.graphic.LinearGradient(
                  1,
                  0,
                  0,
                  0,
                  color.map((item, index) => ({
                    offset: 0.5 * index,
                    color: item,
                  })),
                  false
                );
              },
            },
            label: {
              show: false,
            },
            data: [
              {
                value: 70,
                name: "A",
                color: [
                  "#fff",
                  "#5470c6",
                  "#fff",
                ],
              },
              {
                value: 70,
                name: "B",
                color: [
                  "#fff",
                  "#91cc75",
                  "#fff",
                ],
              },
              {
                value: 70,
                name: "C",
                color: [
                  "rgba(0,244,255,0)",
                  "#fac858",
                  "rgba(0,244,255,0)",
                ],
              },
              {
                value: 70,
                name: "D",
                color: [
                  "rgba(0,244,255,0)",
                  "# ee6666",
                  "rgba(0,244,255,0)",
                ],
              },
              {
                value: 70,
                name: "E",
                color: [
                  "rgba(0,244,255,0)",
                  "#73c0de",
                  "rgba(0,244,255,0)",
                ],
              },
            ],
          },
          {
            name: "Actual",
            type: "funnel",
            left: "36%",
            width: "30%",
            maxSize: "80%",
            minSize: "60%",
            gap: 20,
            labelLine: {
              show: false,
            },
            data: [
              { value: 20, name: "A" },
              { value: 30, name: "B" },
              { value: 50, name: "C" },
              { value: 60, name: "D" },
              { value: 80, name: "E" },
            ],
          },
          {
            name: "Actual",
            type: "funnel",
            left: "36%",
            width: "30%",
            maxSize: "80%",
            minSize: "60%",
            gap: 20,
            label: {
              position: "left",
            },
            labelLine: {
              show: false,
            },
            data: [
              { value: 20, name: "A" },
              { value: 30, name: "B" },
              { value: 50, name: "C" },
              { value: 60, name: "D" },
              { value: 80, name: "E" },
            ],
          },
        ],
      },
    };
  },
  mounted() {
    let charts = this.$echarts.init(this.$refs.pie);
    charts.setOption(this.option);
  },
};
</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

# 1.柱状图 1

查看代码详情
<template>
  <div ref="pie" style="height: 100%"></div>
</template>

<script>
export default {
  data() {
    return {
      option: {
        backgroundColor: {
          type: "linear",
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: "#0c0d2b",
            },
            {
              offset: 0.5,
              color: "#0a0c3d",
            },
            {
              offset: 1,
              color: "#111629",
            },
          ],
        },
        title: {
          text: "转化率",
          subtext: "转化的百分率或分率",
          link: "https://gallery.echartsjs.com/explore.html?u=bd-1841183165&type=work",
          target: "blank",
          x: "center",
          top: "10",
          textStyle: {
            color: "#FFF",
            fontSize: 16,
          },
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c}%",
        },
        legend: {
          data: ["A", "B", "C", "D", "E"],
          x: "center",
          y: "92%",
          textStyle: {
            color: "#FFF",
          },
        },
        color: [
          "#c2c1bd",
          "#00a1e5",
          "#23c768",
          "#e5ce10",
          "#ff7e00",
          "#fe0000",
        ],
        series: [
          {
            name: "TIT",
            type: "funnel",
            left: "center",
            width: "90%",
            sort: "ascending",
            label: {
              normal: {
                formatter: "{b}",
              },
            },
            labelLine: {
              normal: {
                show: true,
                length: 30,
              },
            },
            itemStyle: {
              normal: {
                opacity: 0.3,
              },
            },
            tooltip: {
              show: false,
            },
            data: [
              {
                value: 10,
                name: "A",
              },
              {
                value: 20,
                name: "B",
              },
              {
                value: 40,
                name: "C",
              },
              {
                value: 60,
                name: "D",
              },
              {
                value: 80,
                name: "E",
              },
              {
                value: 100,
                name: "F",
              },
            ],
          },
          {
            name: "TIT",
            type: "funnel",
            left: "center",
            width: "80%",
            maxSize: "80%",
            sort: "ascending",
            label: {
              normal: {
                position: "inside",
                formatter: "{c}%",
                textStyle: {
                  color: "#fff",
                  fontSize: 14,
                },
              },
              emphasis: {
                position: "inside",
                formatter: "{b}: {c}%",
              },
            },
            itemStyle: {
              normal: {
                opacity: 0.8,
                borderColor: "rgba(12, 13, 43, .9)",
                borderWidth: 3,
                shadowBlur: 5,
                shadowOffsetX: 0,
                shadowOffsetY: 5,
                shadowColor: "rgba(0, 0, 0, .6)",
              },
            },
            data: [
              {
                value: 2,
                name: "A",
              },
              {
                value: 5,
                name: "B",
              },
              {
                value: 12,
                name: "C",
              },
              {
                value: 18,
                name: "D",
              },
              {
                value: 25,
                name: "E",
              },
              {
                value: 40,
                name: "F",
              },
            ],
          },
        ],
      },
    };
  },
  mounted() {
    let charts = this.$echarts.init(this.$refs.pie);
    charts.setOption(this.option);
  },
};
</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