From cfb6cdb4b549585981bdc0cdeec7ce610c472f68 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Sun, 23 Sep 2018 08:18:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A5=BC=E5=9B=BEpie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Demo.cs | 2 +- Scripts/BarChart.cs | 16 +- Scripts/BaseChart.cs | 64 +- Scripts/ChartUtils.cs | 40 +- Scripts/LineChart.cs | 31 +- Scripts/PieChart.cs | 116 ++ Scripts/PieChart.cs.meta | 13 + demo.unity | 3978 +++++++++++++++++++++++++++----------- 8 files changed, 3031 insertions(+), 1229 deletions(-) create mode 100644 Scripts/PieChart.cs create mode 100644 Scripts/PieChart.cs.meta diff --git a/Demo.cs b/Demo.cs index bdb18c4a..fd221779 100644 --- a/Demo.cs +++ b/Demo.cs @@ -9,7 +9,7 @@ public class Demo : MonoBehaviour void Awake() { - lineChart = transform.Find("line_chart").GetComponent(); + lineChart = transform.Find("xchart/line_chart").GetComponent(); } void Update() diff --git a/Scripts/BarChart.cs b/Scripts/BarChart.cs index 825bfb49..d99f0612 100644 --- a/Scripts/BarChart.cs +++ b/Scripts/BarChart.cs @@ -4,7 +4,7 @@ using UnityEngine.UI; namespace xcharts { [System.Serializable] - public class BarData + public class BarInfo { public float barWid = 0.7f; public float space; @@ -13,7 +13,7 @@ namespace xcharts public class BarChart : BaseChart { [SerializeField] - private BarData barData; + private BarInfo barInfo; protected override void Awake() { @@ -33,8 +33,8 @@ namespace xcharts { int seriesCount = seriesList.Count; float scaleWid = coordinateHig / (yAxis.splitNumber - 1); - float barWid = barData.barWid > 1 ? barData.barWid : scaleWid * barData.barWid; - float offset = (scaleWid - barWid * seriesCount - barData.space * (seriesCount - 1)) / 2; + float barWid = barInfo.barWid > 1 ? barInfo.barWid : scaleWid * barInfo.barWid; + float offset = (scaleWid - barWid * seriesCount - barInfo.space * (seriesCount - 1)) / 2; float max = GetMaxValue(); for (int j = 0; j < seriesCount; j++) { @@ -53,7 +53,7 @@ namespace xcharts float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1); if (!yAxis.boundaryGap) pY -= scaleWid / 2; float barHig = data.value / max * coordinateWid; - float space = offset + j * (barWid + barData.space); + float space = offset + j * (barWid + barInfo.space); Vector3 p1 = new Vector3(pX, pY + space + barWid); Vector3 p2 = new Vector3(pX + barHig, pY + space + barWid); Vector3 p3 = new Vector3(pX + barHig, pY + space); @@ -66,8 +66,8 @@ namespace xcharts { int seriesCount = seriesList.Count; float scaleWid = coordinateWid / (xAxis.splitNumber - 1); - float barWid = barData.barWid > 1 ? barData.barWid : scaleWid * barData.barWid; - float offset = (scaleWid - barWid * seriesCount - barData.space * (seriesCount - 1)) / 2; + float barWid = barInfo.barWid > 1 ? barInfo.barWid : scaleWid * barInfo.barWid; + float offset = (scaleWid - barWid * seriesCount - barInfo.space * (seriesCount - 1)) / 2; float max = GetMaxValue(); for (int j = 0; j < seriesCount; j++) { @@ -86,7 +86,7 @@ namespace xcharts if (!xAxis.boundaryGap) pX -= scaleWid / 2; float pY = zeroY + coordinate.tickness; float barHig = data.value / max * coordinateHig; - float space = offset + j * (barWid + barData.space); + float space = offset + j * (barWid + barInfo.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); diff --git a/Scripts/BaseChart.cs b/Scripts/BaseChart.cs index 34787cfb..67bfc0e6 100644 --- a/Scripts/BaseChart.cs +++ b/Scripts/BaseChart.cs @@ -110,10 +110,10 @@ namespace xcharts [System.Serializable] public class Legend { - public bool show; + public bool show = true; public Location location; - public float dataWid; - public float dataHig; + public float dataWid = 50.0f; + public float dataHig = 20.0f; public float dataSpace; public float left; public float right; @@ -222,14 +222,16 @@ namespace xcharts private float lastCoordinateHig; private float lastCoordinateScaleLen; - private Align lastTitleAlign; - private float lastTitleLeft; - private float lastTitleRight; - private float lastTitleTop; + //private Align lastTitleAlign; + //private float lastTitleLeft; + //private float lastTitleRight; + //private float lastTitleTop; private float lastXMaxValue; private float lastYMaxValue; + private Coordinate checkCoordinate = new Coordinate(); + private Title checkTitle = new Title(); private Legend checkLegend = new Legend(); private XAxis checkXAxis = new XAxis(); private YAxis checkYAxis = new YAxis(); @@ -269,7 +271,7 @@ namespace xcharts CheckYAxisType(); CheckXAxisType(); CheckMaxValue(); - CheckCoordinateSizeChange(); + CheckCoordinate(); } public void AddData(string legend, string key, float value) @@ -288,13 +290,13 @@ namespace xcharts public void AddXAxisCategory(string category) { xAxis.AddCategory(category); - OnXAxisType(); + OnXAxisChanged(); } public void AddYAxisCategory(string category) { yAxis.AddCategory(category); - OnYAxisType(); + OnYAxisChanged(); } private void HideChild(string match = null) @@ -365,6 +367,7 @@ namespace xcharts new Vector2(coordinate.left, 20)); txt.transform.localPosition = GetYScalePosition(i); txt.text = ((int)(max * i / yAxis.splitNumber)).ToString(); + txt.gameObject.SetActive(coordinate.show); yScaleTextList.Add(txt); } } @@ -378,6 +381,7 @@ namespace xcharts new Vector2(coordinate.left, 20)); txt.transform.localPosition = GetYScalePosition(i); txt.text = yAxis.data[i]; + txt.gameObject.SetActive(coordinate.show); yScaleTextList.Add(txt); } } @@ -399,6 +403,7 @@ namespace xcharts new Vector2(scaleWid, 20)); txt.transform.localPosition = GetXScalePosition(i); txt.text = ((int)(max * i / xAxis.splitNumber)).ToString(); + txt.gameObject.SetActive(coordinate.show); xScaleTextList.Add(txt); } } @@ -413,6 +418,7 @@ namespace xcharts new Vector2(scaleWid, 20)); txt.transform.localPosition = GetXScalePosition(i); txt.text = xAxis.data[i]; + txt.gameObject.SetActive(coordinate.show); xScaleTextList.Add(txt); } } @@ -436,6 +442,7 @@ namespace xcharts data.show = !data.show; btn.GetComponent().color = data.show ? data.color : Color.grey; OnYMaxValueChanged(); + OnLegendButtonClicked(); RefreshChart(); }); } @@ -520,7 +527,7 @@ namespace xcharts } } - private void CheckCoordinateSizeChange() + private void CheckCoordinate() { if (lastCoordinateHig != coordinateHig || lastCoordinateWid != coordinateWid @@ -531,6 +538,12 @@ namespace xcharts lastCoordinateScaleLen = coordinate.scaleLen; OnCoordinateSize(); } + if(checkCoordinate.show != coordinate.show) + { + checkCoordinate.show = coordinate.show; + OnXAxisChanged(); + OnYAxisChanged(); + } } private void CheckYAxisType() @@ -544,7 +557,7 @@ namespace xcharts checkYAxis.boundaryGap = yAxis.boundaryGap; checkYAxis.showSplitLine = yAxis.showSplitLine; checkYAxis.splitNumber = yAxis.splitNumber; - OnYAxisType(); + OnYAxisChanged(); } } @@ -559,7 +572,7 @@ namespace xcharts checkXAxis.boundaryGap = xAxis.boundaryGap; checkXAxis.showSplitLine = xAxis.showSplitLine; checkXAxis.splitNumber = xAxis.splitNumber; - OnXAxisType(); + OnXAxisChanged(); } } @@ -599,15 +612,15 @@ namespace xcharts private void CheckTile() { - if (lastTitleAlign != title.align || - lastTitleLeft != title.left || - lastTitleRight != title.right || - lastTitleTop != title.top) + if (checkTitle.align != title.align || + checkTitle.left != title.left || + checkTitle.right != title.right || + checkTitle.top != title.top) { - lastTitleAlign = title.align; - lastTitleLeft = title.left; - lastTitleRight = title.right; - lastTitleTop = title.top; + checkTitle.align = title.align; + checkTitle.left = title.left; + checkTitle.right = title.right; + checkTitle.top = title.top; OnTitleChanged(); } } @@ -656,13 +669,13 @@ namespace xcharts } } - protected virtual void OnYAxisType() + protected virtual void OnYAxisChanged() { HideChild(YSCALE_TEXT_PREFIX); InitYScale(); } - protected virtual void OnXAxisType() + protected virtual void OnXAxisChanged() { HideChild(XSCALE_TEXT_PREFIX); InitXScale(); @@ -705,6 +718,11 @@ namespace xcharts } } + protected virtual void OnLegendButtonClicked() + { + + } + protected void RefreshChart() { int tempWid = (int)chartWid; diff --git a/Scripts/ChartUtils.cs b/Scripts/ChartUtils.cs index 6e7ec30b..79c98241 100644 --- a/Scripts/ChartUtils.cs +++ b/Scripts/ChartUtils.cs @@ -62,8 +62,8 @@ namespace xcharts btnObj.AddComponent(); btnObj.AddComponent