首先確保在項目中引入了echarts和echarts-gl
"echarts": "^4.9.0",
"echarts-gl": "^1.1.2"
剩下的配置好optionData的數據,交給代碼
<template> <div class="water-eval-container"> <div class="cityGreenLand-charts" id="cityGreenLand-charts"> </div> </div> </template> <script> import echarts from 'echarts' import 'echarts-gl'; export default { name: "cityGreenLand", components: {}, data() { return { optionData: [{ name: '林地面積統計', value: 10000, itemStyle: { color: '#22c4ff', } }, { name: '草地面積統計', value: 12116, itemStyle: { color: '#aaff00' } }, { name: '耕地地面積統計', value: 16616, itemStyle: { color: '#ffaaff' } }], } }, mounted() { this.$nextTick(function() { this.init(); }); }, methods: { init() { //構建3d餅狀圖 let myChart = echarts.init(document.getElementById('cityGreenLand-charts')); // 傳入數據生成 option this.option = this.getPie3D(this.optionData, 0.8); myChart.setOption(this.option); //是否需要label指引線,如果要就添加一個透明的2d餅狀圖並調整角度使得labelLine和3d的餅狀圖對齊,並再次setOption this.option.series.push({ name: 'pie2d', type: 'pie', labelLine:{ length:10, length2:10 }, startAngle: -20 , //起始角度,支持范圍[0, 360]。 clockwise: false,//餅圖的扇區是否是順時針排布。上述這兩項配置主要是為了對齊3d的樣式 radius: ['20%', '50%'], center: ['50%', '50%'], data: this.optionData, itemStyle:{ opacity:0 } }); myChart.setOption(this.option); this.bindListen(myChart); }, getPie3D(pieData, internalDiameterRatio) { //internalDiameterRatio:透明的空心占比 let that = this; let series = []; let sumValue = 0; let startValue = 0; let endValue = 0; let legendData = []; let legendBfb = []; let k = 1 - internalDiameterRatio; pieData.sort((a, b) => { return (b.value - a.value); }); // 為每一個餅圖數據,生成一個 series-surface 配置 for (let i = 0; i < pieData.length; i++) { sumValue += pieData[i].value; let seriesItem = { name: typeof pieData[i].name === 'undefined' ? `series${i}` : pieData[i].name, type: 'surface', parametric: true, wireframe: { show: false }, pieData: pieData[i], pieStatus: { selected: false, hovered: false, k: k }, center: ['10%', '50%'] }; if (typeof pieData[i].itemStyle != 'undefined') { let itemStyle = {}; typeof pieData[i].itemStyle.color != 'undefined' ? itemStyle.color = pieData[i].itemStyle.color : null; typeof pieData[i].itemStyle.opacity != 'undefined' ? itemStyle.opacity = pieData[i].itemStyle.opacity : null; seriesItem.itemStyle = itemStyle; } series.push(seriesItem); } // 使用上一次遍歷時,計算出的數據和 sumValue,調用 getParametricEquation 函數, // 向每個 series-surface 傳入不同的參數方程 series-surface.parametricEquation,也就是實現每一個扇形。 legendData = []; legendBfb = []; for (let i = 0; i < series.length; i++) { endValue = startValue + series[i].pieData.value; series[i].pieData.startRatio = startValue / sumValue; series[i].pieData.endRatio = endValue / sumValue; series[i].parametricEquation = this.getParametricEquation(series[i].pieData.startRatio, series[i].pieData.endRatio, false, false, k, series[i].pieData.value); startValue = endValue; let bfb = that.fomatFloat(series[i].pieData.value / sumValue, 4); legendData.push({ name: series[i].name, value: bfb }); legendBfb.push({ name: series[i].name, value: bfb }); } let boxHeight = this.getHeight3D(series, 26);//通過傳參設定3d餅/環的高度,26代表26px // 准備待返回的配置項,把准備好的 legendData、series 傳入。 let option = { legend: { data: legendData, orient: 'horizontal', left: 10, top: 10, itemGap: 10, textStyle: { color: '#A1E2FF', }, show: true, icon: "circle", formatter: function(param) { let item = legendBfb.filter(item => item.name == param)[0]; let bfs = that.fomatFloat(item.value * 100, 2) + "%"; return `${item.name} ${bfs}`; } }, labelLine: { show: true, lineStyle: { color: '#7BC0CB' } }, label: { show: true, position: 'outside', rich: { b: { color: '#7BC0CB', fontSize: 12, lineHeight: 20 }, c: { fontSize: 16, }, }, formatter: '{b|{b} \n}{c|{c}}{b| 畝}', }, tooltip: { formatter: params => { if (params.seriesName !== 'mouseoutSeries' && params.seriesName !== 'pie2d') { let bfb = ((option.series[params.seriesIndex].pieData.endRatio - option.series[params.seriesIndex].pieData.startRatio) * 100).toFixed(2); return `${params.seriesName}<br/>` + `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color};"></span>` + `${ bfb }%`; } } }, xAxis3D: { min: -1, max: 1 }, yAxis3D: { min: -1, max: 1 }, zAxis3D: { min: -1, max: 1 }, grid3D: { show: false, boxHeight: boxHeight, //圓環的高度 viewControl: { //3d效果可以放大、旋轉等,請自己去查看官方配置 alpha: 40, //角度 distance: 300,//調整視角到主體的距離,類似調整zoom rotateSensitivity: 0, //設置為0無法旋轉 zoomSensitivity: 0, //設置為0無法縮放 panSensitivity: 0, //設置為0無法平移 autoRotate: false //自動旋轉 } }, series: series }; return option; }, //獲取3d丙圖的最高扇區的高度 getHeight3D(series, height) { series.sort((a, b) => { return (b.pieData.value - a.pieData.value); }) return height * 25 / series[0].pieData.value; }, // 生成扇形的曲面參數方程,用於 series-surface.parametricEquation getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, h) { // 計算 let midRatio = (startRatio + endRatio) / 2; let startRadian = startRatio * Math.PI * 2; let endRadian = endRatio * Math.PI * 2; let midRadian = midRatio * Math.PI * 2; // 如果只有一個扇形,則不實現選中效果。 if (startRatio === 0 && endRatio === 1) { isSelected = false; } // 通過扇形內徑/外徑的值,換算出輔助參數 k(默認值 1/3) k = typeof k !== 'undefined' ? k : 1 / 3; // 計算選中效果分別在 x 軸、y 軸方向上的位移(未選中,則位移均為 0) let offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0; let offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0; // 計算高亮效果的放大比例(未高亮,則比例為 1) let hoverRate = isHovered ? 1.05 : 1; // 返回曲面參數方程 return { u: { min: -Math.PI, max: Math.PI * 3, step: Math.PI / 32 }, v: { min: 0, max: Math.PI * 2, step: Math.PI / 20 }, x: function(u, v) { if (u < startRadian) { return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate; } if (u > endRadian) { return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate; } return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate; }, y: function(u, v) { if (u < startRadian) { return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate; } if (u > endRadian) { return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate; } return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate; }, z: function(u, v) { if (u < -Math.PI * 0.5) { return Math.sin(u); } if (u > Math.PI * 2.5) { return Math.sin(u) * h * .1; } return Math.sin(v) > 0 ? 1 * h * .1 : -1; } }; }, fomatFloat(num, n) { var f = parseFloat(num); if (isNaN(f)) { return false; } f = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); // n 冪 var s = f.toString(); var rs = s.indexOf('.'); //判定如果是整數,增加小數點再補0 if (rs < 0) { rs = s.length; s += '.'; } while (s.length <= rs + n) { s += '0'; } return s; }, bindListen(myChart) { // 監聽鼠標事件,實現餅圖選中效果(單選),近似實現高亮(放大)效果。 let that = this; let selectedIndex = ''; let hoveredIndex = ''; // 監聽點擊事件,實現選中效果(單選) myChart.on('click', function(params) { // 從 option.series 中讀取重新渲染扇形所需的參數,將是否選中取反。 let isSelected = !that.option.series[params.seriesIndex].pieStatus.selected; let isHovered = that.option.series[params.seriesIndex].pieStatus.hovered; let k = that.option.series[params.seriesIndex].pieStatus.k; let startRatio = that.option.series[params.seriesIndex].pieData.startRatio; let endRatio = that.option.series[params.seriesIndex].pieData.endRatio; // 如果之前選中過其他扇形,將其取消選中(對 option 更新) if (selectedIndex !== '' && selectedIndex !== params.seriesIndex) { that.option.series[selectedIndex].parametricEquation = that.getParametricEquation(that.option.series[ selectedIndex].pieData .startRatio, that.option.series[selectedIndex].pieData.endRatio, false, false, k, that.option.series[ selectedIndex].pieData .value); that.option.series[selectedIndex].pieStatus.selected = false; } // 對當前點擊的扇形,執行選中/取消選中操作(對 option 更新) that.option.series[params.seriesIndex].parametricEquation = that.getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, that.option.series[params.seriesIndex].pieData.value); that.option.series[params.seriesIndex].pieStatus.selected = isSelected; // 如果本次是選中操作,記錄上次選中的扇形對應的系列號 seriesIndex isSelected ? selectedIndex = params.seriesIndex : null; // 使用更新后的 option,渲染圖表 myChart.setOption(that.option); }); // 監聽 mouseover,近似實現高亮(放大)效果 myChart.on('mouseover', function(params) { // 准備重新渲染扇形所需的參數 let isSelected; let isHovered; let startRatio; let endRatio; let k; // 如果觸發 mouseover 的扇形當前已高亮,則不做操作 if (hoveredIndex === params.seriesIndex) { return; // 否則進行高亮及必要的取消高亮操作 } else { // 如果當前有高亮的扇形,取消其高亮狀態(對 option 更新) if (hoveredIndex !== '') { // 從 option.series 中讀取重新渲染扇形所需的參數,將是否高亮設置為 false。 isSelected = that.option.series[hoveredIndex].pieStatus.selected; isHovered = false; startRatio = that.option.series[hoveredIndex].pieData.startRatio; endRatio = that.option.series[hoveredIndex].pieData.endRatio; k = that.option.series[hoveredIndex].pieStatus.k; // 對當前點擊的扇形,執行取消高亮操作(對 option 更新) that.option.series[hoveredIndex].parametricEquation = that.getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, that.option.series[hoveredIndex].pieData.value); that.option.series[hoveredIndex].pieStatus.hovered = isHovered; // 將此前記錄的上次選中的扇形對應的系列號 seriesIndex 清空 hoveredIndex = ''; } // 如果觸發 mouseover 的扇形不是透明圓環,將其高亮(對 option 更新) if (params.seriesName !== 'mouseoutSeries' && params.seriesName !== 'pie2d') { // 從 option.series 中讀取重新渲染扇形所需的參數,將是否高亮設置為 true。 isSelected = that.option.series[params.seriesIndex].pieStatus.selected; isHovered = true; startRatio = that.option.series[params.seriesIndex].pieData.startRatio; endRatio = that.option.series[params.seriesIndex].pieData.endRatio; k = that.option.series[params.seriesIndex].pieStatus.k; // 對當前點擊的扇形,執行高亮操作(對 option 更新) that.option.series[params.seriesIndex].parametricEquation = that.getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, that.option.series[params.seriesIndex].pieData.value + 5); that.option.series[params.seriesIndex].pieStatus.hovered = isHovered; // 記錄上次高亮的扇形對應的系列號 seriesIndex hoveredIndex = params.seriesIndex; } // 使用更新后的 option,渲染圖表 myChart.setOption(that.option); } }); // 修正取消高亮失敗的 bug myChart.on('globalout', function() { // 准備重新渲染扇形所需的參數 let isSelected; let isHovered; let startRatio; let endRatio; let k; if (hoveredIndex !== '') { // 從 option.series 中讀取重新渲染扇形所需的參數,將是否高亮設置為 true。 isSelected = that.option.series[hoveredIndex].pieStatus.selected; isHovered = false; k = that.option.series[hoveredIndex].pieStatus.k; startRatio = that.option.series[hoveredIndex].pieData.startRatio; endRatio = that.option.series[hoveredIndex].pieData.endRatio; // 對當前點擊的扇形,執行取消高亮操作(對 option 更新) that.option.series[hoveredIndex].parametricEquation = that.getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, that.option.series[hoveredIndex].pieData.value); that.option.series[hoveredIndex].pieStatus.hovered = isHovered; // 將此前記錄的上次選中的扇形對應的系列號 seriesIndex 清空 hoveredIndex = ''; } // 使用更新后的 option,渲染圖表 myChart.setOption(that.option); }); } } } </script> <style lang="less"> .water-eval-container { width: 100%; height: 100%; } .cityGreenLand-charts { height: 200px; width: 400px; } </style>