Przeglądaj źródła

rankingApp: 折线图双击图例隔离系列 + 散点按当前月 + 图例用 legend_entries 规范中文名

- EChart: 所有折线图双击图例只显示该系列,再次双击恢复
- monthPowerScatter: 只展示当前查看月份的散点
- 图例按 metric_key(+分组) 匹配 legend_entries;残差命名为「去同期机群共性的日残差」,窗口 support/current 译为支持期/本期
- 修复 darkLineOption 丢失 metricKey/groupValue 导致图例替换失效
liujiejie 4 godzin temu
rodzic
commit
686a690aba

+ 37 - 1
src/views/rankingApp/components/EChart.vue

@@ -94,12 +94,21 @@ export default {
     option: { type: Object, default: null },
   },
   data() {
-    return { chart: null, pointer: { x: 0, y: 0 } };
+    return {
+      chart: null,
+      pointer: { x: 0, y: 0 },
+      legendClickName: "",
+      legendClickAt: 0,
+      isolatedName: "",
+    };
   },
   watch: {
     option: {
       deep: true,
       handler() {
+        this.legendClickName = "";
+        this.legendClickAt = 0;
+        this.isolatedName = "";
         this.render();
       },
     },
@@ -109,6 +118,7 @@ export default {
     this.chart.getZr().on("mousemove", (event) => {
       this.pointer = { x: event.offsetX, y: event.offsetY };
     });
+    this.chart.on("legendselectchanged", this.onLegendSelectChanged);
     this.render();
     window.addEventListener("resize", this.resize);
   },
@@ -117,6 +127,32 @@ export default {
     if (this.chart) this.chart.dispose();
   },
   methods: {
+    // 折线图:双击图例只显示该系列;再次双击恢复全部
+    onLegendSelectChanged(params) {
+      const series = seriesList(this.option);
+      const isLine = series.some((item) => item && (item.type === "line" || !item.type));
+      if (!isLine) return;
+      const name = params && params.name;
+      if (!name) return;
+      const now = Date.now();
+      if (this.legendClickName === name && now - this.legendClickAt < 320) {
+        this.legendClickName = "";
+        this.legendClickAt = 0;
+        this.isolateLegend(name, series);
+      } else {
+        this.legendClickName = name;
+        this.legendClickAt = now;
+      }
+    },
+    isolateLegend(name, series) {
+      const restore = this.isolatedName === name;
+      const selected = {};
+      series.forEach((item) => {
+        if (item && item.name) selected[item.name] = restore ? true : item.name === name;
+      });
+      this.chart.setOption({ legend: { selected } });
+      this.isolatedName = restore ? "" : name;
+    },
     render() {
       if (this.chart && this.option) {
         const tooltip = this.option.keepTooltip

+ 57 - 23
src/views/rankingApp/utils/sourceFunctionView.js

@@ -702,26 +702,42 @@ export function a23MatrixOf(data, modelName) {
 function legendLabelMap(chart) {
   const byGroup = {};
   const byMetric = {};
+  const byMetricGroup = {};
   const entries = chart && Array.isArray(chart.legend_entries) ? chart.legend_entries : [];
   entries.forEach((entry) => {
     if (!entry || !entry.label) return;
     const gv = entry.group_values || [];
     if (gv.length) byGroup[gv.map(String).join("|")] = entry.label;
     if (gv.length === 1) byGroup[String(gv[0])] = entry.label;
-    if (entry.metric_key) byMetric[String(entry.metric_key)] = entry.label;
+    if (entry.metric_key) {
+      const mk = String(entry.metric_key);
+      byMetric[mk] = entry.label;
+      gv.forEach((g) => {
+        byMetricGroup[`${mk}|${String(g)}`] = entry.label;
+      });
+    }
   });
-  return { byGroup, byMetric };
+  return { byGroup, byMetric, byMetricGroup };
 }
 
-/** 用 legend_entries 的更规范中文 label 替换系列名(优先按 metric_key,其次按 group_values)。 */
+/** 用 legend_entries 的更规范中文 label 替换系列名(优先 metric_key+分组,其次 metric_key,最后按系列名)。 */
 function relabelSeries(option, labelMap) {
   if (!option || !Array.isArray(option.series)) return option;
   const byGroup = (labelMap && labelMap.byGroup) || {};
   const byMetric = (labelMap && labelMap.byMetric) || {};
+  const byMG = (labelMap && labelMap.byMetricGroup) || {};
   if (!Object.keys(byGroup).length && !Object.keys(byMetric).length) return option;
   const series = option.series.map((item) => {
     const name = String(item && item.name != null ? item.name : "");
-    const label = (item && item.metricKey && byMetric[item.metricKey]) || byGroup[name];
+    const mk = item && item.metricKey;
+    // 残差类沿用 historyLines 里的中文名(如「支持期 去同期机群共性的日残差」),不被 legend_entries 覆盖
+    if (mk === "residual" || mk === "residual_c") return item;
+    let label = null;
+    if (item && item.metricKey && item.groupValue != null && byMG[`${item.metricKey}|${item.groupValue}`]) {
+      label = byMG[`${item.metricKey}|${item.groupValue}`];
+    }
+    if (!label && item && item.metricKey) label = byMetric[item.metricKey];
+    if (!label) label = byGroup[name];
     return label ? Object.assign({}, item, { name: label }) : item;
   });
   return Object.assign({}, option, { series });
@@ -1002,7 +1018,7 @@ function buildDatasetChart(id, dataset, title, data, extras) {
     }
   }
   if (!isWindResourceChart(id, title)) {
-    const drawn = schemaChart(id, rows, keys, title, extras);
+    const drawn = schemaChart(id, rows, keys, title, extras, data);
     if (drawn) return drawn;
   }
   const xKey = X_KEYS.find((key) => keys.includes(key));
@@ -1190,6 +1206,8 @@ function darkLineOption(series, extra) {
       const guide = index === 0 ? opt.guide : null;
       return {
         name: item.name,
+        metricKey: item.metricKey,
+        groupValue: item.groupValue,
         type: item.type || "line",
         showSymbol: item.showSymbol != null ? item.showSymbol : item.type === "scatter",
         symbol: item.symbol || "circle",
@@ -1278,7 +1296,7 @@ function isWindResourceChart(id, title) {
   return /wind_frequency|rose_project_aggregate|rose_bins|relative_wind_monthly|wind_ratio_history|unit_wind_ratio_history|风速频率|风向玫瑰|风速比/.test(`${id} ${title || ""}`);
 }
 
-function schemaChart(id, rows, sourceKeys, title, extras) {
+function schemaChart(id, rows, sourceKeys, title, extras, data) {
   const keys = [];
   rows.forEach((row) => {
     Object.keys(row || {}).forEach((key) => {
@@ -1321,7 +1339,9 @@ function schemaChart(id, rows, sourceKeys, title, extras) {
     return scatterOf(id, title, rows, "power_kw", "pitch_deg", "功率 kW", "桨距角 °");
   }
   if (has("month") && has("generator_rpm") && has("power_kw") && !has("wind_ms")) {
-    return monthPowerScatter(id, title, rows);
+    // 只展示当前查看月份(所选周期)的散点
+    const curMonth = data ? (data.month || (data.identity && data.identity.month) || "") : "";
+    return monthPowerScatter(id, title, rows, curMonth);
   }
   if (has("role") && has("month") && has("wind_ms") && has("power_kw")) {
     return monthContractScatter(id, title, rows);
@@ -1833,18 +1853,27 @@ function monthContractScatter(id, title, rows) {
   };
 }
 
-function monthPowerScatter(id, title, rows) {
+function monthPowerScatter(id, title, rows, currentMonth) {
   const months = uniqueValues(rows, "month");
-  const series = months.map((month, index) => {
-    const data = rows.filter((row) => String(row.month) === month).map((row) => [
-      Number(row.generator_rpm),
-      Number(row.power_kw),
-    ]).filter((point) => point.every(Number.isFinite));
-    if (!data.length) return null;
-    return { name: month, type: "scatter", symbolSize: 6, data, color: windColor(index, months.length) };
-  }).filter(Boolean);
-  if (!series.length) return null;
-  return { id, title, option: darkLineOption(series, { xName: "转速 rpm", yName: "功率 kW", yMin: 0 }) };
+  // 只展示当前查看月份(所选周期)的散点;无法匹配时回退到数据里最新月份
+  const target = currentMonth && months.includes(currentMonth)
+    ? currentMonth
+    : (months.slice().sort().pop() || "");
+  if (!target) return null;
+  const data = rows
+    .filter((row) => String(row.month) === String(target))
+    .map((row) => [Number(row.generator_rpm), Number(row.power_kw)])
+    .filter((point) => point.every(Number.isFinite));
+  if (!data.length) return null;
+  return {
+    id,
+    title,
+    option: darkLineOption([{ name: target, type: "scatter", symbolSize: 6, data }], {
+      xName: "转速 rpm",
+      yName: "功率 kW",
+      yMin: 0,
+    }),
+  };
 }
 
 function contractRows(rows, extra) {
@@ -2052,11 +2081,13 @@ function historyLines(id, title, rows, extras) {
     const groups = splitGroups(bundle.rows);
     groups.forEach((group) => {
       valueFields.forEach(([key, label]) => {
-        const valueLabel = residual && key === "residual_c" ? "扣除同型机组共同变化后的温度偏差" : label;
-        const groupName = residual && groups.length > 1 ? windowRoleLabel(group.name) : group.name;
-        const item = pairSeries(group.rows, xKey, key, historyName(bundleName, residual ? groupName : group.name, valueLabel));
+        const valueLabel = key === "residual_c" ? "扣除同型机组共同变化后的温度偏差"
+          : (key === "residual" ? "去同期机群共性的日残差" : label);
+        const groupName = windowRoleLabel(group.name);
+        const item = pairSeries(group.rows, xKey, key, historyName(bundleName, groupName, valueLabel));
         if (!item) return;
         item.metricKey = key;
+        item.groupValue = group.name;
         if (label === "EWMA") {
           item.dash = true;
           item.color = "#c29345";
@@ -2066,15 +2097,16 @@ function historyLines(id, title, rows, extras) {
       });
       limitFields.forEach(([key, label]) => {
         const limitLabel = residual && key === "threshold_c" ? "偏热判断线" : label;
-        const groupName = residual && groups.length > 1 ? windowRoleLabel(group.name) : group.name;
+        const groupName = windowRoleLabel(group.name);
         const constVal = constantLimit(group.rows, key);
         if (constVal != null) {
           guides.push({ y: constVal, label: `${limitLabel} ${formatGuide(constVal)}`, color: residual && key === "threshold_c" ? "#d08a3a" : "#e07070" });
           return;
         }
-        const item = pairSeries(group.rows, xKey, key, historyName(bundleName, residual ? groupName : group.name, limitLabel));
+        const item = pairSeries(group.rows, xKey, key, historyName(bundleName, groupName, limitLabel));
         if (!item) return;
         item.metricKey = key;
+        item.groupValue = group.name;
         item.dash = true;
         item.color = residual && key === "threshold_c" ? "#d08a3a" : "#e07070";
         item.limit = true;
@@ -2198,6 +2230,8 @@ function ambientHistoryChart(id, title, bundles, xKey) {
 function windowRoleLabel(name) {
   if (name === "prior") return "参考窗口";
   if (name === "detection") return "检测窗口";
+  if (name === "support") return "支持期";
+  if (name === "current") return "本期";
   return name || "";
 }