You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.9 KiB

2 months ago
<template>
1 month ago
<!-- 功能区 -->
2 months ago
<div ref="chart" style="width: 30rem; height: 15rem;"></div>
2 months ago
</template>
<script>
import * as echarts from 'echarts';
1 month ago
import { fungong } from '@/api/ManageApi/index';
2 months ago
export default {
1 month ago
name: 'FunctionChart',
data() {
return {
chartData: [
{ value: 54, name: '高贸区', itemStyle: { color: '#36C3FB' } },
{ value: 65, name: '科创区', itemStyle: { color: '#5B76F9' } },
{ value: 32, name: '度假区', itemStyle: { color: '#F08445' } },
{ value: 25, name: '商务区', itemStyle: { color: '#F6B600' } },
{ value: 15, name: '苏相合作区', itemStyle: { color: '#50DFB3' } }
]
};
},
2 months ago
mounted() {
1 month ago
this.getData();
2 months ago
},
methods: {
1 month ago
async getData() {
try {
const response = await fungong();
if (response && response.code === 200 && response.data) {
this.chartData = this.processData(response.data);
this.renderChart();
} else {
console.error('获取数据失败:', response);
}
} catch (error) {
console.error('获取数据失败:', error);
}
},
processData(data) {
const names = ['高贸区', '科创区', '度假区', '商务区', '苏相合作区'];
const counts = data.map(item => item.count);
const total = counts.reduce((sum, count) => sum + count, 0);
return names.map((name, index) => ({
value: counts[index] || 0,
name: name,
itemStyle: { color: ['#36C3FB', '#5B76F9', '#F08445', '#F6B600', '#50DFB3'][index] },
percent: ((counts[index] || 0) / total * 100).toFixed(2)
}));
},
2 months ago
renderChart() {
const chartDom = this.$refs.chart;
const myChart = echarts.init(chartDom);
2 months ago
2 months ago
const option = {
2 months ago
tooltip: {
1 month ago
trigger: 'item',
formatter: function (params) {
return `${params.name}: ${params.value}个 (${params.percent}%)`;
}
2 months ago
},
legend: {
orient: 'vertical', // 图例垂直排列
right: '10%', // 图例在右侧
top: 'center', // 图例垂直居中
1 month ago
itemGap: 40, // 右侧间隔
2 months ago
formatter: function (name) {
// 自定义图例显示格式
1 month ago
const item = this.chartData.find(item => item.name === name);
4 weeks ago
return `${name} ${item.value}`;
1 month ago
}.bind(this)
2 months ago
},
series: [
{
name: '项目分布',
type: 'pie',
1 month ago
radius: ['40%', '60%'],
center: ['30%', '50%'],
data: this.chartData,
2 months ago
label: {
1 month ago
show: false // 隐藏标签
2 months ago
},
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
2 months ago
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
2 months ago
};
2 months ago
myChart.setOption(option);
}
}
2 months ago
};
</script>
<style scoped>
1 month ago
2 months ago
</style>