python_weather/web/js/chart31.js

87 lines
2.6 KiB
JavaScript
Raw Normal View History

2024-06-07 12:15:48 +00:00
function echarts_31() {
var myChart = echarts.init(document.getElementById('fb1'));
var data = Chart31_Data;
var option = {
title: {
text: '天气状况统计',
// subtext: '数据来源:气象局',
left: 'center',
textStyle: {
color: '#fff',
fontSize: '16'
}
},
tooltip: {
trigger: 'item',
position: function (p) { //其中p为当前鼠标的位置
return [p[0] + 10, p[1] - 10];
},
formatter: function (params) {
var dataItem = data.find(item => item.name === params.name);
var value = `${dataItem.name}: ${params.value} (${params.percent}%) <br>`;
dataItem.weather.forEach((v) => {
value += `${v} <br>`;
});
return value; // 动态遍历数据显示地区、天气和百分比
}
},
legend: {
orient: 'vertical',
left: 'left',
top: '70%',
itemWidth: 10,
itemHeight: 10,
textStyle: {
color: 'rgba(255,255,255,.5)',
fontSize: '12',
},
data: ['晴天', '多云', '小雨', '大雨', '阴天', '雷阵雨', '雾']
},
series: [
{
name: '天气状况',
type: 'pie',
center: ['50%', '42%'],
radius: ['40%', '60%'],
avoidLabelOverlap: false,
label: {
show: false,
position: 'center',
formatter: '{b}: {c}' // 显示地区名称和天气数据
},
emphasis: {
label: {
show: true,
fontSize: '30',
fontWeight: 'bold'
}
},
labelLine: {
show: false
},
data: data.map(item => ({
value: item.value,
name: item.name,
itemStyle: {color: item.color},
label: {
normal: {
show: false // 设置标签显示为normal
}
}
}))
}
]
};
myChart.setOption(option);
window.addEventListener("resize", function () {
myChart.resize();
});
}