修复Legend控制的Serie颜色有时候异常的问题

This commit is contained in:
monitor1394
2019-10-03 09:42:03 +08:00
parent 450e091095
commit c148c8ccd4
9 changed files with 163 additions and 217 deletions

View File

@@ -26,53 +26,5 @@ namespace XCharts
}
}
#endif
protected HashSet<string> m_SerieNameSet = new HashSet<string>();
protected Dictionary<int, List<Serie>> m_StackSeries = new Dictionary<int, List<Serie>>();
protected List<float> m_SeriesCurrHig = new List<float>();
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);
}
}
}

View File

@@ -209,6 +209,10 @@ namespace XCharts
/// </summary>
public string name { get { return m_Name; } set { m_Name = value; } }
/// <summary>
/// 图例名称。当系列名称不为空时图例名称即为系列名称反之则为索引index。
/// </summary>
public string legendName { get { return string.IsNullOrEmpty(name) ? ChartCached.IntToStr(index) : name; } }
/// <summary>
/// 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值后后一个系列的值会在前一个系列的值上相加。
/// </summary>

View File

@@ -793,18 +793,27 @@ namespace XCharts
public List<string> 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;
}
/// <summary>
/// 从json中解析数据
/// </summary>

View File

@@ -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<string> m_LegendRealShowName = new List<string>();
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<string> datas;
if (m_Legend.data.Count > 0)
if (m_Legend.show && m_Legend.data.Count > 0)
{
datas = new List<string>();
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>().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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -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<XAxis> m_CheckXAxises = new List<XAxis>();
private List<YAxis> m_CheckYAxises = new List<YAxis>();
private Grid m_CheckCoordinate = Grid.defaultGrid;
private bool m_XAxisChanged;
private bool m_YAxisChanged;
protected bool m_CheckMinMaxValue;
private Dictionary<int, List<Serie>> m_StackSeries = new Dictionary<int, List<Serie>>();
private List<float> m_SeriesCurrHig = new List<float>();
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;
}

View File

@@ -67,14 +67,14 @@ namespace XCharts
}
}
protected void DrawXLineSerie(VertexHelper vh, int serieIndex, Serie serie, ref List<float> seriesHig)
protected void DrawXLineSerie(VertexHelper vh, int serieIndex, Serie serie, int colorIndex, ref List<float> 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<float> seriesHig)
protected void DrawYLineSerie(VertexHelper vh, int serieIndex, Serie serie, int colorIndex, ref List<float> 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);

View File

@@ -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<int, List<Serie>> m_StackSeries = new Dictionary<int, List<Serie>>();
private List<float> m_SeriesCurrHig = new List<float>();
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);
}
}
}

View File

@@ -57,57 +57,5 @@ namespace XCharts
RefreshChart();
}
}
protected HashSet<string> m_SerieNameSet = new HashSet<string>();
protected Dictionary<int, List<Serie>> m_StackSeries = new Dictionary<int, List<Serie>>();
protected List<float> m_SeriesCurrHig = new List<float>();
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);
}
}
}