overviewChartOptions.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import { readCssVar } from "@/utils/chartTheme";
  2. import { formatWakeMetric, sortByTurbineName } from "./wrwTableUtils";
  3. function getOverviewTheme() {
  4. return {
  5. title: readCssVar("--wrw-chart-title", "#b7dff5"),
  6. text: readCssVar("--wrw-chart-text", "#8FA3C8"),
  7. axis: readCssVar("--wrw-chart-axis", "rgba(111, 239, 252, 0.35)"),
  8. split: readCssVar("--wrw-chart-split", "rgba(255, 255, 255, 0.07)"),
  9. polarSplit: readCssVar(
  10. "--wrw-chart-polar-split",
  11. "rgba(111, 239, 252, 0.1)",
  12. ),
  13. tooltipBg: readCssVar("--wrw-chart-tooltip-bg", "rgba(6, 22, 42, 0.96)"),
  14. tooltipBorder: readCssVar(
  15. "--wrw-chart-tooltip-border",
  16. "rgba(111, 239, 252, 0.4)",
  17. ),
  18. tooltipText: readCssVar("--wrw-chart-tooltip-text", "#e6f1ff"),
  19. gridBg: readCssVar("--wrw-chart-grid-bg", "rgba(6, 28, 48, 0.4)"),
  20. gridBorder: readCssVar(
  21. "--wrw-chart-grid-border",
  22. "rgba(111, 239, 252, 0.12)",
  23. ),
  24. emptyText: readCssVar("--wrw-chart-empty", "#8db6d1"),
  25. pointerCross: readCssVar("--wrw-chart-axis", "rgba(111, 239, 252, 0.35)"),
  26. };
  27. }
  28. function getOverviewTooltip(theme, options = {}) {
  29. return {
  30. backgroundColor: theme.tooltipBg,
  31. borderColor: theme.tooltipBorder,
  32. borderWidth: 1,
  33. padding: [10, 14],
  34. textStyle: { color: theme.tooltipText, fontSize: 12 },
  35. appendToBody: true,
  36. confine: false,
  37. enterable: true,
  38. hideDelay: 200,
  39. extraCssText:
  40. "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;",
  41. ...options,
  42. };
  43. }
  44. /** 风速分布图:跟随鼠标并避让边界,内容过多时可进入浮层滚动 */
  45. function getWindDistTooltipPosition() {
  46. return (pos, _params, _dom, rect, size) => {
  47. const gap = 14;
  48. const contentSize = size?.contentSize || [180, 120];
  49. const viewSize = size?.viewSize || [0, 0];
  50. const point = Array.isArray(pos) ? pos : [0, 0];
  51. const boxW = contentSize[0];
  52. const boxH = contentSize[1];
  53. const area = rect || {
  54. x: 0,
  55. y: 0,
  56. width: viewSize[0] || point[0] + boxW + gap * 2,
  57. height: viewSize[1] || point[1] + boxH + gap * 2,
  58. };
  59. let x = point[0] + gap;
  60. let y = point[1] - boxH / 2;
  61. if (x + boxW > area.x + area.width - gap) {
  62. x = point[0] - boxW - gap;
  63. }
  64. if (y + boxH > area.y + area.height - gap) {
  65. y = area.y + area.height - boxH - gap;
  66. }
  67. if (x < area.x + gap) x = area.x + gap;
  68. if (y < area.y + gap) y = area.y + gap;
  69. return [x, y];
  70. };
  71. }
  72. const WIND_DIST_DECIMALS = 3;
  73. const formatWindDistMetric = (value) =>
  74. formatWakeMetric(value, WIND_DIST_DECIMALS);
  75. function formatWindSpeed(value) {
  76. if (value == null || value === "") return "";
  77. const num = Number(value);
  78. if (!Number.isFinite(num)) return String(value);
  79. return Number.isInteger(num) ? String(num) : String(Number(num.toFixed(3)));
  80. }
  81. function formatWindDistTooltip(params) {
  82. const list = Array.isArray(params) ? params : [params];
  83. const first = list[0] || {};
  84. const windSpeed =
  85. first.axisValue ??
  86. (Array.isArray(first.value) ? first.value[0] : first.value);
  87. const rows = list
  88. .map((item) => {
  89. const value = Array.isArray(item.value) ? item.value[1] : item.value;
  90. return `${item.marker}${
  91. item.seriesName
  92. }<span style="float:right;margin-left:18px;font-weight:600">${formatWindDistMetric(
  93. value,
  94. )}</span>`;
  95. })
  96. .join("<br/>");
  97. return `<div style="font-weight:600;margin-bottom:8px">风速:${formatWindSpeed(
  98. windSpeed,
  99. )} m/s</div>${rows}`;
  100. }
  101. const colorList = [
  102. "#6feffc",
  103. "#4D8BFF",
  104. "#E85D5D",
  105. "#66BB6A",
  106. "#E8966A",
  107. "#C0A882",
  108. "#9a7ce6",
  109. "#8FA3C8",
  110. "#D4B86A",
  111. ];
  112. function resolveOverviewTurbineLabel(turbine, turbineList = []) {
  113. const engineId = turbine?.engineId;
  114. const hit = (turbineList || []).find(
  115. (item) => String(item?.engineId) === String(engineId),
  116. );
  117. return (
  118. hit?.engineName ||
  119. turbine?.engineName ||
  120. turbine?.displayName ||
  121. turbine?.engineId ||
  122. turbine?.name ||
  123. ""
  124. );
  125. }
  126. function sortWindDistTurbines(data, turbineList, sortOrder = "asc") {
  127. if (!data?.turbines?.length) return data;
  128. return {
  129. ...data,
  130. turbines: sortByTurbineName(
  131. data.turbines,
  132. (item) => resolveOverviewTurbineLabel(item, turbineList),
  133. sortOrder,
  134. ),
  135. };
  136. }
  137. function buildDistSeriesFromApi(data, turbineList = []) {
  138. if (!data?.turbines?.length) return null;
  139. return data.turbines.map((turbine, idx) => ({
  140. name:
  141. resolveOverviewTurbineLabel(turbine, turbineList) ||
  142. turbine.displayName ||
  143. turbine.engineId ||
  144. `机组${idx + 1}`,
  145. type: "line",
  146. step: "middle",
  147. symbol: "none",
  148. lineStyle: {
  149. width: idx === data.turbines.length - 1 ? 2.2 : 1.8,
  150. color: turbine.color || colorList[idx % colorList.length],
  151. cap: "round",
  152. join: "round",
  153. },
  154. emphasis: {
  155. focus: "series",
  156. lineStyle: {
  157. width: 2.8,
  158. shadowBlur: 12,
  159. shadowColor: `${turbine.color || colorList[idx % colorList.length]}66`,
  160. },
  161. },
  162. blur: { lineStyle: { opacity: 0.2 } },
  163. data: Array.isArray(turbine.points) ? turbine.points : [],
  164. }));
  165. }
  166. export function getWindDistOption(data, options = {}) {
  167. const { turbineList = [], sortOrder = "asc" } = options;
  168. const sortedData = sortWindDistTurbines(data, turbineList, sortOrder);
  169. const overviewTheme = getOverviewTheme();
  170. const overviewTooltip = getOverviewTooltip(overviewTheme);
  171. const apiSeries = buildDistSeriesFromApi(sortedData, turbineList);
  172. return {
  173. animationDuration: 800,
  174. animationEasing: "cubicOut",
  175. color: colorList,
  176. legend: {
  177. type: "scroll",
  178. bottom: 8,
  179. left: "center",
  180. orient: "horizontal",
  181. itemWidth: 18,
  182. itemHeight: 8,
  183. itemGap: 16,
  184. pageIconColor: overviewTheme.text,
  185. pageTextStyle: { color: overviewTheme.text },
  186. textStyle: { color: overviewTheme.text, fontSize: 11 },
  187. },
  188. tooltip: {
  189. ...overviewTooltip,
  190. trigger: "axis",
  191. position: getWindDistTooltipPosition(),
  192. valueFormatter: (value) => formatWindDistMetric(value),
  193. formatter: formatWindDistTooltip,
  194. axisPointer: {
  195. type: "line",
  196. lineStyle: {
  197. color: overviewTheme.pointerCross,
  198. type: "dashed",
  199. },
  200. label: {
  201. show: false,
  202. },
  203. },
  204. },
  205. grid: {
  206. left: 56,
  207. right: 24,
  208. top: 28,
  209. bottom: 72,
  210. show: true,
  211. borderColor: overviewTheme.gridBorder,
  212. backgroundColor: overviewTheme.gridBg,
  213. },
  214. xAxis: {
  215. type: "value",
  216. // min: data?.xAxis?.min ?? 0,
  217. // max: data?.xAxis?.max ?? 25,
  218. // interval: data?.xAxis?.interval ?? 5,
  219. name: data?.xAxis?.name || "风速 (m/s)",
  220. nameLocation: "middle",
  221. nameGap: 32,
  222. nameTextStyle: { color: overviewTheme.text, fontSize: 12 },
  223. axisLine: { lineStyle: { color: overviewTheme.axis } },
  224. axisTick: { show: true, lineStyle: { color: overviewTheme.axis } },
  225. axisLabel: {
  226. color: overviewTheme.text,
  227. fontSize: 10,
  228. formatter: (value) => Math.round(Number(value)),
  229. },
  230. splitLine: {
  231. lineStyle: { color: overviewTheme.split, type: "dashed" },
  232. },
  233. },
  234. yAxis: {
  235. type: "value",
  236. name: data?.yAxis?.name || "概率密度",
  237. nameTextStyle: { color: overviewTheme.text, fontSize: 12 },
  238. // min: data?.yAxis?.min ?? 0,
  239. // max: data?.yAxis?.max ?? 0.24,
  240. // interval: data?.yAxis?.interval ?? 0.05,
  241. axisLine: { show: true, lineStyle: { color: overviewTheme.axis } },
  242. axisTick: { show: true, lineStyle: { color: overviewTheme.axis } },
  243. axisLabel: {
  244. color: overviewTheme.text,
  245. fontSize: 10,
  246. formatter: (value) => formatWindDistMetric(value),
  247. },
  248. splitLine: {
  249. lineStyle: { color: overviewTheme.split, type: "dashed" },
  250. },
  251. },
  252. series: apiSeries || [],
  253. };
  254. }
  255. const ROSE_DIRECTIONS = [
  256. "N",
  257. "",
  258. "NE",
  259. "",
  260. "E",
  261. "",
  262. "SE",
  263. "",
  264. "S",
  265. "",
  266. "SW",
  267. "",
  268. "W",
  269. "",
  270. "NW",
  271. "",
  272. ];
  273. export const ROSE_SPEED_BANDS = [
  274. "0~3 m/s",
  275. "3~6 m/s",
  276. "6~9 m/s",
  277. "9~12 m/s",
  278. ">12 m/s",
  279. ];
  280. export const ROSE_COLORS = [
  281. "#4A72B0",
  282. "#92C1E9",
  283. "#FFDF92",
  284. "#F58D66",
  285. "#E86B45",
  286. ];
  287. function getRoseRadiusMax(turbineRose) {
  288. if (!turbineRose?.bands?.length) return 14;
  289. let max = 0;
  290. turbineRose.bands.forEach((band) => {
  291. (band.values || []).forEach((value) => {
  292. const num = Number(value);
  293. if (Number.isFinite(num) && num > max) max = num;
  294. });
  295. });
  296. if (max <= 0) return 14;
  297. const interval = max <= 5 ? 1 : max <= 20 ? 3.5 : Math.ceil(max / 4);
  298. return Math.ceil(max / interval) * interval;
  299. }
  300. function getRoseBandValues(turbineRose, bandName, bandIdx) {
  301. const bands = turbineRose?.bands || [];
  302. const matched = bands.find((item) => item.band === bandName);
  303. if (matched && Array.isArray(matched.values)) return matched.values;
  304. return bands[bandIdx]?.values || [];
  305. }
  306. export function getSingleWindRoseOption(turbineIndex, roseData, turbineItem) {
  307. const overviewTheme = getOverviewTheme();
  308. const overviewTooltip = getOverviewTooltip(overviewTheme);
  309. const speedBands = roseData?.speedBands || ROSE_SPEED_BANDS;
  310. const directions = roseData?.directions || ROSE_DIRECTIONS;
  311. const turbineRose =
  312. turbineItem ||
  313. (Array.isArray(roseData?.turbines)
  314. ? roseData.turbines[turbineIndex]
  315. : null);
  316. const radiusMax = getRoseRadiusMax(turbineRose);
  317. const radiusInterval =
  318. radiusMax <= 5 ? 1 : radiusMax <= 20 ? 3.5 : radiusMax / 4;
  319. const series = speedBands.map((band, bandIdx) => ({
  320. name: band,
  321. type: "bar",
  322. coordinateSystem: "polar",
  323. stack: "wind",
  324. barWidth: "78%",
  325. itemStyle: {
  326. color: ROSE_COLORS[bandIdx % ROSE_COLORS.length],
  327. opacity: 0.96,
  328. borderColor: "rgba(255, 255, 255, 0.32)",
  329. borderWidth: 0.4,
  330. shadowBlur: 2,
  331. shadowColor: "rgba(0, 0, 0, 0.2)",
  332. },
  333. emphasis: {
  334. focus: "self",
  335. itemStyle: {
  336. opacity: 1,
  337. shadowBlur: 10,
  338. shadowColor: "rgba(146, 193, 233, 0.5)",
  339. },
  340. },
  341. data: getRoseBandValues(turbineRose, band, bandIdx),
  342. }));
  343. return {
  344. animationDuration: 600,
  345. animationEasing: "cubicOut",
  346. color: ROSE_COLORS,
  347. polar: {
  348. center: ["50%", "54%"],
  349. radius: "72%",
  350. },
  351. angleAxis: {
  352. type: "category",
  353. data: directions,
  354. boundaryGap: false,
  355. clockwise: true,
  356. startAngle: 90,
  357. axisLine: { lineStyle: { color: overviewTheme.axis } },
  358. axisTick: { show: false },
  359. axisLabel: {
  360. color: overviewTheme.text,
  361. fontSize: 9,
  362. interval: 0,
  363. margin: 4,
  364. formatter: (v) => v || "",
  365. },
  366. splitLine: {
  367. show: true,
  368. lineStyle: { color: overviewTheme.polarSplit },
  369. },
  370. },
  371. radiusAxis: {
  372. min: 0,
  373. max: radiusMax,
  374. interval: radiusInterval,
  375. axisLabel: {
  376. color: overviewTheme.text,
  377. fontSize: 8,
  378. showMinLabel: false,
  379. formatter: (value) => formatWakeMetric(value),
  380. },
  381. axisLine: { show: false },
  382. axisTick: { show: false },
  383. splitLine: {
  384. lineStyle: { color: overviewTheme.polarSplit, type: "dashed" },
  385. },
  386. splitArea: {
  387. show: true,
  388. areaStyle: {
  389. color: ["rgba(111, 239, 252, 0.04)", "transparent"],
  390. },
  391. },
  392. },
  393. tooltip: {
  394. ...overviewTooltip,
  395. trigger: "item",
  396. formatter: (p) => {
  397. const dir = p.name || "—";
  398. return `${p.marker}${
  399. p.seriesName
  400. }<br/>方向 ${dir} · 频次 ${formatWakeMetric(p.value)}`;
  401. },
  402. },
  403. series,
  404. };
  405. }