Files
XCharts/Scripts/BaseChart.cs

410 lines
13 KiB
C#
Raw Normal View History

2018-09-18 06:56:41 +08:00
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
namespace xcharts
{
[System.Serializable]
public enum ChartType
{
line,
bar
}
[System.Serializable]
public class Title
{
public bool show = true;
public string text;
public Color color;
public Align align;
public float left;
public float right;
public float top;
public float bottom;
}
[System.Serializable]
public enum Align
{
left, //左对齐
right, //右对齐
center //居中对齐
}
[System.Serializable]
2018-09-20 07:10:49 +08:00
public enum Location
2018-09-18 06:56:41 +08:00
{
left,
right,
top,
2018-09-20 07:10:49 +08:00
bottom,
start,
middle,
center,
end,
2018-09-18 06:56:41 +08:00
}
[System.Serializable]
public class LegendData
{
public bool show = true;
public ChartType type;
public string key;
public string text;
public Color color;
public Button button { get; set; }
}
[System.Serializable]
public class Legend
{
2018-09-23 08:18:26 +08:00
public bool show = true;
2018-09-20 07:10:49 +08:00
public Location location;
2018-09-23 08:18:26 +08:00
public float dataWid = 50.0f;
public float dataHig = 20.0f;
2018-09-18 06:56:41 +08:00
public float dataSpace;
public float left;
public float right;
public float top;
public float bottom;
public List<LegendData> dataList = new List<LegendData>();
2018-09-18 08:12:16 +08:00
public Color GetColor(int seriesIndex)
{
2018-09-21 22:30:48 +08:00
if (seriesIndex < 0 || seriesIndex >= dataList.Count) seriesIndex = 0;
2018-09-18 08:12:16 +08:00
return dataList[seriesIndex].color;
}
public bool IsShowSeries(int seriesIndex)
{
2018-09-21 22:30:48 +08:00
if (seriesIndex < 0 || seriesIndex >= dataList.Count) seriesIndex = 0;
2018-09-18 08:12:16 +08:00
return dataList[seriesIndex].show;
}
2018-09-18 06:56:41 +08:00
}
[System.Serializable]
public class SeriesData
{
public string key;
public float value;
2018-09-21 22:30:48 +08:00
public SeriesData(string key, float value)
{
this.key = key;
this.value = value;
}
2018-09-18 06:56:41 +08:00
}
[System.Serializable]
public class Series
{
public string legendKey;
public int showDataNumber = 0;
2018-09-18 06:56:41 +08:00
public List<SeriesData> dataList = new List<SeriesData>();
2018-09-21 22:30:48 +08:00
public float Max
2018-09-18 06:56:41 +08:00
{
get
{
float max = 0;
2018-09-21 22:30:48 +08:00
foreach (var data in dataList)
2018-09-18 06:56:41 +08:00
{
2018-09-21 22:30:48 +08:00
if (data.value > max)
2018-09-18 06:56:41 +08:00
{
max = data.value;
}
}
return max;
}
}
2018-09-21 22:30:48 +08:00
public float Total
2018-09-18 06:56:41 +08:00
{
get
{
float total = 0;
foreach (var data in dataList)
{
total += data.value;
}
return total;
}
}
2018-09-21 22:30:48 +08:00
public void AddData(string key, float value)
{
if (dataList.Count >= showDataNumber && showDataNumber != 0)
2018-09-21 22:30:48 +08:00
{
dataList.RemoveAt(0);
}
dataList.Add(new SeriesData(key, value));
}
2018-09-18 06:56:41 +08:00
}
public class BaseChart : MaskableGraphic
{
private const string TILTE_TEXT = "title";
private const string LEGEND_TEXT = "legend";
[SerializeField]
protected Font font;
[SerializeField]
protected Color backgroundColor = Color.black;
[SerializeField]
protected Title title;
[SerializeField]
protected Legend legend;
[SerializeField]
protected List<Series> seriesList = new List<Series>();
2018-09-28 06:34:19 +08:00
2018-09-23 08:18:26 +08:00
private Title checkTitle = new Title();
2018-09-18 06:56:41 +08:00
private Legend checkLegend = new Legend();
protected Text titleText;
protected List<Text> legendTextList = new List<Text>();
protected float chartWid { get { return rectTransform.sizeDelta.x; } }
protected float chartHig { get { return rectTransform.sizeDelta.y; } }
2018-09-28 06:34:19 +08:00
2018-09-18 06:56:41 +08:00
protected override void Awake()
{
rectTransform.anchorMax = Vector2.zero;
rectTransform.anchorMin = Vector2.zero;
rectTransform.pivot = Vector2.zero;
InitTitle();
InitLegend();
}
protected virtual void Update()
{
CheckTile();
CheckLegend();
}
2018-09-21 22:30:48 +08:00
public void AddData(string legend, string key, float value)
{
for (int i = 0; i < seriesList.Count; i++)
{
if (seriesList[i].legendKey == legend)
{
seriesList[i].AddData(key, value);
break;
}
}
RefreshChart();
}
2018-09-26 09:00:11 +08:00
protected void HideChild(string match = null)
2018-09-18 06:56:41 +08:00
{
for (int i = 0; i < transform.childCount; i++)
{
if (match == null)
transform.GetChild(i).gameObject.SetActive(false);
else
{
var go = transform.GetChild(i);
if (go.name.StartsWith(match))
{
go.gameObject.SetActive(false);
}
}
}
}
private void InitTitle()
{
TextAnchor anchor = TextAnchor.MiddleCenter;
Vector2 anchorMin = new Vector2(0, 0);
Vector2 anchorMax = new Vector2(0, 0);
Vector3 titlePosition;
float titleWid = 200;
float titleHig = 20;
switch (title.align)
{
case Align.left:
anchor = TextAnchor.MiddleLeft;
titlePosition = new Vector3(title.left, chartHig - title.top, 0);
break;
case Align.right:
anchor = TextAnchor.MiddleRight;
titlePosition = new Vector3(chartWid - title.right - titleWid, chartHig - title.top, 0);
break;
case Align.center:
anchor = TextAnchor.MiddleCenter;
titlePosition = new Vector3(chartWid / 2 - titleWid / 2, chartHig - title.top, 0);
break;
default:
anchor = TextAnchor.MiddleCenter;
titlePosition = new Vector3(0, -title.top, 0);
break;
}
titleText = ChartUtils.AddTextObject(TILTE_TEXT, transform, font,
anchor, anchorMin, anchorMax, new Vector2(0, 1),
new Vector2(titleWid, titleHig), 16);
titleText.alignment = anchor;
titleText.gameObject.SetActive(title.show);
titleText.transform.localPosition = titlePosition;
titleText.text = title.text;
}
private void InitLegend()
{
2018-09-21 22:30:48 +08:00
for (int i = 0; i < legend.dataList.Count; i++)
2018-09-18 06:56:41 +08:00
{
LegendData data = legend.dataList[i];
Button btn = ChartUtils.AddButtonObject(LEGEND_TEXT + i, transform, font, Vector2.zero,
Vector2.zero, Vector2.zero, new Vector2(legend.dataWid, legend.dataHig));
legend.dataList[i].button = btn;
Color bcolor = data.show ? data.color : Color.grey;
2018-09-18 08:12:16 +08:00
btn.gameObject.SetActive(legend.show);
2018-09-18 06:56:41 +08:00
btn.transform.localPosition = GetLegendPosition(i);
btn.GetComponent<Image>().color = bcolor;
btn.GetComponentInChildren<Text>().text = data.text;
btn.onClick.AddListener(delegate ()
{
data.show = !data.show;
btn.GetComponent<Image>().color = data.show ? data.color : Color.grey;
2018-09-18 08:12:16 +08:00
OnYMaxValueChanged();
2018-09-23 08:18:26 +08:00
OnLegendButtonClicked();
2018-09-18 08:12:16 +08:00
RefreshChart();
2018-09-18 06:56:41 +08:00
});
}
}
private Vector3 GetLegendPosition(int i)
{
int legendCount = legend.dataList.Count;
2018-09-20 07:10:49 +08:00
switch (legend.location)
2018-09-18 06:56:41 +08:00
{
2018-09-20 07:10:49 +08:00
case Location.bottom:
case Location.top:
2018-09-18 06:56:41 +08:00
float startX = legend.left;
if (startX <= 0)
{
2018-09-21 22:30:48 +08:00
startX = (chartWid - (legendCount * legend.dataWid -
(legendCount - 1) * legend.dataSpace)) / 2;
2018-09-18 06:56:41 +08:00
}
2018-09-21 22:30:48 +08:00
float posY = legend.location == Location.bottom ?
legend.bottom : chartHig - legend.top - legend.dataHig;
return new Vector3(startX + i * (legend.dataWid + legend.dataSpace), posY, 0);
2018-09-20 07:10:49 +08:00
case Location.left:
case Location.right:
2018-09-21 22:30:48 +08:00
float startY = 0;
2018-09-18 06:56:41 +08:00
if (legend.top > 0)
{
2018-09-21 22:30:48 +08:00
startY = chartHig - legend.top - legend.dataHig;
2018-09-18 06:56:41 +08:00
}
else if (startY <= 0)
{
2018-09-21 22:30:48 +08:00
float offset = (chartHig - (legendCount * legend.dataHig - (legendCount - 1) * legend.dataSpace)) / 2;
startY = chartHig - offset - legend.dataHig;
2018-09-18 06:56:41 +08:00
}
2018-09-20 07:10:49 +08:00
float posX = legend.location == Location.left ? legend.left : chartWid - legend.right - legend.dataWid;
2018-09-21 22:30:48 +08:00
return new Vector3(posX, startY - i * (legend.dataHig + legend.dataSpace), 0);
default: break;
2018-09-18 06:56:41 +08:00
}
return Vector3.zero;
}
protected float GetMaxValue()
{
float max = 0;
2018-09-21 22:30:48 +08:00
for (int i = 0; i < seriesList.Count; i++)
2018-09-18 06:56:41 +08:00
{
2018-09-21 22:30:48 +08:00
if (legend.IsShowSeries(i) && seriesList[i].Max > max) max = seriesList[i].Max;
2018-09-18 06:56:41 +08:00
}
2018-09-21 22:30:48 +08:00
int bigger = (int)(max * 1.3f);
return bigger < 10 ? bigger : bigger - bigger % 10;
2018-09-18 06:56:41 +08:00
}
private void CheckTile()
{
2018-09-23 08:18:26 +08:00
if (checkTitle.align != title.align ||
checkTitle.left != title.left ||
checkTitle.right != title.right ||
checkTitle.top != title.top)
2018-09-18 06:56:41 +08:00
{
2018-09-23 08:18:26 +08:00
checkTitle.align = title.align;
checkTitle.left = title.left;
checkTitle.right = title.right;
checkTitle.top = title.top;
2018-09-18 06:56:41 +08:00
OnTitleChanged();
}
}
private void CheckLegend()
{
if (checkLegend.dataWid != legend.dataWid ||
checkLegend.dataHig != legend.dataHig ||
2018-09-21 22:30:48 +08:00
checkLegend.dataSpace != legend.dataSpace ||
2018-09-18 06:56:41 +08:00
checkLegend.left != legend.left ||
checkLegend.right != legend.right ||
checkLegend.bottom != legend.bottom ||
checkLegend.top != legend.top ||
2018-09-20 07:10:49 +08:00
checkLegend.location != legend.location ||
2018-09-18 08:12:16 +08:00
checkLegend.show != legend.show)
2018-09-18 06:56:41 +08:00
{
checkLegend.dataWid = legend.dataWid;
checkLegend.dataHig = legend.dataHig;
checkLegend.dataSpace = legend.dataSpace;
checkLegend.left = legend.left;
checkLegend.right = legend.right;
checkLegend.bottom = legend.bottom;
checkLegend.top = legend.top;
2018-09-20 07:10:49 +08:00
checkLegend.location = legend.location;
2018-09-18 08:12:16 +08:00
checkLegend.show = legend.show;
2018-09-18 06:56:41 +08:00
OnLegendChanged();
}
}
protected virtual void OnTitleChanged()
{
InitTitle();
}
protected virtual void OnLegendChanged()
{
for (int i = 0; i < legend.dataList.Count; i++)
{
Button btn = legend.dataList[i].button;
btn.GetComponent<RectTransform>().sizeDelta = new Vector2(legend.dataWid, legend.dataHig);
2018-09-21 22:30:48 +08:00
Text txt = btn.GetComponentInChildren<Text>();
txt.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(legend.dataWid, legend.dataHig);
txt.transform.localPosition = Vector3.zero;
2018-09-18 06:56:41 +08:00
btn.transform.localPosition = GetLegendPosition(i);
2018-09-18 08:12:16 +08:00
btn.gameObject.SetActive(legend.show);
2018-09-18 06:56:41 +08:00
}
}
2018-09-28 06:34:19 +08:00
protected virtual void OnYMaxValueChanged()
2018-09-23 08:18:26 +08:00
{
2018-09-28 06:34:19 +08:00
}
2018-09-23 08:18:26 +08:00
2018-09-28 06:34:19 +08:00
protected virtual void OnLegendButtonClicked()
{
2018-09-23 08:18:26 +08:00
}
2018-09-18 08:12:16 +08:00
protected void RefreshChart()
{
int tempWid = (int)chartWid;
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempWid - 1);
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempWid);
}
2018-09-18 06:56:41 +08:00
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
DrawBackground(vh);
}
private void DrawBackground(VertexHelper vh)
{
// draw bg
Vector3 p1 = new Vector3(0, chartHig);
Vector3 p2 = new Vector3(chartWid, chartHig);
Vector3 p3 = new Vector3(chartWid, 0);
Vector3 p4 = new Vector3(0, 0);
ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, backgroundColor);
}
}
}