站长统计
echarts⽴体饼图function mastery(data) {
var myChart = echarts.ElementById('mastery'));
var color = ["#3356d2", "#6a43f3", "#f3a722", "#5de8ec"]
var data = data
data.forEach((item, index) => {
item.itemStyle = {
color: color[index],
opacity: 0.6,
}
})
function 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 = 1
// 计算选中效果分别在 x 轴、y 轴⽅向上的位移(未选中,则位移均为 0)
let offsetX = isSelected ? s(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 + s(startRadian) * (1 + s(v) * k) * hoverRate;
}
if (u > endRadian) {
return offsetX + s(endRadian) * (1 + s(v) * k) * hoverRate;
}
return offsetX + s(u) * (1 + s(v) * k) * hoverRate;
},
y: function(u, v) {
if (u < startRadian) {
return offsetY + Math.sin(startRadian) * (1 + s(v) * k) * hoverRate;
}
if (u > endRadian) {
return offsetY + Math.sin(endRadian) * (1 + s(v) * k) * hoverRate;
}
return offsetY + Math.sin(u) * (1 + s(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;
},
};
}
function getPie3D(pieData, internalDiameterRatio) {
let series = [];
let sumValue = 0;
let startValue = 0;
let endValue = 0;
let legendData = [];
let k = typeof internalDiameterRatio !== 'undefined' ? (1 - internalDiameterRatio) / (1 +
internalDiameterRatio) : 1 /
3;
// 为每⼀个饼图数据,⽣成⼀个 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,
value: typeof pieData[i].value === 'undefined' ? `series${i}` : pieData[i].value,
type: 'surface',
parametric: true,
wireframe: {
show: false,
},
pieData: pieData[i],
pieStatus: {
selected: false,
hovered: false,
k: k,
},
};
if (typeof pieData[i].itemStyle != 'undefined') {
let itemStyle = {};
typeof pieData[i].lor != 'undefined' ? (lor = pieData[i].lor) :
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,也就是实现每⼀个扇形。 for (let i = 0; i < series.length; i++) {
endValue = startValue + series[i].pieData.value;
series[i].pieData.startRatio = startValue / sumValue;
series[i].dRatio = endValue / sumValue;
series[i].parametricEquation = getParametricEquation(
series[i].pieData.startRatio,
series[i].dRatio,
false,
false,
k,
series[i].pieData.value
);
startValue = endValue;
legendData.push(series[i].name);
}
}
// 补充⼀个透明的圆环,⽤于⽀撑⾼亮功能的近似实现。
series.push({
name: 'mouseoutSeries',fontweight默认值
type: 'surface',
parametric: true,
wireframe: {
show: false,
},
itemStyle: {
opacity: 0,
},
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);
},
y: function(u, v) {
return Math.sin(v) * s(u) + s(u);
},
z: function(u, v) {
s(v) > 0 ? 0.1 : -0.1;
},
},
}, {
name: 'pie2d',
type: 'pie',
labelLine: {
length: 30,
length2: 30,
},
startAngle: -30, //起始⾓度,⽀持范围[0, 360]。
clockwise: false, //饼图的扇区是否是顺时针排布。上述这两项配置主要是为了对齐3d的样式 radius: ['40%', '40%'],
center: ['50%', '50%'], //指⽰线的位置
data: data,
itemStyle: {
opacity: 0,
},
});
// 准备待返回的配置项,把准备好的 legendData、series 传⼊。
let option = {
labelLine: {
show: true,
lineStyle: {
color: '#fff'
}
},
label: {
show: true,
position: 'outside',
formatter: ['{a|{b}}\n{hr|}\n{c|{c}}'].join('\n'),
rich: {
hr: {
backgroundColor: "#fff",
borderRadius: 5,
width: 5,
height: 5,
padding: [0, -5],
opacity: 1,
},
a: {
fontSize: 14,
需引⼊echarts-gl.min.js
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论