ThreeDCharts3.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <template>
  2. <div class="water-eval-container">
  3. <div class="cityGreenLand-charts" id="cityGreenLand-charts2">
  4. </div>
  5. <div id="number_2" class=num>
  6. <div id="number_3" class=num> </div>
  7. <div id="number_4" class=num></div>
  8. </div>
  9. </div>
  10. </template>
  11. <script>
  12. import echarts from 'echarts'
  13. import 'echarts-gl';
  14. export default {
  15. name: "cityGreenLand",
  16. components: {},
  17. data() {
  18. return {
  19. population: [ // -- 人口情况
  20. {
  21. name: "流动人口",
  22. num: 501,
  23. rate: 22.77
  24. },
  25. {
  26. name: "常住人口",
  27. num: 1699,
  28. rate: 77.23
  29. }
  30. ],
  31. optionData: [{
  32. name: '流动人口',
  33. value: 22.77,
  34. itemStyle: {
  35. color: '#5BF96D',
  36. }
  37. }, {
  38. name: '常住人口',
  39. value: 77.23,
  40. itemStyle: {
  41. color: '#0B61C5'
  42. }
  43. }],
  44. }
  45. },
  46. mounted() {
  47. let clear = setInterval(() => {
  48. if (this.$store.getters.getSzdata.code === 10000&&this.$store.getters.getDaZhongdata.data&&this.$store.getters.getDaZhongdata.data.population) {
  49. this.population = this.$store.getters.getDaZhongdata.data.population
  50. console.log("this.population",this.population)
  51. this.optionData.filter(result => result.name == '常住人口')[0].value = +this.population.filter(result =>
  52. result.name ==
  53. '常住人口')[0].rate;
  54. this.optionData.filter(result => result.name == '流动人口')[0].value = +this.population.filter(result =>
  55. result
  56. .name == '流动人口')[0].rate;
  57. this.init();
  58. clearInterval(clear)
  59. }
  60. }, 500);
  61. },
  62. methods: {
  63. //数据匹配
  64. initData() {
  65. },
  66. init() {
  67. //构建3d饼状图
  68. let myChart = echarts.init(document.getElementById('cityGreenLand-charts2'));
  69. // 传入数据生成 option
  70. this.option = this.getPie3D(this.optionData, 0.8);
  71. myChart.setOption(this.option);
  72. //是否需要label指引线,如果要就添加一个透明的2d饼状图并调整角度使得labelLine和3d的饼状图对齐,并再次setOption
  73. this.option.series.push({
  74. name: 'pie2d',
  75. type: 'pie',
  76. labelLine: {
  77. length: 5,
  78. length2: 10
  79. },
  80. label: {
  81. formatter: '{b|{b}}\n{c|{c}}%',
  82. rich: {
  83. b: {
  84. color: '#fff',
  85. fontSize: 12,
  86. },
  87. c: {
  88. fontSize: 25,
  89. }
  90. }
  91. },
  92. startAngle: -20, //起始角度,支持范围[0, 360]。
  93. clockwise: false, //饼图的扇区是否是顺时针排布。上述这两项配置主要是为了对齐3d的样式
  94. radius: ['20%', '50%'],
  95. center: ['50%', '50%'],
  96. data: this.optionData,
  97. itemStyle: {
  98. opacity: 0
  99. }
  100. });
  101. myChart.setOption(this.option);
  102. let optionData = this.optionData;
  103. myChart.on('mouseover', function (params) {
  104. let num = optionData.filter(result => result.name == params.seriesName);
  105. if (num.length != 0) {
  106. document.getElementById("number_3").innerText = num[0].value + "%\n"
  107. document.getElementById("number_4").innerText = num[0].name
  108. if(num[0].name=="常住人口"){
  109. document.getElementById("number_3").style.color="#0B61C5"
  110. }else{
  111. document.getElementById("number_3").style.color="#5BF96D"
  112. }
  113. }
  114. });
  115. // this.bindListen(myChart);
  116. },
  117. getPie3D(pieData, internalDiameterRatio) {
  118. //internalDiameterRatio:透明的空心占比
  119. let that = this;
  120. let series = [];
  121. let sumValue = 0;
  122. let startValue = 0;
  123. let endValue = 0;
  124. let legendData = [];
  125. let legendBfb = [];
  126. let k = 1 - internalDiameterRatio;
  127. pieData.sort((a, b) => {
  128. return (b.value - a.value);
  129. });
  130. // 为每一个饼图数据,生成一个 series-surface 配置
  131. for (let i = 0; i < pieData.length; i++) {
  132. sumValue += pieData[i].value;
  133. let seriesItem = {
  134. name: typeof pieData[i].name === 'undefined' ? `series${i}` : pieData[i].name,
  135. type: 'surface',
  136. parametric: true,
  137. wireframe: {
  138. show: false
  139. },
  140. pieData: pieData[i],
  141. pieStatus: {
  142. selected: false,
  143. hovered: true,
  144. k: k
  145. },
  146. center: ['10%', '50%']
  147. };
  148. if (typeof pieData[i].itemStyle != 'undefined') {
  149. let itemStyle = {};
  150. typeof pieData[i].itemStyle.color != 'undefined' ? itemStyle.color = pieData[i].itemStyle.color : null;
  151. typeof pieData[i].itemStyle.opacity != 'undefined' ? itemStyle.opacity = pieData[i].itemStyle.opacity :
  152. null;
  153. seriesItem.itemStyle = itemStyle;
  154. }
  155. series.push(seriesItem);
  156. }
  157. // 使用上一次遍历时,计算出的数据和 sumValue,调用 getParametricEquation 函数,
  158. // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation,也就是实现每一个扇形。
  159. legendData = [];
  160. legendBfb = [];
  161. for (let i = 0; i < series.length; i++) {
  162. endValue = startValue + series[i].pieData.value;
  163. series[i].pieData.startRatio = startValue / sumValue;
  164. series[i].pieData.endRatio = endValue / sumValue;
  165. // series[i].parametricEquation = this.getParametricEquation(series[i].pieData.startRatio, series[i].pieData.endRatio,
  166. // false, false, k, series[i].pieData.value);
  167. series[i].parametricEquation = this.getParametricEquation(series[i].pieData.startRatio, series[i].pieData
  168. .endRatio,
  169. false, false, k, 20);
  170. startValue = endValue;
  171. let bfb = that.fomatFloat(series[i].pieData.value / sumValue, 4);
  172. legendData.push({
  173. name: series[i].name,
  174. value: bfb
  175. });
  176. legendBfb.push({
  177. name: series[i].name,
  178. value: bfb
  179. });
  180. }
  181. let boxHeight = this.getHeight3D(series, 20); //通过传参设定3d饼/环的高度,26代表26px
  182. //let boxHeight =0.03;
  183. // 准备待返回的配置项,把准备好的 legendData、series 传入。
  184. let option = {
  185. // legend: {
  186. // data: legendData,
  187. // orient: 'horizontal',
  188. // left: 80,
  189. // bottom: 10,
  190. // itemGap: 10,
  191. // textStyle: {
  192. // color: '#A1E2FF',
  193. // },
  194. // show: true,
  195. // icon: "circle",
  196. // // formatter: function(param) {
  197. // // let item = legendBfb.filter(item => item.name == param)[0];
  198. // // let bfs = that.fomatFloat(item.value * 100, 2) + "%";
  199. // // return `${item.name} ${bfs}`;
  200. // // }
  201. // },
  202. labelLine: {
  203. show: true,
  204. lineStyle: {
  205. color: '#7BC0CB'
  206. // color: '#ffffff'
  207. }
  208. },
  209. label: {
  210. show: true,
  211. position: 'outside',
  212. rich: {
  213. b: {
  214. // color: '#7BC0CB',
  215. color: '#ffffff',
  216. fontSize: 15,
  217. lineHeight: 15
  218. },
  219. c: {
  220. fontSize: 18,
  221. },
  222. },
  223. // formatter: '{b|{b} \n}{c|{c}}{b| 亩}',
  224. formatter: '{b|{b} \n}{c|{c}}{b| }',
  225. },
  226. // tooltip: {
  227. // formatter: params => {
  228. // if (params.seriesName !== 'mouseoutSeries' && params.seriesName !== 'pie2d') {
  229. // let bfb = ((option.series[params.seriesIndex].pieData.endRatio - option.series[params.seriesIndex].pieData.startRatio) *
  230. // 100).toFixed(2);
  231. // return `${params.seriesName}<br/>` +
  232. // `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color};"></span>` +
  233. // `${ bfb }%`;
  234. // }
  235. // }
  236. // },
  237. xAxis3D: {
  238. min: -1,
  239. max: 1
  240. },
  241. yAxis3D: {
  242. min: -1,
  243. max: 1
  244. },
  245. zAxis3D: {
  246. min: -1,
  247. max: 1
  248. },
  249. grid3D: {
  250. show: false,
  251. boxHeight: boxHeight, //圆环的高度
  252. viewControl: { //3d效果可以放大、旋转等,请自己去查看官方配置
  253. alpha: 70, //角度
  254. distance: 300, //调整视角到主体的距离,类似调整zoom
  255. rotateSensitivity: 0, //设置为0无法旋转
  256. zoomSensitivity: 0, //设置为0无法缩放
  257. panSensitivity: 0, //设置为0无法平移
  258. autoRotate: false //自动旋转
  259. }
  260. },
  261. series: series
  262. };
  263. return option;
  264. },
  265. //获取3d丙图的最高扇区的高度
  266. getHeight3D(series, height) {
  267. series.sort((a, b) => {
  268. return (b.pieData.value - a.pieData.value);
  269. })
  270. return height * 25 / series[0].pieData.value;
  271. },
  272. // 生成扇形的曲面参数方程,用于 series-surface.parametricEquation
  273. getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, h) {
  274. // 计算
  275. let midRatio = (startRatio + endRatio) / 2;
  276. let startRadian = startRatio * Math.PI * 2;
  277. let endRadian = endRatio * Math.PI * 2;
  278. let midRadian = midRatio * Math.PI * 2;
  279. // 如果只有一个扇形,则不实现选中效果。
  280. if (startRatio === 0 && endRatio === 1) {
  281. isSelected = false;
  282. }
  283. // 通过扇形内径/外径的值,换算出辅助参数 k(默认值 1/3)
  284. k = typeof k !== 'undefined' ? k : 1 / 3;
  285. // 计算选中效果分别在 x 轴、y 轴方向上的位移(未选中,则位移均为 0)
  286. let offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
  287. let offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
  288. // 计算高亮效果的放大比例(未高亮,则比例为 1)
  289. let hoverRate = isHovered ? 1.05 : 1;
  290. // 返回曲面参数方程
  291. return {
  292. u: {
  293. min: -Math.PI,
  294. max: Math.PI * 3,
  295. step: Math.PI / 32
  296. },
  297. v: {
  298. min: 0,
  299. max: Math.PI * 2,
  300. step: Math.PI / 20
  301. },
  302. x: function (u, v) {
  303. if (u < startRadian) {
  304. return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
  305. }
  306. if (u > endRadian) {
  307. return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
  308. }
  309. return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
  310. },
  311. y: function (u, v) {
  312. if (u < startRadian) {
  313. return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
  314. }
  315. if (u > endRadian) {
  316. return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
  317. }
  318. return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
  319. },
  320. z: function (u, v) {
  321. if (u < -Math.PI * 0.5) {
  322. return Math.sin(u);
  323. }
  324. if (u > Math.PI * 2.5) {
  325. return Math.sin(u) * h * .1;
  326. }
  327. return Math.sin(v) > 0 ? 1 * h * .1 : -1;
  328. }
  329. };
  330. },
  331. fomatFloat(num, n) {
  332. var f = parseFloat(num);
  333. if (isNaN(f)) {
  334. return false;
  335. }
  336. f = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); // n 幂
  337. var s = f.toString();
  338. var rs = s.indexOf('.');
  339. //判定如果是整数,增加小数点再补0
  340. if (rs < 0) {
  341. rs = s.length;
  342. s += '.';
  343. }
  344. while (s.length <= rs + n) {
  345. s += '0';
  346. }
  347. return s;
  348. },
  349. bindListen(myChart) {
  350. // 监听鼠标事件,实现饼图选中效果(单选),近似实现高亮(放大)效果。
  351. let that = this;
  352. let selectedIndex = '';
  353. let hoveredIndex = '';
  354. // 监听点击事件,实现选中效果(单选)
  355. myChart.on('click', function (params) {
  356. // 从 option.series 中读取重新渲染扇形所需的参数,将是否选中取反。
  357. let isSelected = !that.option.series[params.seriesIndex].pieStatus.selected;
  358. let isHovered = that.option.series[params.seriesIndex].pieStatus.hovered;
  359. let k = that.option.series[params.seriesIndex].pieStatus.k;
  360. let startRatio = that.option.series[params.seriesIndex].pieData.startRatio;
  361. let endRatio = that.option.series[params.seriesIndex].pieData.endRatio;
  362. // 如果之前选中过其他扇形,将其取消选中(对 option 更新)
  363. if (selectedIndex !== '' && selectedIndex !== params.seriesIndex) {
  364. that.option.series[selectedIndex].parametricEquation = that.getParametricEquation(that.option.series[
  365. selectedIndex].pieData
  366. .startRatio, that.option.series[selectedIndex].pieData.endRatio, false, false, k, that.option
  367. .series[
  368. selectedIndex].pieData
  369. .value);
  370. that.option.series[selectedIndex].pieStatus.selected = false;
  371. }
  372. // 对当前点击的扇形,执行选中/取消选中操作(对 option 更新)
  373. that.option.series[params.seriesIndex].parametricEquation = that.getParametricEquation(startRatio,
  374. endRatio,
  375. isSelected,
  376. isHovered, k, that.option.series[params.seriesIndex].pieData.value);
  377. that.option.series[params.seriesIndex].pieStatus.selected = isSelected;
  378. // 如果本次是选中操作,记录上次选中的扇形对应的系列号 seriesIndex
  379. isSelected ? selectedIndex = params.seriesIndex : null;
  380. // 使用更新后的 option,渲染图表
  381. myChart.setOption(that.option);
  382. });
  383. // 监听 mouseover,近似实现高亮(放大)效果
  384. myChart.on('mouseover', function (params) {
  385. // 准备重新渲染扇形所需的参数
  386. let isSelected;
  387. let isHovered;
  388. let startRatio;
  389. let endRatio;
  390. let k;
  391. // 如果触发 mouseover 的扇形当前已高亮,则不做操作
  392. if (hoveredIndex === params.seriesIndex) {
  393. return;
  394. // 否则进行高亮及必要的取消高亮操作
  395. } else {
  396. // 如果当前有高亮的扇形,取消其高亮状态(对 option 更新)
  397. if (hoveredIndex !== '') {
  398. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 false。
  399. isSelected = that.option.series[hoveredIndex].pieStatus.selected;
  400. isHovered = false;
  401. startRatio = that.option.series[hoveredIndex].pieData.startRatio;
  402. endRatio = that.option.series[hoveredIndex].pieData.endRatio;
  403. k = that.option.series[hoveredIndex].pieStatus.k;
  404. // 对当前点击的扇形,执行取消高亮操作(对 option 更新)
  405. that.option.series[hoveredIndex].parametricEquation = that.getParametricEquation(startRatio, endRatio,
  406. isSelected,
  407. isHovered, k, that.option.series[hoveredIndex].pieData.value);
  408. that.option.series[hoveredIndex].pieStatus.hovered = isHovered;
  409. // 将此前记录的上次选中的扇形对应的系列号 seriesIndex 清空
  410. hoveredIndex = '';
  411. }
  412. // 如果触发 mouseover 的扇形不是透明圆环,将其高亮(对 option 更新)
  413. if (params.seriesName !== 'mouseoutSeries' && params.seriesName !== 'pie2d') {
  414. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 true。
  415. isSelected = that.option.series[params.seriesIndex].pieStatus.selected;
  416. isHovered = true;
  417. startRatio = that.option.series[params.seriesIndex].pieData.startRatio;
  418. endRatio = that.option.series[params.seriesIndex].pieData.endRatio;
  419. k = that.option.series[params.seriesIndex].pieStatus.k;
  420. // 对当前点击的扇形,执行高亮操作(对 option 更新)
  421. that.option.series[params.seriesIndex].parametricEquation = that.getParametricEquation(startRatio,
  422. endRatio,
  423. isSelected, isHovered, k, that.option.series[params.seriesIndex].pieData.value + 5);
  424. that.option.series[params.seriesIndex].pieStatus.hovered = isHovered;
  425. // 记录上次高亮的扇形对应的系列号 seriesIndex
  426. hoveredIndex = params.seriesIndex;
  427. }
  428. // 使用更新后的 option,渲染图表
  429. myChart.setOption(that.option);
  430. }
  431. });
  432. // 修正取消高亮失败的 bug
  433. myChart.on('globalout', function () {
  434. // 准备重新渲染扇形所需的参数
  435. let isSelected;
  436. let isHovered;
  437. let startRatio;
  438. let endRatio;
  439. let k;
  440. if (hoveredIndex !== '') {
  441. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 true。
  442. isSelected = that.option.series[hoveredIndex].pieStatus.selected;
  443. isHovered = false;
  444. k = that.option.series[hoveredIndex].pieStatus.k;
  445. startRatio = that.option.series[hoveredIndex].pieData.startRatio;
  446. endRatio = that.option.series[hoveredIndex].pieData.endRatio;
  447. // 对当前点击的扇形,执行取消高亮操作(对 option 更新)
  448. that.option.series[hoveredIndex].parametricEquation = that.getParametricEquation(startRatio, endRatio,
  449. isSelected,
  450. isHovered, k, that.option.series[hoveredIndex].pieData.value);
  451. that.option.series[hoveredIndex].pieStatus.hovered = isHovered;
  452. // 将此前记录的上次选中的扇形对应的系列号 seriesIndex 清空
  453. hoveredIndex = '';
  454. }
  455. // 使用更新后的 option,渲染图表
  456. myChart.setOption(that.option);
  457. });
  458. }
  459. }
  460. }
  461. </script>
  462. <style scoped>
  463. .water-eval-container {
  464. position: absolute;
  465. width: 100%;
  466. height: 100%;
  467. top: -30%;
  468. right: 130%
  469. }
  470. .cityGreenLand-charts {
  471. height: 400px;
  472. width: 400px;
  473. }
  474. .num {
  475. position: absolute;
  476. width: 30%;
  477. height: 15%;
  478. right: -75%;
  479. top: 75%;
  480. font-weight: bold;
  481. /* background-color: rgb(255, 0, 0,0.3); */
  482. font-size: 35px;
  483. }
  484. @media screen and (max-height: 1000px) {
  485. .water-eval-container {
  486. position: absolute;
  487. width: 100%;
  488. height: 100%;
  489. top: 1%;
  490. right: 2%;
  491. }
  492. .cityGreenLand-charts {
  493. height: 400px;
  494. width: 400px;
  495. }
  496. .num {
  497. position: absolute;
  498. width: 35%;
  499. height: 15%;
  500. right: 30%;
  501. top: 45%;
  502. font-weight: bold;
  503. /* background-color: rgb(255, 0, 0,0.3); */
  504. font-size: 25px;
  505. }
  506. #number_3{
  507. position: absolute;
  508. width: 60%;
  509. height: 65%;
  510. right: 40%;
  511. top: 5%;
  512. font-weight: bold;
  513. /* background-color: rgb(255, 0, 0,0.3); */
  514. font-size: 30px;
  515. }
  516. #number_4{
  517. position: absolute;
  518. width: 60%;
  519. height: 65%;
  520. right: 35%;
  521. top: 71%;
  522. font-weight: bold;
  523. /* background-color: rgb(255, 0, 0,0.3); */
  524. font-size: 15px;
  525. }
  526. }
  527. </style>