首页 > 其他分享 >echarts 3D 柱状图

echarts 3D 柱状图

时间:2024-08-23 16:05:30浏览次数:12  
标签:const color 柱状图 value shape api echarts 3D

今天开发一个驾驶舱大屏,里面有柱状图的图表,而且是3D的,搜索可视化社区,有找到一个示例【https://www.makeapie.cn/echarts_content/xH0E6KFMcG.html】,纵向的柱子,但我开发的大屏,柱状图有横向、纵向两种,所以在此记录一下,便于下一次开发。

注意:示例中是直接把echarts挂在了window上,作为全局。

效果如下:

 

 

  1 <template>
  2 <!--  横向的3D柱状图 -->
  3   <div ref="echartRef" :style="{ width: width, height: height }"></div>
  4 </template>
  5 <script>
  6 import {chunk} from "lodash";
  7 export default {
  8   props: {
  9     width: {
 10       default: "100%",
 11     },
 12     height: {
 13       default: "100%",
 14     },
 15     data: {
 16       default: () => [],//[{label.value}]
 17       type: Array,
 18     },
 19   },
 20   data() {
 21     return {
 22       myChart: null,
 23     };
 24   },
 25   computed:{
 26     dataStringify(){
 27       return JSON.stringify(this.data||[])
 28     }
 29   },
 30   watch:{
 31     dataStringify:{
 32       handler(){
 33         this.initOption()
 34       }
 35     }
 36   },
 37   mounted() {
 38     this.myChart = this.$echarts.init(this.$refs.echartRef);
 39     this.myChart.resize();
 40     window.addEventListener("resize", this.resize);
 41   },
 42   beforeDestroy() {
 43     this.myChart && this.myChart.dispose();
 44     window.removeEventListener("resize", this.resize);
 45   },
 46   methods: {
 47     resize() {
 48       this.myChart && this.myChart.resize();
 49     },
 50     initOption() {
 51       const VALUE = (this.data||[]).map(one=>one.value);
 52       const NAMES = (this.data||[]).map(one=>one.label);
 53       const CubeLeft = echarts.graphic.extendShape({
 54         shape: {
 55           x: 0,
 56           y: 0,
 57         },
 58         buildPath: function (ctx, shape) {
 59           const yAxisPoint = shape.yAxisPoint;
 60           const c0 = [shape.x, shape.y];
 61           const c1 = [shape.x + 6, shape.y - 6];
 62           const c2 = [yAxisPoint[0] + 6, yAxisPoint[1] - 6];
 63           const c3 = [yAxisPoint[0], yAxisPoint[1]];
 64           ctx
 65             .moveTo(c0[0], c0[1])
 66             .lineTo(c1[0], c1[1])
 67             .lineTo(c2[0], c2[1])
 68             .lineTo(c3[0], c3[1])
 69             .closePath();
 70         },
 71       });
 72       const CubeRight = echarts.graphic.extendShape({
 73         shape: {
 74           x: 0,
 75           y: 0,
 76         },
 77         buildPath: function (ctx, shape) {
 78           const yAxisPoint = shape.yAxisPoint;
 79           const c1 = [shape.x, shape.y];
 80           const c2 = [yAxisPoint[0], yAxisPoint[1]];
 81           const c3 = [yAxisPoint[0] + 6, yAxisPoint[1] + 12];
 82           const c4 = [shape.x + 6, shape.y + 12];
 83           ctx
 84             .moveTo(c1[0], c1[1])
 85             .lineTo(c2[0], c2[1])
 86             .lineTo(c3[0], c3[1])
 87             .lineTo(c4[0], c4[1])
 88             .closePath();
 89         },
 90       });
 91       const CubeTop = echarts.graphic.extendShape({
 92         shape: {
 93           x: 0,
 94           y: 0,
 95         },
 96         buildPath: function (ctx, shape) {
 97           const c1 = [shape.x, shape.y];
 98           const c2 = [shape.x + 6, shape.y + 12];
 99           const c3 = [shape.x + 12, shape.y + 6];
100           const c4 = [shape.x + 6, shape.y - 6];
101           ctx
102             .moveTo(c1[0], c1[1])
103             .lineTo(c2[0], c2[1])
104             .lineTo(c3[0], c3[1])
105             .lineTo(c4[0], c4[1])
106             .closePath();
107         },
108       });
109       echarts.graphic.registerShape("CubeLeft", CubeLeft);
110       echarts.graphic.registerShape("CubeRight", CubeRight);
111       echarts.graphic.registerShape("CubeTop", CubeTop);
112 
113       let option = {
114         backgroundColor: "transparent",
115         tooltip: {
116           show: true,
117           trigger: "axis",
118           axisPointer: {
119             type: "none",
120           },
121           formatter: function (params) {
122             // <span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:#1B7EF2;"></span>
123             const item = params[0];
124             return item.name + " : " + item.value;
125           },
126         },
127         grid: {
128           left: 20,
129           right: 10,
130           bottom: 10,
131           top: 0,
132           containLabel: true,
133         },
134         yAxis: {
135           type: "category",
136           data: NAMES,
137           axisLine: {
138             show: false,
139             lineStyle: {
140               color: "white",
141             },
142           },
143           offset: 0,
144           axisTick: {
145             show: false,
146           },
147           axisLabel: {
148             fontSize: 12,
149             interval: 0,
150             formatter: (label) => {
151               // 最多展示6个文字
152               // let text = chunk((label || "").split(""), 6)
153               //   .map((one) => one.join(""))
154               //   .join("\n");
155               let text = label || "";
156               text = text.substr(0, 6);
157               if (label && label.length > 6) {
158                 text += "...";
159               }
160               return text;
161             },
162           },
163         },
164         xAxis: {
165           type: "value",
166           axisLine: {
167             show: false,
168           },
169           splitLine: {
170             show: false,
171           },
172           axisTick: {
173             show: false,
174           },
175           axisLabel: {
176             show: false,
177           },
178         },
179         series: [
180           {
181             type: "custom",
182             renderItem: (params, api) => {
183               const location = api.coord([api.value(0), api.value(1)]);
184               return {
185                 type: "group",
186                 children: [
187                   {
188                     type: "CubeLeft",
189                     shape: {
190                       api,
191                       xValue: api.value(0),
192                       yValue: api.value(1),
193                       x: location[0],
194                       y: location[1],
195                       yAxisPoint: api.coord([0, api.value(1)]),
196                     },
197                     style: {
198                       fill: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
199                         {
200                           offset: 0,
201                           color: "rgba(0,0,0,0.4)",
202                         },
203                         {
204                           offset: 0.3,
205                           color: "RGBA(16, 76, 145, 0.3)",
206                         },
207                         {
208                           offset: 0.5,
209                           color: "RGBA(16, 76, 145, 0.6)",
210                         },
211                         {
212                           offset: 1,
213                           color: "RGBA(16, 76, 145, 1)",
214                         },
215                       ]),
216                     },
217                   },
218                   {
219                     type: "CubeRight",
220                     shape: {
221                       api,
222                       xValue: api.value(0),
223                       yValue: api.value(1),
224                       x: location[0],
225                       y: location[1],
226                       yAxisPoint: api.coord([0, api.value(1)]),
227                     },
228                     style: {
229                       fill: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
230                         {
231                           offset: 0,
232                           color: "rgba(0,0,0,0.4)",
233                         },
234                         {
235                           offset: 0.3,
236                           color: "rgba(27, 126, 242, 0.3)",
237                         },
238                         {
239                           offset: 0.5,
240                           color: "rgba(27, 126, 242, 0.6)",
241                         },
242                         {
243                           offset: 1,
244                           color: "rgba(27, 126, 242, 1)",
245                         },
246                       ]),
247                     },
248                   },
249                   {
250                     type: "CubeTop",
251                     shape: {
252                       api,
253                       xValue: api.value(0),
254                       yValue: api.value(1),
255                       x: location[0],
256                       y: location[1],
257                       yAxisPoint: api.coord([0, api.value(1)]),
258                     },
259                     style: {
260                       fill: new echarts.graphic.LinearGradient(1, 0, 0, 0, [
261                         {
262                           offset: 0,
263                           color: "#3B80E2",
264                         },
265                         {
266                           offset: 1,
267                           color: "#49BEE5",
268                         },
269                       ]),
270                     },
271                   },
272                 ],
273               };
274             },
275             data: VALUE,
276           },
277         ],
278       };
279       this.myChart.setOption(option);
280       this.myChart.resize();
281     },
282   },
283 };
284 </script>

 

 

 

  1 <template>
  2   <!--  纵向的柱状图 -->
  3   <div ref="echartRef" :style="{ width: width, height: height }"></div>
  4 </template>
  5 <script>
  6 import { chunk } from "lodash";
  7 export default {
  8   props: {
  9     width: {
 10       default: "100%",
 11     },
 12     height: {
 13       default: "100%",
 14     },
 15     data: {
 16       default: () => [], //[{label.value}]
 17       type: Array,
 18     },
 19   },
 20   data() {
 21     return {
 22       myChart: null,
 23     };
 24   },
 25   computed: {
 26     dataStringify() {
 27       return JSON.stringify(this.data || []);
 28     },
 29   },
 30   watch: {
 31     dataStringify: {
 32       handler() {
 33         this.initOption();
 34       },
 35     },
 36   },
 37   mounted() {
 38     this.myChart = this.$echarts.init(this.$refs.echartRef);
 39     this.myChart.resize();
 40     window.addEventListener("resize", this.resize);
 41   },
 42   beforeDestroy() {
 43     this.myChart && this.myChart.dispose();
 44     window.removeEventListener("resize", this.resize);
 45   },
 46   methods: {
 47     resize() {
 48       this.myChart && this.myChart.resize();
 49     },
 50     initOption() {
 51       const VALUE = (this.data || []).map((one) => one.value);
 52       const NAMES = (this.data || []).map((one) => one.label);
 53       // 绘制左侧面
 54       const CubeLeft = echarts.graphic.extendShape({
 55         shape: {
 56           x: 0,
 57           y: 0,
 58         },
 59         buildPath: function (ctx, shape) {
 61           const xAxisPoint = shape.xAxisPoint;
 62           const c0 = [shape.x + 2, shape.y];
 63           const c1 = [shape.x - 7, shape.y - 1];
 64           const c2 = [xAxisPoint[0] - 7, xAxisPoint[1] - 3];
 65           const c3 = [xAxisPoint[0] + 2, xAxisPoint[1]];
 66           ctx
 67             .moveTo(c0[0], c0[1])
 68             .lineTo(c1[0], c1[1])
 69             .lineTo(c2[0], c2[1])
 70             .lineTo(c3[0], c3[1])
 71             .closePath();
 72         },
 73       });
 74       // 绘制右侧面
 75       const CubeRight = echarts.graphic.extendShape({
 76         shape: {
 77           x: 0,
 78           y: 0,
 79         },
 80         buildPath: function (ctx, shape) {
 81           const xAxisPoint = shape.xAxisPoint;
 82           const c1 = [shape.x + 2, shape.y];
 83           const c2 = [xAxisPoint[0] + 2, xAxisPoint[1]];
 84           const c3 = [xAxisPoint[0] + 10, xAxisPoint[1] - 5];
 85           const c4 = [shape.x + 10, shape.y - 5];
 86           ctx
 87             .moveTo(c1[0], c1[1])
 88             .lineTo(c2[0], c2[1])
 89             .lineTo(c3[0], c3[1])
 90             .lineTo(c4[0], c4[1])
 91             .closePath();
 92         },
 93       });
 94       // 绘制顶面
 95       const CubeTop = echarts.graphic.extendShape({
 96         shape: {
 97           x: 0,
 98           y: 0,
 99         },
100         buildPath: function (ctx, shape) {
101           const c1 = [shape.x + 2, shape.y + 3];
102           const c2 = [shape.x + 10, shape.y - 5]; //右点
103           const c3 = [shape.x - 0, shape.y - 7];
104           const c4 = [shape.x - 7, shape.y - 1];
105           ctx
106             .moveTo(c1[0], c1[1])
107             .lineTo(c2[0], c2[1])
108             .lineTo(c3[0], c3[1])
109             .lineTo(c4[0], c4[1])
110             .closePath();
111         },
112       });
113       // 注册三个面图形
114       echarts.graphic.registerShape("CubeLeft", CubeLeft);
115       echarts.graphic.registerShape("CubeRight", CubeRight);
116       echarts.graphic.registerShape("CubeTop", CubeTop);
117       let option = {
118         backgroundColor: "transparent",
119         tooltip: {
120           trigger: "axis",
121           axisPointer: {
122             type: "shadow",
123           },
124           formatter: function (params) {
125             const item = params[0];
126             return item.name + " : " + item.value;
127           },
128         },
129         grid: {
130           left: 20,
131           top: 10,
132           // bottom: 30,
133           bottom:0,
134           right: 20,
135           containLabel: true,
136         },
137         xAxis: {
138           offset:12,
139           type: "category",
140           data: NAMES,
141           axisLine: {
142             show: true,
143             lineStyle: {
144               color: "#0C315D",
145             },
146           },
147           axisTick: {
148             show: false,
149           },
150           axisLabel: {
151             rotate:35,
152             textStyle: {
153               color: "#FFF",
154             },
155             fontSize: 12,
156             interval: 0,
157             formatter: (label) => {
158               let text = chunk(((label||'').substr(0,8) || "").split(""), 4)
159                 .map((one) => one.join(""))
160                 .join("\n");
161               return text;
162             },
163           },
164         },
165         yAxis: [{
166           type: "value",
167           axisLine: {
168             show: false,
169             lineStyle: {
170               color: "#7ebaf2",
171             },
172           },
173           splitLine: {
174             show: false,
175           },
176           axisTick: {
177             show: false,
178           },
179           splitNumber: 4,
180           axisLabel: {
181             fontSize: 12,
182             textStyle: {
183               color: "#FFF",
184             },
185           },
186         }],
187         series: [
188           {
189             type: "custom",
190             renderItem: (params, api) => {
191               let cubeLeftStyle = "";
192               let cubeRightStyle = "";
193               let cubeTopStyle = "";
194               // 颜色
195               cubeLeftStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
196                 {
197                   offset: 0,
198                   color: "rgba(27, 126, 242, 0.8)",
199                 },
200                 {
201                   offset: 0.5,
202                   color: "rgba(27, 126, 242, 0.6)",
203                 },
204                 {
205                   offset: 0.8,
206                   color: "rgba(27, 126, 242, 0.3)",
207                 },
208                 {
209                   offset: 1,
210                   color: "rgba(0,0,0,0.2)",
211                 },
212               ]);
213               cubeRightStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
214                 {
215                   offset: 0,
216                   color: "rgba(23,176,255,1)",
217                 },
218                 {
219                   offset: 0.5,
220                   color: "rgba(23,176,255, 0.6)",
221                 },
222                 {
223                   offset: 0.8,
224                   color: "rgba(23,176,255, 0.3)",
225                 },
226                 {
227                   offset: 1,
228                   color: "rgba(0,0,0,0.2)",
229                 },
230               ]);
231               cubeTopStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
232                 {
233                   offset: 0,
234                   color: "rgba(27, 126, 242, 1)",
235                 },
236                 {
237                   offset: 1,
238                   color: "rgba(27, 126, 242, 1)",
239                 },
240               ]);
241               //颜色end
242               const location = api.coord([api.value(0), api.value(1)]);
243               return {
244                 type: "group",
245                 children: [
246                   {
247                     type: "CubeLeft",
248                     shape: {
249                       api,
250                       xValue: api.value(0),
251                       yValue: api.value(1),
252                       x: location[0],
253                       y: location[1],
254                       xAxisPoint: api.coord([api.value(0), 0]),
255                     },
256                     style: {
257                       fill: cubeLeftStyle,
258                     },
259                   },
260                   {
261                     type: "CubeRight",
262                     shape: {
263                       api,
264                       xValue: api.value(0),
265                       yValue: api.value(1),
266                       x: location[0],
267                       y: location[1],
268                       xAxisPoint: api.coord([api.value(0), 0]),
269                     },
270                     style: {
271                       fill: cubeRightStyle,
272                     },
273                   },
274                   {
275                     type: "CubeTop",
276                     shape: {
277                       api,
278                       xValue: api.value(0),
279                       yValue: api.value(1),
280                       x: location[0],
281                       y: location[1],
282                       xAxisPoint: api.coord([api.value(0), 0]),
283                     },
284                     style: {
285                       fill: cubeTopStyle,
286                     },
287                   },
288                 ],
289               };
290             },
291             data: VALUE,
292           },
293         ],
294       };
295 
296       this.myChart.setOption(option);
297       this.myChart.resize();
298     },
299   },
300 };
301 </script>

 

