mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-19 15:00:08 +00:00
重构代码,增加柱形图
This commit is contained in:
@@ -4,98 +4,41 @@ using UnityEngine.UI;
|
||||
|
||||
namespace xcharts
|
||||
{
|
||||
[System.Serializable]
|
||||
public class BarGroup
|
||||
|
||||
public class BarChart : BaseChart
|
||||
{
|
||||
[SerializeField]
|
||||
public string name;
|
||||
[SerializeField]
|
||||
public Color color;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class BarData
|
||||
{
|
||||
[SerializeField]
|
||||
public string group;
|
||||
[SerializeField]
|
||||
public string key;
|
||||
[SerializeField]
|
||||
public float value;
|
||||
}
|
||||
|
||||
public class BarChart : MaskableGraphic
|
||||
{
|
||||
[SerializeField]
|
||||
private float keyRectWidth = 50;
|
||||
[SerializeField]
|
||||
private float valueRectWidth = 50;
|
||||
[SerializeField]
|
||||
private float barWidth = 50;
|
||||
[SerializeField]
|
||||
private float barSpace = 20;
|
||||
[SerializeField]
|
||||
private Color backgroundColor = Color.black;
|
||||
|
||||
[SerializeField]
|
||||
private List<BarGroup> groupList = new List<BarGroup>();
|
||||
[SerializeField]
|
||||
private List<BarData> dataList = new List<BarData>();
|
||||
|
||||
private float dataTotal = 0;
|
||||
private List<Text> keyTextList = new List<Text>();
|
||||
private List<Text> valueTextList = new List<Text>();
|
||||
|
||||
private float chartWid { get { return rectTransform.sizeDelta.x; } }
|
||||
private float chartHig { get { return rectTransform.sizeDelta.y; } }
|
||||
|
||||
|
||||
void Awake()
|
||||
protected override void Awake()
|
||||
{
|
||||
dataTotal = getDataTotal();
|
||||
base.Awake();
|
||||
}
|
||||
|
||||
void Update()
|
||||
protected override void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private float getDataTotal()
|
||||
{
|
||||
float total = 0;
|
||||
foreach (var data in dataList)
|
||||
{
|
||||
total += data.value;
|
||||
}
|
||||
return total;
|
||||
base.Update();
|
||||
}
|
||||
|
||||
protected override void OnPopulateMesh(VertexHelper vh)
|
||||
{
|
||||
vh.Clear();
|
||||
// draw bg
|
||||
Vector3 p1 = new Vector3(-keyRectWidth, 0);
|
||||
Vector3 p2 = new Vector3(chartWid, 0);
|
||||
Vector3 p3 = new Vector3(chartWid, -chartHig);
|
||||
Vector3 p4 = new Vector3(-keyRectWidth, -chartHig);
|
||||
ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, backgroundColor);
|
||||
|
||||
//draw data bar
|
||||
|
||||
dataTotal = getDataTotal();
|
||||
for(int i=0;i<dataList.Count;i++)
|
||||
base.OnPopulateMesh(vh);
|
||||
foreach(var series in seriesList)
|
||||
{
|
||||
BarData data = dataList[i];
|
||||
float barLen = data.value / dataTotal * (chartWid - valueRectWidth);
|
||||
float posY = i * (barWidth + barSpace);
|
||||
p1 = new Vector3(0,-posY);
|
||||
p2 = new Vector3(barLen, -posY);
|
||||
p3 = new Vector3(barLen, -(posY + barWidth));
|
||||
p4 = new Vector3(0, -(posY + barWidth));
|
||||
ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, Color.grey);
|
||||
float scaleWid = coordinateWid / (xAxis.scaleNum - 1);
|
||||
float barWid = scaleWid * 0.6f;
|
||||
float space = (scaleWid - barWid) / 2;
|
||||
float max = series.max;
|
||||
for (int i = 0; i < series.dataList.Count; i++)
|
||||
{
|
||||
SeriesData data = series.dataList[i];
|
||||
float pX = zeroX + i * coordinateWid / (xAxis.scaleNum - 1);
|
||||
float pY = zeroY + coordinate.tickness;
|
||||
float barHig = data.value / max * coordinateHig;
|
||||
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.blue);
|
||||
}
|
||||
}
|
||||
|
||||
ChartUtils.DrawLine(vh, new Vector3(0, 0), new Vector3(0, -chartHig), 1.5f, Color.white);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user