From a9c801c07702dcccb01575fd36de6cf335a1e79e Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Thu, 4 May 2023 23:33:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96`Axis`=E7=9A=84`MinMax`?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=8C=83=E5=9B=B4=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Documentation~/zh/changelog.md | 2 + Runtime/Component/Axis/AxisHelper.cs | 4 +- Runtime/Component/Label/LabelStyle.cs | 8 ++- Runtime/Internal/Utilities/ChartHelper.cs | 66 +++++++++++++---------- 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/Documentation~/zh/changelog.md b/Documentation~/zh/changelog.md index cf53c481..a9a857dc 100644 --- a/Documentation~/zh/changelog.md +++ b/Documentation~/zh/changelog.md @@ -66,6 +66,8 @@ slug: /changelog ## master +* (2023.05.04) 优化`Axis`在-1到1范围时设置`CeilRate`不生效的问题 +* (2023.05.04) 优化`Axis`的`MinMax`类型范围计算 * (2023.05.04) 修复`AxisLabel`在数据都是小于1的浮点数时显示`Label`格式不对的问题 * (2023.05.04) 修复`Theme`在修改默认主题的参数后运行被重置的问题 * (2023.05.04) 增加`Symbol`选择`Custom`类型时的`Warning`提示 diff --git a/Runtime/Component/Axis/AxisHelper.cs b/Runtime/Component/Axis/AxisHelper.cs index cf6e5896..0b598b3f 100644 --- a/Runtime/Component/Axis/AxisHelper.cs +++ b/Runtime/Component/Axis/AxisHelper.cs @@ -394,8 +394,8 @@ namespace XCharts.Runtime case Axis.AxisMinMaxType.MinMax: - minValue = ceilRate != 0 ? ChartHelper.GetMinDivisibleValue(minValue, ceilRate) : minValue; - maxValue = ceilRate != 0 ? ChartHelper.GetMaxDivisibleValue(maxValue, ceilRate) : maxValue; + minValue = needFormat ? ChartHelper.GetMinDivisibleValue(minValue, ceilRate) : minValue; + maxValue = needFormat ? ChartHelper.GetMaxDivisibleValue(maxValue, ceilRate) : maxValue; break; } } diff --git a/Runtime/Component/Label/LabelStyle.cs b/Runtime/Component/Label/LabelStyle.cs index 762c6c24..a97e5ae4 100644 --- a/Runtime/Component/Label/LabelStyle.cs +++ b/Runtime/Component/Label/LabelStyle.cs @@ -347,9 +347,13 @@ namespace XCharts.Runtime public virtual string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false) { var newNumericFormatter = numericFormatter; - if (string.IsNullOrEmpty(newNumericFormatter) && !isLog) + if (value == 0) { - newNumericFormatter = MathUtil.IsInteger(maxValue) ? "0" : "f" + MathUtil.GetPrecision(maxValue); + newNumericFormatter = "f0"; + } + else if (string.IsNullOrEmpty(newNumericFormatter) && !isLog) + { + newNumericFormatter = MathUtil.IsInteger(maxValue) ? "f0" : "f" + MathUtil.GetPrecision(maxValue); } if (string.IsNullOrEmpty(m_Formatter)) { diff --git a/Runtime/Internal/Utilities/ChartHelper.cs b/Runtime/Internal/Utilities/ChartHelper.cs index d98ffa12..94f14e6b 100644 --- a/Runtime/Internal/Utilities/ChartHelper.cs +++ b/Runtime/Internal/Utilities/ChartHelper.cs @@ -521,7 +521,7 @@ namespace XCharts.Runtime { Vector3 dir = (ep - sp).normalized; float dist = Vector3.Distance(sp, ep); - int segment = (int) (dist / k); + int segment = (int)(dist / k); posList.Clear(); posList.Add(sp); for (int i = 1; i < segment; i++) @@ -676,7 +676,7 @@ namespace XCharts.Runtime { Color color; ColorUtility.TryParseHtmlString(hexColorStr, out color); - return (Color32) color; + return (Color32)color; } public static double GetMaxDivisibleValue(double max, double ceilRate) @@ -685,15 +685,16 @@ namespace XCharts.Runtime if (max > -1 && max < 1) { int count = 1; - int intvalue = (int) (max * Mathf.Pow(10, count)); + int intvalue = (int)(max * Mathf.Pow(10, count)); while (intvalue == 0 && count < 12) { count++; - intvalue = (int) (max * Mathf.Pow(10, count)); + intvalue = (int)(max * Mathf.Pow(10, count)); } var pow = Mathf.Pow(10, count); - if (max > 0) return (int) ((max * pow + 1)) / pow; - else return (int) ((max * pow - 1)) / pow; + var value = (max > 0) ? (int)((max * pow + 1)) / pow : + (int)((max * pow - 1)) / pow; + return GetMaxCeilRate(value, ceilRate); } if (ceilRate == 0) { @@ -723,27 +724,42 @@ namespace XCharts.Runtime } else { - var mod = max % ceilRate; - int rate = (int) (max / ceilRate); - return mod == 0 ? max : (max < 0 ? rate : rate + 1) * ceilRate; + return GetMaxCeilRate(max, ceilRate); } } + public static double GetMaxCeilRate(double value, double ceilRate) + { + if (ceilRate == 0) return value; + var mod = value % ceilRate; + int rate = (int)(value / ceilRate); + return mod == 0 ? value : (value < 0 ? rate : rate + 1) * ceilRate; + } + + public static double GetMinCeilRate(double value, double ceilRate) + { + if (ceilRate == 0) return value; + var mod = value % ceilRate; + int rate = (int)(value / ceilRate); + return mod == 0 ? value : (value < 0 ? rate - 1 : rate) * ceilRate; + } + public static double GetMinDivisibleValue(double min, double ceilRate) { if (min == 0) return 0; if (min > -1 && min < 1) { int count = 1; - int intvalue = (int) (min * Mathf.Pow(10, count)); + int intvalue = (int)(min * Mathf.Pow(10, count)); while (intvalue == 0 && count < 12) { count++; - intvalue = (int) (min * Mathf.Pow(10, count)); + intvalue = (int)(min * Mathf.Pow(10, count)); } var pow = Mathf.Pow(10, count); - if (min > 0) return (int) ((min * pow + 1)) / pow; - else return (int) ((min * pow - 1)) / pow; + var value = (min > 0) ? (int)((min * pow + 1)) / pow : + (int)((min * pow - 1)) / pow; + return GetMinCeilRate(value, ceilRate); } if (ceilRate == 0) { @@ -764,9 +780,7 @@ namespace XCharts.Runtime } else { - var mod = min % ceilRate; - int rate = (int) (min / ceilRate); - return mod == 0 ? min : (min < 0 ? rate - 1 : rate) * ceilRate; + return GetMinCeilRate(min, ceilRate); } } @@ -810,8 +824,6 @@ namespace XCharts.Runtime return min; } - - public static void AddEventListener(GameObject obj, EventTriggerType type, UnityEngine.Events.UnityAction call) { @@ -902,32 +914,32 @@ namespace XCharts.Runtime { if (color.a != 0 && opacity != 1) { - color.a = (byte) (color.a * opacity); + color.a = (byte)(color.a * opacity); } } public static Color32 GetHighlightColor(Color32 color, float rate = 0.8f) { var newColor = color; - newColor.r = (byte) (color.r * rate); - newColor.g = (byte) (color.g * rate); - newColor.b = (byte) (color.b * rate); + newColor.r = (byte)(color.r * rate); + newColor.g = (byte)(color.g * rate); + newColor.b = (byte)(color.b * rate); return newColor; } public static Color32 GetBlurColor(Color32 color, float a = 0.3f) { var newColor = color; - newColor.a = (byte) (a * 255); + newColor.a = (byte)(a * 255); return newColor; } public static Color32 GetSelectColor(Color32 color, float rate = 0.8f) { var newColor = color; - newColor.r = (byte) (color.r * rate); - newColor.g = (byte) (color.g * rate); - newColor.b = (byte) (color.b * rate); + newColor.r = (byte)(color.r * rate); + newColor.g = (byte)(color.g * rate); + newColor.b = (byte)(color.b * rate); return newColor; } @@ -977,7 +989,7 @@ namespace XCharts.Runtime var posX = pos.x + rectTransform.rect.xMin * canvas.scaleFactor; var posY = pos.y + rectTransform.rect.yMin * canvas.scaleFactor; var rect = new Rect(posX, posY, width, height); - var tex = new Texture2D((int) width, (int) height, TextureFormat.RGBA32, false); + var tex = new Texture2D((int)width, (int)height, TextureFormat.RGBA32, false); tex.ReadPixels(rect, 0, 0); tex.Apply(); byte[] bytes;