# 象形柱图

查看代码详情
<template>
  <div ref="pictorialBar" style="height: 100%"></div>
</template>
  
  <script>
const labelRight = {
  position: "right",
};
export default {
  name: "WebPictorialBar",
  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: "pictorialBar",
          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.pictorialBar);
    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 {
      title: {
        text: "xAxis + yAxis + 象形柱图",
      },
      option: {
        grid: {
          left: "12%",
          top: "5%",
          bottom: "12%",
          right: "8%",
        },
        xAxis: {
          data: [
            "驯鹿",
            "火箭",
            "飞机",
            "高铁",
            "轮船",
            "汽车",
            "跑步",
            "步行",
          ],
          axisTick: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "rgba(255, 129, 109, 0.1)",
              width: 1, //这里是为了突出显示加上的
            },
          },
          axisLabel: {
            textStyle: {
              color: "#999",
              fontSize: 12,
            },
          },
        },
        yAxis: [
          {
            splitNumber: 2,
            axisTick: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "rgba(255, 129, 109, 0.1)",
                width: 1, //这里是为了突出显示加上的
              },
            },
            axisLabel: {
              show: false,
            },
            splitArea: {
              areaStyle: {
                color: "rgba(255,255,255,.5)",
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(255, 129, 109, 0.1)",
                width: 0.5,
                type: "dashed",
              },
            },
          },
        ],
        series: [
          {
            name: "hill",
            type: "pictorialBar",
            barCategoryGap: "0%",
            symbol:
              "path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z",
            label: {
              show: true,
              position: "top",
              distance: 15,
              color: "#DB5E6A",
              fontWeight: "bolder",
              fontSize: 20,
            },
            itemStyle: {
              normal: {
                color: {
                  type: "linear",
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: "rgba(232, 94, 106, .8)", //  0%  处的颜色
                    },
                    {
                      offset: 1,
                      color: "rgba(232, 94, 106, .1)", //  100%  处的颜色
                    },
                  ],
                },
              },
              emphasis: {
                opacity: 1,
              },
            },
            data: [123, 60, 25, 18, 12, 9, 2, 1],
            z: 10,
          },
        ],
      },
    };
  },
  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

# 2.多个象形柱图

查看代码详情
<template>
  <WebBar v-bind="option"></WebBar>
</template>

<script>
var lineColor = "#406A92";
var labelColor = "#fff";
var fontSize = "38";
var lineWidth = 3;
export default {
  data() {
    return {
      option: {
        title: {
          text: "多个xAxis + 多个yAxis + 多个象形柱图",
        },
        backgroundColor: "#eee",
        grid: {
          left: "0%",
          right: "0%",
          top: "25%",
          bottom: "7%",
          containLabel: true,
        },
        tooltip: {
          show: true,
          trigger: "axis",
          textStyle: {
            fontSize: fontSize,
          },
        },
        legend: {
          show: true,
          x: "center",
          y: "1%",
          itemWidth: 45,
          itemHeight: 18,
          data: ["可靠率", "合格率"],
        },
        xAxis: [
          {
            type: "category",
            name: "",
            nameTextStyle: {
              fontSize: fontSize,
              color: labelColor,
              lineHeight: 90,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: lineColor,
                width: lineWidth,
              },
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: "#197584",
              },
            },
            data: ["城市", "农村"],
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "",
            nameTextStyle: {
              fontSize: fontSize,
              color: labelColor,
            },
            nameGap: 30,
            axisLine: {
              show: false,
              lineStyle: {
                color: lineColor,
                width: lineWidth,
              },
            },
            splitArea: {
              show: false,
              areaStyle: {
                color: ["rgba(128,160,176,.1)", "rgba(250,250,250,0)"],
              },
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: lineColor,
              },
            },
          },
        ],
        series: [
          {
            name: "",
            type: "pictorialBar",
            symbolSize: [50, 25],
            symbolOffset: [-75, -10],
            symbolPosition: "end",
            z: 12,
            tooltip: {
              show: false,
            },
            label: {
              normal: {
                show: false,
                position: "top",
                fontSize: fontSize,
              },
            },
            color: "#26B2E8",
            data: [99.9, 99.9],
          },
          {
            name: "",
            type: "pictorialBar",
            tooltip: {
              show: false,
            },
            symbolSize: [70, 30],
            symbolOffset: [-75, 15],
            z: 10,
            itemStyle: {
              normal: {
                color: "transparent",
                borderColor: "#2EA9E5",
                borderType: "solid",
                borderWidth: 3,
              },
            },
            data: [99.9, 99.9],
          },
          {
            name: "",
            type: "pictorialBar",
            tooltip: {
              show: false,
            },
            symbolSize: [90, 40],
            symbolOffset: [-75, 20],
            z: 10,
            itemStyle: {
              normal: {
                color: "transparent",
                borderColor: "#26B2E8",
                borderType: "solid",
                borderWidth: 4,
              },
            },
            data: [99.9, 99.9],
          },
          {
            name: "",
            type: "pictorialBar",
            symbolSize: [50, 25],
            tooltip: {
              show: false,
            },
            symbolOffset: [-75, 10],
            z: 12,
            color: "#26B2E8",
            data: [99.9, 99.9],
          },
          {
            type: "bar",
            name: "合格率",
            barWidth: "50",
            barGap: "200%",
            barCateGoryGap: "10%",
            label: {
              normal: {
                show: true,
                position: "top",
                fontSize: fontSize,
                opacity: 1,
                formatter: "{c}%",
                offset: [0, -20],
              },
            },
            itemStyle: {
              normal: {
                color: "#1E93C6",
                opacity: 1,
              },
            },
            data: [99.9, 99.9],
          },
          {
            name: "",
            type: "pictorialBar",
            symbolSize: [50, 25],
            symbolOffset: [75, -10],
            symbolPosition: "end",
            z: 12,
            tooltip: {
              show: false,
            },
            label: {
              normal: {
                show: false,
                position: "top",
                fontSize: fontSize,
                color: "#fff",
              },
            },
            color: "#20D3AB",
            data: [99.9, 99.9],
          },
          {
            name: "",
            type: "pictorialBar",
            tooltip: {
              show: false,
            },
            symbolSize: [70, 30],
            symbolOffset: [75, 15],
            z: 10,
            itemStyle: {
              normal: {
                color: "transparent",
                borderColor: "#20D3AB",
                borderType: "solid",
                borderWidth: 3,
              },
            },
            data: [99.9, 99.9],
          },
          {
            name: "",
            type: "pictorialBar",
            tooltip: {
              show: false,
            },
            symbolSize: [90, 40],
            symbolOffset: [75, 20],
            z: 10,
            itemStyle: {
              normal: {
                color: "transparent",
                borderColor: "#20D3AB",
                borderType: "solid",
                borderWidth: 4,
              },
            },
            data: [99.9, 99.9],
          },
          {
            name: "",
            type: "pictorialBar",
            symbolSize: [50, 25],
            tooltip: {
              show: false,
            },
            symbolOffset: [75, 10],
            z: 12,
            color: "#20D3AB",
            data: [99.9, 99.9],
          },
          {
            type: "bar",
            name: "可靠率",
            barWidth: "50",
            barGap: "200%",
            barCateGoryGap: "10%",
            label: {
              normal: {
                show: true,
                position: "top",
                fontSize: fontSize,
                formatter: "{c}%",
                offset: [0, -20],
              },
            },
            itemStyle: {
              normal: {
                color: "#1AAE96",
                opacity: 1,
              },
            },
            data: [99.9, 99.9],
          },
        ],
      },
    };
  },
};
</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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294