This commit is contained in:
lnn19986213 2024-04-12 17:30:18 +08:00
parent d00beeec98
commit eba9d53f45
1 changed files with 142 additions and 137 deletions

View File

@ -1,5 +1,5 @@
<template> <template>
<div ref="chart" style="width:100%;height:100%;"></div> <div ref="chart" style="width:100%;height:200px;"></div>
</template> </template>
<script setup> <script setup>
@ -8,89 +8,74 @@ import { onMounted, reactive, ref } from "vue";
import * as echarts from 'echarts' import * as echarts from 'echarts'
import 'echarts-gl'; import 'echarts-gl';
const chart =ref(); // DOM const chart = ref(); // DOM
const colorList = ['#FFE35F','#0081FF','#8ABEDB','#25B9C8','#30EB92','rgba(69, 244, 245)', 'rgba(7, 166, 255)', 'rgba(255, 208, 118)', 'rgba(109, 148, 198)', 'rgba(255, 255, 255)']
const pieData = [ const pieData = [
{ {
name: "A", // name: "A", //
value: 10, // value: 10, //
itemStyle: {
color: "#8fd7fce8",//
},
}, },
{ {
name: "B", name: "B",
value: 8, value: 8,
itemStyle: {
color: "#466BE7e8",
},
}, },
{ {
name: "C", name: "C",
value: 2, value: 2,
itemStyle: {
color: "#F4BB29e8",
},
}, },
{ {
name: "D", name: "D",
value: 6, value: 6,
itemStyle: {
color: "#FF8329",
},
}, },
{ {
name: "E", name: "E",
value: 5, value: 5,
itemStyle: {
color: "#11EFC7",
},
}, },
{ {
name: "F", name: "F",
value: 1, value: 1,
itemStyle: {
color: "#006EF5",
},
}, },
{ {
name: "G", name: "G",
value: 5, value: 5
itemStyle: {
color: "#846BCE",
},
}, },
] ]
const serData = pieData.map((dItem, index) => {
return {
...dItem,
value: Number(dItem.value),
itemStyle: {
color: colorList[index]
},
}
})
// //
const option = getPie3D(pieData, 0.8) const option = getPie3D(serData, 0.8)
// 3D
//
function getPie3D(pieData, internalDiameterRatio) { function getPie3D(pieData, internalDiameterRatio) {
let series = []; let series = [];
let sumValue = 0; let sumValue = 0;
let startValue = 0; let startValue = 0;
let endValue = 0; let endValue = 0;
let legendData = []; let legendData = [];
let legendBfb = []; let k =
let k = 1 - internalDiameterRatio; typeof internalDiameterRatio !== 'undefined'
pieData.sort((a, b) => { ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio)
return b.value - a.value; : 1 / 3;
});
// series-surface() // series-surface
for (let i = 0; i < pieData.length; i++) { for (let i = 0; i < pieData.length; i++) {
sumValue += pieData[i].value; sumValue += pieData[i].value;
let seriesItem = { let seriesItem = {
// name: typeof pieData[i].name === 'undefined' ? `series${i}` : pieData[i].name,
name: type: 'surface',
typeof pieData[i].name === "undefined"
? `series${i}`
: pieData[i].name,
type: "surface",
//
parametric: true, parametric: true,
//线
wireframe: { wireframe: {
show: false, show: false,
}, },
@ -98,22 +83,18 @@ function getPie3D(pieData, internalDiameterRatio) {
pieStatus: { pieStatus: {
selected: false, selected: false,
hovered: false, hovered: false,
k: k, k: 1 / 10,
}, },
//()
// center: ['50%', '100%']
}; };
// if (typeof pieData[i].itemStyle != 'undefined') {
if (typeof pieData[i].itemStyle != "undefined") {
let itemStyle = {}; let itemStyle = {};
typeof pieData[i].itemStyle.color != "undefined"
? (itemStyle.color = pieData[i].itemStyle.color) typeof pieData[i].itemStyle.color != 'undefined' ? (itemStyle.color = pieData[i].itemStyle.color) : null;
: null; typeof pieData[i].itemStyle.opacity != 'undefined'
typeof pieData[i].itemStyle.opacity != "undefined"
? (itemStyle.opacity = pieData[i].itemStyle.opacity) ? (itemStyle.opacity = pieData[i].itemStyle.opacity)
: null; : null;
seriesItem.itemStyle = itemStyle; seriesItem.itemStyle = itemStyle;
} }
series.push(seriesItem); series.push(seriesItem);
@ -121,10 +102,9 @@ function getPie3D(pieData, internalDiameterRatio) {
// 使 sumValue getParametricEquation // 使 sumValue getParametricEquation
// series-surface series-surface.parametricEquation // series-surface series-surface.parametricEquation
legendData = [];
legendBfb = [];
for (let i = 0; i < series.length; i++) { for (let i = 0; i < series.length; i++) {
endValue = startValue + series[i].pieData.value * 1; endValue = startValue + series[i].pieData.value;
series[i].pieData.startRatio = startValue / sumValue; series[i].pieData.startRatio = startValue / sumValue;
series[i].pieData.endRatio = endValue / sumValue; series[i].pieData.endRatio = endValue / sumValue;
series[i].parametricEquation = getParametricEquation( series[i].parametricEquation = getParametricEquation(
@ -135,23 +115,84 @@ function getPie3D(pieData, internalDiameterRatio) {
k, k,
series[i].pieData.value series[i].pieData.value
); );
startValue = endValue;
let bfb = 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 = getHeight3D(series, 15); //3d/
// legendDataseries
startValue = endValue;
legendData.push(series[i].name);
}
// //
// series.push({
// name: 'mouseoutSeries',
// type: 'surface',
// parametric: true,
// wireframe: {
// show: false,
// },
// width: '30px',
// itemStyle: {
// opacity: 0.7,
// color: '#fff',
// },
// parametricEquation: {
// u: {
// min: 0,
// max: Math.PI * 2,
// step: Math.PI / 20,
// },
// v: {
// min: 0,
// max: Math.PI ,
// step: Math.PI / 20,
// },
// x: function (u, v) {
// return ((Math.sin(v) * Math.sin(u) + Math.sin(u)) / Math.PI);
// },
// y: function (u, v) {
// return ((Math.sin(v) * Math.cos(u) + Math.cos(u)) / Math.PI) ;
// },
// z: function (u, v) {
// return Math.cos(v) > 0 ? -4 : -4;
// },
// },
// });
// series.push({
// name: 'mouseoutSeries',
// type: 'surface',
// parametric: true,
// wireframe: {
// show: false,
// },
// itemStyle: {
// opacity: 0.5,
// color: '#fff',
// },
// parametricEquation: {
// u: {
// min: 0,
// max: Math.PI * 2,
// step: Math.PI / 20,
// },
// v: {
// min: 1.5,
// max: 3,
// step: Math.PI / 20,
// },
// x: function (u, v) {
// return ((Math.sin(v) * Math.sin(u) + Math.sin(u)) / Math.PI) *1;
// },
// y: function (u, v) {
// return ((Math.sin(v) * Math.cos(u) + Math.cos(u)) / Math.PI)*1;
// },
// z: function (u, v) {
// return Math.cos(v) > 0 ? -4 : -4;
// },
// },
// });
// legendDataseries
let option = { let option = {
// backgroundColor: '#000', //animation: false,
// //
legend: { legend: {
data: legendData, data: legendData,
@ -165,35 +206,36 @@ function getPie3D(pieData, internalDiameterRatio) {
"#F4BB29", "#F4BB29",
"#49C384", "#49C384",
], ],
width: '30%',
// //
// orient: "vertical", // orient: "vertical",
// right: 20, right: 0,
// bottom: 20, // bottom: 20,
bottom: '5%', top: 'center',
left: 'center', // left: '10px',
// //
itemGap: 5, itemGap: 20,
show: true, show: true,
icon: "rect", icon: "rect",
itemHeight: 10, itemHeight: 10,
itemWidth: 10, itemWidth: 10,
textStyle: { textStyle: {
// //
fontSize: 12, fontSize: 14,
color: "#B8DDFF", color: "#B8DDFF",
lineHeight: 20, lineHeight: 20,
}, },
// //
formatter: function (name) { // formatter: function (name) {
var target; // var target;
for (var i = 0, l = pieData.length; i < l; i++) { // for (var i = 0, l = pieData.length; i < l; i++) {
if (pieData[i].name == name) { // if (pieData[i].name == name) {
target = pieData[i].value; // target = pieData[i].value;
} // }
} // }
return `${name} ${target}`; // return `${name} ${target}`;
}, // },
}, },
// //
tooltip: { tooltip: {
@ -221,40 +263,28 @@ function getPie3D(pieData, internalDiameterRatio) {
} }
}, },
}, },
// xAxis3D: {},
xAxis3D: { yAxis3D: {},
min: -1, zAxis3D: {},
max: 1,
},
yAxis3D: {
min: -1,
max: 1,
},
zAxis3D: {
min: -1,
max: 1,
},
//
grid3D: { grid3D: {
show: false,
boxHeight: boxHeight, //
//
left: 0,
top: 0,
viewControl: { viewControl: {
//3d autoRotate: true,
alpha: 30, //( ) rotateSensitivity: 1, //0
distance: 200, //zoom() zoomSensitivity: 1, //0
rotateSensitivity: 0, //0
zoomSensitivity: 0, //0
panSensitivity: 0, //0 panSensitivity: 0, //0
autoRotate: true, // alpha: 25, //( )
distance: 120, //zoom()
}, },
top: '0',
left: '-100',
width: '100%',
show: false,
boxHeight: 20,
}, },
series: series, series: series,
}; };
return option; return option;
}; }
// series-surface.parametricEquation // series-surface.parametricEquation
function getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, h) { function getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, h) {
@ -325,32 +355,7 @@ function getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, h
}, },
}; };
}; };
//
function 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;
};
//3d
function getHeight3D(series, height) {
series.sort((a, b) => {
return b.pieData.value - a.pieData.value;
});
return (height * 20) / series[0].pieData.value;
}
// //
function bindListen(myChart) { function bindListen(myChart) {
let selectedIndex = ""; let selectedIndex = "";
@ -505,7 +510,7 @@ onMounted(() => {
// Vue3 // Vue3
var myChart = echarts.init(chart.value) var myChart = echarts.init(chart.value)
// //
bindListen(myChart) // bindListen(myChart)
// init(); // vue3.2this // init(); // vue3.2this
// 使 // 使
myChart.setOption(option); myChart.setOption(option);