diff --git a/Scripts/UI/BarChart.cs b/Scripts/UI/BarChart.cs index 29b96705..d81697a7 100644 --- a/Scripts/UI/BarChart.cs +++ b/Scripts/UI/BarChart.cs @@ -26,53 +26,5 @@ namespace XCharts } } #endif - - protected HashSet m_SerieNameSet = new HashSet(); - protected Dictionary> m_StackSeries = new Dictionary>(); - protected List m_SeriesCurrHig = new List(); - protected override void DrawChart(VertexHelper vh) - { - base.DrawChart(vh); - if (!m_CheckMinMaxValue) return; - bool yCategory = m_YAxises[0].IsCategory() || m_YAxises[1].IsCategory(); - m_Series.GetStackSeries(ref m_StackSeries); - int seriesCount = m_StackSeries.Count; - int serieNameCount = -1; - m_SerieNameSet.Clear(); - m_BarLastOffset = 0; - for (int j = 0; j < seriesCount; j++) - { - var serieList = m_StackSeries[j]; - m_SeriesCurrHig.Clear(); - for (int n = 0; n < serieList.Count; n++) - { - Serie serie = serieList[n]; - serie.dataPoints.Clear(); - if (string.IsNullOrEmpty(serie.name)) serieNameCount++; - else if (!m_SerieNameSet.Contains(serie.name)) - { - m_SerieNameSet.Add(serie.name); - serieNameCount++; - } - switch (serie.type) - { - case SerieType.Line: - if (yCategory) DrawYLineSerie(vh, j, serie, ref m_SeriesCurrHig); - else DrawXLineSerie(vh, j, serie, ref m_SeriesCurrHig); - break; - case SerieType.Bar: - if (yCategory) DrawYBarSerie(vh, j, serie, serieNameCount, ref m_SeriesCurrHig); - else DrawXBarSerie(vh, j, serie, serieNameCount, ref m_SeriesCurrHig); - break; - } - } - } - DrawLabelBackground(vh); - DrawLinePoint(vh); - DrawLineArrow(vh); - if (yCategory) DrawYTooltipIndicator(vh); - else DrawXTooltipIndicator(vh); - } - } } diff --git a/Scripts/UI/Component/Serie.cs b/Scripts/UI/Component/Serie.cs index c5501839..5923786a 100644 --- a/Scripts/UI/Component/Serie.cs +++ b/Scripts/UI/Component/Serie.cs @@ -209,6 +209,10 @@ namespace XCharts /// public string name { get { return m_Name; } set { m_Name = value; } } /// + /// 图例名称。当系列名称不为空时,图例名称即为系列名称;反之则为索引index。 + /// + public string legendName { get { return string.IsNullOrEmpty(name) ? ChartCached.IntToStr(index) : name; } } + /// /// If stack the value. On the same category axis, the series with the same stack name would be put on top of each other. /// 数据堆叠,同个类目轴上系列配置相同的stack值后,后一个系列的值会在前一个系列的值上相加。 /// diff --git a/Scripts/UI/Component/Series.cs b/Scripts/UI/Component/Series.cs index ff74626e..60083a17 100644 --- a/Scripts/UI/Component/Series.cs +++ b/Scripts/UI/Component/Series.cs @@ -793,18 +793,27 @@ namespace XCharts public List GetSerieNameList() { serieNameList.Clear(); - foreach (var serie in m_Series) + for (int n = 0; n < m_Series.Count; n++) { - if (!string.IsNullOrEmpty(serie.name) && !serieNameList.Contains(serie.name)) + var serie = m_Series[n]; + switch (serie.type) { - serieNameList.Add(serie.name); - } - foreach (var data in serie.data) - { - if (!string.IsNullOrEmpty(data.name) && !serieNameList.Contains(data.name)) - { - serieNameList.Add(data.name); - } + case SerieType.Pie: + case SerieType.Radar: + for (int i = 0; i < serie.data.Count; i++) + { + if (string.IsNullOrEmpty(serie.data[i].name)) + serieNameList.Add(ChartCached.IntToStr(i)); + else if (!serieNameList.Contains(serie.data[i].name)) + serieNameList.Add(serie.data[i].name); + } + break; + default: + if (string.IsNullOrEmpty(serie.name)) + serieNameList.Add(ChartCached.IntToStr(n)); + else if (!serieNameList.Contains(serie.name)) + serieNameList.Add(serie.name); + break; } } return serieNameList; @@ -871,6 +880,16 @@ namespace XCharts } } + public bool IsLegalLegendName(string name) + { + int numName = -1; + if (int.TryParse(name, out numName)) + { + if (numName >= 0 && numName < list.Count) return false; + } + return true; + } + /// /// 从json中解析数据 /// diff --git a/Scripts/UI/Internal/BaseChart.cs b/Scripts/UI/Internal/BaseChart.cs index 3c4f0d86..382bb561 100644 --- a/Scripts/UI/Internal/BaseChart.cs +++ b/Scripts/UI/Internal/BaseChart.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Text.RegularExpressions; +using System.Linq; using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; @@ -53,6 +54,7 @@ namespace XCharts [NonSerialized] private bool m_RefreshLabel = false; [NonSerialized] private bool m_ReinitLabel = false; [NonSerialized] private bool m_CheckAnimation = false; + [NonSerialized] protected List m_LegendRealShowName = new List(); protected Vector2 chartAnchorMax { get { return rectTransform.anchorMax; } } protected Vector2 chartAnchorMin { get { return rectTransform.anchorMin; } } @@ -182,32 +184,42 @@ namespace XCharts var legendObject = ChartHelper.AddObject(s_LegendObjectName, transform, anchorMin, anchorMax, pivot, new Vector2(chartWidth, chartHeight)); legendObject.transform.localPosition = m_Legend.location.GetPosition(chartWidth, chartHeight); - ChartHelper.DestoryAllChilds(legendObject.transform); - if (!m_Legend.show) return; - var serieNameList = m_Series.GetSerieNameList(); + + m_LegendRealShowName = m_Series.GetSerieNameList(); List datas; - if (m_Legend.data.Count > 0) + if (m_Legend.show && m_Legend.data.Count > 0) { datas = new List(); - for (int i = 0; i < m_Legend.data.Count; i++) + for (int i = 0; i < m_LegendRealShowName.Count; i++) { - var category = m_Legend.data[i]; - if (serieNameList.Contains(category)) datas.Add(category); + if (m_Legend.data.Contains(m_LegendRealShowName[i])) datas.Add(m_LegendRealShowName[i]); } } else { - datas = serieNameList; + datas = m_LegendRealShowName; } - m_Legend.RemoveButton(); + int totalLegend = 0; for (int i = 0; i < datas.Count; i++) { + if (!m_Series.IsLegalLegendName(datas[i])) continue; + totalLegend++; + } + m_Legend.RemoveButton(); + ChartHelper.DestoryAllChilds(legendObject.transform); + if (!m_Legend.show) return; + for (int i = 0; i < datas.Count; i++) + { + if (!m_Series.IsLegalLegendName(datas[i])) continue; string legendName = m_Legend.GetFormatterContent(datas[i]); - Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + i + "_" + datas[i], legendObject.transform, + var readIndex = m_LegendRealShowName.IndexOf(datas[i]); + var objName = s_LegendObjectName + "_" + i + "_" + datas[i]; + Button btn = ChartHelper.AddButtonObject(objName, legendObject.transform, m_ThemeInfo.font, m_Legend.itemFontSize, m_ThemeInfo.legendTextColor, anchor, anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight)); - var bgColor = IsActiveByLegend(datas[i]) ? m_ThemeInfo.GetColor(i) : m_ThemeInfo.legendUnableColor; - m_Legend.SetButton(legendName, btn, datas.Count); + var bgColor = IsActiveByLegend(datas[i]) ? + m_ThemeInfo.GetColor(readIndex) : m_ThemeInfo.legendUnableColor; + m_Legend.SetButton(legendName, btn, totalLegend); m_Legend.UpdateButtonColor(legendName, bgColor); btn.GetComponentInChildren().text = legendName; ChartHelper.ClearEventListener(btn.gameObject); @@ -224,12 +236,19 @@ namespace XCharts else { var btnList = m_Legend.buttonList.Values.ToArray(); - for (int n = 0; n < btnList.Length; n++) + if (btnList.Length == 1) { - temp = btnList[n].name.Split('_'); - selectedName = temp[2]; - var index = int.Parse(temp[1]); - OnLegendButtonClick(n, selectedName, index == clickedIndex ? true : false); + OnLegendButtonClick(0, selectedName, !IsActiveByLegend(selectedName)); + } + else + { + for (int n = 0; n < btnList.Length; n++) + { + temp = btnList[n].name.Split('_'); + selectedName = temp[2]; + var index = int.Parse(temp[1]); + OnLegendButtonClick(n, selectedName, index == clickedIndex ? true : false); + } } } }); @@ -252,9 +271,9 @@ namespace XCharts } if (m_Legend.selectedMode == Legend.SelectedMode.Single) { - for (int n = 0; n < datas.Count; n++) + for (int n = 0; n < m_LegendRealShowName.Count; n++) { - OnLegendButtonClick(n, datas[n], n == 0 ? true : false); + OnLegendButtonClick(n, m_LegendRealShowName[n], n == 0 ? true : false); } } } diff --git a/Scripts/UI/Internal/BaseChart_API.cs b/Scripts/UI/Internal/BaseChart_API.cs index 81514e5c..c84aa622 100644 --- a/Scripts/UI/Internal/BaseChart_API.cs +++ b/Scripts/UI/Internal/BaseChart_API.cs @@ -311,7 +311,8 @@ namespace XCharts var serie = m_Series.GetSerie(serieIndex); if (serie != null && !string.IsNullOrEmpty(serie.name)) { - var bgColor1 = active ? m_ThemeInfo.GetColor(serie.index) : m_ThemeInfo.legendUnableColor; + var legendIndex = m_LegendRealShowName.IndexOf(serie.name); + var bgColor1 = active ? m_ThemeInfo.GetColor(legendIndex) : m_ThemeInfo.legendUnableColor; m_Legend.UpdateButtonColor(serie.name, bgColor1); } } diff --git a/Scripts/UI/Internal/CoordinateChart.cs b/Scripts/UI/Internal/CoordinateChart.cs index 4d75f7bf..4f4598ac 100644 --- a/Scripts/UI/Internal/CoordinateChart.cs +++ b/Scripts/UI/Internal/CoordinateChart.cs @@ -22,12 +22,14 @@ namespace XCharts private bool m_DataZoomEndDrag; private float m_DataZoomLastStartIndex; private float m_DataZoomLastEndIndex; + private bool m_XAxisChanged; + private bool m_YAxisChanged; + private bool m_CheckMinMaxValue; private List m_CheckXAxises = new List(); private List m_CheckYAxises = new List(); private Grid m_CheckCoordinate = Grid.defaultGrid; - private bool m_XAxisChanged; - private bool m_YAxisChanged; - protected bool m_CheckMinMaxValue; + private Dictionary> m_StackSeries = new Dictionary>(); + private List m_SeriesCurrHig = new List(); protected override void Awake() { @@ -66,9 +68,58 @@ namespace XCharts { base.DrawChart(vh); DrawCoordinate(vh); + DrawSerie(vh); DrawDataZoom(vh); } + + protected virtual void DrawSerie(VertexHelper vh) + { + base.DrawChart(vh); + if (!m_CheckMinMaxValue) return; + bool yCategory = m_YAxises[0].IsCategory() || m_YAxises[1].IsCategory(); + m_Series.GetStackSeries(ref m_StackSeries); + int seriesCount = m_StackSeries.Count; + m_BarLastOffset = 0; + for (int j = 0; j < seriesCount; j++) + { + var serieList = m_StackSeries[j]; + m_SeriesCurrHig.Clear(); + for (int n = 0; n < serieList.Count; n++) + { + Serie serie = serieList[n]; + serie.dataPoints.Clear(); + var colorIndex = m_LegendRealShowName.IndexOf(serie.legendName); + switch (serie.type) + { + case SerieType.Line: + if (yCategory) DrawYLineSerie(vh, j, serie, colorIndex, ref m_SeriesCurrHig); + else DrawXLineSerie(vh, j, serie, colorIndex, ref m_SeriesCurrHig); + break; + case SerieType.Bar: + if (yCategory) DrawYBarSerie(vh, j, serie, colorIndex, ref m_SeriesCurrHig); + else DrawXBarSerie(vh, j, serie, colorIndex, ref m_SeriesCurrHig); + break; + case SerieType.Scatter: + case SerieType.EffectScatter: + DrawScatterSerie(vh, colorIndex, serie); + if (vh.currentVertCount > 60000) + { + m_Large++; + RefreshChart(); + return; + } + break; + } + } + } + DrawLabelBackground(vh); + DrawLinePoint(vh); + DrawLineArrow(vh); + if (yCategory) DrawYTooltipIndicator(vh); + else DrawXTooltipIndicator(vh); + } + protected override void CheckTootipArea(Vector2 local) { if (local.x < coordinateX - 1 || local.x > coordinateX + coordinateWid + 1 || @@ -692,17 +743,20 @@ namespace XCharts axis.maxValue = tempMaxValue; axis.zeroXOffset = 0; axis.zeroYOffset = 0; - if (axis is XAxis && axis.IsValue()) + if (tempMinValue != 0 || tempMaxValue != 0) { - axis.zeroXOffset = axis.minValue > 0 ? 0 : - axis.maxValue < 0 ? coordinateWid : - Mathf.Abs(axis.minValue) * (coordinateWid / (Mathf.Abs(axis.minValue) + Mathf.Abs(axis.maxValue))); - } - if (axis is YAxis && axis.IsValue()) - { - axis.zeroYOffset = axis.minValue > 0 ? 0 : - axis.maxValue < 0 ? coordinateHig : - Mathf.Abs(axis.minValue) * (coordinateHig / (Mathf.Abs(axis.minValue) + Mathf.Abs(axis.maxValue))); + if (axis is XAxis && axis.IsValue()) + { + axis.zeroXOffset = axis.minValue > 0 ? 0 : + axis.maxValue < 0 ? coordinateWid : + Mathf.Abs(axis.minValue) * (coordinateWid / (Mathf.Abs(axis.minValue) + Mathf.Abs(axis.maxValue))); + } + if (axis is YAxis && axis.IsValue()) + { + axis.zeroYOffset = axis.minValue > 0 ? 0 : + axis.maxValue < 0 ? coordinateHig : + Mathf.Abs(axis.minValue) * (coordinateHig / (Mathf.Abs(axis.minValue) + Mathf.Abs(axis.maxValue))); + } } float coordinateWidth = axis is XAxis ? coordinateWid : coordinateHig; axis.UpdateLabelText(coordinateWidth, m_DataZoom); @@ -772,6 +826,8 @@ namespace XCharts { var size = yAxis.GetScaleNumber(coordinateWid, m_DataZoom); var totalWidth = coordinateY; + var xAxis = m_XAxises[yAxisIndex]; + var zeroPos = new Vector3(coordinateX + xAxis.zeroXOffset, coordinateY); for (int i = 0; i < size; i++) { var scaleWidth = yAxis.GetScaleWidth(coordinateHig, i, m_DataZoom); @@ -807,8 +863,11 @@ namespace XCharts } if (yAxis.showSplitLine) { - DrawSplitLine(vh, yAxis, yAxis.splitLineType, new Vector3(coordinateX, pY), - new Vector3(coordinateX + coordinateWid, pY), m_ThemeInfo.axisSplitLineColor); + if (!xAxis.axisLine.show || zeroPos.y != pY) + { + DrawSplitLine(vh, yAxis, yAxis.splitLineType, new Vector3(coordinateX, pY), + new Vector3(coordinateX + coordinateWid, pY), m_ThemeInfo.axisSplitLineColor); + } } totalWidth += scaleWidth; } @@ -821,6 +880,8 @@ namespace XCharts { var size = xAxis.GetScaleNumber(coordinateWid, m_DataZoom); var totalWidth = coordinateX; + var yAxis = m_YAxises[xAxisIndex]; + var zeroPos = new Vector3(coordinateX, coordinateY + yAxis.zeroYOffset); for (int i = 0; i < size; i++) { var scaleWidth = xAxis.GetScaleWidth(coordinateWid, i, m_DataZoom); @@ -856,8 +917,11 @@ namespace XCharts } if (xAxis.showSplitLine) { - DrawSplitLine(vh, xAxis, xAxis.splitLineType, new Vector3(pX, coordinateY), - new Vector3(pX, coordinateY + coordinateHig), m_ThemeInfo.axisSplitLineColor); + if (!yAxis.axisLine.show || zeroPos.x != pX) + { + DrawSplitLine(vh, xAxis, xAxis.splitLineType, new Vector3(pX, coordinateY), + new Vector3(pX, coordinateY + coordinateHig), m_ThemeInfo.axisSplitLineColor); + } } totalWidth += scaleWidth; } diff --git a/Scripts/UI/Internal/CoordinateChart_DrawLine.cs b/Scripts/UI/Internal/CoordinateChart_DrawLine.cs index f24f4499..a0c4d6f0 100644 --- a/Scripts/UI/Internal/CoordinateChart_DrawLine.cs +++ b/Scripts/UI/Internal/CoordinateChart_DrawLine.cs @@ -67,14 +67,14 @@ namespace XCharts } } - protected void DrawXLineSerie(VertexHelper vh, int serieIndex, Serie serie, ref List seriesHig) + protected void DrawXLineSerie(VertexHelper vh, int serieIndex, Serie serie, int colorIndex, ref List seriesHig) { if (!IsActive(serie.index)) return; var showData = serie.GetDataList(m_DataZoom); if (showData.Count <= 0) return; - Color lineColor = serie.GetLineColor(m_ThemeInfo, serieIndex, false); - Color areaColor = serie.GetAreaColor(m_ThemeInfo, serieIndex, false); - Color areaToColor = serie.GetAreaToColor(m_ThemeInfo, serieIndex, false); + Color lineColor = serie.GetLineColor(m_ThemeInfo, colorIndex, false); + Color areaColor = serie.GetAreaColor(m_ThemeInfo, colorIndex, false); + Color areaToColor = serie.GetAreaToColor(m_ThemeInfo, colorIndex, false); Vector3 lp = Vector3.zero, np = Vector3.zero, llp = Vector3.zero, nnp = Vector3.zero; var yAxis = m_YAxises[serie.axisIndex]; var xAxis = m_XAxises[serie.axisIndex]; @@ -342,7 +342,7 @@ namespace XCharts return yDataHig; } - protected void DrawYLineSerie(VertexHelper vh, int serieIndex, Serie serie, ref List seriesHig) + protected void DrawYLineSerie(VertexHelper vh, int serieIndex, Serie serie, int colorIndex, ref List seriesHig) { if (!IsActive(serie.index)) return; var showData = serie.GetDataList(m_DataZoom); @@ -350,9 +350,9 @@ namespace XCharts Vector3 np = Vector3.zero; Vector3 llp = Vector3.zero; Vector3 nnp = Vector3.zero; - Color lineColor = serie.GetLineColor(m_ThemeInfo, serieIndex, false); - Color areaColor = serie.GetAreaColor(m_ThemeInfo, serieIndex, false); - Color areaToColor = serie.GetAreaToColor(m_ThemeInfo, serieIndex, false); + Color lineColor = serie.GetLineColor(m_ThemeInfo, colorIndex, false); + Color areaColor = serie.GetAreaColor(m_ThemeInfo, colorIndex, false); + Color areaToColor = serie.GetAreaToColor(m_ThemeInfo, colorIndex, false); var xAxis = m_XAxises[serie.axisIndex]; var yAxis = m_YAxises[serie.axisIndex]; var zeroPos = new Vector3(coordinateX + xAxis.zeroXOffset, coordinateY); diff --git a/Scripts/UI/LineChart.cs b/Scripts/UI/LineChart.cs index b266f055..9e030b44 100644 --- a/Scripts/UI/LineChart.cs +++ b/Scripts/UI/LineChart.cs @@ -27,66 +27,5 @@ namespace XCharts } } #endif - - protected override void DrawChart(VertexHelper vh) - { - base.DrawChart(vh); - if (!m_CheckMinMaxValue) return; - if (m_YAxises[0].type == Axis.AxisType.Category - || m_YAxises[1].type == Axis.AxisType.Category) - { - DrawLineChart(vh, true); - } - else - { - DrawLineChart(vh, false); - } - DrawLabelBackground(vh); - } - - private Dictionary> m_StackSeries = new Dictionary>(); - private List m_SeriesCurrHig = new List(); - private void DrawLineChart(VertexHelper vh, bool yCategory) - { - m_BarLastOffset = 0; - m_Series.GetStackSeries(ref m_StackSeries); - int seriesCount = m_StackSeries.Count; - int serieCount = 0; - for (int j = 0; j < seriesCount; j++) - { - var serieList = m_StackSeries[j]; - if (serieList.Count <= 0) continue; - m_SeriesCurrHig.Clear(); - if (m_SeriesCurrHig.Capacity != serieList[0].dataCount) - { - m_SeriesCurrHig.Capacity = serieList[0].dataCount; - } - for (int n = 0; n < serieList.Count; n++) - { - Serie serie = serieList[n]; - serie.dataPoints.Clear(); - switch (serie.type) - { - case SerieType.Line: - if (yCategory) DrawYLineSerie(vh, serieCount, serie, ref m_SeriesCurrHig); - else DrawXLineSerie(vh, serieCount, serie, ref m_SeriesCurrHig); - break; - case SerieType.Bar: - if (yCategory) DrawYBarSerie(vh, serieCount, serie, serieCount, ref m_SeriesCurrHig); - else DrawXBarSerie(vh, serieCount, serie, serieCount, ref m_SeriesCurrHig); - break; - } - - if (serie.show) - { - serieCount++; - } - } - } - DrawLinePoint(vh); - DrawLineArrow(vh); - if (yCategory) DrawYTooltipIndicator(vh); - else DrawXTooltipIndicator(vh); - } } } diff --git a/Scripts/UI/ScatterChart.cs b/Scripts/UI/ScatterChart.cs index c7dd53b7..0db1a40b 100644 --- a/Scripts/UI/ScatterChart.cs +++ b/Scripts/UI/ScatterChart.cs @@ -57,57 +57,5 @@ namespace XCharts RefreshChart(); } } - - protected HashSet m_SerieNameSet = new HashSet(); - protected Dictionary> m_StackSeries = new Dictionary>(); - protected List m_SeriesCurrHig = new List(); - protected override void DrawChart(VertexHelper vh) - { - base.DrawChart(vh); - if (!m_CheckMinMaxValue) return; - bool yCategory = m_YAxises[0].IsCategory() || m_YAxises[1].IsCategory(); - m_Series.GetStackSeries(ref m_StackSeries); - int seriesCount = m_StackSeries.Count; - int serieNameCount = -1; - m_SerieNameSet.Clear(); - m_BarLastOffset = 0; - for (int j = 0; j < seriesCount; j++) - { - var serieList = m_StackSeries[j]; - m_SeriesCurrHig.Clear(); - for (int n = 0; n < serieList.Count; n++) - { - Serie serie = serieList[n]; - serie.dataPoints.Clear(); - if (string.IsNullOrEmpty(serie.name)) serieNameCount++; - else if (!m_SerieNameSet.Contains(serie.name)) - { - m_SerieNameSet.Add(serie.name); - serieNameCount++; - } - switch (serie.type) - { - case SerieType.Line: - if (yCategory) DrawYLineSerie(vh, j, serie, ref m_SeriesCurrHig); - else DrawXLineSerie(vh, j, serie, ref m_SeriesCurrHig); - break; - case SerieType.Scatter: - case SerieType.EffectScatter: - DrawScatterSerie(vh, serieNameCount, serie); - if (vh.currentVertCount > 60000) - { - m_Large++; - RefreshChart(); - return; - } - break; - } - } - } - DrawLinePoint(vh); - DrawLineArrow(vh); - if (yCategory) DrawYTooltipIndicator(vh); - else DrawXTooltipIndicator(vh); - } } }