From 80d9087084257762c3eff0308d7eb0ced737ffac Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Fri, 10 Feb 2023 13:23:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96`Axis`=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E5=80=BC`Label`=E7=9A=84=E9=BB=98=E8=AE=A4=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/Component/Axis/Axis.cs | 12 +++---- Runtime/Component/Axis/AxisHelper.cs | 39 +++++++++++---------- Runtime/Component/Label/LabelStyle.cs | 21 ++++++----- Runtime/Internal/Utilities/ChartCached.cs | 7 ++-- Runtime/Internal/Utilities/ChartHelper.cs | 14 +------- Runtime/Internal/Utilities/MathUtil.cs | 23 +++++++++--- Runtime/Serie/Scatter/BaseScatterHandler.cs | 18 +++++----- 7 files changed, 69 insertions(+), 65 deletions(-) diff --git a/Runtime/Component/Axis/Axis.cs b/Runtime/Component/Axis/Axis.cs index 6e2a4aa9..d74301a1 100644 --- a/Runtime/Component/Axis/Axis.cs +++ b/Runtime/Component/Axis/Axis.cs @@ -827,18 +827,18 @@ namespace XCharts.Runtime return logBaseE ? (float) Math.Log(value) : (float) Math.Log(value, logBase); } - public int GetLogMinIndex() + public double GetLogMinIndex() { return logBaseE ? - (int) Math.Log(context.minValue) : - (int) Math.Log(context.minValue, logBase); + Math.Log(context.minValue) : + Math.Log(context.minValue, logBase); } - public int GetLogMaxIndex() + public double GetLogMaxIndex() { return logBaseE ? - (int) Math.Log(context.maxValue) : - (int) Math.Log(context.maxValue, logBase); + Math.Log(context.maxValue) : + Math.Log(context.maxValue, logBase); } public double GetLabelValue(int index) diff --git a/Runtime/Component/Axis/AxisHelper.cs b/Runtime/Component/Axis/AxisHelper.cs index d6ba3043..cf6e5896 100644 --- a/Runtime/Component/Axis/AxisHelper.cs +++ b/Runtime/Component/Axis/AxisHelper.cs @@ -68,7 +68,7 @@ namespace XCharts.Runtime var min = axis is YAxis ? 20 : 80; if (eachWid > min) return dataCount; var tick = Mathf.CeilToInt(min / eachWid); - return (int) (dataCount / tick); + return (int)(dataCount / tick); } else { @@ -132,7 +132,7 @@ namespace XCharts.Runtime maxValue = -maxValue; } if (forcePercent) - return string.Format("{0}%", (int) value); + return string.Format("{0}%", (int)value); else return axis.axisLabel.GetFormatterContent(index, value, minValue, maxValue); } @@ -255,9 +255,8 @@ namespace XCharts.Runtime { var value = axis.GetLabelValue(index); var lastValue = axis.GetLabelValue(index - 1); - return axis.context.minMaxRange == 0 ? - 0 : - (float) (coordinateWidth * (value - lastValue) / axis.context.minMaxRange); + return axis.context.minMaxRange == 0 ? 0 : + (float)(coordinateWidth * (value - lastValue) / axis.context.minMaxRange); } else { @@ -345,7 +344,10 @@ namespace XCharts.Runtime int maxSplit = 0; maxValue = ChartHelper.GetMaxLogValue(maxValue, axis.logBase, axis.logBaseE, out maxSplit); minValue = ChartHelper.GetMinLogValue(minValue, axis.logBase, axis.logBaseE, out minSplit); - axis.splitNumber = (minSplit > 0 && maxSplit > 0) ? (maxSplit + minSplit - 1) : (maxSplit + minSplit); + var splitNumber = (minSplit > 0 && maxSplit > 0) ? (maxSplit + minSplit - 1) : (maxSplit + minSplit); + if (splitNumber > 15) + splitNumber = 15; + axis.splitNumber = splitNumber; return; } if (axis.type == Axis.AxisType.Time) { } @@ -460,7 +462,7 @@ namespace XCharts.Runtime if (axis.IsCategory()) { if (dataCount == 0) dataCount = axis.data.Count; - var categoryIndex = (int) value; + var categoryIndex = (int)value; var scaleWid = AxisHelper.GetDataWidth(axis, gridHeight, dataCount, dataZoom); float startY = gridXY + (axis.boundaryGap ? scaleWid / 2 : 0); return startY + scaleWid * categoryIndex; @@ -468,9 +470,8 @@ namespace XCharts.Runtime else { var yDataHig = (axis.context.minMaxRange == 0) ? 0f : - (float) ((value - axis.context.minValue) / axis.context.minMaxRange * gridHeight); + (float)((value - axis.context.minValue) / axis.context.minMaxRange * gridHeight); return gridXY + yDataHig; - } } @@ -539,7 +540,7 @@ namespace XCharts.Runtime { if (axis.IsCategory()) { - return (int) value; + return (int)value; } else { @@ -550,9 +551,9 @@ namespace XCharts.Runtime if (totalSplitNumber == -1) totalSplitNumber = GetTotalSplitGridNum(axis); if (axis.minMaxType == Axis.AxisMinMaxType.Custom) - return Mathf.CeilToInt(((float) ((value - axis.min) / axis.max) * totalSplitNumber) - 1); + return Mathf.CeilToInt(((float)((value - axis.min) / axis.max) * totalSplitNumber) - 1); else - return Mathf.CeilToInt(((float) ((value - axis.context.minValue) / axis.context.minMaxRange) * totalSplitNumber) - 1); + return Mathf.CeilToInt(((float)((value - axis.context.minValue) / axis.context.minMaxRange) * totalSplitNumber) - 1); } } } @@ -565,15 +566,15 @@ namespace XCharts.Runtime if (axis.IsLog()) { - int minIndex = axis.GetLogMinIndex(); - float nowIndex = axis.GetLogValue(value); + var minIndex = axis.GetLogMinIndex(); + var nowIndex = axis.GetLogValue(value); return includeGridXY ? - gridXY + (nowIndex - minIndex) / axis.splitNumber * gridHeight : - (nowIndex - minIndex) / axis.splitNumber * gridHeight; + (float)(gridXY + (nowIndex - minIndex) / axis.splitNumber * gridHeight) : + (float)((nowIndex - minIndex) / axis.splitNumber * gridHeight); } else if (axis.IsCategory()) { - var categoryIndex = (int) value; + var categoryIndex = (int)value; return includeGridXY ? gridXY + (axis.boundaryGap ? scaleWidth / 2 : 0) + scaleWidth * categoryIndex : (axis.boundaryGap ? scaleWidth / 2 : 0) + scaleWidth * categoryIndex; @@ -584,9 +585,9 @@ namespace XCharts.Runtime if (axis.context.minMaxRange != 0) { if (realLength) - yDataHig = (float) (value * gridHeight / axis.context.minMaxRange); + yDataHig = (float)(value * gridHeight / axis.context.minMaxRange); else - yDataHig = (float) ((value - axis.context.minValue) / axis.context.minMaxRange * gridHeight); + yDataHig = (float)((value - axis.context.minValue) / axis.context.minMaxRange * gridHeight); } return includeGridXY ? gridXY + yDataHig : diff --git a/Runtime/Component/Label/LabelStyle.cs b/Runtime/Component/Label/LabelStyle.cs index a404945f..030a0cd5 100644 --- a/Runtime/Component/Label/LabelStyle.cs +++ b/Runtime/Component/Label/LabelStyle.cs @@ -336,33 +336,38 @@ 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) + { + newNumericFormatter = MathUtil.IsInteger(maxValue) ? "0" : "f" + MathUtil.GetPrecision(maxValue); + } if (string.IsNullOrEmpty(m_Formatter)) { if (isLog) { - return GetFormatterFunctionContent(labelIndex, value, ChartCached.NumberToStr(value, numericFormatter)); + return GetFormatterFunctionContent(labelIndex, value, ChartCached.NumberToStr(value, newNumericFormatter)); } if (minValue >= -1 && minValue <= 1 && maxValue >= -1 && maxValue <= 1) { - int minAcc = ChartHelper.GetFloatAccuracy(minValue); - int maxAcc = ChartHelper.GetFloatAccuracy(maxValue); - int curAcc = ChartHelper.GetFloatAccuracy(value); + int minAcc = MathUtil.GetPrecision(minValue); + int maxAcc = MathUtil.GetPrecision(maxValue); + int curAcc = MathUtil.GetPrecision(value); int acc = Mathf.Max(Mathf.Max(minAcc, maxAcc), curAcc); - return GetFormatterFunctionContent(labelIndex, value, ChartCached.FloatToStr(value, numericFormatter, acc)); + return GetFormatterFunctionContent(labelIndex, value, ChartCached.FloatToStr(value, newNumericFormatter, acc)); } - return GetFormatterFunctionContent(labelIndex, value, ChartCached.NumberToStr(value, numericFormatter)); + return GetFormatterFunctionContent(labelIndex, value, ChartCached.NumberToStr(value, newNumericFormatter)); } else { var content = m_Formatter; - FormatterHelper.ReplaceAxisLabelContent(ref content, numericFormatter, value); + FormatterHelper.ReplaceAxisLabelContent(ref content, newNumericFormatter, value); return GetFormatterFunctionContent(labelIndex, value, content); } } public string GetFormatterDateTime(int labelIndex, double value, double minValue, double maxValue) { - var timestamp = (int) value; + var timestamp = (int)value; var dateTime = DateTimeUtil.GetDateTime(timestamp); var dateString = string.Empty; if (string.IsNullOrEmpty(numericFormatter) || numericFormatter.Equals("f2")) diff --git a/Runtime/Internal/Utilities/ChartCached.cs b/Runtime/Internal/Utilities/ChartCached.cs index 59a505bd..d376421a 100644 --- a/Runtime/Internal/Utilities/ChartCached.cs +++ b/Runtime/Internal/Utilities/ChartCached.cs @@ -53,10 +53,7 @@ namespace XCharts.Runtime { if (string.IsNullOrEmpty(formatter)) { - if (value - (int) value == 0) - s_NumberToStr[value][formatter] = ((int) value).ToString(); - else - s_NumberToStr[value][formatter] = value.ToString(); + s_NumberToStr[value][formatter] = value.ToString(); } else if (formatter.StartsWith(NUMERIC_FORMATTER_D) || formatter.StartsWith(NUMERIC_FORMATTER_d) || @@ -64,7 +61,7 @@ namespace XCharts.Runtime formatter.StartsWith(NUMERIC_FORMATTER_x) ) { - s_NumberToStr[value][formatter] = ((int) value).ToString(formatter, ci); + s_NumberToStr[value][formatter] = ((int)value).ToString(formatter, ci); } else { diff --git a/Runtime/Internal/Utilities/ChartHelper.cs b/Runtime/Internal/Utilities/ChartHelper.cs index 8f3b7d6f..517a79f8 100644 --- a/Runtime/Internal/Utilities/ChartHelper.cs +++ b/Runtime/Internal/Utilities/ChartHelper.cs @@ -782,19 +782,7 @@ namespace XCharts.Runtime return min; } - public static int GetFloatAccuracy(double value) - { - if (value > 1 || value < -1) return 0; - int count = 1; - int intvalue = (int) (value * Mathf.Pow(10, count)); - while (intvalue == 0 && count < 38) - { - count++; - intvalue = (int) (value * Mathf.Pow(10, count)); - } - if (count == 38 && (value == 0 || value == 1)) return 1; - else return count; - } + public static void AddEventListener(GameObject obj, EventTriggerType type, UnityEngine.Events.UnityAction call) diff --git a/Runtime/Internal/Utilities/MathUtil.cs b/Runtime/Internal/Utilities/MathUtil.cs index 2103a5fa..443d3f97 100644 --- a/Runtime/Internal/Utilities/MathUtil.cs +++ b/Runtime/Internal/Utilities/MathUtil.cs @@ -1,10 +1,5 @@ using System; -using System.Collections.Generic; -using System.Text; -using System.Text.RegularExpressions; using UnityEngine; -using UnityEngine.EventSystems; -using UnityEngine.UI; namespace XCharts.Runtime { @@ -41,5 +36,23 @@ namespace XCharts.Runtime { return a + (b - a) * Clamp01(t); } + + public static bool IsInteger(double value) + { + return Math.Abs(value % 1) <= (Double.Epsilon * 100); + } + + public static int GetPrecision(double value) + { + if (IsInteger(value)) return 0; + int count = 1; + double intvalue = value * Mathf.Pow(10, count); + while (!IsInteger(intvalue) && count < 38) + { + count++; + intvalue = value * Mathf.Pow(10, count); + } + return count; + } } } \ No newline at end of file diff --git a/Runtime/Serie/Scatter/BaseScatterHandler.cs b/Runtime/Serie/Scatter/BaseScatterHandler.cs index aff96bab..eb0b895a 100644 --- a/Runtime/Serie/Scatter/BaseScatterHandler.cs +++ b/Runtime/Serie/Scatter/BaseScatterHandler.cs @@ -191,7 +191,7 @@ namespace XCharts.Runtime for (int count = 0; count < symbol.animationSize.Count; count++) { var nowSize = symbol.animationSize[count]; - color.a = (byte) (255 * (symbolSize - nowSize) / symbolSize); + color.a = (byte)(255 * (symbolSize - nowSize) / symbolSize); chart.DrawSymbol(vh, symbol.type, nowSize, symbolBorder, pos, color, toColor, emptyColor, borderColor, symbol.gap, cornerRadius); } @@ -293,7 +293,7 @@ namespace XCharts.Runtime for (int count = 0; count < symbol.animationSize.Count; count++) { var nowSize = symbol.animationSize[count]; - color.a = (byte) (255 * (symbolSize - nowSize) / symbolSize); + color.a = (byte)(255 * (symbolSize - nowSize) / symbolSize); chart.DrawSymbol(vh, symbol.type, nowSize, symbolBorder, pos, color, toColor, emptyColor, borderColor, symbol.gap, cornerRadius); } @@ -321,25 +321,25 @@ namespace XCharts.Runtime { if (axis.IsLog()) { - int minIndex = axis.GetLogMinIndex(); - float nowIndex = axis.GetLogValue(value); - return (nowIndex - minIndex) / axis.splitNumber * totalWidth; + var minIndex = axis.GetLogMinIndex(); + var nowIndex = axis.GetLogValue(value); + return (float)((nowIndex - minIndex) / axis.splitNumber * totalWidth); } else if (axis.IsCategory()) { if (axis.boundaryGap) { - float tick = (float) (totalWidth / (axis.context.minMaxRange + 1)); - return tick / 2 + (float) (value - axis.context.minValue) * tick; + float tick = (float)(totalWidth / (axis.context.minMaxRange + 1)); + return tick / 2 + (float)(value - axis.context.minValue) * tick; } else { - return (float) ((value - axis.context.minValue) / axis.context.minMaxRange * totalWidth); + return (float)((value - axis.context.minValue) / axis.context.minMaxRange * totalWidth); } } else { - return (float) ((value - axis.context.minValue) / axis.context.minMaxRange * totalWidth); + return (float)((value - axis.context.minValue) / axis.context.minMaxRange * totalWidth); } } }