AxisType.Value类型的坐标轴可以设置AxisMinMaxType控制最大最小刻度

This commit is contained in:
monitor1394
2019-05-16 09:39:58 +08:00
parent e72cfbdd3f
commit 9a914eff3d
8 changed files with 2360 additions and 1304 deletions

View File

@@ -15,6 +15,13 @@ namespace XCharts
//Log
}
public enum AxisMinMaxType
{
Default,
MinMax,
Custom
}
public enum SplitLineType
{
None,
@@ -54,6 +61,9 @@ namespace XCharts
[SerializeField] protected bool m_Show = true;
[SerializeField] protected AxisType m_Type;
[SerializeField] protected AxisMinMaxType m_MinMaxType;
[SerializeField] protected int m_Min;
[SerializeField] protected int m_Max;
[SerializeField] protected int m_SplitNumber = 5;
[SerializeField] protected int m_TextRotation = 0;
[SerializeField] protected bool m_ShowSplitLine = false;
@@ -64,6 +74,9 @@ namespace XCharts
public bool show { get { return m_Show; }set { m_Show = value; } }
public AxisType type { get { return m_Type; } set { m_Type = value; } }
public AxisMinMaxType minMaxType { get { return m_MinMaxType; } set { m_MinMaxType = value; } }
public int min { get { return m_Min; } set { m_Min = value; } }
public int max { get { return m_Max; } set { m_Max = value; } }
public int splitNumber { get { return m_SplitNumber; } set { m_SplitNumber = value; } }
public int textRotation { get { return m_TextRotation; } set { m_TextRotation = value; } }
public bool showSplitLine { get { return m_ShowSplitLine; } set { m_ShowSplitLine = value; } }
@@ -76,6 +89,8 @@ namespace XCharts
{
m_Show = other.show;
m_Type = other.type;
m_Min = other.min;
m_Max = other.max;
m_SplitNumber = other.splitNumber;
m_TextRotation = other.textRotation;
m_ShowSplitLine = other.showSplitLine;
@@ -187,6 +202,8 @@ namespace XCharts
{
return show == other.show &&
type == other.type &&
min == other.min &&
max == other.max &&
splitNumber == other.splitNumber &&
showSplitLine == other.showSplitLine &&
textRotation == other.textRotation &&
@@ -228,6 +245,8 @@ namespace XCharts
{
m_Show = true,
m_Type = AxisType.Category,
m_Min = 0,
m_Max = 0,
m_SplitNumber = 5,
m_TextRotation = 0,
m_ShowSplitLine = false,
@@ -254,6 +273,8 @@ namespace XCharts
{
m_Show = true,
m_Type = AxisType.Value,
m_Min = 0,
m_Max = 0,
m_SplitNumber = 5,
m_TextRotation = 0,
m_ShowSplitLine = false,

View File

@@ -80,7 +80,8 @@ namespace XCharts
CheckLegend();
CheckTooltip();
}
#if UNITY_EDITOR
protected override void Reset()
{
ChartHelper.DestoryAllChilds(transform);
@@ -93,6 +94,7 @@ namespace XCharts
InitLegend();
InitTooltip();
}
#endif
protected override void OnDestroy()
{
@@ -224,6 +226,7 @@ namespace XCharts
anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight));
m_Legend.SetButton(i, btn);
m_Legend.SetActive(i, IsActive(i));
m_Legend.UpdateButtonColor(i, m_ThemeInfo.GetColor(i), m_ThemeInfo.unableColor);
btn.GetComponentInChildren<Text>().text = m_Legend.data[i];
ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerDown, (data) =>

View File

@@ -335,7 +335,60 @@ namespace XCharts
int tempMaxValue = 100;
if (m_Series != null)
{
m_Series.GetMinMaxValue(m_Legend, out tempMinValue, out tempMaxValue);
m_Series.GetMinMaxValue(out tempMinValue, out tempMaxValue);
}
if (m_XAxis.type == Axis.AxisType.Value)
{
switch (m_XAxis.minMaxType)
{
case Axis.AxisMinMaxType.Default:
if (tempMinValue > 0 && tempMaxValue > 0)
{
tempMinValue = 0;
tempMaxValue = ChartHelper.GetMaxDivisibleValue(tempMaxValue);
}
else if (tempMinValue < 0 && tempMaxValue < 0)
{
tempMinValue = ChartHelper.GetMinDivisibleValue(tempMinValue);
tempMaxValue = 0;
}
break;
case Axis.AxisMinMaxType.MinMax:
tempMinValue = ChartHelper.GetMinDivisibleValue(tempMinValue);
tempMaxValue = ChartHelper.GetMaxDivisibleValue(tempMaxValue);
break;
case Axis.AxisMinMaxType.Custom:
if (m_XAxis.min != 0) tempMinValue = m_XAxis.min;
if (m_XAxis.max != 0) tempMaxValue = m_XAxis.max;
break;
}
}
else if (m_YAxis.type == Axis.AxisType.Value)
{
switch (m_YAxis.minMaxType)
{
case Axis.AxisMinMaxType.Default:
if (tempMinValue > 0 && tempMaxValue > 0)
{
tempMinValue = 0;
tempMaxValue = ChartHelper.GetMaxDivisibleValue(tempMaxValue);
}
else if (tempMinValue < 0 && tempMaxValue < 0)
{
tempMinValue = ChartHelper.GetMinDivisibleValue(tempMinValue);
tempMaxValue = 0;
}
break;
case Axis.AxisMinMaxType.MinMax:
tempMinValue = ChartHelper.GetMinDivisibleValue(tempMinValue);
tempMaxValue = ChartHelper.GetMaxDivisibleValue(tempMaxValue);
break;
case Axis.AxisMinMaxType.Custom:
if (m_YAxis.min != 0) tempMinValue = m_YAxis.min;
if (m_YAxis.max != 0) tempMaxValue = m_YAxis.max;
break;
}
}
if (tempMinValue != minValue || tempMaxValue != maxValue)
{
@@ -343,12 +396,12 @@ namespace XCharts
maxValue = tempMaxValue;
if (m_XAxis.type == Axis.AxisType.Value)
{
m_ZeroXOffset = Mathf.Abs(minValue) * (coordinateWid / (Mathf.Abs(minValue) + Mathf.Abs(maxValue)));
m_ZeroXOffset = minValue > 0 ? 0 : Mathf.Abs(minValue) * (coordinateWid / (Mathf.Abs(minValue) + Mathf.Abs(maxValue)));
OnXMaxValueChanged();
}
else if (m_YAxis.type == Axis.AxisType.Value)
{
m_ZeroYOffset = Mathf.Abs(minValue) * (coordinateHig / (Mathf.Abs(minValue) + Mathf.Abs(maxValue)));
m_ZeroYOffset = minValue > 0 ? 0 : Mathf.Abs(minValue) * (coordinateHig / (Mathf.Abs(minValue) + Mathf.Abs(maxValue)));
OnYMaxValueChanged();
}
RefreshChart();

View File

@@ -164,7 +164,7 @@ namespace XCharts
}
}
public void GetMinMaxValue(Legend legend, out int minVaule, out int maxValue)
public void GetMinMaxValue(out int minVaule, out int maxValue)
{
float min = int.MaxValue;
float max = int.MinValue;
@@ -199,7 +199,7 @@ namespace XCharts
{
for (int i = 0; i < m_Series.Count; i++)
{
if (legend.IsActive(i))
if (IsActive(i))
{
if (m_Series[i].Max > max) max = m_Series[i].Max;
if (m_Series[i].Min < min) min = m_Series[i].Min;
@@ -211,21 +211,26 @@ namespace XCharts
minVaule = 0;
maxValue = 100;
}
else if (max > 0 && min > 0)
{
minVaule = 0;
maxValue = ChartHelper.GetMaxDivisibleValue(max);
}
else if (min < 0 && max < 0)
{
minVaule = ChartHelper.GetMaxDivisibleValue(min);
maxValue = 0;
}
else
{
minVaule = ChartHelper.GetMaxDivisibleValue(min);
maxValue = ChartHelper.GetMaxDivisibleValue(max);
minVaule = (int)min;
maxValue = (int)max;
}
//else if (max > 0 && min > 0)
//{
// minVaule = 0;
// maxValue = ChartHelper.GetMaxDivisibleValue(max);
//}
//else if (min < 0 && max < 0)
//{
// minVaule = ChartHelper.GetMaxDivisibleValue(min);
// maxValue = 0;
//}
//else
//{
// minVaule = ChartHelper.GetMaxDivisibleValue(min);
// maxValue = ChartHelper.GetMaxDivisibleValue(max);
//}
}
public float GetMaxValue(int index, int splitNumber = 0)

View File

@@ -49,7 +49,7 @@ namespace XCharts
Transform obj = transform;
while (obj.transform.parent)
{
name += "/"+obj.transform.parent.name;
name += "/" + obj.transform.parent.name;
obj = obj.transform.parent;
}
return name;
@@ -98,7 +98,7 @@ namespace XCharts
public static Text AddTextObject(string name, Transform parent, Font font, Color color,
TextAnchor anchor, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta,
int fontSize = 14,float textRotation = 0)
int fontSize = 14, float textRotation = 0)
{
GameObject txtObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
Text txt = GetOrAddComponent<Text>(txtObj);
@@ -111,7 +111,7 @@ namespace XCharts
txt.color = color;
if (textRotation > 0)
{
txtObj.transform.localEulerAngles = new Vector3(0,0,textRotation);
txtObj.transform.localEulerAngles = new Vector3(0, 0, textRotation);
}
RectTransform rect = GetOrAddComponent<RectTransform>(txtObj);
@@ -435,7 +435,7 @@ namespace XCharts
if (Regex.IsMatch(jsonData, pattern))
{
MatchCollection m = Regex.Matches(jsonData, pattern);
foreach(Match match in m)
foreach (Match match in m)
{
list.Add(match.Groups[1].Value);
}
@@ -460,22 +460,37 @@ namespace XCharts
n++;
}
float mm = bigger;
if(mm > 10)
if (mm > 10)
{
mm = bigger - bigger % (Mathf.Pow(10, n));
//if (mm + Mathf.Pow(10, n) / 2 > bigger)
//{
// mm += Mathf.Pow(10, n) / 2;
//}
//else
{
mm += Mathf.Pow(10, n);
}
mm += max >0 ? Mathf.Pow(10, n) : -Mathf.Pow(10, n);
}
if (max < 0) return (int)-mm;
else return (int)mm;
}
public static int GetMinDivisibleValue(float min)
{
if (min == 0) return 0;
int bigger = (int)Mathf.Abs(min);
int n = 1;
while (bigger / (Mathf.Pow(10, n)) > 10)
{
n++;
}
float mm = bigger;
if (mm > 10)
{
mm = bigger - bigger % (Mathf.Pow(10, n));
mm += min < 0 ? Mathf.Pow(10, n) : -Mathf.Pow(10, n);
}
if (min < 0)
{
return (int)-mm;
}
else return (int)mm;
}
public static void AddEventListener(GameObject obj, EventTriggerType type,
UnityEngine.Events.UnityAction<BaseEventData> call)
{