@liruiyi962464
2024-05-31T07:07:41.000000Z
字数 4833
阅读 82
前端
var angle = 0 //角度,用来做简单的动画效果的
var currentIndex = 0
var color = ['#00FDFE', '#00D8FF']
var legend = ['档口', '仓储']
var seriesData = [
{ name: '档口', value: 5492 },
{ name: '仓储', value: 6492 },
]
option = {
backgroundColor: "#050e31",
color: color,
legend: {
itemHeight: 14,
itemWidth: 14,
icon: "rect",
orient: 'vertical',
top: 'center',
right: 0,
textStyle: {
align: 'left',
verticalAlign: 'middle',
rich: {
name: {
color: '#A0C2BF',
fontSize:14,
},
value: {
color: '#A0C2BF',
fontSize: 14,
},
rate: {
color: '#A0C2BF',
fontSize: 14,
},
},
},
data: legend,
formatter: (name) => {
if (seriesData.length) {
const item = seriesData.filter((item) => item.name === name)[0];
return `{name|${name}:}{rate| ${item.value}%} {value| |} {value| ${item.value}}`;
}
},
},
series: [{
// 内环线 1
name: "ring5",
type: 'custom',
coordinateSystem: "none",
renderItem: function (params, api) {
return {
type: 'arc',
shape: {
cx: api.getWidth() / 2,
cy: api.getHeight() / 2,
r: Math.min(api.getWidth(), api.getHeight()) / 2 * 0.37,// 调整位置
},
style: {
stroke: "#0CD3DB",
fill: "transparent",
lineWidth: 1
},
silent: true
};
},
data: [0]
}, {
name: "ring5",
type: 'custom',
coordinateSystem: "none",
renderItem: function (params, api) {
return {
type: 'arc',
shape: {
cx: api.getWidth() / 2,
cy: api.getHeight() / 2,
r: Math.min(api.getWidth(), api.getHeight()) / 2 * 0.52,
startAngle: (180 + angle) * Math.PI / 180,
endAngle: (270 + angle) * Math.PI / 180
},
style: {
stroke: "#0CD3DB",
fill: "transparent",
lineWidth: 1.5
},
silent: true
};
},
data: [0]
}, {
name: "ring5",
type: 'custom',
coordinateSystem: "none",
renderItem: function (params, api) {
return {
type: 'arc',
shape: {
cx: api.getWidth() / 2,
cy: api.getHeight() / 2,
r: Math.min(api.getWidth(), api.getHeight()) / 2 * 0.65,
startAngle: (360 + -angle) * Math.PI / 180,
endAngle: (80 + -angle) * Math.PI / 180
},
style: {
stroke: "#0CD3DB",
fill: "transparent",
lineWidth: 1
},
silent: true
};
},
data: [0]
}, {
name: "ring5",
type: 'custom',
coordinateSystem: "none",
renderItem: function (params, api) {
return {
type: 'arc',
shape: {
cx: api.getWidth() / 2,
cy: api.getHeight() / 2,
r: Math.min(api.getWidth(), api.getHeight()) / 2 * 0.65,
startAngle: (180 + -angle) * Math.PI / 180,
endAngle: (260 + -angle) * Math.PI / 180
},
style: {
stroke: "#0CD3DB",
fill: "transparent",
lineWidth: 1.5
},
silent: true
};
},
data: [0]
}, {
name: "ring5",
type: 'custom',
coordinateSystem: "none",
renderItem: function (params, api) {
let x0 = api.getWidth() / 2;
let y0 = api.getHeight() / 2;
let r = Math.min(api.getWidth(), api.getHeight()) / 2 * 0.65;
let point = getCirlPoint(x0, y0, r, (180 + -angle))
return {
type: 'circle',
shape: {
cx: point.x,
cy: point.y,
r: 2
},
style: {
stroke: "#0CD3DB",//粉
fill: "#0CD3DB",
lineWidth: 5
},
silent: true
};
},
data: [0]
}, {
name: "ring5", //绿点
type: 'custom',
coordinateSystem: "none",
renderItem: function (params, api) {
let x0 = api.getWidth() / 2;
let y0 = api.getHeight() / 2;
let r = Math.min(api.getWidth(), api.getHeight()) / 2 * 0.65;
let point = getCirlPoint(x0, y0, r, (360 + -angle))
return {
type: 'circle',
shape: {
cx: point.x,
cy: point.y,
r: 2
},
style: {
stroke: "#0CD3DB", //绿
fill: "#0CD3DB",
lineWidth: 5
},
silent: true
};
},
data: [0]
}, {
name: '占比',
type: 'pie',
center: ['50%', '50%'],
radius: ['40%', '50%'],
label: {
normal: {
show: false,
position: 'center',
formatter: '{value|{c}}\n{label|{b}}',
},
},
labelLine: {
show: false,
length: 0,
length2: 0,
},
itemStyle: {//间隔
normal: {
borderWidth: 3,
borderColor: '#050e31',
},
},
data: seriesData,
},
],
title: {
text: '', // 初始标题为空
subtext: '', // 初始子标题为空
left: 'center',
top: 'center',
textStyle: {
fontSize: '25',
// 其他样式设置...
}
}
};
myChart.setOption(option);
myChart.on('mouseover', (params) => {
currentIndex = params.dataIndex;
highlightPie(currentIndex);
updateCenterTitle(params); // 传递params以获取当前高亮部分的详细信息
});
function highlightPie(currentIndex) {
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: currentIndex
});
// 不需要downplay,因为只有一个高亮部分
}
function updateCenterTitle(params) {
var currentItem = seriesData[params.dataIndex]; // 获取当前高亮部分的数据项
option.title.text = currentItem.name ;
option.title.subtext = currentItem.value;
myChart.setOption({
title: {
text: option.title.text, // 设置新的标题文本
subtext: option.title.subtext // 设置新的子标题文本
}
});
}
updateCenterTitle({ dataIndex: 0 }); // 默认加载第一条数据
//获取圆上面某点的坐标(x0,y0表示坐标,r半径,angle角度)
function getCirlPoint(x0, y0, r, angle) {
let x1 = x0 + r * Math.cos(angle * Math.PI / 180)
let y1 = y0 + r * Math.sin(angle * Math.PI / 180)
return {
x: x1,
y: y1
}
}
function draw() {
angle = angle + 3
myChart.setOption(option, true)
}
setInterval(function () {
//用setInterval做动画感觉有问题
draw()
}, 100);
//获取圆上面某点的坐标(x0,y0表示坐标,r半径,angle角度)
function getCirlPoint(x0, y0, r, angle) {
let x1 = x0 + r * Math.cos(angle * Math.PI / 180)
let y1 = y0 + r * Math.sin(angle * Math.PI / 180)
return {
x: x1,
y: y1
}
}
function draw() {
angle = (angle + 3) % 360; // 更新角度,并确保它在0到360之间
startAngle = angle; // 更新旋转扇区的起始角度
// 更新旋转扇区的结束角度
option.series[0].data[option.series[0].data.length - 1].endAngle = startAngle + 3;
// 设置图表的新配置
myChart.setOption(option, true); // 第二个参数为true表示不合并之前的数据和配置项
}
setInterval(function () {
//用setInterval做动画感觉有问题
draw()
}, 100);
//updateCenterTitle({ dataIndex: 0 }); // 默认加载第一条数据
// 示例:假设您有一个初始数据项,想要在图表加载时设置为其标题和子标题
var initialDataItem = {
name: '初始标题',
value: '初始子标题值'
};
// 使用初始数据设置标题和子标题
option.title.text = initialDataItem.name;
option.title.subtext = initialDataItem.value;
// 更新图表以反映初始数据的标题和子标题
myChart.setOption({
title: {
text: option.title.text,
subtext: option.title.subtext
}
});