Overhaul3D.vue 12 KB

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