From 9e57dc3d09a3a4df7e343e9a1bdaf5c844360bb2 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Sat, 16 Mar 2019 07:49:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=8E=A5=E5=8F=A3UpdateData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Demo.cs | 18 ++++---- Assets/XCharts/Scripts/BarChart.cs | 2 +- Assets/XCharts/Scripts/BaseAxesChart.cs | 18 ++++---- Assets/XCharts/Scripts/BaseChart.cs | 57 +++++++++++++++++++------ Assets/XCharts/Scripts/LineChart.cs | 6 +-- Assets/XCharts/Scripts/PieChart.cs | 16 +++---- Assets/XCharts/Scripts/RadarChart.cs | 20 ++++----- Assets/demo_test.unity | 44 ++++++++----------- 8 files changed, 101 insertions(+), 80 deletions(-) diff --git a/Assets/Demo.cs b/Assets/Demo.cs index 0a56f965..ac0ea11a 100644 --- a/Assets/Demo.cs +++ b/Assets/Demo.cs @@ -6,23 +6,23 @@ public class Demo : MonoBehaviour { public Theme theme = Theme.Dark; - private LineChart lineChart; + private BaseChart chart; private float time; private int count; private Theme checkTheme = Theme.Dark; void Awake() { - //lineChart = transform.Find("xchart/line_chart").GetComponent(); - var xchart = transform.Find("xchart"); GridLayoutGroup grid = xchart.GetComponent(); RectTransform rect = transform.GetComponent(); var wid = rect.sizeDelta.x; int childNum = xchart.childCount; - float hig = grid.padding.top + childNum * (grid.cellSize.y+ grid.spacing.y); - rect.sizeDelta = new Vector2(wid,hig); + float hig = grid.padding.top + childNum * (grid.cellSize.y + grid.spacing.y); + rect.sizeDelta = new Vector2(wid, hig); xchart.GetComponent().sizeDelta = new Vector2(wid, hig); + + chart = xchart.gameObject.GetComponentInChildren(); } void Update() @@ -32,11 +32,9 @@ public class Demo : MonoBehaviour { time = 0; count++; - //lineChart.AddXAxisCategory("key" + count); - //lineChart.AddData("line1", "key"+count, Random.Range(24.0f, 60.0f)); - //lineChart.AddData("line2", "key"+count, Random.Range(24.0f, 60.0f)); + chart.UpdateData(0, Random.RandomRange(60, 150)); } - if(checkTheme != theme) + if (checkTheme != theme) { checkTheme = theme; UpdateTheme(theme); @@ -46,7 +44,7 @@ public class Demo : MonoBehaviour void UpdateTheme(Theme theme) { var charts = transform.Find("xchart").GetComponentsInChildren(); - foreach(var chart in charts) + foreach (var chart in charts) { chart.UpdateTheme(theme); } diff --git a/Assets/XCharts/Scripts/BarChart.cs b/Assets/XCharts/Scripts/BarChart.cs index a6ac4dfc..41476a6d 100644 --- a/Assets/XCharts/Scripts/BarChart.cs +++ b/Assets/XCharts/Scripts/BarChart.cs @@ -28,7 +28,7 @@ namespace xcharts protected override void DrawChart(VertexHelper vh) { base.DrawChart(vh); - if(yAxis.type == AxisType.category) + if (yAxis.type == AxisType.category) { int seriesCount = seriesList.Count; float scaleWid = coordinateHig / (yAxis.splitNumber - 1); diff --git a/Assets/XCharts/Scripts/BaseAxesChart.cs b/Assets/XCharts/Scripts/BaseAxesChart.cs index accba7d7..83f16e5f 100644 --- a/Assets/XCharts/Scripts/BaseAxesChart.cs +++ b/Assets/XCharts/Scripts/BaseAxesChart.cs @@ -215,7 +215,7 @@ namespace xcharts else { StringBuilder sb = new StringBuilder(tempAxis.data[index]); - for(int i=0; i dataBtnList.Count - 1) { @@ -93,9 +93,9 @@ namespace xcharts } } - public void SetShowData(string name,bool flag) + public void SetShowData(string name, bool flag) { - for(int i = 0; i < dataList.Count; i++) + for (int i = 0; i < dataList.Count; i++) { if (dataList[i].Equals(name)) { @@ -212,12 +212,20 @@ namespace xcharts public float GetData(int index) { - if (index >=0 && index <= dataList.Count - 1) + if (index >= 0 && index <= dataList.Count - 1) { return dataList[index]; } return 0; } + + public void UpdataData(int index, float value) + { + if (index >= 0 && index <= dataList.Count - 1) + { + dataList[index] = value; + } + } } public class BaseChart : MaskableGraphic @@ -277,7 +285,7 @@ namespace xcharts { for (int i = 0; i < seriesList.Count; i++) { - if (seriesList[i].name == legend) + if (seriesList[i].name.Equals(legend)) { seriesList[i].AddData(value); break; @@ -286,6 +294,32 @@ namespace xcharts RefreshChart(); } + public void UpdateData(string legend, float value, int dataIndex = 0) + { + for (int i = 0; i < seriesList.Count; i++) + { + if (seriesList[i].name.Equals(legend)) + { + seriesList[i].UpdataData(dataIndex, value); + break; + } + } + RefreshChart(); + } + + public void UpdateData(int legendIndex, float value, int dataIndex = 0) + { + for (int i = 0; i < seriesList.Count; i++) + { + if (i == legendIndex) + { + seriesList[i].UpdataData(dataIndex, value); + break; + } + } + RefreshChart(); + } + public void UpdateTheme(Theme theme) { this.theme = theme; @@ -355,7 +389,7 @@ namespace xcharts Button btn = ChartUtils.AddButtonObject(LEGEND_TEXT + i, transform, themeInfo.font, themeInfo.textColor, Vector2.zero, Vector2.zero, Vector2.zero, new Vector2(legend.itemWidth, legend.itemHeight)); - legend.SetDataButton(i,btn); + legend.SetDataButton(i, btn); Color bcolor = themeInfo.GetColor(i); btn.gameObject.SetActive(legend.show); btn.transform.localPosition = GetLegendPosition(i); @@ -363,8 +397,8 @@ namespace xcharts btn.GetComponentInChildren().text = legend.dataList[i]; btn.onClick.AddListener(delegate () { - legend.SetShowData(i,!legend.IsShowSeries(i)); - btn.GetComponent().color = legend.IsShowSeries(i) ? + legend.SetShowData(i, !legend.IsShowSeries(i)); + btn.GetComponent().color = legend.IsShowSeries(i) ? themeInfo.GetColor(i) : themeInfo.unableColor; OnYMaxValueChanged(); OnLegendButtonClicked(); @@ -375,7 +409,7 @@ namespace xcharts private void InitTooltip() { - GameObject obj = ChartUtils.AddTooltipObject("tooltip", transform,themeInfo.font); + GameObject obj = ChartUtils.AddTooltipObject("tooltip", transform, themeInfo.font); tooltip.SetObj(obj); tooltip.SetBackgroundColor(themeInfo.tooltipBackgroundColor); tooltip.SetTextColor(themeInfo.tooltipTextColor); @@ -496,7 +530,7 @@ namespace xcharts if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, null, out local)) return; - + if (local.x < 0 || local.x > chartWid || local.y < 0 || local.y > chartHig) return; @@ -598,4 +632,3 @@ namespace xcharts } } } - \ No newline at end of file diff --git a/Assets/XCharts/Scripts/LineChart.cs b/Assets/XCharts/Scripts/LineChart.cs index 7732a54f..3ec55a3f 100644 --- a/Assets/XCharts/Scripts/LineChart.cs +++ b/Assets/XCharts/Scripts/LineChart.cs @@ -97,7 +97,7 @@ namespace xcharts { float value = series.dataList[i]; - Vector3 p = new Vector3(startX + i * scaleWid, + Vector3 p = new Vector3(startX + i * scaleWid, zeroY + value * coordinateHig / max); float pointWid = lineInfo.pointWid; if (tooltip.show && i == tooltip.DataIndex - 1) @@ -106,8 +106,8 @@ namespace xcharts } if (theme == Theme.Dark) { - - ChartUtils.DrawCricle(vh, p, pointWid, color, + + ChartUtils.DrawCricle(vh, p, pointWid, color, (int)lineInfo.pointWid * 5); } else diff --git a/Assets/XCharts/Scripts/PieChart.cs b/Assets/XCharts/Scripts/PieChart.cs index f5a95d10..752fea22 100644 --- a/Assets/XCharts/Scripts/PieChart.cs +++ b/Assets/XCharts/Scripts/PieChart.cs @@ -52,11 +52,11 @@ namespace xcharts float degree = totalDegree * value / dataTotal; float toDegree = startDegree + degree; - float outSideRadius = pieInfo.outsideRadiusDynamic ? - pieInfo.insideRadius + (pieRadius - pieInfo.insideRadius) * value / dataMax : + float outSideRadius = pieInfo.outsideRadiusDynamic ? + pieInfo.insideRadius + (pieRadius - pieInfo.insideRadius) * value / dataMax : pieRadius; - ChartUtils.DrawDoughnut(vh, new Vector3(pieCenterX, pieCenterY), pieInfo.insideRadius, - outSideRadius,startDegree, toDegree, themeInfo.GetColor(i)); + ChartUtils.DrawDoughnut(vh, new Vector3(pieCenterX, pieCenterY), pieInfo.insideRadius, + outSideRadius, startDegree, toDegree, themeInfo.GetColor(i)); startDegree = toDegree; } } @@ -70,7 +70,7 @@ namespace xcharts private float GetDataTotal() { float total = 0; - for(int i = 0; i < seriesList.Count; i++) + for (int i = 0; i < seriesList.Count; i++) { if (legend.IsShowSeries(i)) { @@ -83,9 +83,9 @@ namespace xcharts private float GetDataMax() { float max = 0; - for(int i = 0; i < seriesList.Count; i++) + for (int i = 0; i < seriesList.Count; i++) { - if(legend.IsShowSeries(i) && seriesList[i].GetData(0) > max) + if (legend.IsShowSeries(i) && seriesList[i].GetData(0) > max) { max = seriesList[i].GetData(0); } @@ -98,7 +98,7 @@ namespace xcharts float diffX = chartWid - pieInfo.left - pieInfo.right; float diffY = chartHig - pieInfo.top - pieInfo.bottom; float diff = Mathf.Min(diffX, diffY); - if(pieInfo.outsideRadius <= 0) + if (pieInfo.outsideRadius <= 0) { pieRadius = diff / 3 * 2; pieCenterX = pieInfo.left + pieRadius; diff --git a/Assets/XCharts/Scripts/RadarChart.cs b/Assets/XCharts/Scripts/RadarChart.cs index c5d6b144..a4878105 100644 --- a/Assets/XCharts/Scripts/RadarChart.cs +++ b/Assets/XCharts/Scripts/RadarChart.cs @@ -16,7 +16,7 @@ namespace xcharts { public bool cricle; public bool area; - + public float radius = 100; public int splitNumber = 5; @@ -80,7 +80,7 @@ namespace xcharts else if (diff > 1f) { anchor = TextAnchor.MiddleLeft; - pos = new Vector3(pos.x + txtWid + 5,pos.y); + pos = new Vector3(pos.x + txtWid + 5, pos.y); } else { @@ -100,7 +100,7 @@ namespace xcharts private void CheckRadarInfoChanged() { - if( checkRadarInfo.radius != radarInfo.radius || + if (checkRadarInfo.radius != radarInfo.radius || checkRadarInfo.left != radarInfo.left || checkRadarInfo.right != radarInfo.right || checkRadarInfo.top != radarInfo.top || @@ -132,7 +132,7 @@ namespace xcharts var x = radarCenterX + radarInfo.radius * Mathf.Sin(angle); var y = radarCenterY + radarInfo.radius * Mathf.Cos(angle); - return new Vector3(x,y); + return new Vector3(x, y); } protected override void DrawChart(VertexHelper vh) @@ -181,13 +181,13 @@ namespace xcharts Vector3 startPoint = Vector3.zero; Vector3 toPoint = Vector3.zero; Vector3 firstPoint = Vector3.zero; - + for (int i = 0; i < seriesList.Count; i++) { if (!legend.IsShowSeries(i)) continue; var dataList = seriesList[i].dataList; var color = themeInfo.GetColor(i); - var areaColor = new Color(color.r,color.g,color.b,color.a*0.7f); + var areaColor = new Color(color.r, color.g, color.b, color.a * 0.7f); var max = radarInfo.indicatorList[i].max > 0 ? radarInfo.indicatorList[i].max : GetMaxValue(); @@ -233,11 +233,11 @@ namespace xcharts float block = radarInfo.radius / radarInfo.splitNumber; int indicatorNum = radarInfo.indicatorList.Count; Vector3 p1, p2, p3, p4; - Vector3 p = new Vector3(radarCenterX,radarCenterY); + Vector3 p = new Vector3(radarCenterX, radarCenterY); float angle = 2 * Mathf.PI / indicatorNum; for (int i = 0; i < radarInfo.splitNumber; i++) { - Color color = radarInfo.backgroundColorList[i% radarInfo.backgroundColorList.Count]; + Color color = radarInfo.backgroundColorList[i % radarInfo.backgroundColorList.Count]; outsideRadius = insideRadius + block; p1 = new Vector3(p.x + insideRadius * Mathf.Sin(0), p.y + insideRadius * Mathf.Cos(0)); p2 = new Vector3(p.x + outsideRadius * Mathf.Sin(0), p.y + outsideRadius * Mathf.Cos(0)); @@ -261,7 +261,7 @@ namespace xcharts float currAngle = j * angle; p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle), p.y + outsideRadius * Mathf.Cos(currAngle)); - ChartUtils.DrawLine(vh, p, p3, radarInfo.lineTickness/2, radarInfo.lineColor); + ChartUtils.DrawLine(vh, p, p3, radarInfo.lineTickness / 2, radarInfo.lineColor); } } @@ -285,7 +285,7 @@ namespace xcharts for (int j = 0; j <= indicatorNum; j++) { float currAngle = j * angle; - p1 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle), + p1 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle), p.y + outsideRadius * Mathf.Cos(currAngle)); ChartUtils.DrawLine(vh, p, p1, radarInfo.lineTickness / 2, radarInfo.lineColor); } diff --git a/Assets/demo_test.unity b/Assets/demo_test.unity index f9802152..849eda44 100644 --- a/Assets/demo_test.unity +++ b/Assets/demo_test.unity @@ -593,8 +593,8 @@ RectTransform: m_PrefabInternal: {fileID: 0} m_GameObject: {fileID: 505350089} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 599, y: 459.5, z: 0} - m_LocalScale: {x: 0.9359375, y: 0.9359375, z: 0.9359375} + m_LocalPosition: {x: 600.5, y: 459.5, z: 0} + m_LocalScale: {x: 0.93828124, y: 0.93828124, z: 0.93828124} m_Children: - {fileID: 2051892027} m_Father: {fileID: 0} @@ -830,27 +830,24 @@ MonoBehaviour: top: 0 bottom: 0 dataList: - - show: 1 - key: pie1 - text: "\u6570\u636E1" - - show: 1 - key: pie2 - text: "\u6570\u636E2" - - show: 1 - key: pie3 - text: "\u6570\u636E3" + - "H\x01" + - "O\x01" + - "V\x01" tooltip: show: 0 seriesList: - - legendKey: + - name: H showDataNumber: 0 - dataList: [] - - legendKey: + dataList: + - 60 + - name: O showDataNumber: 0 - dataList: [] - - legendKey: + dataList: + - 30 + - name: V showDataNumber: 0 - dataList: [] + dataList: + - 20 pieInfo: name: Pie insideRadius: 0 @@ -861,13 +858,6 @@ MonoBehaviour: right: 0 top: 0 bottom: 0 - dataList: - - text: pie1 - value: 10 - - text: pie2 - value: 50 - - text: pie3 - value: 40 --- !u!222 &872320148 CanvasRenderer: m_ObjectHideFlags: 0 @@ -1126,7 +1116,7 @@ MonoBehaviour: m_HorizontalOverflow: 1 m_VerticalOverflow: 1 m_LineSpacing: 1 - m_Text: "\u6570\u636E1" + m_Text: "H\x01" --- !u!222 &1679032979 CanvasRenderer: m_ObjectHideFlags: 0 @@ -1200,7 +1190,7 @@ MonoBehaviour: m_HorizontalOverflow: 1 m_VerticalOverflow: 1 m_LineSpacing: 1 - m_Text: "\u6570\u636E3" + m_Text: "V\x01" --- !u!222 &1790728300 CanvasRenderer: m_ObjectHideFlags: 0 @@ -1484,7 +1474,7 @@ MonoBehaviour: m_HorizontalOverflow: 1 m_VerticalOverflow: 1 m_LineSpacing: 1 - m_Text: "\u6570\u636E2" + m_Text: "O\x01" --- !u!222 &2099261119 CanvasRenderer: m_ObjectHideFlags: 0