标签:const,color,柱状图,value,shape,api,echarts,3D
From: https://www.cnblogs.com/grow-up-up/p/18376158

相关文章

  • 代码随想录算法训练营第 49 天 |LeetCode42 接雨水 LeetCode84 柱状图中最大面积矩形
    代码随想录算法训练营Day49代码随想录算法训练营第49天|LeetCode42接雨水LeetCode84柱状图中最大面积矩形目录代码随想录算法训练营前言LeetCode42接雨水LeetCode84柱状图中最大面积矩形一、LeetCode42接雨水1.题目链接2.思路3.题解二、LeetCode84柱状......
  • 探索风扇产品模型的3D可视化魅力
    在这个科技日新月异的时代,每一个细微的创新都能为我们的生活带来前所未有的便捷与享受。今天,就让我们一起踏入一场视觉与科技的盛宴,探索风扇产品模型如何通过3D可视化技术,重新定义家居生活的舒适与美学。 想象一下,在炎炎夏日,你无需亲临实体店,只需轻点鼠标或滑动指尖,就能全方位......
  • 微信小程序echarts-饼状图
    为了兼容小程序Canvas,我们提供了一个小程序的组件,用这种方式可以方便地使用ECharts。首先,下载GitHub上的 ecomfe/echarts-for-weixin 项目。一、封装pieChart组件pieChart.wxml:<viewclass="container"><ec-canvasid="mychart-dom-bar"class='mychart-bar'can......
  • 企业市值排名3D可视化,重塑商业版图新维度
    在这个数据驱动的时代,每一个数字背后都蕴藏着无限的可能与机遇。企业市值,作为衡量企业综合实力与市场认可度的关键指标,其动态变化不仅是投资者关注的焦点,也是全球商业竞争格局的晴雨表。 当枯燥的数据表格被转化为生动的3D场景,全球数千家企业的市值排名不再只是冷冰冰的数字列......
  • 前端3d动画-----平移 transform: translate3d()
     必须加这个属性:transform-style:preserve-3d;  perspective:900px; 设置了景深才能感到近大远小的感觉       <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible&quo......
  • R绘图(06)——带errorbar的柱状图
      每次找R绘图美化都很麻烦,索性自己写个笔记慢慢补充绘图美化的指令###生成数据####设置种子以获得可重复的结果set.seed(222)#生成字符序列"AAAABBBB"char_sequence<-c("A","A","A","A","B","B","B","B")#......
  • Vue3使用VueEcharts实现图表
    这里以折线图为例,其他以此类推编写折线图子组件 <scriptlang="ts"setup>import{useDark}from'@vueuse/core'importmomentfrom'moment'import{computed}from'vue'importVueEchartsfrom'vue-echarts'constprops......
  • 未来购物新境界:商品样机3D展示可视化引领潮流
    在这个日新月异的数字时代,科技的每一次飞跃都在深刻改变着我们的生活方式,尤其是购物体验。从传统的实体店选购到线上商城的便捷浏览,再到如今商品样机3D展示可视化的兴起,消费者正逐步踏入一个前所未有的沉浸式购物新时代。 想象一下,无需亲临现场,只需轻点鼠标或滑动屏幕,就能360度......
  • “风色幻想XX:交错轨迹游戏卡顿:从解决d3dx9_xx.dll缺失开始——风色幻想XX优化指南
    《风色幻想XX:交错轨迹》作为一款深受玩家喜爱的角色扮演游戏,以其丰富的剧情、独特的角色设定和精美的画面赢得了广泛的赞誉。然而,在游戏过程中,部分玩家可能会遇到游戏卡顿的问题,这不仅影响了游戏体验,还可能让玩家错失精彩的剧情和战斗瞬间。在众多导致游戏卡顿的因素中,d3dx9_xx......
  • vue---echarts环形图
    1、完整代码直接可以cv <template><divid="main1"></div></template><script>import*asechartsfrom'echarts';//import{mapState}from'vuex';//import{Alarm_Device}from'../utils/api.......