From 4120b61f2a28bc1e460577002e84418de1183af4 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Sun, 17 May 2026 18:47:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0`Legend`=E7=9A=84`Width`?= =?UTF-8?q?=E5=92=8C`Height`=E5=8F=AF=E8=AE=BE=E7=BD=AE=E5=9B=BA=E5=AE=9A?= =?UTF-8?q?=E5=AE=BD=E9=AB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Documentation~/zh/changelog.md | 2 ++ Editor/MainComponents/LegendEditor.cs | 2 ++ Runtime/Component/Legend/Legend.cs | 21 +++++++++++++++++++++ Runtime/Component/Legend/LegendHelper.cs | 15 ++++++++++++++- 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Documentation~/zh/changelog.md b/Documentation~/zh/changelog.md index a3993645..1411effe 100644 --- a/Documentation~/zh/changelog.md +++ b/Documentation~/zh/changelog.md @@ -81,6 +81,8 @@ slug: /changelog ## master +* (2026.05.17) 增加`Legend`的`Width`和`Height`可设置固定宽高 +* (2026.05.17) 修复`Serie`的`EndLabel`在`Y`轴是`MinMax`类型时显示的数值不对的问题 * (2026.05.16) 修复`Candlestick`按昨收判断涨跌颜色,一字涨停/跌停显示不对的问题 (#362) * (2026.03.29) 修复`Legend`的`Background`区域在`Horizonal`模式下不对的问题 * (2026.03.25) 增加`Chart`的`Json`导出导入 diff --git a/Editor/MainComponents/LegendEditor.cs b/Editor/MainComponents/LegendEditor.cs index dc3d25f2..53056a76 100644 --- a/Editor/MainComponents/LegendEditor.cs +++ b/Editor/MainComponents/LegendEditor.cs @@ -10,6 +10,8 @@ namespace XCharts.Editor { ++EditorGUI.indentLevel; PropertyField("m_IconType"); + PropertyField("m_Width"); + PropertyField("m_Height"); PropertyField("m_ItemWidth"); PropertyField("m_ItemHeight"); PropertyField("m_ItemGap"); diff --git a/Runtime/Component/Legend/Legend.cs b/Runtime/Component/Legend/Legend.cs index 3b55f8eb..e91bc55c 100644 --- a/Runtime/Component/Legend/Legend.cs +++ b/Runtime/Component/Legend/Legend.cs @@ -80,6 +80,8 @@ namespace XCharts.Runtime [SerializeField] private bool m_ItemAutoColor = true; [SerializeField] private float m_ItemOpacity = 1; [SerializeField][Since("v3.15.0")] private float m_ItemInactiveOpacity = 1; + [SerializeField][Since("v3.16.0")] private float m_Width = 0; + [SerializeField][Since("v3.16.0")] private float m_Height = 0; [SerializeField] private string m_Formatter; [SerializeField] private LabelStyle m_LabelStyle = new LabelStyle(); [SerializeField][Since("v3.10.0")] private TextLimit m_TextLimit = new TextLimit(); @@ -202,6 +204,25 @@ namespace XCharts.Runtime set { if (PropertyUtil.SetClass(ref m_Formatter, value)) SetComponentDirty(); } } /// + /// the width of legend component. Default is 0 for auto adapt. When set a value between 0 and 1, it means the percentage relative to chart width and height. + /// ||图例组件的宽。默认为0自适应。当设置0-1的值时,表示相对于图表宽高的比例。 + /// + public float width + { + get { return m_Width; } + set { if (PropertyUtil.SetStruct(ref m_Width, value)) SetComponentDirty(); } + } + /// + /// the height of legend component. Default is 0 for auto adapt. When set a value between 0 and 1, it means the percentage relative to chart width and height. + /// ||图例组件的高。默认为0自适应。当设置0-1的值时,表示相对于图表宽高的比例。 + /// + /// + public float height + { + get { return m_Height; } + set { if (PropertyUtil.SetStruct(ref m_Height, value)) SetComponentDirty(); } + } + /// /// the style of text. /// ||文本样式。 /// diff --git a/Runtime/Component/Legend/LegendHelper.cs b/Runtime/Component/Legend/LegendHelper.cs index 9604006b..e1334d55 100644 --- a/Runtime/Component/Legend/LegendHelper.cs +++ b/Runtime/Component/Legend/LegendHelper.cs @@ -93,10 +93,23 @@ namespace XCharts.Runtime var startY = 0f; var legendMaxWidth = chartWidth - legend.location.runtimeLeft - legend.location.runtimeRight; var legendMaxHeight = chartHeight - legend.location.runtimeTop - legend.location.runtimeBottom; + var isVertical = legend.orient == Orient.Vertical; + var fixedWidth = legend.width <= 0 ? 0 + : legend.width < 1 ? chartWidth * legend.width + : legend.width; + var fixedHeight = legend.height <= 0 ? 0 + : legend.height < 1 ? chartHeight * legend.height + : legend.height; + // Horizonal: width constrains layout wrapping; Vertical: height constrains layout wrapping. + // The other axis only affects the background size, not the layout. + if (!isVertical && fixedWidth > 0) legendMaxWidth = fixedWidth; + if (isVertical && fixedHeight > 0) legendMaxHeight = fixedHeight; UpdateLegendWidthAndHeight(legend, legendMaxWidth, legendMaxHeight); + // Override context size for fixed dimensions (controls background rect size). + if (fixedWidth > 0) legend.context.width = fixedWidth; + if (fixedHeight > 0) legend.context.height = fixedHeight; var legendRuntimeWidth = legend.context.width; var legendRuntimeHeight = legend.context.height; - var isVertical = legend.orient == Orient.Vertical; switch (legend.location.align) { case Location.Align.TopCenter: