3dPie.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <template>
  2. <div id="pie_echart" ref="echart"></div>
  3. </template>
  4. <script>
  5. let colors = [
  6. "RGBA(235, 184, 86, 1)",
  7. "RGBA(100, 184, 255, 1)",
  8. "RGBA(71, 203, 129, 1)",
  9. ];
  10. let option = {
  11. legend: {
  12. tooltip: {
  13. show: true,
  14. },
  15. orient: "vertical",
  16. itemWidth: 12, // 设置宽度
  17. itemHeight: 12, // 设置高度
  18. data: [],
  19. bottom: "40%",
  20. right: "10%",
  21. itemGap: 10,
  22. textStyle: {
  23. color: "#fff",
  24. fontSize: 14,
  25. },
  26. textStyle: {
  27. rich: {
  28. name: {
  29. color: "#BCD3E5",
  30. fontSize: 14,
  31. },
  32. value: {
  33. color: "#64DAFF",
  34. fontSize: 14,
  35. },
  36. unit: {
  37. color: "#BCD3E5",
  38. fontSize: 14,
  39. },
  40. },
  41. },
  42. },
  43. animation: true,
  44. backgroundColor: "rgba(0,0,0,0)",
  45. labelLine: {
  46. show: false,
  47. lineStyle: {
  48. color: "#7BC0CB",
  49. },
  50. },
  51. label: {
  52. show: false,
  53. position: "outside",
  54. formatter: "{d}%",
  55. color: "inherit",
  56. },
  57. xAxis3D: {
  58. min: -1,
  59. max: 1,
  60. },
  61. yAxis3D: {
  62. min: -1,
  63. max: 1,
  64. },
  65. zAxis3D: {
  66. min: -1,
  67. max: 1,
  68. },
  69. grid3D: {
  70. show: false,
  71. boxHeight: 1,
  72. top: "-5%",
  73. left: "-25%",
  74. // bottom: '-80%',
  75. // environment: '#021041',
  76. viewControl: {
  77. distance: 180,
  78. alpha: 25,
  79. beta: 20,
  80. // autoRotate: true, // 自动旋转
  81. },
  82. },
  83. series: [],
  84. };
  85. export default {
  86. name: "pie_echart",
  87. components: {},
  88. props: {
  89. unit: {
  90. type: String,
  91. },
  92. },
  93. data() {
  94. return {
  95. myChart: null,
  96. };
  97. },
  98. methods: {
  99. setOptions(data) {
  100. if (!this.myChart) {
  101. // var dom = document.getElementById("pie_echart");
  102. this.myChart = echarts.init(this.$refs.echart);
  103. }
  104. this.initEchartXzqh(data);
  105. },
  106. initEchartXzqh(optionsData) {
  107. let _this = this;
  108. option.legend.formatter = function (name) {
  109. const sItem = optionsData.find((item) =>
  110. `${item.name}`.includes(`${name}`)
  111. );
  112. if (sItem) {
  113. return `{name|${name}} {value|${sItem.value}} {unit|${_this.$props.unit}} `;
  114. // return name + 'sItem.value';
  115. } else {
  116. return name;
  117. }
  118. };
  119. const series = this.getPie3D(optionsData, 0.6);
  120. option.series = series;
  121. // series.push({
  122. // name: "pie2d",
  123. // type: "pie",
  124. // label: {
  125. // normal: {
  126. // show: false, //设为false
  127. // position: "center", //center表示文本显示位置为内部
  128. // },
  129. // opacity: 1,
  130. // fontSize: 13,
  131. // lineHeight: 20,
  132. // textStyle: {
  133. // fontSize: 22,
  134. // },
  135. // },
  136. // labelLine: {
  137. // length: 20,
  138. // length2: 40,
  139. // },
  140. // startAngle: -30, //起始角度,支持范围[0, 360]。
  141. // clockwise: false, //饼图的扇区是否是顺时针排布。上述这两项配置主要是为了对齐3d的样式
  142. // radius: ["30%", "55%"],
  143. // center: ["52%", "38%"],
  144. // // 没啥用
  145. // data: [
  146. // {
  147. // name: "征收",
  148. // value: 20,
  149. // itemStyle: {
  150. // // opacity: 0.8,
  151. // color: "#3b8584",
  152. // },
  153. // },
  154. // {
  155. // name: "收购",
  156. // value: 50,
  157. // itemStyle: {
  158. // // opacity: 0.8,
  159. // color: "#64d9e0",
  160. // },
  161. // },
  162. // {
  163. // name: "回收",
  164. // value: 30,
  165. // itemStyle: {
  166. // // opacity: 0.8,
  167. // color: "#dac254",
  168. // },
  169. // },
  170. // ],
  171. // itemStyle: {
  172. // opacity: 0,
  173. // },
  174. // });
  175. // // 准备待返回的配置项,把准备好的 legendData、series 传入。
  176. let total = optionsData.reduce(function (prev, cur, index, arr) {
  177. return prev + cur.value;
  178. }, 0); //数据总和
  179. this.myChart.setOption(option);
  180. },
  181. // 生成扇形的曲面参数方程,用于 series-surface.parametricEquation
  182. getParametricEquation(
  183. startRatio,
  184. endRatio,
  185. isSelected,
  186. isHovered,
  187. k,
  188. height
  189. ) {
  190. // 计算
  191. let midRatio = (startRatio + endRatio) / 2;
  192. let startRadian = startRatio * Math.PI * 2;
  193. let endRadian = endRatio * Math.PI * 2;
  194. let midRadian = midRatio * Math.PI * 2;
  195. // 如果只有一个扇形,则不实现选中效果。
  196. if (startRatio === 0 && endRatio === 1) {
  197. isSelected = false;
  198. }
  199. // 通过扇形内径/外径的值,换算出辅助参数 k(默认值 1/3)
  200. k = typeof k !== "undefined" ? k : 1 / 3;
  201. // 计算选中效果分别在 x 轴、y 轴方向上的位移(未选中,则位移均为 0)
  202. let offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
  203. let offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
  204. // 计算高亮效果的放大比例(未高亮,则比例为 1)
  205. let hoverRate = isHovered ? 1.05 : 1;
  206. // 返回曲面参数方程
  207. return {
  208. u: {
  209. min: -Math.PI,
  210. max: Math.PI * 3,
  211. step: Math.PI / 32,
  212. },
  213. v: {
  214. min: 0,
  215. max: Math.PI * 2,
  216. step: Math.PI / 20,
  217. },
  218. x: function (u, v) {
  219. if (u < startRadian) {
  220. return (
  221. offsetX +
  222. Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate
  223. );
  224. }
  225. if (u > endRadian) {
  226. return (
  227. offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate
  228. );
  229. }
  230. return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
  231. },
  232. y: function (u, v) {
  233. if (u < startRadian) {
  234. return (
  235. offsetY +
  236. Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate
  237. );
  238. }
  239. if (u > endRadian) {
  240. return (
  241. offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate
  242. );
  243. }
  244. return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
  245. },
  246. z: function (u, v) {
  247. if (u < -Math.PI * 0.5) {
  248. return Math.sin(u);
  249. }
  250. if (u > Math.PI * 2.5) {
  251. return Math.sin(u);
  252. }
  253. return Math.sin(v) > 0 ? 1 * height : -1;
  254. },
  255. };
  256. },
  257. // 生成模拟 3D 饼图的配置项
  258. getPie3D(pieData, internalDiameterRatio) {
  259. let series = [];
  260. let sumValue = 0;
  261. let startValue = 0;
  262. let endValue = 0;
  263. let legendData = [];
  264. let k =
  265. typeof internalDiameterRatio !== "undefined"
  266. ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio)
  267. : 1 / 3;
  268. // 为每一个饼图数据,生成一个 series-surface 配置
  269. for (let i = 0; i < pieData.length; i++) {
  270. sumValue += pieData[i].value;
  271. let seriesItem = {
  272. name:
  273. typeof pieData[i].name === "undefined"
  274. ? `series${i}`
  275. : pieData[i].name,
  276. type: "surface",
  277. parametric: true,
  278. wireframe: {
  279. show: false,
  280. },
  281. pieData: pieData[i],
  282. pieStatus: {
  283. selected: false,
  284. hovered: false,
  285. k: k,
  286. },
  287. };
  288. seriesItem.itemStyle = { color: colors[i], opacity: 0.8 };
  289. series.push(seriesItem);
  290. }
  291. // 使用上一次遍历时,计算出的数据和 sumValue,调用 getParametricEquation 函数,
  292. // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation,也就是实现每一个扇形。
  293. for (let i = 0; i < series.length; i++) {
  294. endValue = startValue + series[i].pieData.value;
  295. series[i].pieData.startRatio = startValue / sumValue;
  296. series[i].pieData.endRatio = endValue / sumValue;
  297. series[i].parametricEquation = this.getParametricEquation(
  298. series[i].pieData.startRatio,
  299. series[i].pieData.endRatio,
  300. false,
  301. false,
  302. k,
  303. series[i].pieData.value
  304. );
  305. startValue = endValue;
  306. legendData.push(series[i].name);
  307. }
  308. option.legend.data = legendData;
  309. return series;
  310. },
  311. },
  312. mounted() {},
  313. };
  314. </script>
  315. <style lang="scss" scoped>
  316. .pie_echart {
  317. width: 100%;
  318. height: 100%;
  319. }
  320. </style>