import { readCssVar } from "@/utils/chartTheme";
import { formatWakeMetric, sortByTurbineName } from "./wrwTableUtils";
function getOverviewTheme() {
return {
title: readCssVar("--wrw-chart-title", "#b7dff5"),
text: readCssVar("--wrw-chart-text", "#8FA3C8"),
axis: readCssVar("--wrw-chart-axis", "rgba(111, 239, 252, 0.35)"),
split: readCssVar("--wrw-chart-split", "rgba(255, 255, 255, 0.07)"),
polarSplit: readCssVar(
"--wrw-chart-polar-split",
"rgba(111, 239, 252, 0.1)",
),
tooltipBg: readCssVar("--wrw-chart-tooltip-bg", "rgba(6, 22, 42, 0.96)"),
tooltipBorder: readCssVar(
"--wrw-chart-tooltip-border",
"rgba(111, 239, 252, 0.4)",
),
tooltipText: readCssVar("--wrw-chart-tooltip-text", "#e6f1ff"),
gridBg: readCssVar("--wrw-chart-grid-bg", "rgba(6, 28, 48, 0.4)"),
gridBorder: readCssVar(
"--wrw-chart-grid-border",
"rgba(111, 239, 252, 0.12)",
),
emptyText: readCssVar("--wrw-chart-empty", "#8db6d1"),
pointerCross: readCssVar("--wrw-chart-axis", "rgba(111, 239, 252, 0.35)"),
};
}
function getOverviewTooltip(theme, options = {}) {
return {
backgroundColor: theme.tooltipBg,
borderColor: theme.tooltipBorder,
borderWidth: 1,
padding: [10, 14],
textStyle: { color: theme.tooltipText, fontSize: 12 },
appendToBody: true,
confine: false,
enterable: true,
hideDelay: 200,
extraCssText:
"box-shadow: 0 8px 24px rgba(0,0,0,0.45); border-radius: 8px; max-width: 360px; max-height: 240px; overflow-y: auto; z-index: 999999 !important; pointer-events: auto;",
...options,
};
}
/** 风速分布图:跟随鼠标并避让边界,内容过多时可进入浮层滚动 */
function getWindDistTooltipPosition() {
return (pos, _params, _dom, rect, size) => {
const gap = 14;
const contentSize = size?.contentSize || [180, 120];
const viewSize = size?.viewSize || [0, 0];
const point = Array.isArray(pos) ? pos : [0, 0];
const boxW = contentSize[0];
const boxH = contentSize[1];
const area = rect || {
x: 0,
y: 0,
width: viewSize[0] || point[0] + boxW + gap * 2,
height: viewSize[1] || point[1] + boxH + gap * 2,
};
let x = point[0] + gap;
let y = point[1] - boxH / 2;
if (x + boxW > area.x + area.width - gap) {
x = point[0] - boxW - gap;
}
if (y + boxH > area.y + area.height - gap) {
y = area.y + area.height - boxH - gap;
}
if (x < area.x + gap) x = area.x + gap;
if (y < area.y + gap) y = area.y + gap;
return [x, y];
};
}
const WIND_DIST_DECIMALS = 3;
const formatWindDistMetric = (value) =>
formatWakeMetric(value, WIND_DIST_DECIMALS);
function formatWindSpeed(value) {
if (value == null || value === "") return "";
const num = Number(value);
if (!Number.isFinite(num)) return String(value);
return Number.isInteger(num) ? String(num) : String(Number(num.toFixed(3)));
}
function formatWindDistTooltip(params) {
const list = Array.isArray(params) ? params : [params];
const first = list[0] || {};
const windSpeed =
first.axisValue ??
(Array.isArray(first.value) ? first.value[0] : first.value);
const rows = list
.map((item) => {
const value = Array.isArray(item.value) ? item.value[1] : item.value;
return `${item.marker}${
item.seriesName
}${formatWindDistMetric(
value,
)}`;
})
.join("
");
return `
风速:${formatWindSpeed(
windSpeed,
)} m/s
${rows}`;
}
const colorList = [
"#6feffc",
"#4D8BFF",
"#E85D5D",
"#66BB6A",
"#E8966A",
"#C0A882",
"#9a7ce6",
"#8FA3C8",
"#D4B86A",
];
function resolveOverviewTurbineLabel(turbine, turbineList = []) {
const engineId = turbine?.engineId;
const hit = (turbineList || []).find(
(item) => String(item?.engineId) === String(engineId),
);
return (
hit?.engineName ||
turbine?.engineName ||
turbine?.displayName ||
turbine?.engineId ||
turbine?.name ||
""
);
}
function sortWindDistTurbines(data, turbineList, sortOrder = "asc") {
if (!data?.turbines?.length) return data;
return {
...data,
turbines: sortByTurbineName(
data.turbines,
(item) => resolveOverviewTurbineLabel(item, turbineList),
sortOrder,
),
};
}
function buildDistSeriesFromApi(data, turbineList = []) {
if (!data?.turbines?.length) return null;
return data.turbines.map((turbine, idx) => ({
name:
resolveOverviewTurbineLabel(turbine, turbineList) ||
turbine.displayName ||
turbine.engineId ||
`机组${idx + 1}`,
type: "line",
step: "middle",
symbol: "none",
lineStyle: {
width: idx === data.turbines.length - 1 ? 2.2 : 1.8,
color: turbine.color || colorList[idx % colorList.length],
cap: "round",
join: "round",
},
emphasis: {
focus: "series",
lineStyle: {
width: 2.8,
shadowBlur: 12,
shadowColor: `${turbine.color || colorList[idx % colorList.length]}66`,
},
},
blur: { lineStyle: { opacity: 0.2 } },
data: Array.isArray(turbine.points) ? turbine.points : [],
}));
}
export function getWindDistOption(data, options = {}) {
const { turbineList = [], sortOrder = "asc" } = options;
const sortedData = sortWindDistTurbines(data, turbineList, sortOrder);
const overviewTheme = getOverviewTheme();
const overviewTooltip = getOverviewTooltip(overviewTheme);
const apiSeries = buildDistSeriesFromApi(sortedData, turbineList);
return {
animationDuration: 800,
animationEasing: "cubicOut",
color: colorList,
legend: {
type: "scroll",
bottom: 8,
left: "center",
orient: "horizontal",
itemWidth: 18,
itemHeight: 8,
itemGap: 16,
pageIconColor: overviewTheme.text,
pageTextStyle: { color: overviewTheme.text },
textStyle: { color: overviewTheme.text, fontSize: 11 },
},
tooltip: {
...overviewTooltip,
trigger: "axis",
position: getWindDistTooltipPosition(),
valueFormatter: (value) => formatWindDistMetric(value),
formatter: formatWindDistTooltip,
axisPointer: {
type: "line",
lineStyle: {
color: overviewTheme.pointerCross,
type: "dashed",
},
label: {
show: false,
},
},
},
grid: {
left: 56,
right: 24,
top: 28,
bottom: 72,
show: true,
borderColor: overviewTheme.gridBorder,
backgroundColor: overviewTheme.gridBg,
},
xAxis: {
type: "value",
// min: data?.xAxis?.min ?? 0,
// max: data?.xAxis?.max ?? 25,
// interval: data?.xAxis?.interval ?? 5,
name: data?.xAxis?.name || "风速 (m/s)",
nameLocation: "middle",
nameGap: 32,
nameTextStyle: { color: overviewTheme.text, fontSize: 12 },
axisLine: { lineStyle: { color: overviewTheme.axis } },
axisTick: { show: true, lineStyle: { color: overviewTheme.axis } },
axisLabel: {
color: overviewTheme.text,
fontSize: 10,
formatter: (value) => Math.round(Number(value)),
},
splitLine: {
lineStyle: { color: overviewTheme.split, type: "dashed" },
},
},
yAxis: {
type: "value",
name: data?.yAxis?.name || "概率密度",
nameTextStyle: { color: overviewTheme.text, fontSize: 12 },
// min: data?.yAxis?.min ?? 0,
// max: data?.yAxis?.max ?? 0.24,
// interval: data?.yAxis?.interval ?? 0.05,
axisLine: { show: true, lineStyle: { color: overviewTheme.axis } },
axisTick: { show: true, lineStyle: { color: overviewTheme.axis } },
axisLabel: {
color: overviewTheme.text,
fontSize: 10,
formatter: (value) => formatWindDistMetric(value),
},
splitLine: {
lineStyle: { color: overviewTheme.split, type: "dashed" },
},
},
series: apiSeries || [],
};
}
const ROSE_DIRECTIONS = [
"N",
"",
"NE",
"",
"E",
"",
"SE",
"",
"S",
"",
"SW",
"",
"W",
"",
"NW",
"",
];
export const ROSE_SPEED_BANDS = [
"0~3 m/s",
"3~6 m/s",
"6~9 m/s",
"9~12 m/s",
">12 m/s",
];
export const ROSE_COLORS = [
"#4A72B0",
"#92C1E9",
"#FFDF92",
"#F58D66",
"#E86B45",
];
function getRoseRadiusMax(turbineRose) {
if (!turbineRose?.bands?.length) return 14;
let max = 0;
turbineRose.bands.forEach((band) => {
(band.values || []).forEach((value) => {
const num = Number(value);
if (Number.isFinite(num) && num > max) max = num;
});
});
if (max <= 0) return 14;
const interval = max <= 5 ? 1 : max <= 20 ? 3.5 : Math.ceil(max / 4);
return Math.ceil(max / interval) * interval;
}
function getRoseBandValues(turbineRose, bandName, bandIdx) {
const bands = turbineRose?.bands || [];
const matched = bands.find((item) => item.band === bandName);
if (matched && Array.isArray(matched.values)) return matched.values;
return bands[bandIdx]?.values || [];
}
export function getSingleWindRoseOption(turbineIndex, roseData, turbineItem) {
const overviewTheme = getOverviewTheme();
const overviewTooltip = getOverviewTooltip(overviewTheme);
const speedBands = roseData?.speedBands || ROSE_SPEED_BANDS;
const directions = roseData?.directions || ROSE_DIRECTIONS;
const turbineRose =
turbineItem ||
(Array.isArray(roseData?.turbines)
? roseData.turbines[turbineIndex]
: null);
const radiusMax = getRoseRadiusMax(turbineRose);
const radiusInterval =
radiusMax <= 5 ? 1 : radiusMax <= 20 ? 3.5 : radiusMax / 4;
const series = speedBands.map((band, bandIdx) => ({
name: band,
type: "bar",
coordinateSystem: "polar",
stack: "wind",
barWidth: "78%",
itemStyle: {
color: ROSE_COLORS[bandIdx % ROSE_COLORS.length],
opacity: 0.96,
borderColor: "rgba(255, 255, 255, 0.32)",
borderWidth: 0.4,
shadowBlur: 2,
shadowColor: "rgba(0, 0, 0, 0.2)",
},
emphasis: {
focus: "self",
itemStyle: {
opacity: 1,
shadowBlur: 10,
shadowColor: "rgba(146, 193, 233, 0.5)",
},
},
data: getRoseBandValues(turbineRose, band, bandIdx),
}));
return {
animationDuration: 600,
animationEasing: "cubicOut",
color: ROSE_COLORS,
polar: {
center: ["50%", "54%"],
radius: "72%",
},
angleAxis: {
type: "category",
data: directions,
boundaryGap: false,
clockwise: true,
startAngle: 90,
axisLine: { lineStyle: { color: overviewTheme.axis } },
axisTick: { show: false },
axisLabel: {
color: overviewTheme.text,
fontSize: 9,
interval: 0,
margin: 4,
formatter: (v) => v || "",
},
splitLine: {
show: true,
lineStyle: { color: overviewTheme.polarSplit },
},
},
radiusAxis: {
min: 0,
max: radiusMax,
interval: radiusInterval,
axisLabel: {
color: overviewTheme.text,
fontSize: 8,
showMinLabel: false,
formatter: (value) => formatWakeMetric(value),
},
axisLine: { show: false },
axisTick: { show: false },
splitLine: {
lineStyle: { color: overviewTheme.polarSplit, type: "dashed" },
},
splitArea: {
show: true,
areaStyle: {
color: ["rgba(111, 239, 252, 0.04)", "transparent"],
},
},
},
tooltip: {
...overviewTooltip,
trigger: "item",
formatter: (p) => {
const dir = p.name || "—";
return `${p.marker}${
p.seriesName
}
方向 ${dir} · 频次 ${formatWakeMetric(p.value)}`;
},
},
series,
};
}