代码优化
This commit is contained in:
49
src/util.js
49
src/util.js
@@ -445,3 +445,52 @@ export const calcRing = (bowtargetId, x, y, diameter) => {
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
// 绘制热力图
|
||||
export const drawHeatMap = (width, height, arrows) => {
|
||||
try {
|
||||
const ctx = uni.createCanvasContext("heatMapCanvas");
|
||||
|
||||
let minCount = 0;
|
||||
let maxCount = 0;
|
||||
// 计算最大和最小频次
|
||||
arrows.forEach((point) => {
|
||||
if (point.count > maxCount) {
|
||||
maxCount = point.count;
|
||||
}
|
||||
if (point.count < minCount) {
|
||||
minCount = point.count;
|
||||
}
|
||||
});
|
||||
// 绘制热力点
|
||||
arrows.forEach((point) => {
|
||||
if (point.x === 0 && point.y === 0) return;
|
||||
// 数量越多,半径越小(反比关系)
|
||||
const radius = 20;
|
||||
|
||||
// 根据频次设置同一种颜色的5个深浅度
|
||||
let color = "rgba(255, 232, 143, 0.2)";
|
||||
// if (point.count >= maxCount * 0.5) {
|
||||
// color = "rgba(255, 232, 143, 0.7)"; // 最深 - 频次5次及以上
|
||||
// } else if (point.count >= maxCount * 0.4) {
|
||||
// color = "rgba(255, 232, 143, 0.5)"; // 较深 - 频次4次
|
||||
// } else if (point.count >= maxCount * 0.3) {
|
||||
// color = "rgba(255, 232, 143, 0.3)"; // 中等 - 频次3次
|
||||
// } else if (point.count >= maxCount * 0.2) {
|
||||
// color = "rgba(255, 232, 143, 0.2)"; // 较浅 - 频次2次
|
||||
// } else {
|
||||
// color = "rgba(255, 232, 143, 0.1)"; // 最浅 - 频次1次
|
||||
// }
|
||||
|
||||
// 绘制圆形热力点
|
||||
ctx.setFillStyle(color);
|
||||
ctx.beginPath();
|
||||
ctx.arc(point.x * width, point.y * height, radius, 0, 2 * Math.PI);
|
||||
ctx.fill();
|
||||
});
|
||||
|
||||
ctx.draw();
|
||||
} catch (error) {
|
||||
console.error("绘制热力图失败:", error);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user