增加LabelStylefixedXfixedY可固定label的坐标

This commit is contained in:
monitor1394
2025-03-18 07:41:58 +08:00
parent 2f8a1300d3
commit 47caaa2113
5 changed files with 35 additions and 3 deletions

View File

@@ -80,6 +80,9 @@ slug: /changelog
## master
* (2025.03.18) 增加`LabelStyle``fixedX``fixedY`可固定label的坐标
* (2025.03.17) 增加`ItemStyle``backgroundGap`可设置数据项背景间隙
## v3.14.0
版本要点:

View File

@@ -24,6 +24,8 @@ namespace XCharts.Editor
PropertyField(prop, "m_Rotate");
PropertyField(prop, "m_Width");
PropertyField(prop, "m_Height");
PropertyField(prop, "m_FixedX");
PropertyField(prop, "m_FixedY");
PropertyField(prop, "m_Icon");
PropertyField(prop, "m_Background");
PropertyField(prop, "m_TextStyle");

View File

@@ -536,9 +536,9 @@ namespace XCharts.Runtime
/// <param name="scaleWidth"></param>
/// <param name="value"></param>
/// <returns></returns>
public static float GetAxisValueLength(GridCoord grid, Axis axis, float scaleWidth, double value)
public static float GetAxisValueLength(GridCoord grid, Axis axis, float scaleWidth, double value, float gap = 0)
{
return GetAxisPositionInternal(grid, axis, scaleWidth, value, false, true);
return GetAxisPositionInternal(grid, axis, scaleWidth, value, false, true, gap);
}
/// <summary>
@@ -572,10 +572,11 @@ namespace XCharts.Runtime
}
}
private static float GetAxisPositionInternal(GridCoord grid, Axis axis, float scaleWidth, double value, bool includeGridXY, bool realLength)
private static float GetAxisPositionInternal(GridCoord grid, Axis axis, float scaleWidth, double value, bool includeGridXY, bool realLength, float gap = 0)
{
var isY = axis is YAxis;
var gridHeight = isY ? grid.context.height : grid.context.width;
gridHeight -= gap;
var gridXY = isY ? grid.context.y : grid.context.x;
if (axis.IsLog())

View File

@@ -80,6 +80,8 @@ namespace XCharts.Runtime
[SerializeField] protected string m_NumericFormatter = "";
[SerializeField] protected float m_Width = 0;
[SerializeField] protected float m_Height = 0;
[SerializeField][Since("v3.15.0")] protected float m_FixedX = 0;
[SerializeField][Since("v3.15.0")] protected float m_FixedY = 0;
[SerializeField] protected IconStyle m_Icon = new IconStyle();
[SerializeField] protected ImageStyle m_Background = new ImageStyle();
@@ -288,6 +290,24 @@ namespace XCharts.Runtime
set { if (PropertyUtil.SetStruct(ref m_AutoOffset, value)) SetAllDirty(); }
}
/// <summary>
/// the fixed x of label. When not 0, it will be fixed on the specified x value.
/// ||固定的X值。不为0时会固定在指定的X值上。
/// </summary>
public float fixedX
{
get { return m_FixedX; }
set { if (PropertyUtil.SetStruct(ref m_FixedX, value)) SetComponentDirty(); }
}
/// <summary>
/// the fixed y of label. When not 0, it will be fixed on the specified y value.
/// ||固定的Y值。不为0时会固定在指定的Y值上。
/// </summary>
public float fixedY
{
get { return m_FixedY; }
set { if (PropertyUtil.SetStruct(ref m_FixedY, value)) SetComponentDirty(); }
}
/// <summary>
/// the sytle of background.
/// ||背景图样式。
/// </summary>
@@ -375,6 +395,8 @@ namespace XCharts.Runtime
label.m_Height = m_Height;
label.m_NumericFormatter = m_NumericFormatter;
label.m_AutoOffset = m_AutoOffset;
label.m_FixedX = m_FixedX;
label.m_FixedY = m_FixedY;
label.m_Icon.Copy(m_Icon);
label.m_Background.Copy(m_Background);
label.m_TextPadding = m_TextPadding;
@@ -394,6 +416,8 @@ namespace XCharts.Runtime
m_Height = label.m_Height;
m_NumericFormatter = label.m_NumericFormatter;
m_AutoOffset = label.m_AutoOffset;
m_FixedX = label.m_FixedX;
m_FixedY = label.m_FixedY;
m_Icon.Copy(label.m_Icon);
m_Background.Copy(label.m_Background);
m_TextPadding = label.m_TextPadding;

View File

@@ -610,6 +610,8 @@ namespace XCharts.Runtime
protected Vector3 UpdateLabelPosition(SerieData serieData, LabelStyle currLabel)
{
var labelPosition = GetSerieDataLabelPosition(serieData, currLabel);
if(currLabel.fixedX != 0) labelPosition.x = currLabel.fixedX;
if(currLabel.fixedY != 0) labelPosition.y = currLabel.fixedY;
var offset = GetSerieDataLabelOffset(serieData, currLabel);
serieData.labelObject.SetPosition(labelPosition + offset);
if (currLabel.autoRotate && serieData.context.angle != 0)