前端札记 前端札记
首页
    • 前端
    • 服务器
    • 其他
  • 分类
  • 归档
  • 标签
GitHub (opens new window)

Xin

面朝大海,春暖花开
首页
    • 前端
    • 服务器
    • 其他
  • 分类
  • 归档
  • 标签
GitHub (opens new window)
  • 折线、柱状图

    • axis点击事件
    • 常用配置项
    • 柱状图3D
  • 饼图

  • 地图

  • 《Echarts》笔记
  • 折线、柱状图
Xin
2024-07-17

柱状图3D

<template>
  <div ref="chart" style="width: 800px; height: 450px"></div>
</template>

<script>
export default {
  data() {
    return {
      xData: ['2017', '2018', '2019', '2020', '2021'],
      yData: [150, 126, 260, 220, 184],
    };
  },
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const wid = 30;
      const w1 = Math.sin(Math.PI / 6) * wid;
      const w2 = Math.sin(Math.PI / 3) * wid;
      const snapHeight = wid / 2;

      const CubeLeft = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          const xAxisPoint = shape.xAxisPoint;
          const c0 = [shape.x, shape.y];
          const c1 = [shape.x - w2, shape.y];
          const c2 = [shape.x - w2, xAxisPoint[1]];
          const c3 = [shape.x, xAxisPoint[1]];
          ctx
            .moveTo(c0[0], c0[1])
            .lineTo(c1[0], c1[1])
            .lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .closePath();
        },
      });
      // 绘制右侧面
      const CubeRight = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          const xAxisPoint = shape.xAxisPoint;
          const c1 = [shape.x, shape.y];
          const c2 = [shape.x, xAxisPoint[1]];
          const c3 = [shape.x + w1, xAxisPoint[1] - w2 + snapHeight];
          const c4 = [shape.x + w1, shape.y - w2 + snapHeight];
          ctx
            .moveTo(c1[0], c1[1])
            .lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .lineTo(c4[0], c4[1])
            .closePath();
        },
      });
      // 绘制顶面
      const CubeTop = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          //
          const c1 = [shape.x, shape.y];
          const c2 = [shape.x + w1, shape.y - w2 + snapHeight]; //右点
          const c3 = [shape.x - w2 + w1, shape.y - w2 + snapHeight];
          const c4 = [shape.x - w2, shape.y];
          ctx
            .moveTo(c1[0], c1[1])
            .lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .lineTo(c4[0], c4[1])
            .closePath();
        },
      });

      echarts.graphic.registerShape('CubeLeft', CubeLeft);
      echarts.graphic.registerShape('CubeRight', CubeRight);
      echarts.graphic.registerShape('CubeTop', CubeTop);
      const chart = echarts.init(this.$refs.chart);
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          backgroundColor: 'rgba(9, 24, 48, 0.5)',
          borderColor: 'rgba(75, 253, 238, 0.4)',
          textStyle: {
            color: '#CFE3FC',
          },
          borderWidth: 1,
        },
        grid: {
          top: '10%',
          left: '5%',
          bottom: '5%',
          right: '5%',
          containLabel: true,
        },
        xAxis: {
          type: 'category',
          data: this.xData,
          axisLine: {
            show: true,
            lineStyle: {
              color: '#3e6f8e',
              width: 1,
            },
          },
          axisTick: {
            show: false,
            length: 9,
            alignWithLabel: false,
            lineStyle: {
              color: '#AAA',
            },
          },
          axisLabel: {
            fontSize: 14,
            margin: 10,
            color: '#000',
          },
          splitLine: {
            show: false,
            lineStyle: {
              color: '#000',
              opacity: 0.2,
              width: 1,
            },
          },
        },
        yAxis: {
          name: '单位:万元',
          type: 'value',
          nameTextStyle: {
            color: '#000',
            fontSize: 16,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#3e6f8e',
              width: 1,
            },
          },
          axiosTick: {
            show: false,
          },
          axisLabel: {
            color: '#000',
            fontSize: 14,
            margin: 10,
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#000',
              opacity: 0.2,
              width: 1,
            },
          },
          nameGap: 20,
        },
        series: [
          {
            type: 'bar',
            label: {
              normal: {
                show: true,
                position: 'top',
                fontSize: 16,
                color: '#000',
                offset: [0, -10],
              },
            },
            tooltip: {
              show: false,
            },
            itemStyle: {
              color: 'transparent',
            },
            data: this.yData,
          },
          {
            type: 'custom',
            renderItem: (params, api) => {
              const location = api.coord([api.value(0), api.value(1)]);
              location[0] = location[0] + wid * 0;
              const xlocation = api.coord([api.value(0), 0]);
              xlocation[0] = xlocation[0] + wid * 0;
              return {
                type: 'group',
                children: [
                  {
                    type: 'CubeLeft',
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: xlocation,
                    },
                    style: {
                      fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {
                          offset: 0,
                          color: '#059de6',
                        },
                        {
                          offset: 1,
                          color: '#059de6',
                        },
                      ]),
                    },
                  },
                  {
                    type: 'CubeRight',
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: xlocation,
                    },
                    style: {
                      fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {
                          offset: 0,
                          color: '#254193',
                        },
                        {
                          offset: 1,
                          color: '#254193',
                        },
                      ]),
                    },
                  },
                  {
                    type: 'CubeTop',
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: xlocation,
                    },
                    style: {
                      fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {
                          offset: 0,
                          color: '#17e0fe',
                        },
                        {
                          offset: 1,
                          color: '#17e0fe',
                        },
                      ]),
                    },
                  },
                ],
              };
            },
            color: 'blue',
            data: this.yData,
          },
        ],
      };
      chart.setOption(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
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
显示代码 复制代码 复制代码
编辑 (opens new window)
#Echarts
上次更新: 2024-07-17 16:05:08
常用配置项
单环图

← 常用配置项 单环图→

Theme by Vdoing | Copyright © 2021-2025
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式