Files
qingdao-employment-service/packageA/pages/post/component/radarMap.vue

149 lines
4.6 KiB
Vue
Raw Normal View History

2025-05-13 11:10:38 +08:00
<template>
<view style="display: flex; justify-content: center; padding: 0px 0">
<canvas canvas-id="radarCanvas" id="radarCanvas" style="width: 300px; height: 250px"></canvas>
</view>
</template>
<script setup>
import { reactive, inject, watch, ref, onMounted, computed } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
// const src = ref('');
const props = defineProps({
value: {
type: Object,
default: () => ({}),
},
});
// 监听页面初始化
onMounted(() => {
if (Object.keys(props.value).length > 0) {
rawRadarChart();
}
});
// 监听 props.value 变化
watch(
() => props.value,
(newVal) => {
if (newVal && Object.keys(newVal).length > 0) {
// const { skill, experience, education, salary, age, location } = newVal.radarChart;
const { experience, education, salary, age, location } = newVal.radarChart;
// const labels = ['学历', '年龄', '工作地', '技能', '工作经验', '期望薪资'];
const labels = ['学历', '年龄', '工作地', '工作经验', '期望薪资'];
// const data = [education, age, location, skill, experience, salary].map((item) => item * 0.05);
const data = [education, age, location, experience, salary].map((item) => item * 0.05);
2025-05-13 11:10:38 +08:00
rawRadarChart(labels, data);
}
},
{ deep: true, immediate: false } // deep 递归监听对象内部变化
);
function rawRadarChart(labels, data) {
const ctx = uni.createCanvasContext('radarCanvas');
const width = 80;
const height = 80;
const centerX = 150;
const centerY = 125;
const colors = ['#F5F5F5', '#F5F5F5', '#F5F5F5', '#F5F5F5', '#F5F5F5'];
2025-11-21 15:41:47 +08:00
const maxScore = 5;
2025-05-13 11:10:38 +08:00
const angleStep = (2 * Math.PI) / labels.length;
// 绘制背景多边形
ctx.setLineWidth(1);
ctx.setStrokeStyle('#F6F6F6');
//底部圆盘
ctx.beginPath();
ctx.arc(centerX, centerY, width, 0, 2 * Math.PI);
ctx.setFillStyle('#FFFFFF');
ctx.closePath();
ctx.fill();
//多边形圈
for (let i = 5; i > 0; i--) {
2025-11-21 15:41:47 +08:00
ctx.setStrokeStyle(colors[i - 1]);
2025-05-13 11:10:38 +08:00
ctx.beginPath();
labels.forEach((label, index) => {
const x = centerX + (width / 5) * i * Math.cos(angleStep * index - Math.PI / 2);
const y = centerY + (height / 5) * i * Math.sin(angleStep * index - Math.PI / 2);
if (index === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
});
ctx.closePath();
2025-11-21 15:41:47 +08:00
ctx.stroke();
2025-05-13 11:10:38 +08:00
}
2025-11-21 15:41:47 +08:00
//竖线
2025-05-13 11:10:38 +08:00
labels.forEach((label, index) => {
ctx.setStrokeStyle('#F5F5F5');
ctx.setFillStyle('#F5F5F5');
ctx.beginPath();
2025-11-21 15:41:47 +08:00
// 修改坐标计算,使用与多边形圈相同的角度计算方式
const x1 = centerX + width * 0.6 * Math.cos(angleStep * index - Math.PI / 2);
const y1 = centerY + height * 0.6 * Math.sin(angleStep * index - Math.PI / 2);
const x = centerX + width * Math.cos(angleStep * index - Math.PI / 2);
const y = centerY + height * Math.sin(angleStep * index - Math.PI / 2);
2025-05-13 11:10:38 +08:00
ctx.moveTo(x1, y1);
ctx.lineTo(x, y);
ctx.closePath();
ctx.stroke();
});
// 绘制雷达图数据
ctx.setStrokeStyle('#256BFA');
ctx.setFillStyle('rgba(37,107,250, 0.24)');
ctx.setLineWidth(2);
ctx.beginPath();
2025-11-21 15:41:47 +08:00
const pointList = [];
2025-05-13 11:10:38 +08:00
data.forEach((score, index) => {
const x = centerX + width * (score / maxScore) * Math.cos(angleStep * index - Math.PI / 2);
const y = centerY + height * (score / maxScore) * Math.sin(angleStep * index - Math.PI / 2);
2025-11-21 15:41:47 +08:00
pointList.push({ x, y });
2025-05-13 11:10:38 +08:00
if (index === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
});
ctx.closePath();
ctx.fill();
ctx.stroke();
// 绘制每个小圆点
2025-11-21 15:41:47 +08:00
ctx.setFillStyle('#256BFA');
2025-05-13 11:10:38 +08:00
pointList.forEach((point) => {
ctx.beginPath();
2025-11-21 15:41:47 +08:00
ctx.arc(point.x, point.y, 4, 0, 2 * Math.PI);
2025-05-13 11:10:38 +08:00
ctx.fill();
});
2025-11-21 15:41:47 +08:00
// 绘制标签
2025-05-13 11:10:38 +08:00
ctx.setTextAlign('center');
2025-11-21 15:41:47 +08:00
ctx.setTextBaseline('middle');
2025-05-13 11:10:38 +08:00
labels.forEach((label, index) => {
const x = centerX + (width + 30) * Math.cos(angleStep * index - Math.PI / 2);
const y = centerY + (height + 26) * Math.sin(angleStep * index - Math.PI / 2);
//标题
ctx.setFillStyle('#000');
ctx.setFontSize(12);
ctx.font = 'bold 12px sans-serif';
ctx.fillText(label, x, y);
});
ctx.draw();
}
</script>
<style></style>