mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-21 16:00:24 +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);
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
@@ -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 :
|
||||
|
||||
@@ -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"))
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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<BaseEventData> call)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user