增加簇状柱形图

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

View File

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

View File

@@ -107,6 +107,18 @@ namespace xcharts
public float top;
public float bottom;
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]
@@ -360,6 +372,7 @@ namespace xcharts
Vector2.zero, Vector2.zero, new Vector2(legend.dataWid, legend.dataHig));
legend.dataList[i].button = btn;
Color bcolor = data.show ? data.color : Color.grey;
btn.gameObject.SetActive(legend.show);
btn.transform.localPosition = GetLegendPosition(i);
btn.GetComponent<Image>().color = bcolor;
btn.GetComponentInChildren<Text>().text = data.text;
@@ -367,6 +380,8 @@ namespace xcharts
{
data.show = !data.show;
btn.GetComponent<Image>().color = data.show ? data.color : Color.grey;
OnYMaxValueChanged();
RefreshChart();
});
}
}
@@ -487,9 +502,9 @@ namespace xcharts
protected float GetMaxValue()
{
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;
}
@@ -518,7 +533,8 @@ namespace xcharts
checkLegend.right != legend.right ||
checkLegend.bottom != legend.bottom ||
checkLegend.top != legend.top ||
checkLegend.layout != legend.layout)
checkLegend.layout != legend.layout ||
checkLegend.show != legend.show)
{
checkLegend.dataWid = legend.dataWid;
checkLegend.dataHig = legend.dataHig;
@@ -528,6 +544,7 @@ namespace xcharts
checkLegend.bottom = legend.bottom;
checkLegend.top = legend.top;
checkLegend.layout = legend.layout;
checkLegend.show = legend.show;
OnLegendChanged();
}
}
@@ -573,9 +590,10 @@ namespace xcharts
protected virtual void OnYMaxValueChanged()
{
float max = GetMaxValue();
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.localPosition = Vector3.zero;
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)
{
vh.Clear();

File diff suppressed because it is too large Load Diff