增加簇状柱形图

This commit is contained in:
monitor1394
2018-09-18 08:12:16 +08:00
parent c81496aa95
commit 8ad22195b9
3 changed files with 1612 additions and 36 deletions

View File

@@ -4,9 +4,18 @@ using UnityEngine.UI;
namespace xcharts namespace xcharts
{ {
[System.Serializable]
public class BarData
{
public float barWid = 0.7f;
public float space;
}
public class BarChart : BaseChart public class BarChart : BaseChart
{ {
[SerializeField]
private BarData barData;
protected override void Awake() protected override void Awake()
{ {
base.Awake(); base.Awake();
@@ -20,23 +29,29 @@ namespace xcharts
protected override void OnPopulateMesh(VertexHelper vh) protected override void OnPopulateMesh(VertexHelper vh)
{ {
base.OnPopulateMesh(vh); base.OnPopulateMesh(vh);
foreach(var series in seriesList)
{ float seriesCount = seriesList.Count;
float scaleWid = coordinateWid / (xAxis.scaleNum - 1); float scaleWid = coordinateWid / (xAxis.scaleNum - 1);
float barWid = scaleWid * 0.6f; float barWid = barData.barWid > 1 ? barData.barWid : scaleWid * barData.barWid;
float space = (scaleWid - barWid) / 2; float offset = (scaleWid - barWid * seriesCount - barData.space *(seriesCount-1)) / 2;
float max = series.max; float max = GetMaxValue();
for (int j = 0; j < seriesCount; j++)
{
if (!legend.IsShowSeries(j)) continue;
Series series = seriesList[j];
Color color = legend.GetColor(j);
for (int i = 0; i < series.dataList.Count; i++) for (int i = 0; i < series.dataList.Count; i++)
{ {
SeriesData data = series.dataList[i]; SeriesData data = series.dataList[i];
float pX = zeroX + i * coordinateWid / (xAxis.scaleNum - 1); float pX = zeroX + i * coordinateWid / (xAxis.scaleNum - 1);
float pY = zeroY + coordinate.tickness; float pY = zeroY + coordinate.tickness;
float barHig = data.value / max * coordinateHig; float barHig = data.value / max * coordinateHig;
float space = offset + j * (barWid + barData.space);
Vector3 p1 = new Vector3(pX + space, pY); Vector3 p1 = new Vector3(pX + space, pY);
Vector3 p2 = new Vector3(pX + space, pY + barHig); Vector3 p2 = new Vector3(pX + space, pY + barHig);
Vector3 p3 = new Vector3(pX + space + barWid, pY + barHig); Vector3 p3 = new Vector3(pX + space + barWid, pY + barHig);
Vector3 p4 = new Vector3(pX + space + barWid, pY); Vector3 p4 = new Vector3(pX + space + barWid, pY);
ChartUtils.DrawPolygon(vh,p1,p2,p3,p4,Color.blue); ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, color);
} }
} }
} }

View File

@@ -107,6 +107,18 @@ namespace xcharts
public float top; public float top;
public float bottom; public float bottom;
public List<LegendData> dataList = new List<LegendData>(); public List<LegendData> dataList = new List<LegendData>();
public Color GetColor(int seriesIndex)
{
if (seriesIndex < 0 || seriesIndex > dataList.Count) seriesIndex = 0;
return dataList[seriesIndex].color;
}
public bool IsShowSeries(int seriesIndex)
{
if (seriesIndex < 0 || seriesIndex > dataList.Count) seriesIndex = 0;
return dataList[seriesIndex].show;
}
} }
[System.Serializable] [System.Serializable]
@@ -360,6 +372,7 @@ namespace xcharts
Vector2.zero, Vector2.zero, new Vector2(legend.dataWid, legend.dataHig)); Vector2.zero, Vector2.zero, new Vector2(legend.dataWid, legend.dataHig));
legend.dataList[i].button = btn; legend.dataList[i].button = btn;
Color bcolor = data.show ? data.color : Color.grey; Color bcolor = data.show ? data.color : Color.grey;
btn.gameObject.SetActive(legend.show);
btn.transform.localPosition = GetLegendPosition(i); btn.transform.localPosition = GetLegendPosition(i);
btn.GetComponent<Image>().color = bcolor; btn.GetComponent<Image>().color = bcolor;
btn.GetComponentInChildren<Text>().text = data.text; btn.GetComponentInChildren<Text>().text = data.text;
@@ -367,6 +380,8 @@ namespace xcharts
{ {
data.show = !data.show; data.show = !data.show;
btn.GetComponent<Image>().color = data.show ? data.color : Color.grey; btn.GetComponent<Image>().color = data.show ? data.color : Color.grey;
OnYMaxValueChanged();
RefreshChart();
}); });
} }
} }
@@ -487,9 +502,9 @@ namespace xcharts
protected float GetMaxValue() protected float GetMaxValue()
{ {
float max = 0; float max = 0;
foreach(var series in seriesList) for(int i = 0; i < seriesList.Count; i++)
{ {
if (series.max > max) max = series.max; if (legend.IsShowSeries(i) && seriesList[i].max > max) max = seriesList[i].max;
} }
return max; return max;
} }
@@ -518,7 +533,8 @@ namespace xcharts
checkLegend.right != legend.right || checkLegend.right != legend.right ||
checkLegend.bottom != legend.bottom || checkLegend.bottom != legend.bottom ||
checkLegend.top != legend.top || checkLegend.top != legend.top ||
checkLegend.layout != legend.layout) checkLegend.layout != legend.layout ||
checkLegend.show != legend.show)
{ {
checkLegend.dataWid = legend.dataWid; checkLegend.dataWid = legend.dataWid;
checkLegend.dataHig = legend.dataHig; checkLegend.dataHig = legend.dataHig;
@@ -528,6 +544,7 @@ namespace xcharts
checkLegend.bottom = legend.bottom; checkLegend.bottom = legend.bottom;
checkLegend.top = legend.top; checkLegend.top = legend.top;
checkLegend.layout = legend.layout; checkLegend.layout = legend.layout;
checkLegend.show = legend.show;
OnLegendChanged(); OnLegendChanged();
} }
} }
@@ -573,9 +590,10 @@ namespace xcharts
protected virtual void OnYMaxValueChanged() protected virtual void OnYMaxValueChanged()
{ {
float max = GetMaxValue();
for (int i = 0; i < yScaleTextList.Count; i++) for (int i = 0; i < yScaleTextList.Count; i++)
{ {
yScaleTextList[i].text = ((int)(lastYMaxValue * i / (yScaleTextList.Count -1))).ToString(); yScaleTextList[i].text = ((int)(max * i / (yScaleTextList.Count -1))).ToString();
} }
} }
@@ -593,9 +611,17 @@ namespace xcharts
btn.GetComponentInChildren<Text>().transform.GetComponent<RectTransform>().sizeDelta = new Vector2(legend.dataWid, legend.dataHig); btn.GetComponentInChildren<Text>().transform.GetComponent<RectTransform>().sizeDelta = new Vector2(legend.dataWid, legend.dataHig);
btn.GetComponentInChildren<Text>().transform.localPosition = Vector3.zero; btn.GetComponentInChildren<Text>().transform.localPosition = Vector3.zero;
btn.transform.localPosition = GetLegendPosition(i); btn.transform.localPosition = GetLegendPosition(i);
btn.gameObject.SetActive(legend.show);
} }
} }
protected void RefreshChart()
{
int tempWid = (int)chartWid;
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempWid - 1);
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempWid);
}
protected override void OnPopulateMesh(VertexHelper vh) protected override void OnPopulateMesh(VertexHelper vh)
{ {
vh.Clear(); vh.Clear();

1583
demo.unity

File diff suppressed because it is too large Load Diff