Overhaul3D.vue 12 KB

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