3dPie.vue 8.7 KB

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