From 2f8a1300d361b90c66e73af90b0235bf8c0efe33 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Tue, 18 Mar 2025 07:37:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0`ItemStyle`=E7=9A=84`backgrou?= =?UTF-8?q?ndGap`=E5=8F=AF=E8=AE=BE=E7=BD=AE=E6=95=B0=E6=8D=AE=E9=A1=B9?= =?UTF-8?q?=E8=83=8C=E6=99=AF=E9=97=B4=E9=9A=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/ChildComponents/ItemStyleDrawer.cs | 1 + Runtime/Component/Child/ItemStyle.cs | 10 ++++++++++ Runtime/Serie/Bar/BarHandler.cs | 18 ++++++++++-------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Editor/ChildComponents/ItemStyleDrawer.cs b/Editor/ChildComponents/ItemStyleDrawer.cs index 1f772079..5a875e07 100644 --- a/Editor/ChildComponents/ItemStyleDrawer.cs +++ b/Editor/ChildComponents/ItemStyleDrawer.cs @@ -21,6 +21,7 @@ namespace XCharts.Editor PropertyField(prop, "m_MarkColor"); PropertyField(prop, "m_BackgroundColor"); PropertyField(prop, "m_BackgroundWidth"); + PropertyField(prop, "m_BackgroundGap"); PropertyField(prop, "m_CenterColor"); PropertyField(prop, "m_CenterGap"); PropertyField(prop, "m_BorderWidth"); diff --git a/Runtime/Component/Child/ItemStyle.cs b/Runtime/Component/Child/ItemStyle.cs index 8fe51003..9732b3e4 100644 --- a/Runtime/Component/Child/ItemStyle.cs +++ b/Runtime/Component/Child/ItemStyle.cs @@ -16,6 +16,7 @@ namespace XCharts.Runtime [SerializeField][Since("v3.6.0")] private Color32 m_MarkColor; [SerializeField] private Color32 m_BackgroundColor; [SerializeField] private float m_BackgroundWidth; + [SerializeField][Since("v3.15.0")] private float m_BackgroundGap; [SerializeField] private Color32 m_CenterColor; [SerializeField] private float m_CenterGap; [SerializeField] private float m_BorderWidth = 0; @@ -129,6 +130,15 @@ namespace XCharts.Runtime set { if (PropertyUtil.SetStruct(ref m_BackgroundWidth, value)) SetVerticesDirty(); } } /// + /// the gap between background and data item. + /// ||数据项背景间隙。 + /// + public float backgroundGap + { + get { return m_BackgroundGap; } + set { if (PropertyUtil.SetStruct(ref m_BackgroundGap, value)) SetVerticesDirty(); } + } + /// /// 中心区域颜色。 /// public Color32 centerColor diff --git a/Runtime/Serie/Bar/BarHandler.cs b/Runtime/Serie/Bar/BarHandler.cs index f2990c19..69197a70 100644 --- a/Runtime/Serie/Bar/BarHandler.cs +++ b/Runtime/Serie/Bar/BarHandler.cs @@ -228,6 +228,7 @@ namespace XCharts.Runtime var borderGap = relativedValue == 0 ? 0 : itemStyle.borderGap; var borderGapAndWidth = borderWidth + borderGap; var backgroundColor = itemStyle.backgroundColor; + var backgroundGap = itemStyle.backgroundGap; if (!serieData.interact.TryGetColor(ref areaColor, ref areaToColor, ref interacting, interactDuration)) { @@ -237,16 +238,17 @@ namespace XCharts.Runtime var pX = 0f; var pY = 0f; - UpdateXYPosition(m_SerieGrid, isY, axis, relativedAxis, i, categoryWidth, relativedCategoryWidth, barWidth, isStack, value, ref pX, ref pY); + UpdateXYPosition(m_SerieGrid, isY, axis, relativedAxis, i, categoryWidth, relativedCategoryWidth, + barWidth, isStack, value, backgroundGap, ref pX, ref pY); var barHig = 0f; if (isPercentStack) { var valueTotal = chart.GetSerieSameStackTotalValue(serie.stack, i, m_SerieGrid.index); - barHig = valueTotal != 0 ? (float)(relativedValue / valueTotal * relativedAxisLength) : 0; + barHig = valueTotal != 0 ? (float)(relativedValue / valueTotal * (relativedAxisLength - 2 * backgroundGap)) : 0; } else { - barHig = AxisHelper.GetAxisValueLength(m_SerieGrid, relativedAxis, relativedCategoryWidth, relativedValue); + barHig = AxisHelper.GetAxisValueLength(m_SerieGrid, relativedAxis, relativedCategoryWidth, relativedValue, 2 * backgroundGap); } float currHig = AnimationStyleHelper.CheckDataAnimation(chart, serie, i, barHig); Vector3 plb, plt, prt, prb, top; @@ -257,8 +259,8 @@ namespace XCharts.Runtime serieData.context.rect = Rect.MinMaxRect(plb.x + borderGapAndWidth, plb.y + borderGapAndWidth, prt.x - borderGapAndWidth, prt.y - borderGapAndWidth); serieData.context.backgroundRect = isY ? - Rect.MinMaxRect(m_SerieGrid.context.x, plb.y, m_SerieGrid.context.x + relativedAxisLength, prt.y) : - Rect.MinMaxRect(plb.x, m_SerieGrid.context.y, prb.x, m_SerieGrid.context.y + relativedAxisLength); + Rect.MinMaxRect(m_SerieGrid.context.x, plb.y - backgroundGap, m_SerieGrid.context.x + relativedAxisLength, prt.y + backgroundGap) : + Rect.MinMaxRect(plb.x - backgroundGap, m_SerieGrid.context.y, prb.x + backgroundGap, m_SerieGrid.context.y + relativedAxisLength); if (!serie.clip || (serie.clip && m_SerieGrid.Contains(top))) { @@ -303,7 +305,7 @@ namespace XCharts.Runtime private void UpdateXYPosition(GridCoord grid, bool isY, Axis axis, Axis relativedAxis, int i, float categoryWidth, float relativedCategoryWidth, float barWidth, bool isStack, - double value, ref float pX, ref float pY) + double value, float backgroundGap, ref float pX, ref float pY) { if (isY) { @@ -320,7 +322,7 @@ namespace XCharts.Runtime pY = grid.context.y + valueLen - categoryWidth * 0.5f; } } - pX = AxisHelper.GetAxisValuePosition(grid, relativedAxis, relativedCategoryWidth, 0); + pX = AxisHelper.GetAxisValuePosition(grid, relativedAxis, relativedCategoryWidth, 0) + backgroundGap; if (isStack) { for (int n = 0; n < m_StackSerieData.Count - 1; n++) @@ -342,7 +344,7 @@ namespace XCharts.Runtime pX = grid.context.x + valueLen - categoryWidth * 0.5f; } } - pY = AxisHelper.GetAxisValuePosition(grid, relativedAxis, relativedCategoryWidth, 0); + pY = AxisHelper.GetAxisValuePosition(grid, relativedAxis, relativedCategoryWidth, 0) + backgroundGap; if (isStack) { for (int n = 0; n < m_StackSerieData.Count - 1; n++)