优化Axis的数值Label的默认显示格式

This commit is contained in:
monitor1394
2023-02-10 13:23:38 +08:00
parent d6630ef5bf
commit 80d9087084
7 changed files with 69 additions and 65 deletions

View File

@@ -827,18 +827,18 @@ namespace XCharts.Runtime
return logBaseE ? (float) Math.Log(value) : (float) Math.Log(value, logBase); return logBaseE ? (float) Math.Log(value) : (float) Math.Log(value, logBase);
} }
public int GetLogMinIndex() public double GetLogMinIndex()
{ {
return logBaseE ? return logBaseE ?
(int) Math.Log(context.minValue) : Math.Log(context.minValue) :
(int) Math.Log(context.minValue, logBase); Math.Log(context.minValue, logBase);
} }
public int GetLogMaxIndex() public double GetLogMaxIndex()
{ {
return logBaseE ? return logBaseE ?
(int) Math.Log(context.maxValue) : Math.Log(context.maxValue) :
(int) Math.Log(context.maxValue, logBase); Math.Log(context.maxValue, logBase);
} }
public double GetLabelValue(int index) public double GetLabelValue(int index)

View File

@@ -255,8 +255,7 @@ namespace XCharts.Runtime
{ {
var value = axis.GetLabelValue(index); var value = axis.GetLabelValue(index);
var lastValue = axis.GetLabelValue(index - 1); var lastValue = axis.GetLabelValue(index - 1);
return axis.context.minMaxRange == 0 ? return axis.context.minMaxRange == 0 ? 0 :
0 :
(float)(coordinateWidth * (value - lastValue) / axis.context.minMaxRange); (float)(coordinateWidth * (value - lastValue) / axis.context.minMaxRange);
} }
else else
@@ -345,7 +344,10 @@ namespace XCharts.Runtime
int maxSplit = 0; int maxSplit = 0;
maxValue = ChartHelper.GetMaxLogValue(maxValue, axis.logBase, axis.logBaseE, out maxSplit); maxValue = ChartHelper.GetMaxLogValue(maxValue, axis.logBase, axis.logBaseE, out maxSplit);
minValue = ChartHelper.GetMinLogValue(minValue, axis.logBase, axis.logBaseE, out minSplit); 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; return;
} }
if (axis.type == Axis.AxisType.Time) { } if (axis.type == Axis.AxisType.Time) { }
@@ -470,7 +472,6 @@ namespace XCharts.Runtime
var yDataHig = (axis.context.minMaxRange == 0) ? 0f : 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; return gridXY + yDataHig;
} }
} }
@@ -565,11 +566,11 @@ namespace XCharts.Runtime
if (axis.IsLog()) if (axis.IsLog())
{ {
int minIndex = axis.GetLogMinIndex(); var minIndex = axis.GetLogMinIndex();
float nowIndex = axis.GetLogValue(value); var nowIndex = axis.GetLogValue(value);
return includeGridXY ? return includeGridXY ?
gridXY + (nowIndex - minIndex) / axis.splitNumber * gridHeight : (float)(gridXY + (nowIndex - minIndex) / axis.splitNumber * gridHeight) :
(nowIndex - minIndex) / axis.splitNumber * gridHeight; (float)((nowIndex - minIndex) / axis.splitNumber * gridHeight);
} }
else if (axis.IsCategory()) else if (axis.IsCategory())
{ {

View File

@@ -336,26 +336,31 @@ namespace XCharts.Runtime
public virtual string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false) 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 (string.IsNullOrEmpty(m_Formatter))
{ {
if (isLog) 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) if (minValue >= -1 && minValue <= 1 && maxValue >= -1 && maxValue <= 1)
{ {
int minAcc = ChartHelper.GetFloatAccuracy(minValue); int minAcc = MathUtil.GetPrecision(minValue);
int maxAcc = ChartHelper.GetFloatAccuracy(maxValue); int maxAcc = MathUtil.GetPrecision(maxValue);
int curAcc = ChartHelper.GetFloatAccuracy(value); int curAcc = MathUtil.GetPrecision(value);
int acc = Mathf.Max(Mathf.Max(minAcc, maxAcc), curAcc); 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 else
{ {
var content = m_Formatter; var content = m_Formatter;
FormatterHelper.ReplaceAxisLabelContent(ref content, numericFormatter, value); FormatterHelper.ReplaceAxisLabelContent(ref content, newNumericFormatter, value);
return GetFormatterFunctionContent(labelIndex, value, content); return GetFormatterFunctionContent(labelIndex, value, content);
} }
} }

View File

@@ -53,9 +53,6 @@ namespace XCharts.Runtime
{ {
if (string.IsNullOrEmpty(formatter)) 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) || else if (formatter.StartsWith(NUMERIC_FORMATTER_D) ||

View File

@@ -782,19 +782,7 @@ namespace XCharts.Runtime
return min; 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, public static void AddEventListener(GameObject obj, EventTriggerType type,
UnityEngine.Events.UnityAction<BaseEventData> call) UnityEngine.Events.UnityAction<BaseEventData> call)

View File

@@ -1,10 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace XCharts.Runtime namespace XCharts.Runtime
{ {
@@ -41,5 +36,23 @@ namespace XCharts.Runtime
{ {
return a + (b - a) * Clamp01(t); 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;
}
} }
} }

View File

@@ -321,9 +321,9 @@ namespace XCharts.Runtime
{ {
if (axis.IsLog()) if (axis.IsLog())
{ {
int minIndex = axis.GetLogMinIndex(); var minIndex = axis.GetLogMinIndex();
float nowIndex = axis.GetLogValue(value); var nowIndex = axis.GetLogValue(value);
return (nowIndex - minIndex) / axis.splitNumber * totalWidth; return (float)((nowIndex - minIndex) / axis.splitNumber * totalWidth);
} }
else if (axis.IsCategory()) else if (axis.IsCategory())
{ {