diff --git a/Scripts/BarChart.cs b/Scripts/BarChart.cs index a6ac4dfc..41476a6d 100644 --- a/Scripts/BarChart.cs +++ b/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/Scripts/BaseAxesChart.cs b/Scripts/BaseAxesChart.cs index accba7d7..83f16e5f 100644 --- a/Scripts/BaseAxesChart.cs +++ b/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/Scripts/LineChart.cs b/Scripts/LineChart.cs index 7732a54f..3ec55a3f 100644 --- a/Scripts/LineChart.cs +++ b/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/Scripts/PieChart.cs b/Scripts/PieChart.cs index f5a95d10..752fea22 100644 --- a/Scripts/PieChart.cs +++ b/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/Scripts/RadarChart.cs b/Scripts/RadarChart.cs index c5d6b144..a4878105 100644 --- a/Scripts/RadarChart.cs +++ b/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); }