Files
XCharts/Scripts/LineChart.cs

134 lines
4.8 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-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;
2018-10-02 08:57:47 +08:00
public float pointWid = 2.5f;
2018-09-20 07:10:49 +08:00
[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
}
2019-03-12 08:10:25 +08:00
protected override void DrawChart(VertexHelper vh)
2018-09-15 06:52:42 +08:00
{
2019-03-12 08:10:25 +08:00
base.DrawChart(vh);
2018-09-19 07:51:33 +08:00
int seriesCount = seriesList.Count;
float max = GetMaxValue();
2019-03-31 09:11:35 +08:00
float scaleWid = xAxis.GetSplitWidth(coordinateWid);
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];
2019-03-26 08:03:28 +08:00
Color32 color = themeInfo.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;
2019-04-02 00:24:57 +08:00
if (series.showDataNumber > 0 && series.DataList.Count > series.showDataNumber)
{
2019-04-02 00:24:57 +08:00
startIndex = series.DataList.Count - series.showDataNumber;
}
2019-04-02 00:24:57 +08:00
for (int i = startIndex; i < series.DataList.Count; i++)
2018-09-15 06:52:42 +08:00
{
2019-04-02 00:24:57 +08:00
float value = series.DataList[i];
2018-09-19 07:51:33 +08:00
np = new Vector3(startX + i * scaleWid, zeroY + 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];
2019-03-22 08:22:02 +08:00
for (int k = 1; k < list.Length; 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),
2018-10-01 17:00:15 +08:00
new Vector3(lp.x, zeroY), color);
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
{
2019-04-02 00:24:57 +08:00
for (int i = 0; i < series.DataList.Count; i++)
2018-09-20 07:10:49 +08:00
{
2019-04-02 00:24:57 +08:00
float value = series.DataList[i];
2018-09-19 07:51:33 +08:00
2019-03-16 07:49:36 +08:00
Vector3 p = new Vector3(startX + i * scaleWid,
zeroY + value * coordinateHig / max);
2019-03-12 08:10:25 +08:00
float pointWid = lineInfo.pointWid;
if (tooltip.show && i == tooltip.DataIndex - 1)
2018-10-02 08:57:47 +08:00
{
2019-03-12 08:10:25 +08:00
pointWid = pointWid * 1.8f;
}
if (theme == Theme.Dark)
{
2019-03-16 07:49:36 +08:00
ChartUtils.DrawCricle(vh, p, pointWid, color,
2018-10-03 22:30:25 +08:00
(int)lineInfo.pointWid * 5);
2018-10-02 08:57:47 +08:00
}
else
2018-09-20 07:10:49 +08:00
{
2019-03-12 08:10:25 +08:00
ChartUtils.DrawCricle(vh, p, pointWid, Color.white);
ChartUtils.DrawDoughnut(vh, p, pointWid - lineInfo.tickness,
pointWid, 0, 360, color);
2018-09-20 07:10:49 +08:00
}
}
2018-09-15 06:52:42 +08:00
}
}
2019-03-12 08:10:25 +08:00
//draw tooltip line
if (tooltip.show && tooltip.DataIndex > 0)
{
2019-03-31 09:11:35 +08:00
float splitWid = coordinateWid / (xAxis.GetSplitNumber() - 1);
2019-03-12 08:10:25 +08:00
float px = zeroX + (tooltip.DataIndex - 1) * splitWid + (xAxis.boundaryGap ? splitWid / 2 : 0);
Vector2 sp = new Vector2(px, zeroY);
Vector2 ep = new Vector2(px, zeroY + coordinateHig);
ChartUtils.DrawLine(vh, sp, ep, coordinate.tickness, themeInfo.tooltipFlagAreaColor);
}
2018-09-15 06:52:42 +08:00
}
}
}