From 59c9d758e18770832f9346b0ba68ec4cfd89dec0 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Wed, 30 Nov 2022 09:39:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0`Serie`=E7=9A=84`barMaxWidth`?= =?UTF-8?q?=E5=8F=AF=E8=AE=BE=E7=BD=AE`Bar`=E7=9A=84=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=AE=BD=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/Series/BarEditor.cs | 1 + Runtime/Serie/Serie.cs | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Editor/Series/BarEditor.cs b/Editor/Series/BarEditor.cs index 0e16bccc..55a1af79 100644 --- a/Editor/Series/BarEditor.cs +++ b/Editor/Series/BarEditor.cs @@ -21,6 +21,7 @@ namespace XCharts.Editor PropertyField("m_BarType"); PropertyField("m_BarWidth"); PropertyField("m_BarGap"); + PropertyField("m_BarMaxWidth"); if (serie.IsUseCoord()) { PropertyField("m_RoundCap"); diff --git a/Runtime/Serie/Serie.cs b/Runtime/Serie/Serie.cs index 478bd67e..5cabe856 100644 --- a/Runtime/Serie/Serie.cs +++ b/Runtime/Serie/Serie.cs @@ -252,6 +252,7 @@ namespace XCharts.Runtime [SerializeField] private BarType m_BarType = BarType.Normal; [SerializeField] private bool m_BarPercentStack = false; [SerializeField] private float m_BarWidth = 0; + [SerializeField][Since("v3.5.0")] private float m_BarMaxWidth = 0; [SerializeField] private float m_BarGap = 0.1f; [SerializeField] private float m_BarZebraWidth = 4f; [SerializeField] private float m_BarZebraGap = 2f; @@ -573,6 +574,15 @@ namespace XCharts.Runtime set { if (PropertyUtil.SetStruct(ref m_BarWidth, value)) SetVerticesDirty(); } } /// + /// The max width of the bar. Adaptive when default 0. + /// |柱条的最大宽度,默认为0为不限制最大宽度。支持设置成相对于类目宽度的百分比。 + /// + public float barMaxWidth + { + get { return m_BarMaxWidth; } + set { if (PropertyUtil.SetStruct(ref m_BarMaxWidth, value)) SetVerticesDirty(); } + } + /// /// The gap between bars between different series, is a percent value like '0.3f' , which means 30% of the bar width, can be set as a fixed value. /// Set barGap as '-1' can overlap bars that belong to different series, which is useful when making a series of bar be background. /// In a single coodinate system, this attribute is shared by multiple 'bar' series. @@ -1742,17 +1752,32 @@ namespace XCharts.Runtime public float GetBarWidth(float categoryWidth, int barCount = 0) { - if (categoryWidth < 2) return categoryWidth; - if (m_BarWidth == 0) + var realWidth = 0f; + if (categoryWidth < 2) + { + realWidth = categoryWidth; + } + else if (m_BarWidth == 0) { var width = ChartHelper.GetActualValue(0.6f, categoryWidth); if (barCount == 0) - return width < 1 ? categoryWidth : width; + realWidth = width < 1 ? categoryWidth : width; else - return width / barCount; + realWidth = width / barCount; } else - return ChartHelper.GetActualValue(m_BarWidth, categoryWidth); + { + realWidth = ChartHelper.GetActualValue(m_BarWidth, categoryWidth); + } + if (m_BarMaxWidth == 0) + { + return realWidth; + } + else + { + var maxWidth = ChartHelper.GetActualValue(m_BarMaxWidth, categoryWidth); + return realWidth > maxWidth ? maxWidth : realWidth; + } } public bool IsIgnoreIndex(int index, int dimension = 1)