Files
XCharts/Scripts/LineChart.cs

124 lines
4.0 KiB
C#
Raw Normal View History

2018-09-15 06:52:42 +08:00

using System.Collections.Generic;
using UnityEngine;
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]
public class LineData
{
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-20 07:10:49 +08:00
public Color areaStartColor;
public Color areaToColor;
2018-09-15 06:52:42 +08:00
}
2018-09-19 07:51:33 +08:00
public class LineChart : BaseChart
2018-09-15 06:52:42 +08:00
{
[SerializeField]
2018-09-19 07:51:33 +08:00
private LineData lineData;
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);
2018-09-19 07:51:33 +08:00
for (int i = 0; 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-19 20:33:40 +08:00
if (lineData.smooth)
{
2018-09-20 07:10:49 +08:00
var list = ChartUtils.GetBezierList(lp, np, lineData.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];
ChartUtils.DrawLine(vh, start, to, lineData.tickness, color);
start = to;
}
}
else
{
ChartUtils.DrawLine(vh, lp, np, lineData.tickness, color);
if (lineData.area)
{
2018-09-20 07:10:49 +08:00
ChartUtils.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY), new Vector3(lp.x, zeroY),
lineData.areaStartColor,lineData.areaToColor);
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-20 07:10:49 +08:00
if (lineData.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);
switch (lineData.pointType)
{
case PointType.square:
ChartUtils.DrawPolygon(vh, p, lineData.pointWid, lineData.pointColor);
break;
case PointType.cicle:
ChartUtils.DrawCricle(vh, p, lineData.pointWid, lineData.pointColor, (int)lineData.pointWid * 5);
break;
}
}
2018-09-15 06:52:42 +08:00
}
}
}
}
}