PieChart3D.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <script setup>
  2. import { onMounted, ref, watch } from "vue";
  3. import * as echarts from "echarts";
  4. import "echarts-gl";
  5. const props = defineProps({
  6. glxsList: Array,
  7. })
  8. let chart, option;
  9. const pieChart3d = ref();
  10. const legendItems = ref([
  11. {
  12. name: '有管理单位(物业)',
  13. color: '#5ef8c9'
  14. },
  15. {
  16. name: '有管理单位(非物业)',
  17. color: '#5e92f8'
  18. },
  19. {
  20. name: '居民自主管理',
  21. color: '#F4F85E'
  22. },
  23. {
  24. name: '街道社区代管',
  25. color: '#FC2626'
  26. },
  27. {
  28. name: '其他管理形式',
  29. color: '#1162fe'
  30. },
  31. {
  32. name: '无管理主体',
  33. color: '#FF7A00'
  34. },
  35. ])
  36. function showValue(name) {
  37. return props.glxsList.find(item => item.name === name)?.value
  38. }
  39. function initChart() {
  40. chart = echarts.init(pieChart3d.value);
  41. option = getPie3D(props.glxsList.map((item) => ({
  42. ...item,
  43. label: {
  44. show: true
  45. },
  46. itemStyle: {
  47. color: (() => {
  48. const temp = legendItems.value.find(i => i.name === item.name)
  49. return temp.color
  50. })()
  51. }
  52. })),
  53. 0.75
  54. );
  55. // 监听鼠标事件,实现饼图选中效果(单选),近似实现高亮(放大)效果。
  56. let hoveredIndex = "";
  57. // 准备重新渲染扇形所需的参数
  58. let isSelected;
  59. let isHovered;
  60. let startRatio;
  61. let endRatio;
  62. let k;
  63. // 监听 mouseover,近似实现高亮(放大)效果
  64. chart.on("mouseover", function (params) {
  65. // 如果触发 mouseover 的扇形当前已高亮,则不做操作
  66. if (hoveredIndex === params.seriesIndex) {
  67. return;
  68. // 否则进行高亮及必要的取消高亮操作
  69. } else {
  70. // 如果当前有高亮的扇形,取消其高亮状态(对 option 更新)
  71. if (hoveredIndex !== "") {
  72. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 false。
  73. isSelected = option.series[hoveredIndex].pieStatus.selected;
  74. isHovered = false;
  75. startRatio = option.series[hoveredIndex].pieData.startRatio;
  76. endRatio = option.series[hoveredIndex].pieData.endRatio;
  77. k = option.series[hoveredIndex].pieStatus.k;
  78. // 对当前点击的扇形,执行取消高亮操作(对 option 更新)
  79. option.series[hoveredIndex].parametricEquation = getParametricEquation(
  80. startRatio,
  81. endRatio,
  82. isSelected,
  83. isHovered,
  84. k,
  85. option.series[hoveredIndex].pieData.value
  86. );
  87. option.series[hoveredIndex].pieStatus.hovered = isHovered;
  88. // 将此前记录的上次选中的扇形对应的系列号 seriesIndex 清空
  89. hoveredIndex = "";
  90. }
  91. // 如果触发 mouseover 的扇形不是透明圆环,将其高亮(对 option 更新)
  92. if (params.seriesName !== "mouseoutSeries") {
  93. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 true。
  94. isSelected = option.series[params.seriesIndex].pieStatus.selected;
  95. isHovered = true;
  96. startRatio = option.series[params.seriesIndex].pieData.startRatio;
  97. endRatio = option.series[params.seriesIndex].pieData.endRatio;
  98. k = option.series[params.seriesIndex].pieStatus.k;
  99. // 对当前点击的扇形,执行高亮操作(对 option 更新)
  100. option.series[params.seriesIndex].parametricEquation =
  101. getParametricEquation(
  102. startRatio,
  103. endRatio,
  104. isSelected,
  105. isHovered,
  106. k,
  107. option.series[params.seriesIndex].pieData.value + 5
  108. );
  109. option.series[params.seriesIndex].pieStatus.hovered = isHovered;
  110. // 记录上次高亮的扇形对应的系列号 seriesIndex
  111. hoveredIndex = params.seriesIndex;
  112. }
  113. // 使用更新后的 option,渲染图表
  114. chart.setOption(option);
  115. }
  116. });
  117. // 修正取消高亮失败的 bug
  118. chart.on("globalout", function () {
  119. if (hoveredIndex !== "") {
  120. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 true。
  121. isSelected = option.series[hoveredIndex].pieStatus.selected;
  122. isHovered = false;
  123. k = option.series[hoveredIndex].pieStatus.k;
  124. startRatio = option.series[hoveredIndex].pieData.startRatio;
  125. endRatio = option.series[hoveredIndex].pieData.endRatio;
  126. // 对当前点击的扇形,执行取消高亮操作(对 option 更新)
  127. option.series[hoveredIndex].parametricEquation = getParametricEquation(
  128. startRatio,
  129. endRatio,
  130. isSelected,
  131. isHovered,
  132. k,
  133. option.series[hoveredIndex].pieData.value
  134. );
  135. option.series[hoveredIndex].pieStatus.hovered = isHovered;
  136. // 将此前记录的上次选中的扇形对应的系列号 seriesIndex 清空
  137. hoveredIndex = "";
  138. }
  139. // 使用更新后的 option,渲染图表
  140. chart.setOption(option);
  141. });
  142. if (option && typeof option === "object") {
  143. chart.setOption(option);
  144. }
  145. }
  146. onMounted(() => {
  147. initChart();
  148. });
  149. watch(() => props.glxsList, () => {
  150. initChart();
  151. })
  152. // 生成扇形的曲面参数方程
  153. function getParametricEquation(
  154. startRatio,
  155. endRatio,
  156. isSelected,
  157. isHovered,
  158. k
  159. ) {
  160. // 计算
  161. const midRatio = (startRatio + endRatio) / 2;
  162. const startRadian = startRatio * Math.PI * 2;
  163. const endRadian = endRatio * Math.PI * 2;
  164. const midRadian = midRatio * Math.PI * 2;
  165. // 如果只有一个扇形,则不实现选中效果。
  166. if (startRatio === 0 && endRatio === 1) {
  167. // eslint-disable-next-line no-param-reassign
  168. isSelected = false;
  169. }
  170. // 通过扇形内径/外径的值,换算出辅助参数 k(默认值 1/3)
  171. // eslint-disable-next-line no-param-reassign
  172. k = typeof k !== "undefined" ? k : 1 / 3;
  173. // 计算选中效果分别在 x 轴、y 轴方向上的位移(未选中,则位移均为 0)
  174. const offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
  175. const offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
  176. // 计算高亮效果的放大比例(未高亮,则比例为 1)
  177. const hoverRate = isHovered ? 1.05 : 1;
  178. // 返回曲面参数方程
  179. return {
  180. u: {
  181. min: -Math.PI,
  182. max: Math.PI * 3,
  183. step: Math.PI / 32,
  184. },
  185. v: {
  186. min: 0,
  187. max: Math.PI * 2,
  188. step: Math.PI / 20,
  189. },
  190. x(u, v) {
  191. if (u < startRadian) {
  192. return (
  193. offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate
  194. );
  195. }
  196. if (u > endRadian) {
  197. return (
  198. offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate
  199. );
  200. }
  201. return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
  202. },
  203. y(u, v) {
  204. if (u < startRadian) {
  205. return (
  206. offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate
  207. );
  208. }
  209. if (u > endRadian) {
  210. return (
  211. offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate
  212. );
  213. }
  214. return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
  215. },
  216. z(u, v) {
  217. if (u < -Math.PI * 0.5) {
  218. return Math.sin(u);
  219. }
  220. if (u > Math.PI * 2.5) {
  221. return Math.sin(u);
  222. }
  223. return Math.sin(v) > 0 ? 1 : -1;
  224. },
  225. };
  226. }
  227. // 生成模拟 3D 饼图的配置项
  228. function getPie3D(pieData, internalDiameterRatio) {
  229. const series = [];
  230. // 总和
  231. let sumValue = 0;
  232. let startValue = 0;
  233. let endValue = 0;
  234. const legendData = [];
  235. const k =
  236. typeof internalDiameterRatio !== "undefined"
  237. ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio)
  238. : 1 / 3;
  239. // 为每一个饼图数据,生成一个 series-surface 配置
  240. for (let i = 0; i < pieData.length; i += 1) {
  241. sumValue += pieData[i].value;
  242. // console.log(pieData[i].name);
  243. const seriesItem = {
  244. name:
  245. typeof pieData[i].name === "undefined" ? `series${i}` : pieData[i].name,
  246. type: "surface",
  247. parametric: true,
  248. wireframe: {
  249. show: false,
  250. },
  251. pieData: pieData[i],
  252. pieStatus: {
  253. selected: false,
  254. hovered: false,
  255. k,
  256. }
  257. };
  258. if (typeof pieData[i].itemStyle !== "undefined") {
  259. const { itemStyle } = pieData[i];
  260. typeof pieData[i].itemStyle.color !== "undefined"
  261. ? (itemStyle.color = pieData[i].itemStyle.color)
  262. : null;
  263. typeof pieData[i].itemStyle.opacity !== "undefined"
  264. ? (itemStyle.opacity = pieData[i].itemStyle.opacity)
  265. : null;
  266. seriesItem.itemStyle = itemStyle;
  267. }
  268. series.push(seriesItem);
  269. }
  270. // 使用上一次遍历时,计算出的数据和 sumValue,调用 getParametricEquation 函数,
  271. // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation,也就是实现每一个扇形。
  272. for (let i = 0; i < series.length; i += 1) {
  273. endValue = startValue + series[i].pieData.value;
  274. series[i].pieData.startRatio = startValue / sumValue;
  275. series[i].pieData.endRatio = endValue / sumValue;
  276. series[i].parametricEquation = getParametricEquation(
  277. series[i].pieData.startRatio,
  278. series[i].pieData.endRatio,
  279. false,
  280. false,
  281. k,
  282. // ,使除了第一个之外的值都是10
  283. series[i].pieData.value
  284. );
  285. startValue = endValue;
  286. legendData.push(series[i].name);
  287. }
  288. // 准备待返回的配置项,把准备好的 legendData、series 传入。
  289. const option = {
  290. tooltip: {
  291. formatter: (params) => {
  292. if (params.seriesName !== "mouseoutSeries") {
  293. return `${
  294. params.seriesName
  295. }<br/><span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${
  296. params.color
  297. };"></span>${option.series[params.seriesIndex].pieData?.value}`;
  298. }
  299. return "";
  300. },
  301. backgroundColor: "rgba(0,224,255,0.46)",
  302. borderColor: "rgba(0,213,255, .6)",
  303. textStyle: {
  304. color: '#fff',
  305. }
  306. },
  307. title: [
  308. {
  309. text: "单位:栋",
  310. top: 8,
  311. left: 10,
  312. textStyle: {
  313. fontSize: 12,
  314. color: "#5ed3f8",
  315. },
  316. },
  317. ],
  318. xAxis3D: {
  319. min: -1,
  320. max: 1,
  321. },
  322. yAxis3D: {
  323. min: -1,
  324. max: 1,
  325. },
  326. zAxis3D: {
  327. min: -1,
  328. max: 'dataMax',
  329. },
  330. grid3D: {
  331. show: false,
  332. boxHeight: 15,
  333. top: "-15%",
  334. left: "-17%",
  335. viewControl: {
  336. // 3d效果可以放大、旋转等,请自己去查看官方配置
  337. alpha: 27,
  338. beta: 70, //旋转角度
  339. rotateSensitivity: 1,
  340. zoomSensitivity: 0,
  341. panSensitivity: 0,
  342. distance: 150,
  343. },
  344. // 后处理特效可以为画面添加高光、景深、环境光遮蔽(SSAO)、调色等效果。可以让整个画面更富有质感。
  345. postEffect: {
  346. // 配置这项会出现锯齿,请自己去查看官方配置有办法解决
  347. enable: false,
  348. bloom: {
  349. enable: true,
  350. bloomIntensity: 0.1,
  351. },
  352. SSAO: {
  353. enable: true,
  354. quality: "medium",
  355. radius: 2,
  356. },
  357. },
  358. },
  359. series,
  360. };
  361. return option;
  362. }
  363. </script>
  364. <template>
  365. <div class="container">
  366. <div ref="pieChart3d" class="chart"></div>
  367. <ul class="legend">
  368. <li v-for="(legend,idx) in legendItems" @click="$emit('click-item', legend.name)" :style="{
  369. '--bg': legend.color
  370. }" :key="idx">{{ legend.name }}({{ showValue(legend.name) }})</li>
  371. </ul>
  372. </div>
  373. </template>
  374. <style scoped lang="less">
  375. .container {
  376. width: 440px;
  377. height: 264px;
  378. position: relative;
  379. }
  380. .container .legend {
  381. position: absolute;
  382. right: 10px;
  383. top: 55px;
  384. display: flex;
  385. flex-direction: column;
  386. gap: 10px;
  387. z-index: 500;
  388. & > li {
  389. list-style: none;
  390. cursor: pointer;
  391. }
  392. & > li::before {
  393. content: "";
  394. display: inline-block;
  395. margin-right: 5px;
  396. width: 10px;
  397. height: 10px;
  398. border-radius: 2px;
  399. background-color: var(--bg);
  400. }
  401. }
  402. .container::after {
  403. content: "";
  404. position: absolute;
  405. width: 290px;
  406. height: 198px;
  407. background: url("../assets/images/1.png") no-repeat;
  408. background-size: 100% 100%;
  409. left: calc(50% - 145px);
  410. bottom: 1%;
  411. left: 0;
  412. }
  413. .container::before {
  414. content: attr(data-num);
  415. position: absolute;
  416. font-size: 18px;
  417. font-family: YouSheBiaoTiHei;
  418. color: #ffffff;
  419. line-height: 42px;
  420. text-shadow: 0px 2px 6px rgba(0, 0, 0, 0.5);
  421. left: 50%;
  422. transform: translate(-50%);
  423. top: 30%;
  424. }
  425. .chart {
  426. width: 400px;
  427. height: 100%;
  428. position: relative;
  429. z-index: 1;
  430. }
  431. </style>