import { renderChart } from "../chartService/index.js"; // 用你的统一渲染服务
import { colorSchemes } from "../colors.js";
export const getWindRoseChart = async (
data,
bucketName,
objectName,
analysisTypeCode,
) => {
try {
const windRoseList = data.data;
// ===== 数据处理 =====
const windDirections = Array.from({ length: 16 }, (_, i) => i * 22.5);
const windSpeedRanges = new Set();
const counts = {};
windRoseList.forEach((engine) => {
engine.windRoseData.forEach((item) => {
windSpeedRanges.add(item.windSpeedRange);
});
});
const speedLabels = Array.from(windSpeedRanges).sort();
const colors = colorSchemes[0].colors;
const colorscale = {};
speedLabels.forEach((label, index) => {
colorscale[label] = colors[(index % colors.length) + 4];
counts[label] = Array(windDirections.length).fill(0);
});
// ===== 数据聚合 =====
windRoseList.forEach((engine) => {
engine.windRoseData.forEach((item) => {
const { windDirection, windSpeedRange } = item;
const index = windDirections.findIndex(
(dir, i) =>
windDirection >= dir &&
windDirection < (windDirections[i + 1] ?? 360),
);
if (index !== -1) {
counts[windSpeedRange][index] += item.frequency;
}
});
});
// ===== traces =====
const traces = speedLabels.map((speedLabel) => ({
r: counts[speedLabel],
theta: windDirections,
name: speedLabel,
type: "barpolar",
marker: {
color: colorscale[speedLabel],
line: {
color: "white",
width: 1,
},
},
hovertemplate: "频率: %{r}
风向: %{theta}",
}));
// ===== layout =====
const layout = {
title: {
text: `风向玫瑰图 - ${windRoseList[0]?.enginName || ""}`,
font: { size: 16 },
},
plot_bgcolor: "#e5ecf6",
polar: {
bgcolor: "#e5ecf6",
radialaxis: {
title: {
text: data?.axes?.radial || "频率百分比(%)",
},
gridcolor: "#fff",
},
angularaxis: {
title: { text: "风向" },
tickvals: windDirections,
ticktext: windDirections,
},
},
showlegend: true,
legend: {
title: { text: "风速范围" },
},
};
// ===== 统一渲染(核心!)=====
return await renderChart({
traces,
layout,
bucketName,
objectName,
});
} catch (error) {
console.error("风玫瑰图生成失败:", error);
throw error;
}
};