mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-16 05:10:12 +00:00
重构代码
This commit is contained in:
@@ -24,27 +24,6 @@ namespace xcharts
|
||||
public float bottom;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Coordinate
|
||||
{
|
||||
public bool show = true;
|
||||
public float left = 40f;
|
||||
public float right = 10f;
|
||||
public float top = 10;
|
||||
public float bottom = 20f;
|
||||
public float tickness = 0.8f;
|
||||
public float scaleLen = 5.0f;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public enum AxisType
|
||||
{
|
||||
value,
|
||||
category,
|
||||
time,
|
||||
log
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public enum Align
|
||||
{
|
||||
@@ -66,36 +45,6 @@ namespace xcharts
|
||||
end,
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Axis
|
||||
{
|
||||
public AxisType type;
|
||||
public int splitNumber = 5;
|
||||
public int maxSplitNumber = 5;
|
||||
public bool showSplitLine;
|
||||
public bool boundaryGap = true;
|
||||
public List<string> data;
|
||||
|
||||
public void AddCategory(string category)
|
||||
{
|
||||
if (data.Count >= maxSplitNumber)
|
||||
{
|
||||
data.RemoveAt(0);
|
||||
}
|
||||
data.Add(category);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class XAxis : Axis
|
||||
{
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class YAxis : Axis
|
||||
{
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class LegendData
|
||||
{
|
||||
@@ -195,9 +144,6 @@ namespace xcharts
|
||||
|
||||
public class BaseChart : MaskableGraphic
|
||||
{
|
||||
private const int DEFAULT_YSACLE_NUM = 5;
|
||||
private const string YSCALE_TEXT_PREFIX = "yScale";
|
||||
private const string XSCALE_TEXT_PREFIX = "xScale";
|
||||
private const string TILTE_TEXT = "title";
|
||||
private const string LEGEND_TEXT = "legend";
|
||||
[SerializeField]
|
||||
@@ -207,60 +153,25 @@ namespace xcharts
|
||||
[SerializeField]
|
||||
protected Title title;
|
||||
[SerializeField]
|
||||
protected Coordinate coordinate;
|
||||
[SerializeField]
|
||||
protected XAxis xAxis;
|
||||
[SerializeField]
|
||||
protected YAxis yAxis;
|
||||
[SerializeField]
|
||||
protected Legend legend;
|
||||
[SerializeField]
|
||||
protected List<Series> seriesList = new List<Series>();
|
||||
|
||||
//============check changed=================
|
||||
private float lastCoordinateWid;
|
||||
private float lastCoordinateHig;
|
||||
private float lastCoordinateScaleLen;
|
||||
|
||||
//private Align lastTitleAlign;
|
||||
//private float lastTitleLeft;
|
||||
//private float lastTitleRight;
|
||||
//private float lastTitleTop;
|
||||
|
||||
private float lastXMaxValue;
|
||||
private float lastYMaxValue;
|
||||
|
||||
private Coordinate checkCoordinate = new Coordinate();
|
||||
|
||||
private Title checkTitle = new Title();
|
||||
private Legend checkLegend = new Legend();
|
||||
private XAxis checkXAxis = new XAxis();
|
||||
private YAxis checkYAxis = new YAxis();
|
||||
//===========================================
|
||||
|
||||
protected Text titleText;
|
||||
protected List<Text> yScaleTextList = new List<Text>();
|
||||
protected List<Text> xScaleTextList = new List<Text>();
|
||||
protected List<Text> legendTextList = new List<Text>();
|
||||
|
||||
protected float zeroX { get { return coordinate.left; } }
|
||||
protected float zeroY { get { return coordinate.bottom; } }
|
||||
protected float chartWid { get { return rectTransform.sizeDelta.x; } }
|
||||
protected float chartHig { get { return rectTransform.sizeDelta.y; } }
|
||||
protected float coordinateWid { get { return chartWid - coordinate.left - coordinate.right; } }
|
||||
protected float coordinateHig { get { return chartHig - coordinate.top - coordinate.bottom; } }
|
||||
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
rectTransform.anchorMax = Vector2.zero;
|
||||
rectTransform.anchorMin = Vector2.zero;
|
||||
rectTransform.pivot = Vector2.zero;
|
||||
lastCoordinateHig = chartHig;
|
||||
lastCoordinateWid = chartWid;
|
||||
lastCoordinateScaleLen = coordinate.scaleLen;
|
||||
|
||||
InitTitle();
|
||||
InitXScale();
|
||||
InitYScale();
|
||||
InitLegend();
|
||||
}
|
||||
|
||||
@@ -268,10 +179,6 @@ namespace xcharts
|
||||
{
|
||||
CheckTile();
|
||||
CheckLegend();
|
||||
CheckYAxisType();
|
||||
CheckXAxisType();
|
||||
CheckMaxValue();
|
||||
CheckCoordinate();
|
||||
}
|
||||
|
||||
public void AddData(string legend, string key, float value)
|
||||
@@ -287,18 +194,6 @@ namespace xcharts
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
public void AddXAxisCategory(string category)
|
||||
{
|
||||
xAxis.AddCategory(category);
|
||||
OnXAxisChanged();
|
||||
}
|
||||
|
||||
public void AddYAxisCategory(string category)
|
||||
{
|
||||
yAxis.AddCategory(category);
|
||||
OnYAxisChanged();
|
||||
}
|
||||
|
||||
protected void HideChild(string match = null)
|
||||
{
|
||||
for (int i = 0; i < transform.childCount; i++)
|
||||
@@ -352,78 +247,6 @@ namespace xcharts
|
||||
titleText.text = title.text;
|
||||
}
|
||||
|
||||
private void InitYScale()
|
||||
{
|
||||
yScaleTextList.Clear();
|
||||
if (yAxis.type == AxisType.value)
|
||||
{
|
||||
float max = GetMaxValue();
|
||||
if (max <= 0) max = 400;
|
||||
yAxis.splitNumber = DEFAULT_YSACLE_NUM;
|
||||
for (int i = 0; i < yAxis.splitNumber; i++)
|
||||
{
|
||||
Text txt = ChartUtils.AddTextObject(YSCALE_TEXT_PREFIX + i, transform, font,
|
||||
TextAnchor.MiddleRight, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f),
|
||||
new Vector2(coordinate.left, 20));
|
||||
txt.transform.localPosition = GetYScalePosition(i);
|
||||
txt.text = ((int)(max * i / yAxis.splitNumber)).ToString();
|
||||
txt.gameObject.SetActive(coordinate.show);
|
||||
yScaleTextList.Add(txt);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
yAxis.splitNumber = yAxis.boundaryGap ? yAxis.data.Count + 1 : yAxis.data.Count;
|
||||
for (int i = 0; i < yAxis.data.Count; i++)
|
||||
{
|
||||
Text txt = ChartUtils.AddTextObject(YSCALE_TEXT_PREFIX + i, transform, font,
|
||||
TextAnchor.MiddleRight, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f),
|
||||
new Vector2(coordinate.left, 20));
|
||||
txt.transform.localPosition = GetYScalePosition(i);
|
||||
txt.text = yAxis.data[i];
|
||||
txt.gameObject.SetActive(coordinate.show);
|
||||
yScaleTextList.Add(txt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitXScale()
|
||||
{
|
||||
xScaleTextList.Clear();
|
||||
if (xAxis.type == AxisType.value)
|
||||
{
|
||||
float max = GetMaxValue();
|
||||
if (max <= 0) max = 400;
|
||||
xAxis.splitNumber = DEFAULT_YSACLE_NUM;
|
||||
float scaleWid = coordinateWid / (xAxis.splitNumber - 1);
|
||||
for (int i = 0; i < xAxis.splitNumber; i++)
|
||||
{
|
||||
Text txt = ChartUtils.AddTextObject(XSCALE_TEXT_PREFIX + i, transform, font,
|
||||
TextAnchor.MiddleCenter, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f),
|
||||
new Vector2(scaleWid, 20));
|
||||
txt.transform.localPosition = GetXScalePosition(i);
|
||||
txt.text = ((int)(max * i / xAxis.splitNumber)).ToString();
|
||||
txt.gameObject.SetActive(coordinate.show);
|
||||
xScaleTextList.Add(txt);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
xAxis.splitNumber = xAxis.boundaryGap ? xAxis.data.Count + 1 : xAxis.data.Count;
|
||||
float scaleWid = coordinateWid / (xAxis.data.Count - 1);
|
||||
for (int i = 0; i < xAxis.data.Count; i++)
|
||||
{
|
||||
Text txt = ChartUtils.AddTextObject(XSCALE_TEXT_PREFIX + i, transform, font,
|
||||
TextAnchor.MiddleCenter, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f),
|
||||
new Vector2(scaleWid, 20));
|
||||
txt.transform.localPosition = GetXScalePosition(i);
|
||||
txt.text = xAxis.data[i];
|
||||
txt.gameObject.SetActive(coordinate.show);
|
||||
xScaleTextList.Add(txt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitLegend()
|
||||
{
|
||||
for (int i = 0; i < legend.dataList.Count; i++)
|
||||
@@ -483,122 +306,6 @@ namespace xcharts
|
||||
return Vector3.zero;
|
||||
}
|
||||
|
||||
private Vector3 GetYScalePosition(int i)
|
||||
{
|
||||
float scaleWid = coordinateHig / (yAxis.splitNumber - 1);
|
||||
if (yAxis.type == AxisType.value)
|
||||
{
|
||||
return new Vector3(zeroX - coordinate.scaleLen - 2f,
|
||||
zeroY + i * scaleWid, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (yAxis.boundaryGap)
|
||||
{
|
||||
return new Vector3(zeroX - coordinate.scaleLen - 2f,
|
||||
zeroY + (i + 0.5f) * scaleWid, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Vector3(zeroX - coordinate.scaleLen - 2f,
|
||||
zeroY + i * scaleWid, 0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 GetXScalePosition(int i)
|
||||
{
|
||||
float scaleWid = coordinateWid / (xAxis.splitNumber - 1);
|
||||
if (xAxis.type == AxisType.value)
|
||||
{
|
||||
return new Vector3(zeroX + (i + 1 - 0.5f) * scaleWid, zeroY - coordinate.scaleLen - 10, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (xAxis.boundaryGap)
|
||||
{
|
||||
return new Vector3(zeroX + (i + 1) * scaleWid, zeroY - coordinate.scaleLen - 5, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Vector3(zeroX + (i + 1 - 0.5f) * scaleWid, zeroY - coordinate.scaleLen - 5, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckCoordinate()
|
||||
{
|
||||
if (lastCoordinateHig != coordinateHig
|
||||
|| lastCoordinateWid != coordinateWid
|
||||
|| lastCoordinateScaleLen != coordinate.scaleLen)
|
||||
{
|
||||
lastCoordinateWid = coordinateWid;
|
||||
lastCoordinateHig = coordinateHig;
|
||||
lastCoordinateScaleLen = coordinate.scaleLen;
|
||||
OnCoordinateSize();
|
||||
}
|
||||
if(checkCoordinate.show != coordinate.show)
|
||||
{
|
||||
checkCoordinate.show = coordinate.show;
|
||||
OnXAxisChanged();
|
||||
OnYAxisChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckYAxisType()
|
||||
{
|
||||
if (checkYAxis.type != yAxis.type ||
|
||||
checkYAxis.boundaryGap != yAxis.boundaryGap ||
|
||||
checkYAxis.showSplitLine != yAxis.showSplitLine ||
|
||||
checkYAxis.splitNumber != yAxis.splitNumber)
|
||||
{
|
||||
checkYAxis.type = yAxis.type;
|
||||
checkYAxis.boundaryGap = yAxis.boundaryGap;
|
||||
checkYAxis.showSplitLine = yAxis.showSplitLine;
|
||||
checkYAxis.splitNumber = yAxis.splitNumber;
|
||||
OnYAxisChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckXAxisType()
|
||||
{
|
||||
if (checkXAxis.type != xAxis.type ||
|
||||
checkXAxis.boundaryGap != xAxis.boundaryGap ||
|
||||
checkXAxis.showSplitLine != xAxis.showSplitLine ||
|
||||
checkXAxis.splitNumber != xAxis.splitNumber)
|
||||
{
|
||||
checkXAxis.type = xAxis.type;
|
||||
checkXAxis.boundaryGap = xAxis.boundaryGap;
|
||||
checkXAxis.showSplitLine = xAxis.showSplitLine;
|
||||
checkXAxis.splitNumber = xAxis.splitNumber;
|
||||
OnXAxisChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckMaxValue()
|
||||
{
|
||||
if (xAxis.type == AxisType.value)
|
||||
{
|
||||
float max = GetMaxValue();
|
||||
if (lastXMaxValue != max)
|
||||
{
|
||||
lastXMaxValue = max;
|
||||
OnXMaxValueChanged();
|
||||
}
|
||||
}
|
||||
else if (yAxis.type == AxisType.value)
|
||||
{
|
||||
|
||||
float max = GetMaxValue();
|
||||
if (lastYMaxValue != max)
|
||||
{
|
||||
lastYMaxValue = max;
|
||||
OnYMaxValueChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected float GetMaxValue()
|
||||
{
|
||||
float max = 0;
|
||||
@@ -650,55 +357,6 @@ namespace xcharts
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnCoordinateSize()
|
||||
{
|
||||
//update yScale pos
|
||||
for (int i = 0; i < yAxis.splitNumber; i++)
|
||||
{
|
||||
if (i < yScaleTextList.Count && yScaleTextList[i])
|
||||
{
|
||||
yScaleTextList[i].transform.localPosition = GetYScalePosition(i);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < xAxis.splitNumber; i++)
|
||||
{
|
||||
if (i < xScaleTextList.Count && xScaleTextList[i])
|
||||
{
|
||||
xScaleTextList[i].transform.localPosition = GetXScalePosition(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnYAxisChanged()
|
||||
{
|
||||
HideChild(YSCALE_TEXT_PREFIX);
|
||||
InitYScale();
|
||||
}
|
||||
|
||||
protected virtual void OnXAxisChanged()
|
||||
{
|
||||
HideChild(XSCALE_TEXT_PREFIX);
|
||||
InitXScale();
|
||||
}
|
||||
|
||||
protected virtual void OnXMaxValueChanged()
|
||||
{
|
||||
float max = GetMaxValue();
|
||||
for (int i = 0; i < xScaleTextList.Count; i++)
|
||||
{
|
||||
xScaleTextList[i].text = ((int)(max * i / xScaleTextList.Count)).ToString();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnYMaxValueChanged()
|
||||
{
|
||||
float max = GetMaxValue();
|
||||
for (int i = 0; i < yScaleTextList.Count; i++)
|
||||
{
|
||||
yScaleTextList[i].text = ((int)(max * i / (yScaleTextList.Count - 1))).ToString();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnTitleChanged()
|
||||
{
|
||||
InitTitle();
|
||||
@@ -718,9 +376,12 @@ namespace xcharts
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnYMaxValueChanged()
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void OnLegendButtonClicked()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected void RefreshChart()
|
||||
@@ -734,7 +395,6 @@ namespace xcharts
|
||||
{
|
||||
vh.Clear();
|
||||
DrawBackground(vh);
|
||||
DrawCoordinate(vh);
|
||||
}
|
||||
|
||||
private void DrawBackground(VertexHelper vh)
|
||||
@@ -746,40 +406,5 @@ namespace xcharts
|
||||
Vector3 p4 = new Vector3(0, 0);
|
||||
ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, backgroundColor);
|
||||
}
|
||||
|
||||
private void DrawCoordinate(VertexHelper vh)
|
||||
{
|
||||
if (!coordinate.show) return;
|
||||
// draw scale
|
||||
for (int i = 1; i < yAxis.splitNumber; i++)
|
||||
{
|
||||
float pX = zeroX - coordinate.scaleLen;
|
||||
float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1);
|
||||
ChartUtils.DrawLine(vh, new Vector3(pX, pY), new Vector3(zeroX, pY), coordinate.tickness,
|
||||
Color.white);
|
||||
if (yAxis.showSplitLine)
|
||||
{
|
||||
ChartUtils.DrawLine(vh, new Vector3(zeroX, pY),
|
||||
new Vector3(zeroX + coordinateWid, pY), coordinate.tickness, Color.grey);
|
||||
}
|
||||
}
|
||||
for (int i = 1; i < xAxis.splitNumber; i++)
|
||||
{
|
||||
float pX = zeroX + i * coordinateWid / (xAxis.splitNumber - 1);
|
||||
float pY = zeroY - coordinate.scaleLen - 2;
|
||||
ChartUtils.DrawLine(vh, new Vector3(pX, zeroY), new Vector3(pX, pY), coordinate.tickness,
|
||||
Color.white);
|
||||
if (xAxis.showSplitLine)
|
||||
{
|
||||
ChartUtils.DrawLine(vh, new Vector3(pX, zeroY), new Vector3(pX, zeroY + coordinateHig),
|
||||
coordinate.tickness, Color.grey);
|
||||
}
|
||||
}
|
||||
//draw x,y axis
|
||||
ChartUtils.DrawLine(vh, new Vector3(zeroX, zeroY - coordinate.scaleLen),
|
||||
new Vector3(zeroX, zeroY + coordinateHig + 2), coordinate.tickness, Color.white);
|
||||
ChartUtils.DrawLine(vh, new Vector3(zeroX - coordinate.scaleLen, zeroY),
|
||||
new Vector3(zeroX + coordinateWid + 2, zeroY), coordinate.tickness, Color.white);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user