From d0f0da9c8368242ba5b5f8276205e33a460b1d80 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Thu, 20 Sep 2018 07:10:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=98=E7=BA=BF=E5=9B=BE=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=9B=B4=E5=A4=9A=E5=8F=82=E6=95=B0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Scripts/BarChart.cs | 8 ++-- Scripts/BaseChart.cs | 66 +++++++++++++++------------- Scripts/ChartUtils.cs | 19 +++++--- Scripts/LineChart.cs | 58 +++++++++++++++++++----- demo.unity | 100 +++++++++++++++++++++++++----------------- 5 files changed, 157 insertions(+), 94 deletions(-) diff --git a/Scripts/BarChart.cs b/Scripts/BarChart.cs index e2fe1c03..a4884b83 100644 --- a/Scripts/BarChart.cs +++ b/Scripts/BarChart.cs @@ -33,7 +33,7 @@ namespace xcharts if(yAxis.type == AxisType.category) { int seriesCount = seriesList.Count; - float scaleWid = coordinateHig / (yAxis.scaleNum - 1); + 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 max = GetMaxValue(); @@ -46,7 +46,7 @@ namespace xcharts { SeriesData data = series.dataList[i]; float pX = zeroX + coordinate.tickness; - float pY = zeroY + i * coordinateHig / (yAxis.scaleNum - 1); + float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1); float barHig = data.value / max * coordinateWid; float space = offset + j * (barWid + barData.space); Vector3 p1 = new Vector3(pX, pY + space + barWid); @@ -60,7 +60,7 @@ namespace xcharts else { int seriesCount = seriesList.Count; - float scaleWid = coordinateWid / (xAxis.scaleNum - 1); + 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 max = GetMaxValue(); @@ -72,7 +72,7 @@ namespace xcharts for (int i = 0; i < series.dataList.Count; i++) { SeriesData data = series.dataList[i]; - float pX = zeroX + i * coordinateWid / (xAxis.scaleNum - 1); + float pX = zeroX + i * coordinateWid / (xAxis.splitNumber - 1); float pY = zeroY + coordinate.tickness; float barHig = data.value / max * coordinateHig; float space = offset + j * (barWid + barData.space); diff --git a/Scripts/BaseChart.cs b/Scripts/BaseChart.cs index f812eb83..03594754 100644 --- a/Scripts/BaseChart.cs +++ b/Scripts/BaseChart.cs @@ -56,19 +56,23 @@ namespace xcharts } [System.Serializable] - public enum Layout + public enum Location { left, right, top, - bottom + bottom, + start, + middle, + center, + end, } [System.Serializable] public class Axis { public AxisType type; - public int scaleNum = 5; + public int splitNumber = 5; public bool showSplitLine; public List data; } @@ -98,7 +102,7 @@ namespace xcharts public class Legend { public bool show; - public Layout layout; + public Location location; public float dataWid; public float dataHig; public float dataSpace; @@ -304,8 +308,8 @@ namespace xcharts yScaleTextList.Clear(); if (yAxis.type == AxisType.value) { - yAxis.scaleNum = DEFAULT_YSACLE_NUM; - for (int i = 0; i < yAxis.scaleNum; i++) + yAxis.splitNumber = DEFAULT_YSACLE_NUM; + for (int i = 0; i < yAxis.splitNumber; i++) { Text txt = ChartUtils.AddTextObject(YSCALE_TEXT_PREFIX + i, transform, font, TextAnchor.MiddleRight, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f), @@ -317,8 +321,8 @@ namespace xcharts } else { - yAxis.scaleNum = yAxis.data.Count + 1; - for (int i = 0; i < yAxis.scaleNum - 1; i++) + yAxis.splitNumber = yAxis.data.Count + 1; + for (int i = 0; i < yAxis.splitNumber - 1; i++) { Text txt = ChartUtils.AddTextObject(YSCALE_TEXT_PREFIX + i, transform, font, TextAnchor.MiddleRight, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f), @@ -335,9 +339,9 @@ namespace xcharts xScaleTextList.Clear(); if (xAxis.type == AxisType.value) { - xAxis.scaleNum = DEFAULT_YSACLE_NUM; - float scaleWid = coordinateWid / (xAxis.scaleNum - 1); - for (int i = 0; i < xAxis.scaleNum; i++) + xAxis.splitNumber = DEFAULT_YSACLE_NUM; + float scaleWid = coordinateWid / (xAxis.splitNumber - 1); + for (int i = 0; i < xAxis.splitNumber; i++) { Text txt = ChartUtils.AddTextObject(XSCALE_TEXT_PREFIX + i, transform, font, TextAnchor.MiddleCenter, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f), @@ -349,9 +353,9 @@ namespace xcharts } else { - xAxis.scaleNum = xAxis.data.Count + 1; - float scaleWid = coordinateWid / (xAxis.scaleNum - 1); - for (int i = 0; i < xAxis.scaleNum - 1; i++) + xAxis.splitNumber = xAxis.data.Count + 1; + float scaleWid = coordinateWid / (xAxis.splitNumber - 1); + for (int i = 0; i < xAxis.splitNumber - 1; i++) { Text txt = ChartUtils.AddTextObject(XSCALE_TEXT_PREFIX + i, transform, font, TextAnchor.MiddleCenter, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f), @@ -389,19 +393,19 @@ namespace xcharts private Vector3 GetLegendPosition(int i) { int legendCount = legend.dataList.Count; - switch (legend.layout) + switch (legend.location) { - case Layout.bottom: - case Layout.top: + case Location.bottom: + case Location.top: float startX = legend.left; if (startX <= 0) { startX = (chartWid -(legendCount *legend.dataWid - (legendCount-1) * legend.dataSpace)) / 2; } - float posY = legend.layout == Layout.bottom ? legend.bottom : chartHig - legend.top - legend.dataHig; + float posY = legend.location == Location.bottom ? legend.bottom : chartHig - legend.top - legend.dataHig; return new Vector3(startX + i * (legend.dataWid+legend.dataSpace),posY,0); - case Layout.left: - case Layout.right: + case Location.left: + case Location.right: float startY =0; if (legend.top > 0) { @@ -411,7 +415,7 @@ namespace xcharts { startY = chartHig - (chartHig - (legendCount * legend.dataHig - (legendCount - 1) * legend.dataSpace)) / 2 - legend.dataHig; } - float posX = legend.layout == Layout.left ? legend.left : chartWid - legend.right - legend.dataWid; + float posX = legend.location == Location.left ? legend.left : chartWid - legend.right - legend.dataWid; return new Vector3(posX,startY - i * (legend.dataHig + legend.dataSpace), 0); default:break; } @@ -420,7 +424,7 @@ namespace xcharts private Vector3 GetYScalePosition(int i) { - float scaleWid = coordinateHig / (yAxis.scaleNum - 1); + float scaleWid = coordinateHig / (yAxis.splitNumber - 1); if (yAxis.type == AxisType.value) { return new Vector3(zeroX - coordinate.scaleLen - 2f, @@ -435,7 +439,7 @@ namespace xcharts private Vector3 GetXScalePosition(int i) { - float scaleWid = coordinateWid / (xAxis.scaleNum - 1); + float scaleWid = coordinateWid / (xAxis.splitNumber - 1); if (xAxis.type == AxisType.value) { return new Vector3(zeroX + (i + 1 - 0.5f) * scaleWid, zeroY - coordinate.scaleLen - 10, 0); @@ -533,7 +537,7 @@ namespace xcharts checkLegend.right != legend.right || checkLegend.bottom != legend.bottom || checkLegend.top != legend.top || - checkLegend.layout != legend.layout || + checkLegend.location != legend.location || checkLegend.show != legend.show) { checkLegend.dataWid = legend.dataWid; @@ -543,7 +547,7 @@ namespace xcharts checkLegend.right = legend.right; checkLegend.bottom = legend.bottom; checkLegend.top = legend.top; - checkLegend.layout = legend.layout; + checkLegend.location = legend.location; checkLegend.show = legend.show; OnLegendChanged(); } @@ -552,14 +556,14 @@ namespace xcharts protected virtual void OnCoordinateSize() { //update yScale pos - for (int i = 0; i < yAxis.scaleNum; i++) + for (int i = 0; i < yAxis.splitNumber; i++) { if (i < yScaleTextList.Count && yScaleTextList[i]) { yScaleTextList[i].transform.localPosition = GetYScalePosition(i); } } - for (int i = 0; i < xAxis.scaleNum; i++) + for (int i = 0; i < xAxis.splitNumber; i++) { if (i < xScaleTextList.Count && xScaleTextList[i]) { @@ -643,19 +647,19 @@ namespace xcharts { if (!coordinate.show) return; // draw scale - for (int i = 1; i < yAxis.scaleNum; i++) + for (int i = 1; i < yAxis.splitNumber; i++) { float pX = zeroX - coordinate.scaleLen; - float pY = zeroY + i * coordinateHig / (yAxis.scaleNum - 1); + float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1); ChartUtils.DrawLine(vh, new Vector3(pX, pY), new Vector3(zeroX, pY), coordinate.tickness, Color.white); if (yAxis.showSplitLine) { ChartUtils.DrawLine(vh, new Vector3(zeroX, pY), new Vector3(zeroX + coordinateWid, pY), coordinate.tickness, Color.grey); } } - for (int i = 1; i < xAxis.scaleNum; i++) + for (int i = 1; i < xAxis.splitNumber; i++) { - float pX = zeroX + i * coordinateWid / (xAxis.scaleNum - 1); + float pX = zeroX + i * coordinateWid / (xAxis.splitNumber - 1); float pY = zeroY - coordinate.scaleLen - 2; ChartUtils.DrawLine(vh, new Vector3(pX, zeroY), new Vector3(pX, pY), coordinate.tickness, Color.white); if (xAxis.showSplitLine) diff --git a/Scripts/ChartUtils.cs b/Scripts/ChartUtils.cs index 1942544b..6e7ec30b 100644 --- a/Scripts/ChartUtils.cs +++ b/Scripts/ChartUtils.cs @@ -62,8 +62,8 @@ namespace xcharts btnObj.AddComponent(); btnObj.AddComponent