我想再该vue页面添加饼图或环形图,但是尝试了几个方法 ECharts、chart.js方法,但是因为未知原因导致页面无法加载对应饼图
下面是未加入饼图的代码,可以显示对应人数和占比,请大家帮我再html代码中加入正确的饼图样式来显示对应数据占比
<template>
<div v-if="surveyDetails">
<h1>{{ surveyDetails.title }}</h1>
<p>{{ surveyDetails.description }}</p>
<div v-for="question in surveyDetails.questions" :key="question.id">
<h2>{{ question.title }}</h2>
<p>{{ question.description }}</p>
<div v-if="question.type === 'single' || question.type === 'multiple'">
<div v-for="option in question.options" :key="option.id">
<input
:type="question.type === 'single' ? 'radio' : 'checkbox'"
:name="`question-${question.id}`"
:value="option.id"
/>
<label>{{ option.optionText }} ({{ option.count }} 人, {{ option.percentage }}%)</label>
</div>
</div>
</div>
</div>
</template>
<script>
import { fetchSubmit } from '@/api/votes';
import { getOption } from '@/api/votes.js'; // 确保这是正确的导入路径
export default {
data() {
return {
surveyDetails: null,
optionStats: {}
};
},
created() {
this.fetchSurveyDetails(this.$route.params.surveyId);
},
methods: {
async fetchSurveyDetails(surveyId) {
try {
const response = await fetchSubmit(surveyId);
this.surveyDetails = response; // 或者 response.data,取决于你的 API 响应结构
// 获取选项统计数据
const optionResponse = await getOption(surveyId);
this.optionStats = optionResponse.data;
// 合并选项统计数据到每个问题
this.surveyDetails.questions.forEach(question => {
const questionStats = this.optionStats[question.id];
if (questionStats) {
question.options.forEach(option => {
const optionStat = questionStats.find(stat => stat.id === option.id);
if (optionStat) {
// 合并统计信息到选项对象
option.count = optionStat.count;
option.percentage = optionStat.percentage;
}
});
}
});
} catch (error) {
console.error('Failed to fetch survey details:', error);
}
}
}
};
</script>
<style scoped>
/* 页面整体样式 */
.survey-container {
max-width: 800px;
margin: 30px auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
/* 标题样式 */
h1 {
text-align: center;
font-size: 24px;
color: #333;
margin-bottom: 20px;
}
/* 描述样式 */
p {
font-size: 16px;
color: #666;
text-align: center;
margin-bottom: 20px;
}
/* 问题样式 */
.question {
margin-bottom: 20px;
}
.question h2 {
font-size: 20px;
color: #333;
margin-bottom: 10px;
}
.question p {
font-size: 14px;
color: #666;
margin-bottom: 15px;
}
/* 选项样式 */
.option {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.option input[type="radio"],
.option input[type="checkbox"] {
margin-right: 10px;
}
.option label {
font-size: 14px;
color: #333;
cursor: pointer;
}
/* 响应式设计 */
@media (max-width: 768px) {
.survey-container {
margin: 20px;
}
}
</style>
下面是后端返回的数据
{status: true, message: "获取选项统计成功",…}
data
:
{33: [{id: 80, optionText: "苹果", count: 1, percentage: "100.00"},…],…}
33
:
[{id: 80, optionText: "苹果", count: 1, percentage: "100.00"},…]
0
:
{id: 80, optionText: "苹果", count: 1, percentage: "100.00"}
1
:
{id: 81, optionText: "西瓜", count: 0, percentage: "0.00"}
2
:
{id: 82, optionText: "蓝莓", count: 0, percentage: "0.00"}
3
:
{id: 83, optionText: "草莓", count: 0, percentage: "0.00"}
34
:
[{id: 84, optionText: "好看", count: 1, percentage: "25.00"},…]
0
:
{id: 84, optionText: "好看", count: 1, percentage: "25.00"}
1
:
{id: 85, optionText: "营养丰富", count: 1, percentage: "25.00"}
2
:
{id: 86, optionText: "便宜", count: 1, percentage: "25.00"}
3
:
{id: 87, optionText: "其他", count: 1, percentage: "25.00"}