P26:标签按"标签列"1:1 落到记录、窗口、元素、文本四处(规范 §35)

- XChartColumnMath 补标签半边(LabelCount / LabelSlotOf / DefinitionOfLabelSlot / SizedLabels),
  记录的标签长度只由这一处定;TryAddRecord 在 labels 长度≠标签列数时整条拒收
- 导入按标签字段个数校验并点名行号;记录行少到连时间戳都没有时点名拒绝(不再抛异常)
- 数据窗口:一个显示序两个读者(标签列在前),格子身份 CellRef{IsLabel,Slot},
  标签格走 TrySetLabel 的字符串分支,「+标签」走 SetDimensions 这唯一定长路径
- 新标签元素 ChartToolLabels + XChartLabelConfig:13 旋钮全自动置脏、位置/折法同散点、
  文本走池且多余槽位必须擦、锚点与对齐出自同一张表(anchorMin/Max 不写)
- 面板把"要几个标签列、表里有几个"与数值维、记录数一起印(§25.1 第三半 RequiredLabelColumns)
- 探针先红的两个真缺陷:SizedLabels(null,n) 补 null 不是空串;锚点表漏 MiddleRight 与 Center 重合
- 雷达 AddSeries 带系列名,两个 SetDimensions 重载都声明 series 标签列(否则新门口整条拒收)
- 判据:探针 492/0、label_gate 15 claim/15 臂全红、dirty_gate 149/149、slot_reset PASS、
  编译门三臂+3 对照绿(source present 点名四个新文件)、p4_control 78 支各红在点名断言
  (同一棵树指纹 478727e6748c67cddda40995874b20968e9eae3706f2a21c24726d3412059747)
