Files
XCharts/Scripts/LineChart.cs

127 lines
4.2 KiB
C#
Raw Normal View History

2018-09-21 22:30:48 +08:00
using UnityEngine;
2018-09-15 06:52:42 +08:00
using UnityEngine.UI;
namespace xcharts
{
2018-09-20 07:10:49 +08:00
[System.Serializable]
public enum PointType
{
square,
cicle
}
2018-09-15 06:52:42 +08:00
[System.Serializable]
2018-09-23 08:18:26 +08:00
public class LineInfo
2018-09-15 06:52:42 +08:00
{
2018-09-20 07:10:49 +08:00
public float tickness = 0.8f;
[Header("Point")]
public bool showPoint = true;
public PointType pointType = PointType.square;
public float pointWid = 1.0f;
public Color pointColor = Color.white;
[Header("Smooth")]
2018-09-19 20:33:40 +08:00
public bool smooth = false;
2018-09-20 07:10:49 +08:00
[Range(1f, 10f)]
public float smoothStyle = 2f;
[Header("Area")]
2018-09-19 20:33:40 +08:00
public bool area = false;
2018-09-23 08:18:26 +08:00
public Color areaColor;
2018-09-15 06:52:42 +08:00
}
2018-09-28 06:34:19 +08:00
public class LineChart : BaseAxesChart
2018-09-15 06:52:42 +08:00
{
[SerializeField]
2018-09-23 08:18:26 +08:00
private LineInfo lineInfo;
2018-09-15 06:52:42 +08:00
protected override void Awake()
{
base.Awake();
}
2018-09-19 07:51:33 +08:00
protected override void Update()
2018-09-15 06:52:42 +08:00
{
2018-09-19 07:51:33 +08:00
base.Update();
2018-09-15 06:52:42 +08:00
}
protected override void OnPopulateMesh(VertexHelper vh)
{
2018-09-19 07:51:33 +08:00
base.OnPopulateMesh(vh);
int seriesCount = seriesList.Count;
float max = GetMaxValue();
2018-09-20 07:10:49 +08:00
float scaleWid = coordinateWid / (xAxis.splitNumber - 1);
2018-09-19 07:51:33 +08:00
for (int j = 0; j < seriesCount; j++)
2018-09-15 06:52:42 +08:00
{
2018-09-19 07:51:33 +08:00
if (!legend.IsShowSeries(j)) continue;
Series series = seriesList[j];
Color color = legend.GetColor(j);
2018-09-15 06:52:42 +08:00
Vector3 lp = Vector3.zero;
Vector3 np = Vector3.zero;
float startX = zeroX + (xAxis.boundaryGap ? scaleWid / 2 : 0);
int showDataNumber = series.showDataNumber;
int startIndex = 0;
if (series.showDataNumber > 0 && series.dataList.Count > series.showDataNumber)
{
startIndex = series.dataList.Count - series.showDataNumber;
}
for (int i = startIndex; i < series.dataList.Count; i++)
2018-09-15 06:52:42 +08:00
{
2018-09-19 07:51:33 +08:00
SeriesData data = series.dataList[i];
np = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max);
2018-09-15 06:52:42 +08:00
if (i > 0)
{
2018-09-23 08:18:26 +08:00
if (lineInfo.smooth)
2018-09-19 20:33:40 +08:00
{
2018-09-23 08:18:26 +08:00
var list = ChartUtils.GetBezierList(lp, np, lineInfo.smoothStyle);
2018-09-19 20:33:40 +08:00
Vector3 start, to;
start = list[0];
2018-09-20 07:10:49 +08:00
for (int k = 1; k < list.Count; k++)
2018-09-19 20:33:40 +08:00
{
to = list[k];
2018-09-23 08:18:26 +08:00
ChartUtils.DrawLine(vh, start, to, lineInfo.tickness, color);
2018-09-19 20:33:40 +08:00
start = to;
}
}
else
{
2018-09-23 08:18:26 +08:00
ChartUtils.DrawLine(vh, lp, np, lineInfo.tickness, color);
if (lineInfo.area)
2018-09-19 20:33:40 +08:00
{
2018-09-23 08:18:26 +08:00
ChartUtils.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY),
new Vector3(lp.x, zeroY), lineInfo.areaColor);
2018-09-19 20:33:40 +08:00
}
}
2018-09-20 07:10:49 +08:00
2018-09-15 06:52:42 +08:00
}
lp = np;
}
// draw point
2018-09-23 08:18:26 +08:00
if (lineInfo.showPoint)
2018-09-15 06:52:42 +08:00
{
2018-09-20 07:10:49 +08:00
for (int i = 0; i < series.dataList.Count; i++)
{
SeriesData data = series.dataList[i];
2018-09-19 07:51:33 +08:00
2018-09-20 07:10:49 +08:00
Vector3 p = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max);
2018-09-23 08:18:26 +08:00
switch (lineInfo.pointType)
2018-09-20 07:10:49 +08:00
{
case PointType.square:
2018-09-23 08:18:26 +08:00
ChartUtils.DrawPolygon(vh, p, lineInfo.pointWid, lineInfo.pointColor);
2018-09-20 07:10:49 +08:00
break;
case PointType.cicle:
2018-09-23 08:18:26 +08:00
ChartUtils.DrawCricle(vh, p, lineInfo.pointWid, lineInfo.pointColor,
(int)lineInfo.pointWid * 5);
2018-09-20 07:10:49 +08:00
break;
}
}
2018-09-15 06:52:42 +08:00
}
}
}
}
}