增加Axis数值轴的最大最小值可设置为小数的支持,优化极小数图表的表现效果。

This commit is contained in:
monitor1394
2019-10-26 05:00:26 +08:00
parent 55a3533230
commit 912e0b55eb
4 changed files with 65 additions and 22 deletions

View File

@@ -95,8 +95,8 @@ namespace XCharts
[SerializeField] protected bool m_Show = true; [SerializeField] protected bool m_Show = true;
[SerializeField] protected AxisType m_Type; [SerializeField] protected AxisType m_Type;
[SerializeField] protected AxisMinMaxType m_MinMaxType; [SerializeField] protected AxisMinMaxType m_MinMaxType;
[SerializeField] protected int m_Min; [SerializeField] protected float m_Min;
[SerializeField] protected int m_Max; [SerializeField] protected float m_Max;
[SerializeField] protected int m_SplitNumber = 5; [SerializeField] protected int m_SplitNumber = 5;
[SerializeField] protected float m_Interval = 0; [SerializeField] protected float m_Interval = 0;
[SerializeField] protected bool m_ShowSplitLine = false; [SerializeField] protected bool m_ShowSplitLine = false;
@@ -132,12 +132,12 @@ namespace XCharts
/// The minimun value of axis. /// The minimun value of axis.
/// 设定的坐标轴刻度最小值当minMaxType为Custom时有效。 /// 设定的坐标轴刻度最小值当minMaxType为Custom时有效。
/// </summary> /// </summary>
public int min { get { return m_Min; } set { m_Min = value; } } public float min { get { return m_Min; } set { m_Min = value; } }
/// <summary> /// <summary>
/// The maximum value of axis. /// The maximum value of axis.
/// 设定的坐标轴刻度最大值当minMaxType为Custom时有效。 /// 设定的坐标轴刻度最大值当minMaxType为Custom时有效。
/// </summary> /// </summary>
public int max { get { return m_Max; } set { m_Max = value; } } public float max { get { return m_Max; } set { m_Max = value; } }
/// <summary> /// <summary>
/// Number of segments that the axis is split into. /// Number of segments that the axis is split into.
/// 坐标轴的分割段数。 /// 坐标轴的分割段数。
@@ -456,7 +456,7 @@ namespace XCharts
value = (minValue + (maxValue - minValue) * index / (split - 1)); value = (minValue + (maxValue - minValue) * index / (split - 1));
} }
if (forcePercent) return string.Format("{0}%", (int)value); if (forcePercent) return string.Format("{0}%", (int)value);
else return m_AxisLabel.GetFormatterContent(value); else return m_AxisLabel.GetFormatterContent(value, minValue, maxValue);
} }
var showData = GetDataList(dataZoom); var showData = GetDataList(dataZoom);
int dataCount = showData.Count; int dataCount = showData.Count;
@@ -583,7 +583,7 @@ namespace XCharts
/// </summary> /// </summary>
/// <param name="minValue"></param> /// <param name="minValue"></param>
/// <param name="maxValue"></param> /// <param name="maxValue"></param>
public void AdjustMinMaxValue(ref int minValue, ref int maxValue) public void AdjustMinMaxValue(ref float minValue, ref float maxValue)
{ {
if (minMaxType == Axis.AxisMinMaxType.Custom) if (minMaxType == Axis.AxisMinMaxType.Custom)
{ {
@@ -655,6 +655,8 @@ namespace XCharts
m_AxisLabel.Equals(other.axisLabel) && m_AxisLabel.Equals(other.axisLabel) &&
splitLineType == other.splitLineType && splitLineType == other.splitLineType &&
boundaryGap == other.boundaryGap && boundaryGap == other.boundaryGap &&
minValue == other.minValue &&
maxValue == other.maxValue &&
axisName.Equals(other.axisName) && axisName.Equals(other.axisName) &&
ChartHelper.IsValueEqualsList<string>(m_Data, other.data); ChartHelper.IsValueEqualsList<string>(m_Data, other.data);
} }

View File

@@ -556,7 +556,7 @@ namespace XCharts
/// <param name="minVaule"></param> /// <param name="minVaule"></param>
/// <param name="maxValue"></param> /// <param name="maxValue"></param>
public void GetXMinMaxValue(DataZoom dataZoom, int axisIndex, bool isValueAxis, public void GetXMinMaxValue(DataZoom dataZoom, int axisIndex, bool isValueAxis,
out int minVaule, out int maxValue) out float minVaule, out float maxValue)
{ {
GetMinMaxValue(dataZoom, axisIndex, isValueAxis, false, out minVaule, out maxValue); GetMinMaxValue(dataZoom, axisIndex, isValueAxis, false, out minVaule, out maxValue);
} }
@@ -569,7 +569,7 @@ namespace XCharts
/// <param name="minVaule"></param> /// <param name="minVaule"></param>
/// <param name="maxValue"></param> /// <param name="maxValue"></param>
public void GetYMinMaxValue(DataZoom dataZoom, int axisIndex, bool isValueAxis, public void GetYMinMaxValue(DataZoom dataZoom, int axisIndex, bool isValueAxis,
out int minVaule, out int maxValue) out float minVaule, out float maxValue)
{ {
GetMinMaxValue(dataZoom, axisIndex, isValueAxis, true, out minVaule, out maxValue); GetMinMaxValue(dataZoom, axisIndex, isValueAxis, true, out minVaule, out maxValue);
} }
@@ -577,7 +577,7 @@ namespace XCharts
private Dictionary<int, List<Serie>> _stackSeriesForMinMax = new Dictionary<int, List<Serie>>(); private Dictionary<int, List<Serie>> _stackSeriesForMinMax = new Dictionary<int, List<Serie>>();
private Dictionary<int, float> _serieTotalValueForMinMax = new Dictionary<int, float>(); private Dictionary<int, float> _serieTotalValueForMinMax = new Dictionary<int, float>();
public void GetMinMaxValue(DataZoom dataZoom, int axisIndex, bool isValueAxis, bool yValue, public void GetMinMaxValue(DataZoom dataZoom, int axisIndex, bool isValueAxis, bool yValue,
out int minVaule, out int maxValue) out float minVaule, out float maxValue)
{ {
float min = int.MaxValue; float min = int.MaxValue;
float max = int.MinValue; float max = int.MinValue;
@@ -593,7 +593,6 @@ namespace XCharts
{ {
if (isPercentStack && IsPercentStack(serie.name, SerieType.Bar)) if (isPercentStack && IsPercentStack(serie.name, SerieType.Bar))
{ {
Debug.LogError("minmax:" + serie.name);
if (100 > max) max = 100; if (100 > max) max = 100;
if (0 < min) min = 0; if (0 < min) min = 0;
} }
@@ -664,9 +663,15 @@ namespace XCharts
maxValue = 0; maxValue = 0;
} }
else else
{
if (max > 1)
{ {
minVaule = Mathf.FloorToInt(min); minVaule = Mathf.FloorToInt(min);
maxValue = Mathf.CeilToInt(max); maxValue = Mathf.CeilToInt(max);
}else{
minVaule = min;
maxValue = max;
}
} }
} }

View File

@@ -319,8 +319,8 @@ namespace XCharts
if (serieData != null && serieData.highlighted) if (serieData != null && serieData.highlighted)
{ {
sb.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : ""); sb.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "");
sb.Append("[").Append(ChartCached.FloatToStr(xValue)).Append(",") sb.Append("[").Append(ChartCached.FloatToStr(xValue, 0, m_Tooltip.forceENotation)).Append(",")
.Append(ChartCached.FloatToStr(yValue)).Append("]\n"); .Append(ChartCached.FloatToStr(yValue, 0, m_Tooltip.forceENotation)).Append("]\n");
} }
} }
else else
@@ -328,7 +328,7 @@ namespace XCharts
sb.Append("\n") sb.Append("\n")
.Append("<color=#").Append(m_ThemeInfo.GetColorStr(i)).Append(">● </color>") .Append("<color=#").Append(m_ThemeInfo.GetColorStr(i)).Append(">● </color>")
.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "") .Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "")
.Append(ChartCached.FloatToStr(yValue)); .Append(ChartCached.FloatToStr(yValue,0,m_Tooltip.forceENotation));
} }
} }
} }
@@ -766,8 +766,8 @@ namespace XCharts
private void UpdateAxisMinMaxValue(int axisIndex, Axis axis, bool updateChart = true) private void UpdateAxisMinMaxValue(int axisIndex, Axis axis, bool updateChart = true)
{ {
if (axis.IsCategory() || !axis.show) return; if (axis.IsCategory() || !axis.show) return;
int tempMinValue = 0; float tempMinValue = 0;
int tempMaxValue = 0; float tempMaxValue = 0;
if (IsValue()) if (IsValue())
{ {
@@ -788,7 +788,6 @@ namespace XCharts
if (tempMinValue != axis.minValue || tempMaxValue != axis.maxValue) if (tempMinValue != axis.minValue || tempMaxValue != axis.maxValue)
{ {
m_CheckMinMaxValue = true; m_CheckMinMaxValue = true;
axis.minValue = tempMinValue; axis.minValue = tempMinValue;
axis.maxValue = tempMaxValue; axis.maxValue = tempMaxValue;
axis.zeroXOffset = 0; axis.zeroXOffset = 0;
@@ -1039,8 +1038,8 @@ namespace XCharts
float scaleWid = coordinateWidth / (showData.Count - 1); float scaleWid = coordinateWidth / (showData.Count - 1);
Vector3 lp = Vector3.zero; Vector3 lp = Vector3.zero;
Vector3 np = Vector3.zero; Vector3 np = Vector3.zero;
int minValue = 0; float minValue = 0;
int maxValue = 0; float maxValue = 0;
m_Series.GetYMinMaxValue(null, 0, IsValue(), out minValue, out maxValue); m_Series.GetYMinMaxValue(null, 0, IsValue(), out minValue, out maxValue);
axis.AdjustMinMaxValue(ref minValue, ref maxValue); axis.AdjustMinMaxValue(ref minValue, ref maxValue);

View File

@@ -409,9 +409,21 @@ namespace XCharts
return (Color32)color; return (Color32)color;
} }
public static int GetMaxDivisibleValue(float max) public static float GetMaxDivisibleValue(float max)
{ {
if (max == 0) return 0; if (max == 0) return 0;
if (max > -1 && max < 1)
{
int count = 1;
int intvalue = (int)(max * Mathf.Pow(10, count));
while (intvalue == 0 && count < 12)
{
count++;
intvalue = (int)(max * Mathf.Pow(10, count));
}
if (max > 0) return 1 / Mathf.Pow(10, count - 1);
else return -1 / Mathf.Pow(10, count);
}
int bigger = Mathf.CeilToInt(Mathf.Abs(max)); int bigger = Mathf.CeilToInt(Mathf.Abs(max));
int n = 1; int n = 1;
while (bigger / (Mathf.Pow(10, n)) > 10) while (bigger / (Mathf.Pow(10, n)) > 10)
@@ -428,9 +440,21 @@ namespace XCharts
else return Mathf.CeilToInt(mm); else return Mathf.CeilToInt(mm);
} }
public static int GetMinDivisibleValue(float min) public static float GetMinDivisibleValue(float min)
{ {
if (min == 0) return 0; if (min == 0) return 0;
if (min > -1 && min < 1)
{
int count = 1;
int intvalue = (int)(min * Mathf.Pow(10, count));
while (intvalue == 0 && count < 12)
{
count++;
intvalue = (int)(min * Mathf.Pow(10, count));
}
if (min > 0) return 1 / Mathf.Pow(10, count);
else return -1 / Mathf.Pow(10, count - 1);
}
int bigger = Mathf.FloorToInt(Mathf.Abs(min)); int bigger = Mathf.FloorToInt(Mathf.Abs(min));
int n = 1; int n = 1;
while (bigger / (Mathf.Pow(10, n)) > 10) while (bigger / (Mathf.Pow(10, n)) > 10)
@@ -447,6 +471,19 @@ namespace XCharts
else return Mathf.FloorToInt(mm); else return Mathf.FloorToInt(mm);
} }
public static int GetFloatAccuracy(float value)
{
if (value > 1 || value < -1) return 0;
int count = 1;
int intvalue = (int)(value * Mathf.Pow(10, count));
while (intvalue == 0 && count < 12)
{
count++;
intvalue = (int)(value * Mathf.Pow(10, count));
}
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)
{ {