使用:
<RingGraph
:id="storageNmae"
v-if="storageNmae"
ref="ring"
:key="storageNmae"
/>
<template>
<div class="water-eval-container">
<div class="cityGreenLand-charts" id="cityGreenLand-charts"></div>
</div>
</template>
<script>
import request from "@/utils/request";
import * as echarts from "echarts/core";
import {
TooltipComponent,
TooltipComponentOption,
LegendComponent,
} from "echarts/components";
import { SurfaceChart, Scatter3DChart } from "echarts-gl/charts";
import { Grid3DComponent } from "echarts-gl/components";
echarts.use([TooltipComponent, LegendComponent]);
export default {
name: "cityGreenLand",
components: {},
props: {
id: {
typeof: String,
default: null,
},
},
watch: {
deep: true,
handler(val) {
this.getData();
},
},
data() {
return {
option: {},
optionData: [],
colorArr: ["#909399", "#67C23A", "#4DC38D", "#C3BF4D"],
};
},
mounted() {
this.getData();
},
methods: {
getData() {
request({
url: `/api/ureport/warehouseBoard/warehouseCapacity?warehouseId=${this.id}`,
method: "get",
}).then((res) => {
this.optionData = res.data.map((item, index) => {
const raw = item.value;
const num = Number(raw);
return {
name: item.name,
value: isNaN(num) ? 0 : Math.abs(num),
itemStyle: {
color: this.colorArr[index],
},
};
});
this.$nextTick(function () {
this.init();
});
});
},
init() {
let myChart = echarts.init(
document.getElementById("cityGreenLand-charts")
);
if (this.optionData.length) {
this.option = this.getPie3D(this.optionData, 0.8);
myChart.setOption(this.option);
}
myChart.setOption(this.option);
this.bindListen(myChart);
},
getPie3D(pieData, 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;
});
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%", "10%"],
};
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);
}
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, 30);
let option = {
legend: {
data: legendData,
orient: "horizontal",
left: 10,
top: 0,
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}`;
},
},
labelLine: {
show: false,
lineStyle: {
color: "#fff",
},
},
label: {
color: "#fff",
show: false,
position: "outside",
rich: {
b: {
color: "#fff",
fontSize: 18,
lineHeight: 20,
},
c: {
fontSize: 18,
color: "#00d3fe",
},
},
formatter: "{b|{b} \n}{c|{c}}",
},
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>` +
`${option.series[params.seriesIndex].pieData.value}`
);
}
},
},
xAxis3D: {
min: -1,
max: 1,
},
yAxis3D: {
min: -1,
max: 1,
},
zAxis3D: {
min: -1,
max: 1,
},
grid3D: {
show: false,
boxHeight: boxHeight,
viewControl: {
alpha: 30,
distance: 200,
rotateSensitivity: 0,
zoomSensitivity: 0,
panSensitivity: 0,
autoRotate: false,
},
},
series: series,
};
return option;
},
getHeight3D(series, height) {
series.sort((a, b) => {
return b.pieData.value - a.pieData.value;
});
return (height * 25) / series[0].pieData.value;
},
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 = typeof k !== "undefined" ? k : 1 / 3;
let offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
let offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
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 * 0.1;
}
return Math.sin(v) > 0 ? 1 * h * 0.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);
var s = f.toString();
var rs = s.indexOf(".");
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) {
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;
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;
}
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;
isSelected ? (selectedIndex = params.seriesIndex) : null;
myChart.setOption(that.option);
});
myChart.on("mouseover", function (params) {
let isSelected;
let isHovered;
let startRatio;
let endRatio;
let k;
if (hoveredIndex === params.seriesIndex) {
return;
} else {
if (hoveredIndex !== "") {
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;
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;
hoveredIndex = "";
}
if (
params.seriesName !== "mouseoutSeries" &&
params.seriesName !== "pie2d"
) {
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;
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;
hoveredIndex = params.seriesIndex;
}
myChart.setOption(that.option);
}
});
myChart.on("globalout", function () {
let isSelected;
let isHovered;
let startRatio;
let endRatio;
let k;
if (hoveredIndex !== "") {
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;
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;
hoveredIndex = "";
}
myChart.setOption(that.option);
});
},
},
};
</script>
<style lang="scss" scoped>
.water-eval-container {
width: 100%;
height: 280px;
position: relative;
}
.cityGreenLand-charts {
margin-top: 35px;
width: 100%;
height: 280px;
}
</style>