示例(參考此處餅圖修改https://www.isqqw.com/viewer?id=37497)
話不多說直接上代碼
此套代碼可以直接再echarts官網(wǎng)中的此處運(yùn)行文章來源:http://www.zghlxwxcb.cn/news/detail-631717.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-631717.html
let selectedIndex = '';
let hoveredIndex = '';
option = getPie3D(
[
{
name: '數(shù)學(xué)',
value: 60,
itemStyle: {
color: '#1890FF',
},
},
{
name: '難啊',
value: 44,
itemStyle: {
color: '#1EE7E7',
},
},
],
0.9 // 可做為調(diào)整內(nèi)環(huán)大小
);
// 生成扇形的曲面參數(shù)方程
function getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, h) {
// 計算
const midRatio = (startRatio + endRatio) / 2;
const startRadian = startRatio * Math.PI * 2;
const endRadian = endRatio * Math.PI * 2;
const midRadian = midRatio * Math.PI * 2;
// 如果只有一個扇形,則不實(shí)現(xiàn)選中效果。
if (startRatio === 0 && endRatio === 1) {
// eslint-disable-next-line no-param-reassign
isSelected = false;
}
// 通過扇形內(nèi)徑/外徑的值,換算出輔助參數(shù) k(默認(rèn)值 1/3)
// eslint-disable-next-line no-param-reassign
k = typeof k !== 'undefined' ? k : 1 / 3;
// 計算選中效果分別在 x 軸、y 軸方向上的位移(未選中,則位移均為 0)
const offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
const offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
// 計算高亮效果的放大比例(未高亮,則比例為 1)
const hoverRate = isHovered ? 1.05 : 1; // 可以做為調(diào)整外環(huán)大小
// 返回曲面參數(shù)方程
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(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(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(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;
// }
// 當(dāng)前圖形的高度是Z根據(jù)h(每個value的值決定的)
return 1.3 * Math.sin(v)
// return Math.sin(v) > 0 ? 1 * h * 0.1 : -1;
},
};
}
// 生成模擬 3D 餅圖的配置項(xiàng)
function getPie3D(pieData, internalDiameterRatio) {
const series = [];
// 總和
let sumValue = 0;
let startValue = 0;
let endValue = 0;
const legendData = [];
const k =
typeof internalDiameterRatio !== 'undefined'
? (1 - internalDiameterRatio) / (1 + internalDiameterRatio)
: 1 / 3;
// 為每一個餅圖數(shù)據(jù),生成一個 series-surface 配置
for (let i = 0; i < pieData.length; i += 1) {
sumValue += pieData[i].value;
const 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,
},
};
if (typeof pieData[i].itemStyle !== 'undefined') {
const { itemStyle } = pieData[i];
// eslint-disable-next-line no-unused-expressions
typeof pieData[i].itemStyle.color !== 'undefined' ? (itemStyle.color = pieData[i].itemStyle.color) : null;
// eslint-disable-next-line no-unused-expressions
typeof pieData[i].itemStyle.opacity !== 'undefined'
? (itemStyle.opacity = pieData[i].itemStyle.opacity)
: null;
seriesItem.itemStyle = itemStyle;
}
series.push(seriesItem);
}
// 使用上一次遍歷時,計算出的數(shù)據(jù)和 sumValue,調(diào)用 getParametricEquation 函數(shù),
// 向每個 series-surface 傳入不同的參數(shù)方程 series-surface.parametricEquation,也就是實(shí)現(xiàn)每一個扇形。
console.log(series);
for (let i = 0; i < series.length; i += 1) {
endValue = startValue + series[i].pieData.value;
series[i].pieData.startRatio = startValue / sumValue;
series[i].pieData.endRatio = endValue / sumValue;
series[i].parametricEquation = getParametricEquation(
series[i].pieData.startRatio,
series[i].pieData.endRatio,
false,
false,
k,
// 我這里做了一個處理,使除了第一個之外的值都是10
series[i].pieData.value === series[0].pieData.value ? 10 : 10
);
startValue = endValue;
legendData.push(series[i].name);
}
// 準(zhǔn)備待返回的配置項(xiàng),把準(zhǔn)備好的 legendData、series 傳入。
const option = {
legend:{
show: true
},
title: {
text: '3D 曲狀餅圖展示',
x: 'center',
top:170,
textStyle: {
color: '#000',
fontSize: 14,
}
},
// animation: false,
tooltip: {
formatter: (params) => {
if (params.seriesName !== 'mouseoutSeries') {
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}`;
}
return '';
},
},
xAxis3D: {
min: -1,
max: 1,
},
yAxis3D: {
min: -1,
max: 1,
},
zAxis3D: {
min: -1,
max: 1,
},
grid3D: {
show: false,
boxHeight: 5,
top: '-20%',
viewControl: {
// 3d效果可以放大、旋轉(zhuǎn)等,請自己去查看官方配置
alpha: 20,
// beta: 30,
rotateSensitivity: 1,
zoomSensitivity: 0,
panSensitivity: 0,
autoRotate: true,
distance: 300,
},
// 后處理特效可以為畫面添加高光、景深、環(huán)境光遮蔽(SSAO)、調(diào)色等效果??梢宰屨麄€畫面更富有質(zhì)感。
postEffect: {
// 配置這項(xiàng)會出現(xiàn)鋸齒,請自己去查看官方配置有辦法解決
enable: false,
bloom: {
enable: true,
bloomIntensity: 0.1,
},
SSAO: {
enable: true,
quality: 'medium',
radius: 2,
},
// temporalSuperSampling: {
// enable: true,
// },
},
},
series,
};
return option;
}
// // 修正取消高亮失敗的 bug
// // 監(jiān)聽 mouseover,近似實(shí)現(xiàn)高亮(放大)效果
// option.title.text = option.series[0].name + ' '+option.series[0].pieData.value
// function handleOver(params) {
// // 準(zhǔn)備重新渲染扇形所需的參數(shù)
// let isSelected;
// let isHovered;
// let startRatio;
// let endRatio;
// let k;
// let i;
// // 如果觸發(fā) mouseover 的扇形當(dāng)前已高亮,則不做操作
// if (hoveredIndex === params.seriesIndex) {
// return;
// // 否則進(jìn)行高亮及必要的取消高亮操作
// } else {
// // 如果當(dāng)前有高亮的扇形,取消其高亮狀態(tài)(對 option 更新)
// if (hoveredIndex !== '') {
// // 從 option.series 中讀取重新渲染扇形所需的參數(shù),將是否高亮設(shè)置為 false。
// isSelected = option.series[hoveredIndex].pieStatus.selected;
// isHovered = false;
// startRatio = option.series[hoveredIndex].pieData.startRatio;
// endRatio = option.series[hoveredIndex].pieData.endRatio;
// k = option.series[hoveredIndex].pieStatus.k;
// i = option.series[hoveredIndex].pieData.value === option.series[0].pieData.value ? 35 : 10;
// // 對當(dāng)前點(diǎn)擊的扇形,執(zhí)行取消高亮操作(對 option 更新)
// option.series[hoveredIndex].parametricEquation = getParametricEquation(
// startRatio,
// endRatio,
// isSelected,
// isHovered,
// k,
// i
// );
// option.series[hoveredIndex].pieStatus.hovered = isHovered;
// // 將此前記錄的上次選中的扇形對應(yīng)的系列號 seriesIndex 清空
// hoveredIndex = '';
// }
// // 如果觸發(fā) mouseover 的扇形不是透明圓環(huán),將其高亮(對 option 更新)
// if (params.seriesName !== 'mouseoutSeries') {
// option.title.text=' ' +option.series[seriesIndex].name + ' ' + option.series[params.seriesIndex].pieData.value
// // 從 option.series 中讀取重新渲染扇形所需的參數(shù),將是否高亮設(shè)置為 true。
// isSelected = option.series[params.seriesIndex].pieStatus.selected;
// isHovered = true;
// startRatio = option.series[params.seriesIndex].pieData.startRatio;
// endRatio = option.series[params.seriesIndex].pieData.endRatio;
// k = option.series[params.seriesIndex].pieStatus.k;
// // 對當(dāng)前點(diǎn)擊的扇形,執(zhí)行高亮操作(對 option 更新)
// option.series[params.seriesIndex].parametricEquation = getParametricEquation(
// startRatio,
// endRatio,
// isSelected,
// isHovered,
// k,
// option.series[params.seriesIndex].pieData.value + 5
// );
// option.series[params.seriesIndex].pieStatus.hovered = isHovered;
// // 記錄上次高亮的扇形對應(yīng)的系列號 seriesIndex
// hoveredIndex = params.seriesIndex;
// }
// // 使用更新后的 option,渲染圖表
// myChart.setOption(option);
// }
// }
// myChart.on('mouseover', function (params) {
// handleOver(params)
// });
// function handleOut() {
// if (hoveredIndex !== '') {
// // 從 option.series 中讀取重新渲染扇形所需的參數(shù),將是否高亮設(shè)置為 true。
// isSelected = option.series[hoveredIndex].pieStatus.selected;
// isHovered = false;
// k = option.series[hoveredIndex].pieStatus.k;
// startRatio = option.series[hoveredIndex].pieData.startRatio;
// endRatio = option.series[hoveredIndex].pieData.endRatio;
// // 對當(dāng)前點(diǎn)擊的扇形,執(zhí)行取消高亮操作(對 option 更新)
// i = option.series[hoveredIndex].pieData.value === option.series[0].pieData.value ? 35 : 10;
// option.series[hoveredIndex].parametricEquation = getParametricEquation(
// startRatio,
// endRatio,
// isSelected,
// isHovered,
// k,
// i
// );
// option.series[hoveredIndex].pieStatus.hovered = isHovered;
// // 將此前記錄的上次選中的扇形對應(yīng)的系列號 seriesIndex 清空
// hoveredIndex = '';
// }
// // 使用更新后的 option,渲染圖表
// myChart.setOption(option);
// }
// // 修正取消高亮失敗的 bug
// myChart.on('globalout', function () {
// handleOut()
// });
// console.log(option)
// let seriesIndex = 0
// setInterval(() => {
// handleOver({seriesIndex:seriesIndex})
// seriesIndex++
// if (seriesIndex === option.series.length) {
// seriesIndex = 0
// }
// }, 3000)
到了這里,關(guān)于echarts-pie---------3D曲狀環(huán)形餅圖實(shí)現(xiàn)?。?!的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!