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-18 08:12:16 +08:00
|
|
|
|
[System.Serializable]
|
2018-09-23 08:18:26 +08:00
|
|
|
|
public class BarInfo
|
2018-09-18 08:12:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
public float barWid = 0.7f;
|
|
|
|
|
|
public float space;
|
|
|
|
|
|
}
|
2018-09-15 06:52:42 +08:00
|
|
|
|
|
2018-09-28 06:34:19 +08:00
|
|
|
|
public class BarChart : BaseAxesChart
|
2018-09-15 06:52:42 +08:00
|
|
|
|
{
|
2018-09-18 08:12:16 +08:00
|
|
|
|
[SerializeField]
|
2018-09-23 08:18:26 +08:00
|
|
|
|
private BarInfo barInfo;
|
2018-09-18 08:12:16 +08:00
|
|
|
|
|
2018-09-18 06:56:41 +08:00
|
|
|
|
protected override void Awake()
|
2018-09-15 06:52:42 +08:00
|
|
|
|
{
|
2018-09-18 06:56:41 +08:00
|
|
|
|
base.Awake();
|
2018-09-15 06:52:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-18 06:56:41 +08:00
|
|
|
|
protected override void Update()
|
2018-09-15 06:52:42 +08:00
|
|
|
|
{
|
2018-09-18 06:56:41 +08:00
|
|
|
|
base.Update();
|
2018-09-15 06:52:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnPopulateMesh(VertexHelper vh)
|
|
|
|
|
|
{
|
2018-09-18 06:56:41 +08:00
|
|
|
|
base.OnPopulateMesh(vh);
|
2018-09-18 08:12:16 +08:00
|
|
|
|
|
2018-09-19 07:12:27 +08:00
|
|
|
|
if(yAxis.type == AxisType.category)
|
2018-09-15 06:52:42 +08:00
|
|
|
|
{
|
2018-09-19 07:51:33 +08:00
|
|
|
|
int seriesCount = seriesList.Count;
|
2018-09-20 07:10:49 +08:00
|
|
|
|
float scaleWid = coordinateHig / (yAxis.splitNumber - 1);
|
2018-09-23 08:18:26 +08:00
|
|
|
|
float barWid = barInfo.barWid > 1 ? barInfo.barWid : scaleWid * barInfo.barWid;
|
|
|
|
|
|
float offset = (scaleWid - barWid * seriesCount - barInfo.space * (seriesCount - 1)) / 2;
|
2018-09-19 07:12:27 +08:00
|
|
|
|
float max = GetMaxValue();
|
|
|
|
|
|
for (int j = 0; j < seriesCount; j++)
|
2018-09-18 06:56:41 +08:00
|
|
|
|
{
|
2018-09-19 07:12:27 +08:00
|
|
|
|
if (!legend.IsShowSeries(j)) continue;
|
|
|
|
|
|
Series series = seriesList[j];
|
|
|
|
|
|
Color color = legend.GetColor(j);
|
2018-09-21 22:43:21 +08:00
|
|
|
|
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-19 07:12:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
SeriesData data = series.dataList[i];
|
|
|
|
|
|
float pX = zeroX + coordinate.tickness;
|
2018-09-20 07:10:49 +08:00
|
|
|
|
float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1);
|
2018-09-20 09:09:12 +08:00
|
|
|
|
if (!yAxis.boundaryGap) pY -= scaleWid / 2;
|
2018-09-19 07:12:27 +08:00
|
|
|
|
float barHig = data.value / max * coordinateWid;
|
2018-09-23 08:18:26 +08:00
|
|
|
|
float space = offset + j * (barWid + barInfo.space);
|
2018-09-19 07:12:27 +08:00
|
|
|
|
Vector3 p1 = new Vector3(pX, pY + space + barWid);
|
|
|
|
|
|
Vector3 p2 = new Vector3(pX + barHig, pY + space + barWid);
|
|
|
|
|
|
Vector3 p3 = new Vector3(pX + barHig, pY + space);
|
|
|
|
|
|
Vector3 p4 = new Vector3(pX, pY + space);
|
|
|
|
|
|
ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, color);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-09-19 07:51:33 +08:00
|
|
|
|
int seriesCount = seriesList.Count;
|
2018-09-20 07:10:49 +08:00
|
|
|
|
float scaleWid = coordinateWid / (xAxis.splitNumber - 1);
|
2018-09-23 08:18:26 +08:00
|
|
|
|
float barWid = barInfo.barWid > 1 ? barInfo.barWid : scaleWid * barInfo.barWid;
|
|
|
|
|
|
float offset = (scaleWid - barWid * seriesCount - barInfo.space * (seriesCount - 1)) / 2;
|
2018-09-19 07:12:27 +08:00
|
|
|
|
float max = GetMaxValue();
|
|
|
|
|
|
for (int j = 0; j < seriesCount; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!legend.IsShowSeries(j)) continue;
|
|
|
|
|
|
Series series = seriesList[j];
|
|
|
|
|
|
Color color = legend.GetColor(j);
|
2018-09-21 22:43:21 +08:00
|
|
|
|
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-19 07:12:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
SeriesData data = series.dataList[i];
|
2018-09-20 07:10:49 +08:00
|
|
|
|
float pX = zeroX + i * coordinateWid / (xAxis.splitNumber - 1);
|
2018-09-20 09:09:12 +08:00
|
|
|
|
if (!xAxis.boundaryGap) pX -= scaleWid / 2;
|
2018-09-19 07:12:27 +08:00
|
|
|
|
float pY = zeroY + coordinate.tickness;
|
|
|
|
|
|
float barHig = data.value / max * coordinateHig;
|
2018-09-23 08:18:26 +08:00
|
|
|
|
float space = offset + j * (barWid + barInfo.space);
|
2018-09-19 07:12:27 +08:00
|
|
|
|
Vector3 p1 = new Vector3(pX + space, pY);
|
|
|
|
|
|
Vector3 p2 = new Vector3(pX + space, pY + barHig);
|
|
|
|
|
|
Vector3 p3 = new Vector3(pX + space + barWid, pY + barHig);
|
|
|
|
|
|
Vector3 p4 = new Vector3(pX + space + barWid, pY);
|
|
|
|
|
|
ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, color);
|
|
|
|
|
|
}
|
2018-09-18 06:56:41 +08:00
|
|
|
|
}
|
2018-09-15 06:52:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|