This commit is contained in:
2026-11-09 06:58:39 +08:00
parent 4a40b317be
commit 084591d104
18 changed files with 968 additions and 84 deletions
+20 -6
View File
@@ -208,18 +208,32 @@ namespace XericUI.XChart.Radar
/// <summary>设置维度名称</summary>
public void SetDimensions(params string[] dimensionNames)
{
m_DataModel.SetDimensions(dimensionNames);
var dimensions = new System.Collections.Generic.List<XChartDimensionDefinition>(dimensionNames.Length);
for (int i = 0; i < dimensionNames.Length; i++)
dimensions.Add(new XChartDimensionDefinition($"radar_{i}", dimensionNames[i], XChartDimensionKind.Number));
DataModel.SetDimensions(dimensions);
// 由 Update 自动同步
ApplyDimensionNames(dimensionNames);
}
/// <summary>设置维度名称(从列表)</summary>
public void SetDimensions(System.Collections.Generic.IList<string> dimensionNames)
{
var names = new string[dimensionNames != null ? dimensionNames.Count : 0];
for (int i = 0; i < names.Length; i++) names[i] = dimensionNames[i];
ApplyDimensionNames(names);
}
/// <summary>
/// 两个重载都走这里:<see cref="AddSeries"/> 给每条记录带一个系列名,
/// 而共享表要有一个<b>标签列</b>那个名字才落得进去(1:1 是门口那条规矩)。
/// 只更新雷达自己的模型、不交代共享表,系列名就会被整条记录一起拒掉。
/// </summary>
private void ApplyDimensionNames(string[] dimensionNames)
{
m_DataModel.SetDimensions(dimensionNames);
if (dimensionNames == null) return;
var dimensions = new System.Collections.Generic.List<XChartDimensionDefinition>(dimensionNames.Length + 1);
for (int i = 0; i < dimensionNames.Length; i++)
dimensions.Add(new XChartDimensionDefinition($"radar_{i}", dimensionNames[i], XChartDimensionKind.Number));
dimensions.Add(new XChartDimensionDefinition("series", "系列", XChartDimensionKind.Label));
DataModel.SetDimensions(dimensions);
}
/// <summary>添加一个数据系列(自动分配颜色)</summary>
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2b931f6ed464bca4daa43d0230027e92
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+268
View File
@@ -0,0 +1,268 @@
using System;
using TMPro;
using UnityEngine;
using XericUI.Core;
using XericUI.XChart.Space;
namespace XericUI.XChart.Text
{
/// <summary>
/// 数据标签元素:一条记录一个标签,标签读的是那一行在<b>某一标签列</b>上的字符串。
///
/// <para><b>1:1 是结构保证而不是约定</b>:记录身上的标签数组长度=标签列个数,
/// 所以这一列读得到的就是那一行的那一格;比列更多的字符串没有列叫得出它的名字,
/// 它在窗口里显示得出来、元素读不到(口径只有一份:<see cref="XChartColumnMath"/>)。
/// 一条记录配几个数值维+一个字符串维,画面上就是"点+它自己的名字"。</para>
///
/// <para><b>位置从哪儿来</b>:和散点/圆柱/矢量场<b>同一个</b>空间与同一份折法
/// (<see cref="XChartCubeFoldMath"/>),显示范围只定标尺;超范围怎么办由这一档自己决定(§32)。
/// <b>显示从哪儿来</b>:文本槽位池(运行时里唯一造 TMP 标签的地方),一件对应一条记录。</para>
///
/// <para><b>锚点和对齐出自同一张表</b>:贴在左下角却把字居中,字就会横跨在那一点两侧。
/// 锚点决定矩形从哪一侧从点起排,对齐由同一个锚点算出来(<see cref="XChartLabelAnchorMath"/>)。</para>
///
/// 子节点结构:
/// [Labels]
/// └─ GeneratedTexts(文本槽位的宿主)
/// </summary>
[AddComponentMenu(XericRegistrationPaths.Chart + "/Text/Data Label Tool")]
public class ChartToolLabels : XChartToolBase
{
// ── 数据装配 ──────────────────────────────────────────────
private int m_LabelSlot;
/// <summary>读第几个<b>标签列</b>(不是表里的行号;换算只有 <see cref="XChartColumnMath.LabelSlotOf"/> 那一处)</summary>
public int LabelSlot { get => m_LabelSlot; set { m_LabelSlot = value; MarkDirty(); } }
private int m_DimensionX;
/// <summary>标签挂在哪条记录的哪一个位置:三个数值维下标(显示范围归 chart 的数据维度行)</summary>
public int DimensionX { get => m_DimensionX; set { m_DimensionX = value; MarkDirty(); } }
private int m_DimensionY = 1;
public int DimensionY { get => m_DimensionY; set { m_DimensionY = value; MarkDirty(); } }
private int m_DimensionZ = 2;
public int DimensionZ { get => m_DimensionZ; set { m_DimensionZ = value; MarkDirty(); } }
private XChartToolTransform m_Transform = XChartToolTransform.Identity;
public XChartToolTransform Transform { get => m_Transform; set { m_Transform = value; MarkDirty(); } }
private XChartValueOutOfRange m_OutOfRange = XChartValueOutOfRange.RenderAsIs;
/// <summary>标签的位置受显示范围之外的影响时怎么办(§32:范围只定标尺,画不画由数据图元自己决定)</summary>
public XChartValueOutOfRange OutOfRange { get => m_OutOfRange; set { m_OutOfRange = value; MarkDirty(); } }
// ── 外观 ──────────────────────────────────────────────────
private XChartLabelAnchor m_Anchor = XChartLabelAnchor.UpperCenter;
/// <summary>标签矩形相对它那一点的位置(9 档)。<b>文字对齐由同一档算出来</b>,不另给一个字段</summary>
public XChartLabelAnchor Anchor { get => m_Anchor; set { m_Anchor = value; MarkDirty(); } }
private Vector2 m_Offset = new Vector2(0f, 6f);
/// <summary>从那个点再挪多少像素(非有限数按零算:NaN 进 anchoredPosition 会把这一件字整个挪出画面而不报错)</summary>
public Vector2 Offset { get => m_Offset; set { m_Offset = value; MarkDirty(); } }
private int m_FontSize = 14;
public int FontSize { get => m_FontSize; set { m_FontSize = Mathf.Max(1, value); MarkDirty(); } }
private Color m_LabelColor = Color.white;
public Color LabelColor { get => m_LabelColor; set { m_LabelColor = value; MarkDirty(); } }
private float m_OutlineWidth;
public float OutlineWidth { get => m_OutlineWidth; set { m_OutlineWidth = Mathf.Max(0f, value); MarkDirty(); } }
private Vector2 m_RectSize = new Vector2(160f, 30f);
public Vector2 RectSize { get => m_RectSize; set { m_RectSize = new Vector2(Mathf.Max(0f, value.x), Mathf.Max(0f, value.y)); MarkDirty(); } }
private bool m_Wrap;
public bool Wrap { get => m_Wrap; set { m_Wrap = value; MarkDirty(); } }
// ── 内部 ──────────────────────────────────────────────────
private bool m_GeometryDirty = true;
private long m_LastDataVersion = -1;
private int m_LastSpaceVersion = -1;
private Matrix4x4 m_Composed = Matrix4x4.identity;
private int m_Records;
private int m_Written;
private int m_Cleared;
private int m_Empty;
private int m_Hidden;
private int m_Skipped;
public override void MarkDirty() => m_GeometryDirty = true;
public override void OnRender()
{
base.OnRender();
var space = Context?.Space;
if (space == null) return;
var data = Data;
bool spaceChanged = space.ProjectionVersion != m_LastSpaceVersion;
bool dataChanged = data != null && data.Version != m_LastDataVersion;
if (!m_GeometryDirty && !spaceChanged && !dataChanged) return;
if (spaceChanged || dataChanged) m_GeometryDirty = true;
m_LastSpaceVersion = space.ProjectionVersion;
m_LastDataVersion = data != null ? data.Version : -1;
m_Composed = space.Compose(Transform);
if (data == null || !data.HasData)
{
m_Records = 0;
RequestTextSlots(0);
ClearTexts();
m_GeometryDirty = false;
return;
}
int records = data.RecordCount;
m_Records = records;
RequestTextSlots(records);
double minX, maxX, minY, maxY, minZ, maxZ;
ResolveRange(DimensionX, out minX, out maxX);
ResolveRange(DimensionY, out minY, out maxY);
ResolveRange(DimensionZ, out minZ, out maxZ);
var texts = Texts;
m_Written = 0;
m_Cleared = 0;
m_Empty = 0;
m_Hidden = 0;
m_Skipped = 0;
for (int i = 0; i < records; i++)
{
bool ready = i < texts.Count && texts[i] != null;
string text;
if (!data.TryGetLabelAt(i, LabelSlot, out text))
{
// 这一列没有这一格:数据不可用(不是"标签是空的")
m_Skipped++;
if (ready) BlankSlot(texts[i]);
continue;
}
if (string.IsNullOrEmpty(text))
{
// 那一格是空的(被清空过):这一件不画字,照实擦掉上一轮的字
m_Empty++;
if (ready) BlankSlot(texts[i]);
continue;
}
double x = data.GetValueAt(i, DimensionX);
double y = data.GetValueAt(i, DimensionY);
double z = data.GetValueAt(i, DimensionZ);
// 三个 out 先给初值:`&&` 短路时后面那两次调用根本没跑,编译器认不出"折得成就说明三个都赋过"
// (散点那一处是 `if (!a || !b || !c) continue;` 的写法,所以它没这个问题)
float fx = 0f, fy = 0f, fz = 0f;
bool foldable = XChartCubeFoldMath.TryRatio01(x, minX, maxX, OutOfRange, out fx)
&& XChartCubeFoldMath.TryRatio01(y, minY, maxY, OutOfRange, out fy)
&& XChartCubeFoldMath.TryRatio01(z, minZ, maxZ, OutOfRange, out fz);
if (!foldable)
{
// 三个数都可用却被拒 ⇒ 是这一档主动不画;否则是坏数据(本来就不该画,另算)
if (!IsBad(x) && !IsBad(y) && !IsBad(z)) m_Hidden++;
else m_Skipped++;
if (ready) BlankSlot(texts[i]);
continue;
}
if (!ready) continue; // 件还没到位:欠着件不算"这一行没标签"(pending= 那一行说的是它)
Vector2 local = space.Project(ChartToolSpace.NormalizedToCube(new Vector3(fx, fy, fz)), m_Composed)
+ XChartLabelAnchorMath.SafeOffset(Offset);
WriteLabel(texts[i], local, text);
m_Written++;
}
// 槽位数比记录数多(上一轮表更长)时多出来的那些必须擦干净,否则上一轮的标签留在原地
for (int i = records; i < texts.Count; i++)
if (texts[i] != null) BlankSlot(texts[i]);
m_GeometryDirty = texts.Count < records;
}
private void BlankSlot(TMP_Text slot)
{
slot.text = string.Empty;
m_Cleared++;
}
private void WriteLabel(TMP_Text label, Vector2 local, string text)
{
Vector2 anchor = XChartLabelAnchorMath.AnchorOf(Anchor);
label.text = text;
label.fontSize = FontSize;
label.color = LabelColor;
label.enableWordWrapping = Wrap;
label.outlineWidth = OutlineWidth;
label.alignment = XChartLabelAnchorMath.AlignmentFor(Anchor);
var rect = label.rectTransform;
rect.sizeDelta = RectSize;
// 锚点写在 pivot 上:这样"那一格从点起排"和"点正好落在矩形的这一角"是同一件事。
// anchorMin/Max 留在池的默认(中心),否则 anchoredPosition 的参照点会跟着换,
// 同一个读数在两种锚法下答得不同。
rect.pivot = anchor;
rect.anchoredPosition = local;
}
private void ClearTexts()
{
var texts = Texts;
m_Cleared = 0;
for (int i = 0; i < texts.Count; i++)
if (texts[i] != null) BlankSlot(texts[i]);
}
/// <summary>显示范围只问上下文要(覆盖优先、否则该维聚合):元素自己存一份范围就是"标签和轴对不上"的来路</summary>
private void ResolveRange(int dimension, out double min, out double max)
{
var context = Context;
if (context != null && context.TryResolveDomain(dimension, out min, out max)) return;
min = 0.0;
max = 1.0;
}
private static bool IsBad(double value)
=> double.IsNaN(value) || double.IsInfinity(value);
protected override void OnDispose()
{
m_Records = 0;
m_Written = 0;
}
#if UNITY_EDITOR
/// <summary>
/// 报表里这一行只印"这一帧到底有没有写、没写的话卡在哪":<b>数据侧</b>(skipped/hidden/empty)
/// 与<b>槽位侧</b>(want/have/pending)必须分开,否则"这一行没标签"和"件还没发落"在画面上同形。
/// </summary>
internal override void AppendDebugLine(System.Text.StringBuilder sb)
{
base.AppendDebugLine(sb);
sb.Append(" labelSlot=").Append(LabelSlot)
.Append(" dim=").Append(DimensionX).Append('/').Append(DimensionY).Append('/').Append(DimensionZ)
.Append(" records=").Append(m_Records)
.Append(" want=").Append(RequestedTextCount)
.Append(" have=").Append(Texts.Count)
.Append(" pending=").Append(TextSlotsPending)
.Append(" written=").Append(m_Written)
.Append(" cleared=").Append(m_Cleared)
.Append(" empty=").Append(m_Empty)
.Append(" hidden=").Append(m_Hidden)
.Append(" skipped=").Append(m_Skipped)
.Append(" out=").Append(OutOfRange.ToString().ToLower())
.Append(" anchor=").Append(Anchor.ToString().ToLower())
.Append(" wrap=").Append(Wrap);
}
#endif
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 04e8bfc387c126047b089da0749b1cda
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,72 @@
using System;
using TMPro;
using UnityEngine;
namespace XericUI.XChart.Text
{
/// <summary>标签矩形相对它那一点的位置:9 档(四角 + 四边中点 + 中心)。</summary>
public enum XChartLabelAnchor
{
LowerLeft,
LowerCenter,
LowerRight,
MiddleLeft,
Center,
MiddleRight,
UpperLeft,
UpperCenter,
UpperRight,
}
/// <summary>
/// 锚点的<b>纯算</b>部分(离线可验)。
///
/// <para><b>为什么锚点与对齐必须出自同一张表</b>:锚定在左下角的矩形如果把字居中对齐,
/// 字就横跨在那一点两侧,"标签贴在点上"这句话当场作废。以前这两件事各写各的
/// (锚点一处、对齐来自池的默认值),所以改锚点而字仍居中是画面上最难归因的一种错。</para>
/// </summary>
public static class XChartLabelAnchorMath
{
/// <summary>九档锚点各自的那个位置([0,1] 内的矩形锚位)。九档互不相同是判据,不是巧合。</summary>
public static Vector2 AnchorOf(XChartLabelAnchor anchor)
{
switch (anchor)
{
case XChartLabelAnchor.LowerLeft: return new Vector2(0f, 0f);
case XChartLabelAnchor.LowerCenter: return new Vector2(0.5f, 0f);
case XChartLabelAnchor.LowerRight: return new Vector2(1f, 0f);
case XChartLabelAnchor.MiddleLeft: return new Vector2(0f, 0.5f);
case XChartLabelAnchor.MiddleRight: return new Vector2(1f, 0.5f);
case XChartLabelAnchor.UpperLeft: return new Vector2(0f, 1f);
case XChartLabelAnchor.UpperCenter: return new Vector2(0.5f, 1f);
case XChartLabelAnchor.UpperRight: return new Vector2(1f, 1f);
default: return new Vector2(0.5f, 0.5f); // Center
}
}
/// <summary>
/// 这一档锚点对应的文字对齐:<b>由同一个锚点算出来</b>(横分量取锚点的 x、纵分量取锚点的 y)。
/// 这样"贴在左下角"和"字从左下角起排"是同一件事,不再有第二处定义。
/// </summary>
public static TextAlignmentOptions AlignmentFor(XChartLabelAnchor anchor)
{
Vector2 a = AnchorOf(anchor);
if (Mathf.Abs(a.x - 0.5f) < 1e-4f && Mathf.Abs(a.y - 0.5f) < 1e-4f) return TextAlignmentOptions.Center;
if (Mathf.Abs(a.y - 0.5f) < 1e-4f) return a.x < 0.5f ? TextAlignmentOptions.MidlineLeft : TextAlignmentOptions.MidlineRight;
if (Mathf.Abs(a.x - 0.5f) < 1e-4f) return a.y < 0.5f ? TextAlignmentOptions.Bottom : TextAlignmentOptions.Top;
if (a.y < 0.5f) return a.x < 0.5f ? TextAlignmentOptions.BottomLeft : TextAlignmentOptions.BottomRight;
return a.x < 0.5f ? TextAlignmentOptions.TopLeft : TextAlignmentOptions.TopRight;
}
/// <summary>
/// 偏移:<b>非有限数当成零</b>。NaN 进 anchoredPosition 会把这一件字整个挪出画面,
/// 而且它不报错——这正是"计数全对、画面空无一物"那一族。
/// </summary>
public static Vector2 SafeOffset(Vector2 offset)
{
if (float.IsNaN(offset.x) || float.IsInfinity(offset.x)) offset.x = 0f;
if (float.IsNaN(offset.y) || float.IsInfinity(offset.y)) offset.y = 0f;
return offset;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 887ef129beec6e841875636169cb0a4d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+101
View File
@@ -0,0 +1,101 @@
using System;
using UnityEngine;
using XericUI.XChart.Space;
namespace XericUI.XChart.Text
{
/// <summary>
/// 数据标签元素的配置。<b>面板这一侧只存"选哪一档/哪一列"</b>:
/// 锚点与文字对齐的对应关系住在 <see cref="XChartLabelAnchorMath"/>,
/// 列号到标签槽的换算住在 <see cref="XChartColumnMath"/>——配置里再写一遍就会各说一套。
///
/// <para><c>LabelSlot</c> 是<b>标签列的序号</b>(不是数据表里的行号):窗口里排在最前面那些列就是标签列,
/// 面板上看到的列号要换算成槽号才读得对,所以这一格存的是槽号而由同一处算出来。</para>
/// </summary>
[Serializable]
[XChartElement("label", "标签", 60)]
public sealed class XChartLabelConfig : XChartElementConfig
{
[Header("数据来源")]
[SerializeField, Min(0), Tooltip("读第几个标签列(标签列在数据表里排在最前面;这一格是槽号,不是表里的行号)")]
private int m_LabelSlot;
[SerializeField, Tooltip("标签挂在哪儿:三个数值维下标(显示范围在 chart 的数据板块逐维给)")]
private int m_DimensionX;
[SerializeField] private int m_DimensionY = 1;
[SerializeField] private int m_DimensionZ = 2;
[SerializeField, Tooltip("标签自己的变换(叠在图的空间之上)")]
private XChartToolTransform m_Transform = XChartToolTransform.Identity;
[SerializeField, Tooltip("数据落在显示范围之外时怎么办:原样画(默认)/ 夹到范围端点 / 整条隐藏")]
private XChartValueOutOfRange m_OutOfRange = XChartValueOutOfRange.RenderAsIs;
[Header("呈现")]
[SerializeField, Tooltip("标签矩形相对它那一点的位置(9 档)。文字对齐由同一档算出来,不另给一个字段")]
private XChartLabelAnchor m_Anchor = XChartLabelAnchor.UpperCenter;
[SerializeField, Tooltip("从那个点再挪多少像素(非有限数按零算)")]
private Vector2 m_Offset = new Vector2(0f, 6f);
[SerializeField, Min(1), Tooltip("字号")]
private int m_FontSize = 14;
[SerializeField, Tooltip("字色")]
private Color m_LabelColor = Color.white;
[SerializeField, Min(0f), Tooltip("描边宽度")]
private float m_OutlineWidth;
[SerializeField, Tooltip("标签矩形大小(像素)。字超出的部分按下面的换行档处理")]
private Vector2 m_RectSize = new Vector2(160f, 30f);
[SerializeField, Tooltip("字是否在这个矩形内换行")]
private bool m_Wrap;
public override string ElementName => "Label";
public override XChartToolBase CreateTool() => new ChartToolLabels();
public override void ApplyTo(XChartToolBase tool)
{
if (!(tool is ChartToolLabels labels)) return;
labels.LabelSlot = m_LabelSlot;
labels.DimensionX = m_DimensionX;
labels.DimensionY = m_DimensionY;
labels.DimensionZ = m_DimensionZ;
labels.Transform = m_Transform;
labels.OutOfRange = m_OutOfRange;
labels.Anchor = m_Anchor;
labels.Offset = m_Offset;
labels.FontSize = m_FontSize;
labels.LabelColor = m_LabelColor;
labels.OutlineWidth = m_OutlineWidth;
labels.RectSize = m_RectSize;
labels.Wrap = m_Wrap;
labels.MarkDirty();
}
public override void CaptureFrom(XChartToolBase tool)
{
if (!(tool is ChartToolLabels labels)) return;
m_LabelSlot = labels.LabelSlot;
m_Anchor = labels.Anchor;
m_Offset = labels.Offset;
m_FontSize = labels.FontSize;
m_LabelColor = labels.LabelColor;
m_RectSize = labels.RectSize;
m_Wrap = labels.Wrap;
}
public override int RequiredNumericDimensions => 3;
public override int RequiredRecordCount => 1;
public override int RequiredLabelColumns => 1;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c3ac62f975d4c9f4180064e4839edabc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+65
View File
@@ -60,5 +60,70 @@ namespace XericUI.XChart
}
return null;
}
// ── 标签列(String 那一族):与数值列同一套规则,只是占的是标签数组的格 ──
//
// 记录身上有两个数组:值数组的长度=<see cref="ValueCount"/>,标签数组的长度=
// <see cref="LabelCount"/>。<b>标签数组的长度就是它自己的判据</b>:一条记录能写出
// 比标签列更多的字符串,那些字符串没有一个列能叫出名字,屏幕上便凭空多出一个标签
// (以前这里靠调用方自觉,所以导入能塞进去、窗口显示得出来、元素却读不到)。
/// <summary>标签列的个数=每条记录标签数组的长度(非标签列不占格)</summary>
public static int LabelCount(IReadOnlyList<XChartDimensionDefinition> dimensions)
{
if (dimensions == null) return 0;
int count = 0;
for (int i = 0; i < dimensions.Count; i++)
if (dimensions[i] != null && dimensions[i].Kind == XChartDimensionKind.Label) count++;
return count;
}
/// <summary>
/// 表里第 <paramref name="tableIndex"/> 行对应的<b>标签下标</b>;
/// 那一列不占标签格(种类不是 Label)或行号越界时返回 <c>-1</c>。
/// </summary>
public static int LabelSlotOf(IReadOnlyList<XChartDimensionDefinition> dimensions, int tableIndex)
{
if (dimensions == null || tableIndex < 0 || tableIndex >= dimensions.Count) return -1;
var target = dimensions[tableIndex];
if (target == null || target.Kind != XChartDimensionKind.Label) return -1;
int slot = 0;
for (int i = 0; i < tableIndex; i++)
if (dimensions[i] != null && dimensions[i].Kind == XChartDimensionKind.Label) slot++;
return slot;
}
/// <summary>
/// 把一条记录的标签数组按<b>标签列个数</b>定长——这是"记录标签长度"唯一的定义处:
/// 按位置逐格搬(不是按列号搬:列号已经被调用方换算过),短的补空串、长的截掉。
/// <b>加/删列之后重排就靠它</b>,所以各写入路径不许各自 Copy 一遍。
/// </summary>
public static string[] SizedLabels(IReadOnlyList<string> labels, int labelCount)
{
if (labelCount < 0) labelCount = 0;
var copy = new string[labelCount];
// 空位一律写空串,不写 null:null 与 "" 在窗口里都显示为空,却在"这一格是空的"这条判据上
// 各答一套(导入写出的是 "",而这里补 null,往返一次就变了样)。
for (int i = 0; i < labelCount; i++)
copy[i] = labels != null && i < labels.Count ? labels[i] : string.Empty;
return copy;
}
/// <summary>反方向:标签下标属于表里哪一列(拿到那一列的定义)——与 <see cref="DefinitionOfValueSlot"/> 对称</summary>
public static XChartDimensionDefinition DefinitionOfLabelSlot(
IReadOnlyList<XChartDimensionDefinition> dimensions, int labelSlot)
{
if (dimensions == null || labelSlot < 0) return null;
int slot = 0;
for (int i = 0; i < dimensions.Count; i++)
{
var candidate = dimensions[i];
if (candidate == null || candidate.Kind != XChartDimensionKind.Label) continue;
if (slot == labelSlot) return candidate;
slot++;
}
return null;
}
}
}
+56 -17
View File
@@ -222,17 +222,26 @@ namespace XericUI.XChart
/// </summary>
public int NumericDimensionCount => XChartColumnMath.ValueCount(m_Dimensions);
/// <summary>
/// 标签列个数=每条记录标签数组的长度(<see cref="XChartColumnMath.LabelCount"/>,唯一那一条规则)。
/// <b>1:1 是形状而不是一句承诺</b>:记录身上可以写出比标签列更多的字符串,那些字符串没有一个列
/// 叫得出名字,屏幕上就凭空多出一个标签,而报表里看不出是谁写的。
/// </summary>
public int LabelDimensionCount => XChartColumnMath.LabelCount(m_Dimensions);
public void SetDimensions(IList<XChartDimensionDefinition> dimensions)
{
m_Dimensions.Clear();
if (dimensions != null) for (int i = 0; i < dimensions.Count; i++) m_Dimensions.Add(dimensions[i]);
int numericCount = NumericDimensionCount;
int labelCount = LabelDimensionCount;
for (int i = 0; i < m_Records.Count; i++)
{
var values = new float[numericCount];
int copyCount = Math.Min(values.Length, m_Records[i].Values.Count);
for (int j = 0; j < copyCount; j++) values[j] = m_Records[i].Values[j];
m_Records[i] = new XChartRecord(m_Records[i].RecordId, m_Records[i].Timestamp, values, CopyLabels(m_Records[i].Labels));
m_Records[i] = new XChartRecord(m_Records[i].RecordId, m_Records[i].Timestamp, values,
XChartColumnMath.SizedLabels(m_Records[i].Labels, labelCount));
}
Publish();
}
@@ -293,19 +302,26 @@ namespace XericUI.XChart
// 值数组按"数值维中的序号"排,不是按维度表下标排:中间夹一个非数值维时两者会错开
// 值下标由那一条规则给(非数值列返回 -1:本来就没有格要摘)
int column = XChartColumnMath.ValueSlot(m_Dimensions, index);
int labelSlot = XChartColumnMath.LabelSlotOf(m_Dimensions, index);
m_Dimensions.RemoveAt(index);
if (column >= 0)
if (column >= 0 || labelSlot >= 0)
{
for (int i = 0; i < m_Records.Count; i++)
{
var record = m_Records[i];
if (column >= record.Values.Count) continue; // 这一行短了一截:本就没有这一格
var values = new float[record.Values.Count - 1];
// 标签列同理:摘掉的是"这一列在标签数组里那一格",不是"第 index 个字符串"
var values = new float[column >= 0 ? record.Values.Count - 1 : record.Values.Count];
for (int j = 0, k = 0; j < record.Values.Count; j++)
if (j != column) values[k++] = record.Values[j];
m_Records[i] = new XChartRecord(record.RecordId, record.Timestamp, values, CopyLabels(record.Labels));
if (column < 0 || j != column) values[k++] = record.Values[j];
var labels = new string[labelSlot >= 0 ? record.Labels.Count - 1 : record.Labels.Count];
for (int j = 0, k = 0; j < record.Labels.Count; j++)
if (labelSlot < 0 || j != labelSlot) labels[k++] = record.Labels[j];
m_Records[i] = new XChartRecord(record.RecordId, record.Timestamp, values, CopyLabels(labels));
}
}
@@ -313,10 +329,17 @@ namespace XericUI.XChart
return true;
}
/// <summary>
/// 加一条记录。两个数组都必须<b>对得上当前的列</b>:值数组长度=数值列个数,
/// 标签数组长度=标签列个数(或 <c>null</c>=这一条还没有标签)。
/// 长度不对就整条拒收、不改一半——"多出来的那个字符串"没有列叫得出它的名字,
/// 落进表里就是屏幕上一个没主的标签(1:1 靠门口这一关守住,不靠调用方自觉)。
/// </summary>
public bool TryAddRecord(double timestamp, float[] values, string[] labels = null)
{
if (double.IsNaN(timestamp) || double.IsInfinity(timestamp) || values == null || values.Length != NumericDimensionCount) return false;
for (int i = 0; i < values.Length; i++) if (float.IsNaN(values[i]) || float.IsInfinity(values[i])) return false;
if (labels != null && labels.Length != LabelDimensionCount) return false;
if (m_Records.Count > 0 && timestamp < m_Records[m_Records.Count - 1].Timestamp) return false;
if (m_Records.Count == Capacity)
{
@@ -325,7 +348,7 @@ namespace XericUI.XChart
}
var copy = new float[values.Length];
Array.Copy(values, copy, values.Length);
m_Records.Add(new XChartRecord(m_NextRecordId++, timestamp, copy, labels ?? Array.Empty<string>()));
m_Records.Add(new XChartRecord(m_NextRecordId++, timestamp, copy, CopyLabels(labels)));
Publish();
return true;
}
@@ -377,15 +400,19 @@ namespace XericUI.XChart
return true;
}
/// <summary>改某条记录的某个标签(下标越界或空串一律拒绝)</summary>
public bool TrySetLabel(int recordIndex, int labelIndex, string text)
/// <summary>
/// 改某条记录在某一标签列上的字符串。<b>下标是标签下标</b>(第几个标签列,不是表里的行号),
/// 由 <see cref="XChartColumnMath.LabelSlotOf"/> 换算。行号或下标越界拒绝;<b>空串是合法值</b>
/// (把标签清空),所以清空与"这一列本来没有格"分得开——后者压根进不到这里。
/// </summary>
public bool TrySetLabel(int recordIndex, int labelSlot, string text)
{
if (recordIndex < 0 || recordIndex >= m_Records.Count) return false;
var record = m_Records[recordIndex];
if (labelIndex < 0 || labelIndex >= record.Labels.Count) return false;
if (labelSlot < 0 || labelSlot >= LabelDimensionCount) return false;
var labels = CopyLabels(record.Labels);
labels[labelIndex] = text ?? string.Empty;
labels[labelSlot] = text ?? string.Empty;
m_Records[recordIndex] = new XChartRecord(record.RecordId, record.Timestamp, CopyValues(record.Values), labels);
Publish();
return true;
@@ -484,6 +511,20 @@ namespace XericUI.XChart
return definition != null;
}
/// <summary>
/// 取某条记录在某一标签列上的字符串(<b>标签下标</b>,第几个标签列)。
/// 行号或下标越界返回 false;列存在但那一格是空串时返回 true + 空串——
/// "这一列没这一格"和"这一格是空的"是两件事,画不画由调用方决定。
/// </summary>
public bool TryGetLabelAt(int recordIndex, int labelSlot, out string text)
{
text = null;
if (recordIndex < 0 || recordIndex >= m_Records.Count) return false;
if (labelSlot < 0 || labelSlot >= LabelDimensionCount) return false;
text = m_Records[recordIndex].Labels[labelSlot] ?? string.Empty;
return true;
}
/// <summary>
/// 一次扫描算 Count/First/Last/Min/Max/Sum/Mean/Rms;中位数单独排序一次。
/// 结果按(版本, 维度)缓存,请求的是缓存子集时直接复用。
@@ -620,12 +661,10 @@ namespace XericUI.XChart
return low;
}
private static string[] CopyLabels(IReadOnlyList<string> labels)
{
var copy = new string[labels.Count];
for (int i = 0; i < labels.Count; i++) copy[i] = labels[i];
return copy;
}
/// <summary>标签数组的拷贝<b>并且按那一条规则定长</b>——长度只由标签列个数决定,
/// 所以任何一次写入之后,"这一行的标签个数"都还等于表里的标签列数。</summary>
private string[] CopyLabels(IReadOnlyList<string> labels)
=> XChartColumnMath.SizedLabels(labels, LabelDimensionCount);
}
public class XChartDataModel3D
+14
View File
@@ -155,6 +155,12 @@ namespace XericUI.XChart
/// <summary>数值维度数量</summary>
int NumericDimensionCount { get; }
/// <summary>
/// 标签(String)列数量=每条记录身上标签数组的长度。<b>元素只能读到这里说得出的那些格</b>:
/// 记录里比这更多的字符串没有一个列能叫出名字,所以这一数就是"1:1"的形状判据。
/// </summary>
int LabelDimensionCount { get; }
/// <summary>是否有数据</summary>
bool HasData { get; }
@@ -182,6 +188,14 @@ namespace XericUI.XChart
/// </summary>
bool TryGetDimension(int dimension, out XChartDimensionDefinition definition);
/// <summary>
/// 取某条记录在某一标签列上的字符串。<b>下标是标签下标</b>(同一个口径:
/// <see cref="XChartColumnMath.LabelSlotOf"/> 把"表里那一列"换算成它)。
/// 越界返回 false(渲染路径里调用,不抛)——"这一格没有"和"这一格是空串"要分得开,
/// 所以空串照实返回 true,由调用方决定要不要画。
/// </summary>
bool TryGetLabelAt(int recordIndex, int labelSlot, out string text);
/// <summary>按时间窗打开一个范围游标(虚拟指针)</summary>
XChartRangeCursor OpenRange(int dimension, double fromTime, double toTime);
}
+21 -2
View File
@@ -213,6 +213,14 @@ namespace XericUI.XChart
return false;
}
// 少字段的行要在门口拒:直接读 fields[1] 会抛出去,而"导入失败"的语义是
// **指出行号并整份拒绝**,不是把异常丢给调用方(半份数据比没数据更坏)
if (fields.Length < 2)
{
error = "第 " + (i + 1) + " 行的记录少了时间戳字段";
return false;
}
double timestamp;
if (!TryParseDouble(fields[1], out timestamp))
{
@@ -224,11 +232,22 @@ namespace XericUI.XChart
for (int d = 0; d < dimensions.Count; d++)
if (dimensions[d].Kind == XChartDimensionKind.Number) numericCount++;
// 标签段的长度由**标签列个数**定(同一处规则:XChartColumnMath.LabelCount)。
// 多出来的字符串没有列叫得出它的名字:它落进记录后窗口显示得出来、元素却读不到,
// 所以这里当场拒收并点名行号,而不是"先收下再说"。
int labelCount = XChartColumnMath.LabelCount(dimensions);
int labelFields = fields.Length - 2 - numericCount;
if (fields.Length - 2 < numericCount)
{
error = "第 " + (i + 1) + " 行的数值个数(" + (fields.Length - 2) + ")少于数值维个数(" + numericCount + ")";
return false;
}
if (labelFields != labelCount)
{
error = "第 " + (i + 1) + " 行的标签个数(" + labelFields + ")与标签列个数(" + labelCount + ")对不上";
return false;
}
var values = new float[numericCount];
for (int d = 0; d < numericCount; d++)
@@ -247,8 +266,8 @@ namespace XericUI.XChart
values[d] = (float)value;
}
var labels = new string[Math.Max(fields.Length - 2 - numericCount, 0)];
for (int l = 0; l < labels.Length; l++) labels[l] = fields[2 + numericCount + l];
var labels = new string[labelCount];
for (int l = 0; l < labelCount; l++) labels[l] = fields[2 + numericCount + l];
timestamps.Add(timestamp);
recordValues.Add(values);
+7
View File
@@ -114,6 +114,13 @@ namespace XericUI.XChart
/// </summary>
public virtual int RequiredRecordCount => 0;
/// <summary>
/// 这个元素要<b>几个标签列</b>才画得出来(0=不需要)。标签元素读的是那一列在每一行上的那一格:
/// 表里没有标签列时它一条都不画,而这在画面上和"没数据"长得一模一样——所以"要几个、表里有几个"
/// 必须和数值维、记录数一起印出来(§25.1 的第三半)。
/// </summary>
public virtual int RequiredLabelColumns => 0;
/// <summary>
/// 互斥显示:<paramref name="fieldName"/> 这个字段在当前配置下要不要从面板上<b>藏掉</b>。
///