mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-23 17:30:10 +00:00
重构代码
This commit is contained in:
@@ -10,7 +10,7 @@ namespace xcharts
|
|||||||
public float space;
|
public float space;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class BarChart : BaseChart
|
public class BarChart : BaseAxesChart
|
||||||
{
|
{
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private BarInfo barInfo;
|
private BarInfo barInfo;
|
||||||
|
|||||||
400
Scripts/BaseAxesChart.cs
Normal file
400
Scripts/BaseAxesChart.cs
Normal file
@@ -0,0 +1,400 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using System.Collections;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace xcharts
|
||||||
|
{
|
||||||
|
|
||||||
|
[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 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
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BaseAxesChart : BaseChart
|
||||||
|
{
|
||||||
|
private const int DEFAULT_YSACLE_NUM = 5;
|
||||||
|
private const string YSCALE_TEXT_PREFIX = "yScale";
|
||||||
|
private const string XSCALE_TEXT_PREFIX = "xScale";
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
protected Coordinate coordinate;
|
||||||
|
[SerializeField]
|
||||||
|
protected XAxis xAxis;
|
||||||
|
[SerializeField]
|
||||||
|
protected YAxis yAxis;
|
||||||
|
|
||||||
|
private float lastXMaxValue;
|
||||||
|
private float lastYMaxValue;
|
||||||
|
private float lastCoordinateWid;
|
||||||
|
private float lastCoordinateHig;
|
||||||
|
private float lastCoordinateScaleLen;
|
||||||
|
|
||||||
|
private XAxis checkXAxis = new XAxis();
|
||||||
|
private YAxis checkYAxis = new YAxis();
|
||||||
|
private Coordinate checkCoordinate = new Coordinate();
|
||||||
|
|
||||||
|
protected List<Text> yScaleTextList = new List<Text>();
|
||||||
|
protected List<Text> xScaleTextList = new List<Text>();
|
||||||
|
protected float zeroX { get { return coordinate.left; } }
|
||||||
|
protected float zeroY { get { return coordinate.bottom; } }
|
||||||
|
protected float coordinateWid { get { return chartWid - coordinate.left - coordinate.right; } }
|
||||||
|
protected float coordinateHig { get { return chartHig - coordinate.top - coordinate.bottom; } }
|
||||||
|
|
||||||
|
protected override void Awake()
|
||||||
|
{
|
||||||
|
base.Awake();
|
||||||
|
lastCoordinateHig = chartHig;
|
||||||
|
lastCoordinateWid = chartWid;
|
||||||
|
lastCoordinateScaleLen = coordinate.scaleLen;
|
||||||
|
InitXScale();
|
||||||
|
InitYScale();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Update()
|
||||||
|
{
|
||||||
|
base.Update();
|
||||||
|
CheckYAxisType();
|
||||||
|
CheckXAxisType();
|
||||||
|
CheckMaxValue();
|
||||||
|
CheckCoordinate();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnPopulateMesh(VertexHelper vh)
|
||||||
|
{
|
||||||
|
base.OnPopulateMesh(vh);
|
||||||
|
DrawCoordinate(vh);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddXAxisCategory(string category)
|
||||||
|
{
|
||||||
|
xAxis.AddCategory(category);
|
||||||
|
OnXAxisChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddYAxisCategory(string category)
|
||||||
|
{
|
||||||
|
yAxis.AddCategory(category);
|
||||||
|
OnYAxisChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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 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 override void OnYMaxValueChanged()
|
||||||
|
{
|
||||||
|
float max = GetMaxValue();
|
||||||
|
for (int i = 0; i < yScaleTextList.Count; i++)
|
||||||
|
{
|
||||||
|
yScaleTextList[i].text = ((int)(max * i / (yScaleTextList.Count - 1))).ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: b842dc365aa23a2429a273eda5296231
|
guid: c20b1e83703d1084c899f1f1b605c278
|
||||||
timeCreated: 1537312570
|
timeCreated: 1538087401
|
||||||
licenseType: Free
|
licenseType: Free
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
@@ -24,27 +24,6 @@ namespace xcharts
|
|||||||
public float bottom;
|
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]
|
[System.Serializable]
|
||||||
public enum Align
|
public enum Align
|
||||||
{
|
{
|
||||||
@@ -66,36 +45,6 @@ namespace xcharts
|
|||||||
end,
|
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]
|
[System.Serializable]
|
||||||
public class LegendData
|
public class LegendData
|
||||||
{
|
{
|
||||||
@@ -195,9 +144,6 @@ namespace xcharts
|
|||||||
|
|
||||||
public class BaseChart : MaskableGraphic
|
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 TILTE_TEXT = "title";
|
||||||
private const string LEGEND_TEXT = "legend";
|
private const string LEGEND_TEXT = "legend";
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
@@ -207,60 +153,25 @@ namespace xcharts
|
|||||||
[SerializeField]
|
[SerializeField]
|
||||||
protected Title title;
|
protected Title title;
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
protected Coordinate coordinate;
|
|
||||||
[SerializeField]
|
|
||||||
protected XAxis xAxis;
|
|
||||||
[SerializeField]
|
|
||||||
protected YAxis yAxis;
|
|
||||||
[SerializeField]
|
|
||||||
protected Legend legend;
|
protected Legend legend;
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
protected List<Series> seriesList = new List<Series>();
|
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 Title checkTitle = new Title();
|
||||||
private Legend checkLegend = new Legend();
|
private Legend checkLegend = new Legend();
|
||||||
private XAxis checkXAxis = new XAxis();
|
|
||||||
private YAxis checkYAxis = new YAxis();
|
|
||||||
//===========================================
|
|
||||||
|
|
||||||
protected Text titleText;
|
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 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 chartWid { get { return rectTransform.sizeDelta.x; } }
|
||||||
protected float chartHig { get { return rectTransform.sizeDelta.y; } }
|
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()
|
protected override void Awake()
|
||||||
{
|
{
|
||||||
rectTransform.anchorMax = Vector2.zero;
|
rectTransform.anchorMax = Vector2.zero;
|
||||||
rectTransform.anchorMin = Vector2.zero;
|
rectTransform.anchorMin = Vector2.zero;
|
||||||
rectTransform.pivot = Vector2.zero;
|
rectTransform.pivot = Vector2.zero;
|
||||||
lastCoordinateHig = chartHig;
|
|
||||||
lastCoordinateWid = chartWid;
|
|
||||||
lastCoordinateScaleLen = coordinate.scaleLen;
|
|
||||||
|
|
||||||
InitTitle();
|
InitTitle();
|
||||||
InitXScale();
|
|
||||||
InitYScale();
|
|
||||||
InitLegend();
|
InitLegend();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,10 +179,6 @@ namespace xcharts
|
|||||||
{
|
{
|
||||||
CheckTile();
|
CheckTile();
|
||||||
CheckLegend();
|
CheckLegend();
|
||||||
CheckYAxisType();
|
|
||||||
CheckXAxisType();
|
|
||||||
CheckMaxValue();
|
|
||||||
CheckCoordinate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddData(string legend, string key, float value)
|
public void AddData(string legend, string key, float value)
|
||||||
@@ -287,18 +194,6 @@ namespace xcharts
|
|||||||
RefreshChart();
|
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)
|
protected void HideChild(string match = null)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < transform.childCount; i++)
|
for (int i = 0; i < transform.childCount; i++)
|
||||||
@@ -352,78 +247,6 @@ namespace xcharts
|
|||||||
titleText.text = title.text;
|
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()
|
private void InitLegend()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < legend.dataList.Count; i++)
|
for (int i = 0; i < legend.dataList.Count; i++)
|
||||||
@@ -483,122 +306,6 @@ namespace xcharts
|
|||||||
return Vector3.zero;
|
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()
|
protected float GetMaxValue()
|
||||||
{
|
{
|
||||||
float max = 0;
|
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()
|
protected virtual void OnTitleChanged()
|
||||||
{
|
{
|
||||||
InitTitle();
|
InitTitle();
|
||||||
@@ -718,9 +376,12 @@ namespace xcharts
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual void OnYMaxValueChanged()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual void OnLegendButtonClicked()
|
protected virtual void OnLegendButtonClicked()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void RefreshChart()
|
protected void RefreshChart()
|
||||||
@@ -734,7 +395,6 @@ namespace xcharts
|
|||||||
{
|
{
|
||||||
vh.Clear();
|
vh.Clear();
|
||||||
DrawBackground(vh);
|
DrawBackground(vh);
|
||||||
DrawCoordinate(vh);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawBackground(VertexHelper vh)
|
private void DrawBackground(VertexHelper vh)
|
||||||
@@ -746,40 +406,5 @@ namespace xcharts
|
|||||||
Vector3 p4 = new Vector3(0, 0);
|
Vector3 p4 = new Vector3(0, 0);
|
||||||
ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, backgroundColor);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ namespace xcharts
|
|||||||
public Color areaColor;
|
public Color areaColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class LineChart : BaseChart
|
public class LineChart : BaseAxesChart
|
||||||
{
|
{
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private LineInfo lineInfo;
|
private LineInfo lineInfo;
|
||||||
|
|||||||
@@ -1,462 +0,0 @@
|
|||||||
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using UnityEngine;
|
|
||||||
using UnityEngine.UI;
|
|
||||||
|
|
||||||
namespace xcharts
|
|
||||||
{
|
|
||||||
[System.Serializable]
|
|
||||||
public class LineData_old
|
|
||||||
{
|
|
||||||
[SerializeField]
|
|
||||||
public string name;
|
|
||||||
[SerializeField]
|
|
||||||
public string key;
|
|
||||||
[SerializeField]
|
|
||||||
public Color lineColor;
|
|
||||||
[SerializeField]
|
|
||||||
public Color pointColor;
|
|
||||||
[SerializeField]
|
|
||||||
public Button button;
|
|
||||||
|
|
||||||
private List<float> _dataList = new List<float>();
|
|
||||||
public List<float> dataList
|
|
||||||
{
|
|
||||||
get { return _dataList; }
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool _visible = true;
|
|
||||||
public bool visible
|
|
||||||
{
|
|
||||||
get { return _visible; }
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_visible = value;
|
|
||||||
if (button)
|
|
||||||
{
|
|
||||||
button.GetComponent<Image>().color = visible ? lineColor : Color.grey;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int _min = 0;
|
|
||||||
public int min
|
|
||||||
{
|
|
||||||
get { return _min; }
|
|
||||||
}
|
|
||||||
|
|
||||||
private int _max = 10;
|
|
||||||
public int max
|
|
||||||
{
|
|
||||||
get { return _max; }
|
|
||||||
}
|
|
||||||
private int _step = 10;
|
|
||||||
public int step
|
|
||||||
{
|
|
||||||
get { return _step; }
|
|
||||||
set { _step = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddData(float data, int maxCount)
|
|
||||||
{
|
|
||||||
dataList.Add(data);
|
|
||||||
if (dataList.Count > maxCount)
|
|
||||||
{
|
|
||||||
dataList.RemoveAt(0);
|
|
||||||
UpdateMinMax();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (data < _min)
|
|
||||||
{
|
|
||||||
_min = (int)data;
|
|
||||||
}
|
|
||||||
if (data > _max)
|
|
||||||
{
|
|
||||||
_max = (int)data;
|
|
||||||
}
|
|
||||||
CheckMax();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClearData()
|
|
||||||
{
|
|
||||||
_dataList.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateMinMax()
|
|
||||||
{
|
|
||||||
_min = 0;
|
|
||||||
_max = 4;
|
|
||||||
foreach (var data in dataList)
|
|
||||||
{
|
|
||||||
if (data < _min)
|
|
||||||
{
|
|
||||||
_min = (int)data;
|
|
||||||
}
|
|
||||||
if (data > _max)
|
|
||||||
{
|
|
||||||
_max = (int)data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
CheckMax();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CheckMax()
|
|
||||||
{
|
|
||||||
if (_max <= 10)
|
|
||||||
{
|
|
||||||
if (_max < 4) _max = 4;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int diff = _max % _step;
|
|
||||||
if (diff > 1)
|
|
||||||
{
|
|
||||||
_max = (_max - diff) + _step;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class LineChart_old : MaskableGraphic
|
|
||||||
{
|
|
||||||
private const int MAX_GRADUATION = 10;
|
|
||||||
|
|
||||||
[SerializeField]
|
|
||||||
private int pointWidth = 15;
|
|
||||||
[SerializeField]
|
|
||||||
private float lineSize = 1f;
|
|
||||||
[SerializeField]
|
|
||||||
private float pointSize = 1.5f;
|
|
||||||
[SerializeField]
|
|
||||||
private int graduationCount = 4;
|
|
||||||
[SerializeField]
|
|
||||||
private int graduationStep = 10;
|
|
||||||
[SerializeField]
|
|
||||||
private int graduationWidth = 50;
|
|
||||||
|
|
||||||
private float arrowLen = 10;
|
|
||||||
private float arrowSize = 6;
|
|
||||||
|
|
||||||
[SerializeField]
|
|
||||||
private Color backgroundColor;
|
|
||||||
[SerializeField]
|
|
||||||
private Font font;
|
|
||||||
|
|
||||||
[SerializeField]
|
|
||||||
private List<LineData_old> lineList = new List<LineData_old>();
|
|
||||||
|
|
||||||
private Button btnAll;
|
|
||||||
private bool isShowAll = true;
|
|
||||||
private List<Text> graduationList = new List<Text>();
|
|
||||||
private Dictionary<string, LineData_old> lineMap = new Dictionary<string, LineData_old>();
|
|
||||||
private float lastMaxData = 0;
|
|
||||||
private float lastChartHig = 0;
|
|
||||||
private float lastGraduationWid = 0;
|
|
||||||
|
|
||||||
private float chartWid { get { return rectTransform.sizeDelta.x; } }
|
|
||||||
private float chartHig { get { return rectTransform.sizeDelta.y; } }
|
|
||||||
|
|
||||||
protected override void Awake()
|
|
||||||
{
|
|
||||||
base.Awake();
|
|
||||||
InitGraduation();
|
|
||||||
InitLineButton();
|
|
||||||
InitHideAndShowButton();
|
|
||||||
for (int i = 0; i < lineList.Count; i++)
|
|
||||||
{
|
|
||||||
LineData_old line = lineList[i];
|
|
||||||
line.dataList.Clear();
|
|
||||||
if (line.button)
|
|
||||||
{
|
|
||||||
Color bcolor = line.visible ? line.lineColor : Color.grey;
|
|
||||||
line.button.GetComponent<Image>().color = bcolor;
|
|
||||||
line.button.GetComponentInChildren<Text>().text = line.name;
|
|
||||||
line.button.onClick.AddListener(delegate ()
|
|
||||||
{
|
|
||||||
OnClickButton(line.key);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
AddLineToLineMap(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitGraduation()
|
|
||||||
{
|
|
||||||
float graduationHig = chartHig / graduationCount;
|
|
||||||
for (int i = 0; i < MAX_GRADUATION; i++)
|
|
||||||
{
|
|
||||||
if (i >= graduationCount + 1)
|
|
||||||
{
|
|
||||||
if (transform.Find("graduation" + i))
|
|
||||||
{
|
|
||||||
transform.Find("graduation" + i).gameObject.SetActive(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Text txt = ChartUtils.AddTextObject("graduation" + i, transform, font,
|
|
||||||
TextAnchor.MiddleRight, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f),
|
|
||||||
new Vector2(50, 20));
|
|
||||||
txt.transform.localPosition = new Vector3(-8, i * graduationHig, 0);
|
|
||||||
txt.text = (i * 100).ToString();
|
|
||||||
graduationList.Add(txt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitLineButton()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < lineList.Count; i++)
|
|
||||||
{
|
|
||||||
if (lineList[i].button) continue;
|
|
||||||
Button btn = ChartUtils.AddButtonObject("button" + i, transform, font, Vector2.zero,
|
|
||||||
Vector2.zero, Vector2.zero, new Vector2(50, 20));
|
|
||||||
btn.transform.localPosition = new Vector3(i * 50, chartHig + 30, 0);
|
|
||||||
lineList[i].button = btn;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitHideAndShowButton()
|
|
||||||
{
|
|
||||||
if (lineList.Count <= 0) return;
|
|
||||||
btnAll = ChartUtils.AddButtonObject("buttonall", transform, font, Vector2.zero,
|
|
||||||
Vector2.zero, Vector2.zero, new Vector2(graduationWidth, 20));
|
|
||||||
btnAll.transform.localPosition = new Vector3(-graduationWidth, chartHig + 30, 0);
|
|
||||||
btnAll.GetComponentInChildren<Text>().text = isShowAll ? "HIDE" : "SHOW";
|
|
||||||
btnAll.GetComponent<Image>().color = backgroundColor;
|
|
||||||
btnAll.onClick.AddListener(delegate ()
|
|
||||||
{
|
|
||||||
isShowAll = !isShowAll;
|
|
||||||
btnAll.GetComponentInChildren<Text>().text = isShowAll ? "HIDE" : "SHOW";
|
|
||||||
foreach (var line in lineList)
|
|
||||||
{
|
|
||||||
line.visible = isShowAll;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Update()
|
|
||||||
{
|
|
||||||
CheckLineSizeChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OnClickButton(string key)
|
|
||||||
{
|
|
||||||
LineData_old line = lineMap[key];
|
|
||||||
line.visible = !line.visible;
|
|
||||||
if (line.visible)
|
|
||||||
{
|
|
||||||
line.step = graduationStep;
|
|
||||||
line.UpdateMinMax();
|
|
||||||
}
|
|
||||||
CheckMaxDataChange();
|
|
||||||
UpdateMesh();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void AddLineToLineMap(LineData_old line)
|
|
||||||
{
|
|
||||||
if (lineMap.ContainsKey(line.key))
|
|
||||||
{
|
|
||||||
Debug.LogError("LineChart:line key is duplicated:" + line.key);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lineMap[line.key] = line;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private float GetAllLineMax()
|
|
||||||
{
|
|
||||||
float max = 4;
|
|
||||||
foreach (var line in lineList)
|
|
||||||
{
|
|
||||||
if (line.visible && line.max > max)
|
|
||||||
{
|
|
||||||
max = line.max;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return max;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int GetMaxPointCount()
|
|
||||||
{
|
|
||||||
int max = (int)(chartWid / pointWidth);
|
|
||||||
return max;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddLine(string key, string name, Color lineColor, Color pointColor)
|
|
||||||
{
|
|
||||||
LineData_old line = new LineData_old();
|
|
||||||
line.key = key;
|
|
||||||
line.name = name;
|
|
||||||
line.lineColor = lineColor;
|
|
||||||
line.pointColor = pointColor;
|
|
||||||
lineList.Add(line);
|
|
||||||
AddLineToLineMap(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddPoint(string key, float point)
|
|
||||||
{
|
|
||||||
if (!lineMap.ContainsKey(key))
|
|
||||||
{
|
|
||||||
Debug.LogError("LineChart:not contain line key:" + key);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
LineData_old line = lineMap[key];
|
|
||||||
line.AddData(point, GetMaxPointCount());
|
|
||||||
UpdateMesh();
|
|
||||||
CheckMaxDataChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ResetDataStart()
|
|
||||||
{
|
|
||||||
foreach (var line in lineList)
|
|
||||||
{
|
|
||||||
line.ClearData();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ResetData(string key, float data)
|
|
||||||
{
|
|
||||||
if (!lineMap.ContainsKey(key))
|
|
||||||
{
|
|
||||||
Debug.LogError("LineChart:not contain line key:" + key);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
LineData_old line = lineMap[key];
|
|
||||||
line.AddData(data, GetMaxPointCount());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ResetDataEnd()
|
|
||||||
{
|
|
||||||
foreach (var line in lineList)
|
|
||||||
{
|
|
||||||
line.UpdateMinMax();
|
|
||||||
}
|
|
||||||
UpdateMesh();
|
|
||||||
CheckMaxDataChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateMesh()
|
|
||||||
{
|
|
||||||
int tempWid = (int)chartWid;
|
|
||||||
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempWid - 1);
|
|
||||||
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempWid);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CheckLineSizeChange()
|
|
||||||
{
|
|
||||||
if (lastChartHig != chartHig)
|
|
||||||
{
|
|
||||||
lastChartHig = chartHig;
|
|
||||||
//update graduation pos
|
|
||||||
for (int i = 0; i < graduationList.Count; i++)
|
|
||||||
{
|
|
||||||
Vector3 pos = graduationList[i].rectTransform.localPosition;
|
|
||||||
float posY = lastChartHig * i / (graduationList.Count - 1);
|
|
||||||
graduationList[i].rectTransform.localPosition = new Vector3(pos.x, posY, pos.z);
|
|
||||||
}
|
|
||||||
//update line button pos
|
|
||||||
btnAll.transform.localPosition = new Vector3(-graduationWidth, chartHig + 30, 0);
|
|
||||||
for (int i = 0; i < lineList.Count; i++)
|
|
||||||
{
|
|
||||||
LineData_old line = lineList[i];
|
|
||||||
if (line.button)
|
|
||||||
{
|
|
||||||
line.button.transform.localPosition = new Vector3(i * 50, chartHig + 30, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (lastGraduationWid != graduationWidth)
|
|
||||||
{
|
|
||||||
if (graduationWidth < 40) graduationWidth = 40;
|
|
||||||
lastGraduationWid = graduationWidth;
|
|
||||||
Vector2 sizeDelta = new Vector2(graduationWidth, 20);
|
|
||||||
btnAll.GetComponent<RectTransform>().sizeDelta = sizeDelta;
|
|
||||||
btnAll.transform.Find("Text").GetComponent<RectTransform>().sizeDelta = sizeDelta;
|
|
||||||
btnAll.transform.localPosition = new Vector3(-graduationWidth, chartHig + 30, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CheckMaxDataChange()
|
|
||||||
{
|
|
||||||
float dataMax = GetAllLineMax();
|
|
||||||
if (lastMaxData != dataMax)
|
|
||||||
{
|
|
||||||
lastMaxData = dataMax;
|
|
||||||
for (int i = 0; i < graduationList.Count; i++)
|
|
||||||
{
|
|
||||||
graduationList[i].text = ((int)(dataMax * i / graduationList.Count)).ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnPopulateMesh(VertexHelper vh)
|
|
||||||
{
|
|
||||||
vh.Clear();
|
|
||||||
int dataRectWid = (int)(chartWid / pointWidth) * pointWidth;
|
|
||||||
float dataMax = GetAllLineMax();
|
|
||||||
// draw bg
|
|
||||||
Vector3 p1 = new Vector3(-graduationWidth, chartHig + 30);
|
|
||||||
Vector3 p2 = new Vector3(dataRectWid + 50, chartHig + 30);
|
|
||||||
Vector3 p3 = new Vector3(dataRectWid + 50, -20);
|
|
||||||
Vector3 p4 = new Vector3(-graduationWidth, -20);
|
|
||||||
ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, backgroundColor);
|
|
||||||
// draw coordinate
|
|
||||||
Vector3 coordZero = Vector3.zero;
|
|
||||||
ChartUtils.DrawLine(vh, new Vector3(dataRectWid + 5, -5),
|
|
||||||
new Vector3(dataRectWid + 5, chartHig + 0.5f), 1, Color.grey);
|
|
||||||
// draw graduation
|
|
||||||
for (int i = 0; i < graduationList.Count; i++)
|
|
||||||
{
|
|
||||||
Vector3 sp = new Vector3(-5, chartHig * i / (graduationList.Count - 1));
|
|
||||||
Vector3 ep = new Vector3(dataRectWid + 5, chartHig * i / (graduationList.Count - 1));
|
|
||||||
ChartUtils.DrawLine(vh, sp, ep, 0.5f, Color.grey);
|
|
||||||
}
|
|
||||||
|
|
||||||
// draw line
|
|
||||||
for (int index = 0; index < lineList.Count; index++)
|
|
||||||
{
|
|
||||||
LineData_old line = lineList[index];
|
|
||||||
if (!line.visible) continue;
|
|
||||||
Vector3 lp = Vector3.zero;
|
|
||||||
Vector3 np = Vector3.zero;
|
|
||||||
|
|
||||||
for (int i = 0; i < line.dataList.Count; i++)
|
|
||||||
{
|
|
||||||
float data = line.dataList[i] * chartHig / dataMax;
|
|
||||||
np = new Vector3(i * pointWidth, data);
|
|
||||||
if (i > 0)
|
|
||||||
{
|
|
||||||
ChartUtils.DrawLine(vh, lp, np, lineSize, line.lineColor);
|
|
||||||
}
|
|
||||||
lp = np;
|
|
||||||
}
|
|
||||||
|
|
||||||
// draw point
|
|
||||||
for (int i = 0; i < line.dataList.Count; i++)
|
|
||||||
{
|
|
||||||
UIVertex[] quadverts = new UIVertex[4];
|
|
||||||
float data = line.dataList[i] * chartHig / dataMax;
|
|
||||||
Vector3 p = new Vector3(i * pointWidth, data);
|
|
||||||
ChartUtils.DrawPolygon(vh, p, pointSize, line.pointColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//draw x,y axis
|
|
||||||
float xLen = dataRectWid + 25;
|
|
||||||
float yLen = chartHig + 15;
|
|
||||||
float xPos = 0;
|
|
||||||
float yPos = -5;
|
|
||||||
ChartUtils.DrawLine(vh, new Vector3(xPos, yPos - 1.5f), new Vector3(xPos, yLen), 1.5f, Color.white);
|
|
||||||
ChartUtils.DrawLine(vh, new Vector3(xPos, yPos), new Vector3(xLen, yPos), 1.5f, Color.white);
|
|
||||||
//draw arrows
|
|
||||||
ChartUtils.DrawTriangle(vh, new Vector3(xPos - arrowSize, yLen - arrowLen), new Vector3(xPos, yLen + 4),
|
|
||||||
new Vector3(xPos + arrowSize, yLen - arrowLen), Color.white);
|
|
||||||
ChartUtils.DrawTriangle(vh, new Vector3(xLen - arrowLen, yPos + arrowSize), new Vector3(xLen + 4, yPos),
|
|
||||||
new Vector3(xLen - arrowLen, yPos - arrowSize), Color.white);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user