mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-31 13:58:48 +00:00
优化Axis的数值Label的默认显示格式
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ namespace XCharts.Runtime
|
|||||||
var min = axis is YAxis ? 20 : 80;
|
var min = axis is YAxis ? 20 : 80;
|
||||||
if (eachWid > min) return dataCount;
|
if (eachWid > min) return dataCount;
|
||||||
var tick = Mathf.CeilToInt(min / eachWid);
|
var tick = Mathf.CeilToInt(min / eachWid);
|
||||||
return (int) (dataCount / tick);
|
return (int)(dataCount / tick);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -132,7 +132,7 @@ namespace XCharts.Runtime
|
|||||||
maxValue = -maxValue;
|
maxValue = -maxValue;
|
||||||
}
|
}
|
||||||
if (forcePercent)
|
if (forcePercent)
|
||||||
return string.Format("{0}%", (int) value);
|
return string.Format("{0}%", (int)value);
|
||||||
else
|
else
|
||||||
return axis.axisLabel.GetFormatterContent(index, value, minValue, maxValue);
|
return axis.axisLabel.GetFormatterContent(index, value, minValue, maxValue);
|
||||||
}
|
}
|
||||||
@@ -255,9 +255,8 @@ 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) { }
|
||||||
@@ -460,7 +462,7 @@ namespace XCharts.Runtime
|
|||||||
if (axis.IsCategory())
|
if (axis.IsCategory())
|
||||||
{
|
{
|
||||||
if (dataCount == 0) dataCount = axis.data.Count;
|
if (dataCount == 0) dataCount = axis.data.Count;
|
||||||
var categoryIndex = (int) value;
|
var categoryIndex = (int)value;
|
||||||
var scaleWid = AxisHelper.GetDataWidth(axis, gridHeight, dataCount, dataZoom);
|
var scaleWid = AxisHelper.GetDataWidth(axis, gridHeight, dataCount, dataZoom);
|
||||||
float startY = gridXY + (axis.boundaryGap ? scaleWid / 2 : 0);
|
float startY = gridXY + (axis.boundaryGap ? scaleWid / 2 : 0);
|
||||||
return startY + scaleWid * categoryIndex;
|
return startY + scaleWid * categoryIndex;
|
||||||
@@ -468,9 +470,8 @@ namespace XCharts.Runtime
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -539,7 +540,7 @@ namespace XCharts.Runtime
|
|||||||
{
|
{
|
||||||
if (axis.IsCategory())
|
if (axis.IsCategory())
|
||||||
{
|
{
|
||||||
return (int) value;
|
return (int)value;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -550,9 +551,9 @@ namespace XCharts.Runtime
|
|||||||
if (totalSplitNumber == -1)
|
if (totalSplitNumber == -1)
|
||||||
totalSplitNumber = GetTotalSplitGridNum(axis);
|
totalSplitNumber = GetTotalSplitGridNum(axis);
|
||||||
if (axis.minMaxType == Axis.AxisMinMaxType.Custom)
|
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
|
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())
|
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())
|
||||||
{
|
{
|
||||||
var categoryIndex = (int) value;
|
var categoryIndex = (int)value;
|
||||||
return includeGridXY ?
|
return includeGridXY ?
|
||||||
gridXY + (axis.boundaryGap ? scaleWidth / 2 : 0) + scaleWidth * categoryIndex :
|
gridXY + (axis.boundaryGap ? scaleWidth / 2 : 0) + scaleWidth * categoryIndex :
|
||||||
(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 (axis.context.minMaxRange != 0)
|
||||||
{
|
{
|
||||||
if (realLength)
|
if (realLength)
|
||||||
yDataHig = (float) (value * gridHeight / axis.context.minMaxRange);
|
yDataHig = (float)(value * gridHeight / axis.context.minMaxRange);
|
||||||
else
|
else
|
||||||
yDataHig = (float) ((value - axis.context.minValue) / axis.context.minMaxRange * gridHeight);
|
yDataHig = (float)((value - axis.context.minValue) / axis.context.minMaxRange * gridHeight);
|
||||||
}
|
}
|
||||||
return includeGridXY ?
|
return includeGridXY ?
|
||||||
gridXY + yDataHig :
|
gridXY + yDataHig :
|
||||||
|
|||||||
@@ -336,33 +336,38 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetFormatterDateTime(int labelIndex, double value, double minValue, double maxValue)
|
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 dateTime = DateTimeUtil.GetDateTime(timestamp);
|
||||||
var dateString = string.Empty;
|
var dateString = string.Empty;
|
||||||
if (string.IsNullOrEmpty(numericFormatter) || numericFormatter.Equals("f2"))
|
if (string.IsNullOrEmpty(numericFormatter) || numericFormatter.Equals("f2"))
|
||||||
|
|||||||
@@ -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) ||
|
||||||
@@ -64,7 +61,7 @@ namespace XCharts.Runtime
|
|||||||
formatter.StartsWith(NUMERIC_FORMATTER_x)
|
formatter.StartsWith(NUMERIC_FORMATTER_x)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
s_NumberToStr[value][formatter] = ((int) value).ToString(formatter, ci);
|
s_NumberToStr[value][formatter] = ((int)value).ToString(formatter, ci);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,7 +191,7 @@ namespace XCharts.Runtime
|
|||||||
for (int count = 0; count < symbol.animationSize.Count; count++)
|
for (int count = 0; count < symbol.animationSize.Count; count++)
|
||||||
{
|
{
|
||||||
var nowSize = symbol.animationSize[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,
|
chart.DrawSymbol(vh, symbol.type, nowSize, symbolBorder, pos,
|
||||||
color, toColor, emptyColor, borderColor, symbol.gap, cornerRadius);
|
color, toColor, emptyColor, borderColor, symbol.gap, cornerRadius);
|
||||||
}
|
}
|
||||||
@@ -293,7 +293,7 @@ namespace XCharts.Runtime
|
|||||||
for (int count = 0; count < symbol.animationSize.Count; count++)
|
for (int count = 0; count < symbol.animationSize.Count; count++)
|
||||||
{
|
{
|
||||||
var nowSize = symbol.animationSize[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,
|
chart.DrawSymbol(vh, symbol.type, nowSize, symbolBorder, pos,
|
||||||
color, toColor, emptyColor, borderColor, symbol.gap, cornerRadius);
|
color, toColor, emptyColor, borderColor, symbol.gap, cornerRadius);
|
||||||
}
|
}
|
||||||
@@ -321,25 +321,25 @@ 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())
|
||||||
{
|
{
|
||||||
if (axis.boundaryGap)
|
if (axis.boundaryGap)
|
||||||
{
|
{
|
||||||
float tick = (float) (totalWidth / (axis.context.minMaxRange + 1));
|
float tick = (float)(totalWidth / (axis.context.minMaxRange + 1));
|
||||||
return tick / 2 + (float) (value - axis.context.minValue) * tick;
|
return tick / 2 + (float)(value - axis.context.minValue) * tick;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return (float) ((value - axis.context.minValue) / axis.context.minMaxRange * totalWidth);
|
return (float)((value - axis.context.minValue) / axis.context.minMaxRange * totalWidth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return (float) ((value - axis.context.minValue) / axis.context.minMaxRange * totalWidth);
|
return (float)((value - axis.context.minValue) / axis.context.minMaxRange * totalWidth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user