增加LegendWidthHeight可设置固定宽高

This commit is contained in:
monitor1394
2026-05-17 18:47:47 +08:00
parent 0174453b05
commit 4120b61f2a
4 changed files with 39 additions and 1 deletions

View File

@@ -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`导出导入

View File

@@ -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");

View File

@@ -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(); }
}
/// <summary>
/// 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的值时表示相对于图表宽高的比例。
/// </summary>
public float width
{
get { return m_Width; }
set { if (PropertyUtil.SetStruct(ref m_Width, value)) SetComponentDirty(); }
}
/// <summary>
/// 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的值时表示相对于图表宽高的比例。
/// </summary>
/// <value></value>
public float height
{
get { return m_Height; }
set { if (PropertyUtil.SetStruct(ref m_Height, value)) SetComponentDirty(); }
}
/// <summary>
/// the style of text.
/// ||文本样式。
/// </summary>

View File

@@ -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: