当前位置:   article > 正文

e-charts 3D饼图_import 'echarts-gl' // 3d图表库

import 'echarts-gl' // 3d图表库

一、安装相应插件

  1.  "echarts": "^5.1.2"
  2.  "echarts-gl": "^2.0.8"

二、 引入

  1. import Vue from 'vue'

  2. import * as echarts from 'echarts'

  3. import 'echarts-gl' // 3d图表库

三、html代码 

  1. <template>
  2. <div class="chart-container">
  3. <div class="chart" ref="chart"></div>
  4. <!-- 底座背景 -->
  5. <div class="bg"></div>
  6. </div>
  7. </template>
  8. <script>
  9. import * as echarts from "echarts";
  10. import "echarts-gl";
  11. import { getPie3D, getParametricEquation } from "./chart.js"; //工具类js,页面路径自己修改
  12. const color = ["#3cd6b7", "#fe9326", "#124fb7", "#383893"];
  13. export default {
  14. name: "chart",
  15. data() {
  16. return {
  17. optionData: [
  18. {
  19. name: "A类企业",
  20. value: 176,
  21. },
  22. {
  23. name: "B类企业",
  24. value: 288,
  25. },
  26. {
  27. name: "C类企业",
  28. value: 88,
  29. },
  30. {
  31. name: "D类企业",
  32. value: 78,
  33. },
  34. ],
  35. statusChart: null,
  36. option: {},
  37. };
  38. },
  39. created() {
  40. //实际开发中,先调后台返回数据
  41. this.initData()
  42. },
  43. mounted() {
  44. //根据窗口变化自动调节图表大小
  45. const that = this;
  46. window.onresize = function () {
  47. that.changeSize();
  48. };
  49. },
  50. methods: {
  51. initData () {
  52. //调用后台
  53. .$axios.post().then(data)
  54. //后台数据给this.optionData,非常重要
  55. this.optionData=data;
  56. this.setLabel(data)
  57. },
  58. // 初始化label样式
  59. setLabel() {
  60. this.optionData.forEach((item, index) => {
  61. item.itemStyle = {
  62. color: color[index],
  63. };
  64. item.label = {
  65. normal: {
  66. show: true,
  67. color: color[index],
  68. position:'right',
  69. // distance:-10,
  70. offset:[0, 3] ,
  71. formatter: [
  72. "{d|{d}%}",
  73. "————",
  74. // '{c|{c}}{b|台}',
  75. "{b|{b}}",
  76. ].join("\n"), // 用\n来换行
  77. rich: {
  78. b: {
  79. // color: '#fff',
  80. lineHeight: 25,
  81. align: "left",
  82. color: color[index],
  83. },
  84. c: {
  85. fontSize: 22,
  86. // color: '#fff',
  87. textShadowColor: "#1c90a6",
  88. textShadowOffsetX: 0,
  89. textShadowOffsetY: 2,
  90. textShadowBlur: 5,
  91. color: color[index],
  92. },
  93. d: {
  94. color: color[index],
  95. align: "left",
  96. },
  97. },
  98. },
  99. };
  100. item.labelLine = {
  101. normal: {
  102. length2: 30,
  103. lineStyle: {
  104. width: 1,
  105. color: color[index],
  106. },
  107. },
  108. };
  109. });
  110. //初始化图标
  111. this.initChart();
  112. },
  113. // 图表初始化
  114. initChart() {
  115. this.statusChart = echarts.init(this.$refs.chart);
  116. // 传入数据生成 option, 构建3d饼状图, 参数工具文件已经备注的很详细
  117. this.option = getPie3D(this.optionData, 0.6, 180, 26, 18, 1);
  118. this.statusChart.setOption(this.option);
  119. // 是否需要label指引线,如果要就添加一个透明的2d饼状图并调整角度使得labelLine和3d的饼状图对齐,并再次setOption
  120. this.option.series.push({
  121. name: "信用评价", //自己根据场景修改
  122. backgroundColor: "transparent",
  123. type: "pie",
  124. label: {
  125. opacity: 1,
  126. fontSize: 13,
  127. lineHeight: 20,
  128. },
  129. startAngle: -40, // 起始角度,支持范围[0, 360]。
  130. clockwise: false, // 饼图的扇区是否是顺时针排布。上述这两项配置主要是为了对齐3d的样式
  131. radius: ["20%", "60%"],
  132. center: ["50%", "50%"],
  133. data: this.optionData,
  134. itemStyle: {
  135. opacity: 0, //这里必须是0,不然2d的图会覆盖在表面
  136. },
  137. });
  138. this.statusChart.setOption(this.option);
  139. this.bindListen(this.statusChart);
  140. },
  141. // 监听鼠标事件,实现饼图选中效果(单选),近似实现高亮(放大)效果。
  142. // optionName是防止有多个图表进行定向option传递,单个图表可以不传,默认是opiton
  143. bindListen(myChart, optionName = "option") {
  144. let selectedIndex = "";
  145. let hoveredIndex = "";
  146. // 监听点击事件,实现选中效果(单选)
  147. // myChart.on("click", (params) => {
  148. // console.log(this[optionName].series[params.seriesIndex],'this[optionName].series[params.seriesIndex].pieStatus');
  149. // // 从 option.series 中读取重新渲染扇形所需的参数,将是否选中取反。
  150. // const isSelected =
  151. // !this[optionName].series[params.seriesIndex].pieStatus.selected;
  152. // const isHovered =
  153. // this[optionName].series[params.seriesIndex].pieStatus.hovered;
  154. // const k = this[optionName].series[params.seriesIndex].pieStatus.k;
  155. // const startRatio =
  156. // this[optionName].series[params.seriesIndex].pieData.startRatio;
  157. // const endRatio =
  158. // this[optionName].series[params.seriesIndex].pieData.endRatio;
  159. // // 如果之前选中过其他扇形,将其取消选中(对 option 更新)
  160. // if (selectedIndex !== "" && selectedIndex !== params.seriesIndex) {
  161. // this[optionName].series[selectedIndex].parametricEquation =
  162. // getParametricEquation(
  163. // this[optionName].series[selectedIndex].pieData.startRatio,
  164. // this[optionName].series[selectedIndex].pieData.endRatio,
  165. // false,
  166. // false,
  167. // k,
  168. // this[optionName].series[selectedIndex].pieData.value
  169. // );
  170. // this[optionName].series[selectedIndex].pieStatus.selected = false;
  171. // }
  172. // // 对当前点击的扇形,执行选中/取消选中操作(对 option 更新)
  173. // this[optionName].series[params.seriesIndex].parametricEquation =
  174. // getParametricEquation(
  175. // startRatio,
  176. // endRatio,
  177. // isSelected,
  178. // isHovered,
  179. // k,
  180. // this[optionName].series[params.seriesIndex].pieData.value
  181. // );
  182. // this[optionName].series[params.seriesIndex].pieStatus.selected =
  183. // isSelected;
  184. // // 如果本次是选中操作,记录上次选中的扇形对应的系列号 seriesIndex
  185. // selectedIndex = isSelected ? params.seriesIndex : null;
  186. // // 使用更新后的 option,渲染图表
  187. // myChart.setOption(this[optionName]);
  188. // });
  189. // 监听 mouseover,近似实现高亮(放大)效果
  190. myChart.on("mouseover", (params) => {
  191. // 准备重新渲染扇形所需的参数
  192. let isSelected;
  193. let isHovered;
  194. let startRatio;
  195. let endRatio;
  196. let k;
  197. // 如果触发 mouseover 的扇形当前已高亮,则不做操作
  198. if (hoveredIndex === params.seriesIndex) {
  199. // 否则进行高亮及必要的取消高亮操作
  200. } else {
  201. // 如果当前有高亮的扇形,取消其高亮状态(对 option 更新)
  202. if (hoveredIndex !== "") {
  203. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 false
  204. isSelected =
  205. this[optionName].series[hoveredIndex].pieStatus.selected;
  206. isHovered = false;
  207. startRatio =
  208. this[optionName].series[hoveredIndex].pieData.startRatio;
  209. endRatio = this[optionName].series[hoveredIndex].pieData.endRatio;
  210. k = this[optionName].series[hoveredIndex].pieStatus.k;
  211. // 对当前点击的扇形,执行取消高亮操作(对 option 更新)
  212. this[optionName].series[hoveredIndex].parametricEquation =
  213. getParametricEquation(
  214. startRatio,
  215. endRatio,
  216. isSelected,
  217. isHovered,
  218. k,
  219. this[optionName].series[hoveredIndex].pieData.value
  220. );
  221. this[optionName].series[hoveredIndex].pieStatus.hovered = isHovered;
  222. // 将此前记录的上次选中的扇形对应的系列号 seriesIndex 清空
  223. hoveredIndex = "";
  224. }
  225. // 如果触发 mouseover 的扇形不是透明圆环,将其高亮(对 option 更新)
  226. if (
  227. params.seriesName !== "mouseoutSeries" &&
  228. params.seriesName !== "信用评价"
  229. ) {
  230. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 true
  231. isSelected =
  232. this[optionName].series[params.seriesIndex].pieStatus.selected;
  233. isHovered = true;
  234. startRatio =
  235. this[optionName].series[params.seriesIndex].pieData.startRatio;
  236. endRatio =
  237. this[optionName].series[params.seriesIndex].pieData.endRatio;
  238. k = this[optionName].series[params.seriesIndex].pieStatus.k;
  239. // 对当前点击的扇形,执行高亮操作(对 option 更新)
  240. this[optionName].series[params.seriesIndex].parametricEquation =
  241. getParametricEquation(
  242. startRatio,
  243. endRatio,
  244. isSelected,
  245. isHovered,
  246. k,
  247. this[optionName].series[params.seriesIndex].pieData.value + 60
  248. );
  249. this[optionName].series[params.seriesIndex].pieStatus.hovered =
  250. isHovered;
  251. // 记录上次高亮的扇形对应的系列号 seriesIndex
  252. hoveredIndex = params.seriesIndex;
  253. }
  254. // 使用更新后的 option,渲染图表
  255. myChart.setOption(this[optionName]);
  256. }
  257. });
  258. // 修正取消高亮失败的 bug
  259. myChart.on("globalout", () => {
  260. // 准备重新渲染扇形所需的参数
  261. let isSelected;
  262. let isHovered;
  263. let startRatio;
  264. let endRatio;
  265. let k;
  266. if (hoveredIndex !== "") {
  267. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 true
  268. isSelected = this[optionName].series[hoveredIndex].pieStatus.selected;
  269. isHovered = false;
  270. k = this[optionName].series[hoveredIndex].pieStatus.k;
  271. startRatio = this[optionName].series[hoveredIndex].pieData.startRatio;
  272. endRatio = this[optionName].series[hoveredIndex].pieData.endRatio;
  273. // 对当前点击的扇形,执行取消高亮操作(对 option 更新)
  274. this[optionName].series[hoveredIndex].parametricEquation =
  275. getParametricEquation(
  276. startRatio,
  277. endRatio,
  278. isSelected,
  279. isHovered,
  280. k,
  281. this[optionName].series[hoveredIndex].pieData.value
  282. );
  283. this[optionName].series[hoveredIndex].pieStatus.hovered = isHovered;
  284. // 将此前记录的上次选中的扇形对应的系列号 seriesIndex 清空
  285. hoveredIndex = "";
  286. }
  287. // 使用更新后的 option,渲染图表
  288. myChart.setOption(this[optionName]);
  289. });
  290. },
  291. // 自适应宽高
  292. changeSize() {
  293. this.statusChart.resize();
  294. },
  295. },
  296. };
  297. </script>
  298. <style lang='less' scoped>
  299. .chart-container {
  300. position: relative;
  301. width: 100%;
  302. height: 100%;
  303. .chart {
  304. z-index: 1;
  305. }
  306. .chart,
  307. .bg {
  308. width: 100%;
  309. height: 100%;
  310. }
  311. .bg {
  312. position: absolute;
  313. bottom: -10px;
  314. left: 50%;
  315. z-index: 0;
  316. height: calc(200 / 1080 * 100vh);
  317. width: calc(240 / 1920 * 100vw);
  318. background: no-repeat center;
  319. background-image: url("~@/assets/imgs/piebg.png");
  320. background-size: 100% 100%;
  321. transform: translateX(-50%);
  322. }
  323. }
  324. </style>

四、echat.js代码

  1. /**
  2. * 绘制3d图
  3. * @param pieData 总数据
  4. * @param internalDiameterRatio:透明的空心占比
  5. * @param distance 视角到主体的距离
  6. * @param alpha 旋转角度
  7. * @param pieHeight 立体的高度
  8. * @param opacity 饼或者环的透明度
  9. */
  10. const getPie3D = (
  11. pieData,
  12. internalDiameterRatio,
  13. distance,
  14. alpha,
  15. pieHeight,
  16. opacity = 1
  17. ) => {
  18. const series = [];
  19. let sumValue = 0;
  20. let startValue = 0;
  21. let endValue = 0;
  22. let legendData = [];
  23. let legendBfb = [];
  24. const k = 1 - internalDiameterRatio;
  25. pieData.sort((a, b) => {
  26. return b.value - a.value;
  27. });
  28. // 为每一个饼图数据,生成一个 series-surface 配置
  29. for (let i = 0; i < pieData.length; i++) {
  30. sumValue += pieData[i].value;
  31. const seriesItem = {
  32. name:
  33. typeof pieData[i].name === "undefined" ? `series${i}` : pieData[i].name,
  34. type: "surface",
  35. parametric: true,
  36. wireframe: {
  37. show: false,
  38. },
  39. pieData: pieData[i],
  40. pieStatus: {
  41. selected: false,
  42. hovered: false,
  43. k: k,
  44. },
  45. center: ["10%", "50%"],
  46. };
  47. if (typeof pieData[i].itemStyle !== "undefined") {
  48. const itemStyle = {};
  49. itemStyle.color =
  50. typeof pieData[i].itemStyle.color !== "undefined"
  51. ? pieData[i].itemStyle.color
  52. : opacity;
  53. itemStyle.opacity =
  54. typeof pieData[i].itemStyle.opacity !== "undefined"
  55. ? pieData[i].itemStyle.opacity
  56. : opacity;
  57. seriesItem.itemStyle = itemStyle;
  58. }
  59. series.push(seriesItem);
  60. }
  61. // 使用上一次遍历时,计算出的数据和 sumValue,调用 getParametricEquation 函数,
  62. // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation,也就是实现每一个扇形。
  63. legendData = [];
  64. legendBfb = [];
  65. for (let i = 0; i < series.length; i++) {
  66. endValue = startValue + series[i].pieData.value;
  67. series[i].pieData.startRatio = startValue / sumValue;
  68. series[i].pieData.endRatio = endValue / sumValue;
  69. series[i].parametricEquation = getParametricEquation(
  70. series[i].pieData.startRatio,
  71. series[i].pieData.endRatio,
  72. false,
  73. false,
  74. k,
  75. series[i].pieData.value
  76. );
  77. startValue = endValue;
  78. const bfb = fomatFloat(series[i].pieData.value / sumValue, 4);
  79. legendData.push({
  80. name: series[i].name,
  81. value: bfb,
  82. });
  83. legendBfb.push({
  84. name: series[i].name,
  85. value: bfb,
  86. });
  87. }
  88. const boxHeight = getHeight3D(series, pieHeight); // 通过pieHeight设定3d饼/环的高度,单位是px
  89. // 准备待返回的配置项,把准备好的 legendData、series 传入。
  90. const option = {
  91. legend: {
  92. show: false,
  93. data: legendData,
  94. orient: "vertical",
  95. left: 10,
  96. top: 10,
  97. itemGap: 10,
  98. textStyle: {
  99. color: "#A1E2FF",
  100. },
  101. icon: "circle",
  102. formatter: function (param) {
  103. const item = legendBfb.filter((item) => item.name === param)[0];
  104. const bfs = fomatFloat(item.value * 100, 2) + "%";
  105. return `${item.name} ${bfs}`;
  106. },
  107. },
  108. labelLine: {
  109. show: true,
  110. lineStyle: {
  111. color: "#fff",
  112. },
  113. },
  114. label: {
  115. show: true,
  116. position: "outside",
  117. formatter: "{b} \n{c} {d}%",
  118. },
  119. tooltip: {
  120. backgroundColor: "#033b77",
  121. borderColor: "#21f2c4",
  122. textStyle: {
  123. color: "#fff",
  124. fontSize: 13,
  125. },
  126. formatter: (params) => {
  127. if (
  128. params.seriesName !== "mouseoutSeries" &&
  129. params.seriesName !== "信用评价"
  130. ) {
  131. // console.log(option.series,params.seriesName,'option.series[params.seriesIndex].pieData');
  132. const bfb = (
  133. (option.series[params.seriesIndex].pieData.endRatio -
  134. option.series[params.seriesIndex].pieData.startRatio) *
  135. 100
  136. ).toFixed(2);
  137. return (
  138. `${params.seriesName}<br/>` +
  139. `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color};"></span>` +
  140. `${bfb}%`
  141. );
  142. }
  143. },
  144. },
  145. xAxis3D: {
  146. min: -1,
  147. max: 1,
  148. },
  149. yAxis3D: {
  150. min: -1,
  151. max: 1,
  152. },
  153. zAxis3D: {
  154. min: -1,
  155. max: 1,
  156. },
  157. grid3D: {
  158. show: false,
  159. boxHeight: boxHeight, // 圆环的高度
  160. //更改饼图位置
  161. top:'-10%',
  162. left:'-10%',
  163. viewControl: {
  164. // 3d效果可以放大、旋转等,请自己去查看官方配置
  165. alpha, // 角度
  166. distance, // 调整视角到主体的距离,类似调整zoom
  167. rotateSensitivity: 0, // 设置为0无法旋转
  168. zoomSensitivity: 0, // 设置为0无法缩放
  169. panSensitivity: 0, // 设置为0无法平移
  170. autoRotate: false, // 自动旋转
  171. },
  172. },
  173. series: series,
  174. };
  175. return option;
  176. };
  177. /**
  178. * 生成扇形的曲面参数方程,用于 series-surface.parametricEquation
  179. */
  180. const getParametricEquation = (
  181. startRatio,
  182. endRatio,
  183. isSelected,
  184. isHovered,
  185. k,
  186. h
  187. ) => {
  188. // 计算
  189. const midRatio = (startRatio + endRatio) / 2;
  190. const startRadian = startRatio * Math.PI * 2;
  191. const endRadian = endRatio * Math.PI * 2;
  192. const midRadian = midRatio * Math.PI * 2;
  193. // 如果只有一个扇形,则不实现选中效果。
  194. if (startRatio === 0 && endRatio === 1) {
  195. isSelected = false;
  196. }
  197. // 通过扇形内径/外径的值,换算出辅助参数 k(默认值 1/3
  198. k = typeof k !== "undefined" ? k : 1 / 3;
  199. // 计算选中效果分别在 x 轴、y 轴方向上的位移(未选中,则位移均为 0
  200. const offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
  201. const offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
  202. // 计算高亮效果的放大比例(未高亮,则比例为 1
  203. const hoverRate = isHovered ? 1.05 : 1;
  204. // 返回曲面参数方程
  205. return {
  206. u: {
  207. min: -Math.PI,
  208. max: Math.PI * 3,
  209. step: Math.PI / 32,
  210. },
  211. v: {
  212. min: 0,
  213. max: Math.PI * 2,
  214. step: Math.PI / 20,
  215. },
  216. x: function (u, v) {
  217. if (u < startRadian) {
  218. return (
  219. offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate
  220. );
  221. }
  222. if (u > endRadian) {
  223. return (
  224. offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate
  225. );
  226. }
  227. return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
  228. },
  229. y: function (u, v) {
  230. if (u < startRadian) {
  231. return (
  232. offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate
  233. );
  234. }
  235. if (u > endRadian) {
  236. return (
  237. offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate
  238. );
  239. }
  240. return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
  241. },
  242. z: function (u, v) {
  243. if (u < -Math.PI * 0.5) {
  244. return Math.sin(u);
  245. }
  246. if (u > Math.PI * 2.5) {
  247. return Math.sin(u) * h * 0.1;
  248. }
  249. return Math.sin(v) > 0 ? 1 * h * 0.1 : -1;
  250. },
  251. };
  252. };
  253. /**
  254. * 获取3d丙图的最高扇区的高度
  255. */
  256. const getHeight3D = (series, height) => {
  257. series.sort((a, b) => {
  258. return b.pieData.value - a.pieData.value;
  259. });
  260. return (height * 25) / series[0].pieData.value;
  261. };
  262. /**
  263. * 格式化浮点数
  264. */
  265. const fomatFloat = (num, n) => {
  266. let f = parseFloat(num);
  267. if (isNaN(f)) {
  268. return false;
  269. }
  270. f = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); // n 幂
  271. let s = f.toString();
  272. let rs = s.indexOf(".");
  273. // 判定如果是整数,增加小数点再补0
  274. if (rs < 0) {
  275. rs = s.length;
  276. s += ".";
  277. }
  278. while (s.length <= rs + n) {
  279. s += "0";
  280. }
  281. return s;
  282. };
  283. export { getPie3D, getParametricEquation };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/82886
推荐阅读
相关标签
  

闽ICP备14008679号