1
0

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