Files
XCharts/Scripts/LineChart.cs

90 lines
2.9 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
{
[System.Serializable]
public class LineData
{
2018-09-19 20:33:40 +08:00
public bool smooth = false;
public bool area = false;
2018-09-19 07:51:33 +08:00
public float pointWid = 1;
public float tickness = 0.8f;
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();
float scaleWid = coordinateWid / (xAxis.scaleNum - 1);
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;
2018-09-19 07:51:33 +08:00
float startX = zeroX + scaleWid / 2;
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)
{
var list = ChartUtils.GetBezierList(lp, np);
Vector3 start, to;
start = list[0];
for(int k = 1; k < list.Count; k++)
{
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)
{
ChartUtils.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY), new Vector3(lp.x, zeroY), color);
}
}
2018-09-15 06:52:42 +08:00
}
lp = np;
}
// draw point
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];
Vector3 p = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max);
ChartUtils.DrawPolygon(vh, p, lineData.pointWid, Color.white);
2018-09-15 06:52:42 +08:00
}
}
}
}
}