Files
XCharts/Scripts/PieChart.cs

127 lines
3.8 KiB
C#
Raw Normal View History

2018-09-23 08:18:26 +08:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace xcharts
{
[System.Serializable]
public class PieData
{
public string text;
public float value;
}
[System.Serializable]
public class PieInfo
{
2018-09-23 09:09:17 +08:00
public float insideRadius = 0f;
public float outsideRadius = 80f;
2018-09-23 13:32:46 +08:00
public bool outsideRadiusDynamic = false;
2018-09-23 08:18:26 +08:00
public float space;
public float left;
public float right;
public float top;
public float bottom;
public List<PieData> dataList;
}
public class PieChart : BaseChart
{
[SerializeField]
private PieInfo pieInfo;
private float pieCenterX = 0f;
private float pieCenterY = 0f;
private float pieRadius = 0;
protected override void Awake()
{
base.Awake();
}
protected override void Update()
{
base.Update();
}
protected override void OnPopulateMesh(VertexHelper vh)
{
base.OnPopulateMesh(vh);
UpdatePieCenter();
float totalDegree = 360;
float startDegree = 0;
float dataTotal = GetDataTotal();
2018-09-23 13:32:46 +08:00
float dataMax = GetDataMax();
2018-09-23 08:18:26 +08:00
for (int i = 0; i < pieInfo.dataList.Count; i++)
{
if (!legend.IsShowSeries(i)) continue;
float value = pieInfo.dataList[i].value;
float degree = totalDegree * value / dataTotal;
float toDegree = startDegree + degree;
2018-09-23 13:32:46 +08:00
float outSideRadius = pieInfo.outsideRadiusDynamic ?
pieInfo.insideRadius + (pieRadius - pieInfo.insideRadius) * value / dataMax :
pieRadius;
ChartUtils.DrawDoughnut(vh, new Vector3(pieCenterX, pieCenterY), pieInfo.insideRadius,
outSideRadius,startDegree, toDegree, legend.GetColor(i));
2018-09-23 08:18:26 +08:00
startDegree = toDegree;
}
}
protected override void OnLegendButtonClicked()
{
base.OnLegendButtonClicked();
}
private float GetDataTotal()
{
float total = 0;
for(int i = 0; i < pieInfo.dataList.Count; i++)
{
if (legend.IsShowSeries(i))
{
total += pieInfo.dataList[i].value;
}
}
return total;
}
2018-09-23 13:32:46 +08:00
private float GetDataMax()
{
float max = 0;
for(int i = 0; i < pieInfo.dataList.Count; i++)
{
if(legend.IsShowSeries(i) && pieInfo.dataList[i].value > max)
{
max = pieInfo.dataList[i].value;
}
}
return max;
}
2018-09-23 08:18:26 +08:00
private void UpdatePieCenter()
{
float diffX = chartWid - pieInfo.left - pieInfo.right;
float diffY = chartHig - pieInfo.top - pieInfo.bottom;
float diff = Mathf.Min(diffX, diffY);
2018-09-23 09:09:17 +08:00
if(pieInfo.outsideRadius <= 0)
2018-09-23 08:18:26 +08:00
{
pieRadius = diff / 3 * 2;
pieCenterX = pieInfo.left + pieRadius;
pieCenterY = pieInfo.bottom + pieRadius;
}
else
{
2018-09-23 09:09:17 +08:00
pieRadius = pieInfo.outsideRadius;
2018-09-23 08:18:26 +08:00
pieCenterX = chartWid / 2;
pieCenterY = chartHig / 2;
if (pieInfo.left > 0) pieCenterX = pieInfo.left + pieRadius;
if (pieInfo.right > 0) pieCenterX = chartWid - pieInfo.right - pieRadius;
if (pieInfo.top > 0) pieCenterY = chartHig - pieInfo.top - pieRadius;
if (pieInfo.bottom > 0) pieCenterY = pieInfo.bottom + pieRadius;
}
}
}
}