123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
-
- namespace XCharts.Runtime
- {
- public static class SeriesHelper
- {
-
- public static bool IsLegalLegendName(string name)
- {
- int numName = -1;
- if (int.TryParse(name, out numName))
- {
- if (numName >= 0 && numName < 100) return false;
- }
- return true;
- }
-
- public static List<string> GetLegalSerieNameList(List<Serie> series)
- {
- var list = new List<string>();
- for (int n = 0; n < series.Count; n++)
- {
- var serie = series[n];
- if (serie.placeHolder) continue;
- if (serie.colorByData)
- {
- for (int i = 0; i < serie.data.Count; i++)
- {
- var dataName = serie.data[i].name;
- if (!string.IsNullOrEmpty(dataName) && IsLegalLegendName(dataName) && !list.Contains(dataName))
- list.Add(dataName);
- }
- }
- else
- {
- if (!string.IsNullOrEmpty(serie.serieName) && !list.Contains(serie.serieName) && IsLegalLegendName(serie.serieName))
- list.Add(serie.serieName);
- }
- }
- return list;
- }
-
- /// <summary>
- /// 获得所有系列名,不包含空名字。
- /// </summary>
- /// <returns></returns>
- public static void UpdateSerieNameList(BaseChart chart, ref List<string> serieNameList)
- {
- serieNameList.Clear();
- for (int n = 0; n < chart.series.Count; n++)
- {
- var serie = chart.series[n];
- if (serie.placeHolder) continue;
- if (serie.colorByData)
- {
- for (int i = 0; i < serie.data.Count; i++)
- {
- var serieData = serie.data[i];
- if (serie is Pie && serie.IsIgnoreValue(serieData)) continue;
- if (string.IsNullOrEmpty(serieData.name))
- serieNameList.Add(ChartCached.IntToStr(i));
- else if (!serieNameList.Contains(serieData.name))
- serieNameList.Add(serieData.name);
- }
- }
- else
- {
- if (string.IsNullOrEmpty(serie.serieName))
- serieNameList.Add(ChartCached.IntToStr(n));
- else if (!serieNameList.Contains(serie.serieName))
- serieNameList.Add(serie.serieName);
- }
- }
- }
-
- public static Color GetNameColor(BaseChart chart, int index, string name)
- {
- Serie destSerie = null;
- SerieData destSerieData = null;
- var series = chart.series;
- for (int n = 0; n < series.Count; n++)
- {
- var serie = series[n];
- if (serie.placeHolder) continue;
- if (serie.colorByData)
- {
- bool found = false;
- for (int i = 0; i < serie.data.Count; i++)
- {
- if (name.Equals(serie.data[i].name))
- {
- destSerie = serie;
- destSerieData = serie.data[i];
- found = true;
- break;
- }
- }
- if (found) break;
- }
- if (name.Equals(serie.serieName))
- {
- destSerie = serie;
- destSerieData = null;
- break;
- }
- }
- var itemStyle = SerieHelper.GetItemStyle(destSerie, destSerieData, SerieState.Normal);
- if (ChartHelper.IsClearColor(itemStyle.markColor))
- {
- Color32 color, toColor;
- SerieHelper.GetItemColor(out color, out toColor, destSerie, destSerieData, chart.theme, index, SerieState.Normal);
- return color;
- }
- else
- {
- return itemStyle.markColor;
- }
- }
-
- /// <summary>
- /// 是否有需裁剪的serie。
- /// </summary>
- /// <returns></returns>
- public static bool IsAnyClipSerie(List<Serie> series)
- {
- foreach (var serie in series)
- {
- if (serie.clip) return true;
- }
- return false;
- }
-
- /// <summary>
- /// 获得上一个同堆叠且显示的serie。
- /// </summary>
- /// <param name="serie"></param>
- /// <returns></returns>
- public static Serie GetLastStackSerie(List<Serie> series, Serie serie)
- {
- if (serie == null || string.IsNullOrEmpty(serie.stack)) return null;
- for (int i = serie.index - 1; i >= 0; i--)
- {
- var temp = series[i];
- if (temp.show && serie.stack.Equals(temp.stack)) return temp;
- }
- return null;
- }
-
- private static HashSet<string> _setForStack = new HashSet<string>();
- /// <summary>
- /// 是否由数据堆叠
- /// </summary>
- /// <returns></returns>
- public static bool IsStack(List<Serie> series)
- {
- _setForStack.Clear();
- foreach (var serie in series)
- {
- if (string.IsNullOrEmpty(serie.stack)) continue;
- if (_setForStack.Contains(serie.stack)) return true;
- _setForStack.Add(serie.stack);
- }
- return false;
- }
-
- /// <summary>
- /// 是否堆叠
- /// </summary>
- /// <param name="stackName"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public static bool IsStack<T>(List<Serie> series, string stackName) where T : Serie
- {
- if (string.IsNullOrEmpty(stackName)) return false;
- int count = 0;
- foreach (var serie in series)
- {
- if (serie.show && serie is T)
- {
- if (stackName.Equals(serie.stack)) count++;
- if (count >= 2) return true;
- }
- }
- return false;
- }
-
- /// <summary>
- /// 是否时百分比堆叠
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static bool IsPercentStack<T>(List<Serie> series) where T : Serie
- {
- int count = 0;
- bool isPercentStack = false;
- foreach (var serie in series)
- {
- if (serie.show && serie is T)
- {
- if (!string.IsNullOrEmpty(serie.stack))
- {
- count++;
- if (serie.barPercentStack) isPercentStack = true;
- }
- if (count >= 2 && isPercentStack) return true;
- }
- }
- return false;
- }
-
- /// <summary>
- /// 是否时百分比堆叠
- /// </summary>
- /// <param name="stackName"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public static bool IsPercentStack<T>(List<Serie> series, string stackName) where T : Serie
- {
- if (string.IsNullOrEmpty(stackName)) return false;
- int count = 0;
- bool isPercentStack = false;
- foreach (var serie in series)
- {
- if (serie.show && serie is T)
- {
- if (stackName.Equals(serie.stack))
- {
- count++;
- if (serie.barPercentStack) isPercentStack = true;
- }
- if (count >= 2 && isPercentStack) return true;
- }
- }
- return false;
- }
-
- private static Dictionary<string, int> sets = new Dictionary<string, int>();
- /// <summary>
- /// 获得堆叠系列列表
- /// </summary>
- /// <param name="Dictionary<int"></param>
- /// <param name="stackSeries"></param>
- public static void GetStackSeries(List<Serie> series, ref Dictionary<int, List<Serie>> stackSeries)
- {
- int count = 0;
- var serieCount = series.Count;
- sets.Clear();
- if (stackSeries == null)
- {
- stackSeries = new Dictionary<int, List<Serie>>(serieCount);
- }
- else
- {
- foreach (var kv in stackSeries)
- {
- kv.Value.Clear();
- }
- }
- for (int i = 0; i < serieCount; i++)
- {
- var serie = series[i];
- serie.index = i;
- if (string.IsNullOrEmpty(serie.stack))
- {
- if (!stackSeries.ContainsKey(count))
- stackSeries[count] = new List<Serie>(serieCount);
- stackSeries[count].Add(serie);
- count++;
- }
- else
- {
- if (!sets.ContainsKey(serie.stack))
- {
- sets.Add(serie.stack, count);
- if (!stackSeries.ContainsKey(count))
- stackSeries[count] = new List<Serie>(serieCount);
- stackSeries[count].Add(serie);
- count++;
- }
- else
- {
- int stackIndex = sets[serie.stack];
- stackSeries[stackIndex].Add(serie);
- }
- }
- }
- }
-
- public static void UpdateStackDataList(List<Serie> series, Serie currSerie, DataZoom dataZoom, List<List<SerieData>> dataList)
- {
- dataList.Clear();
- for (int i = 0; i <= currSerie.index; i++)
- {
- var serie = series[i];
- if (serie.GetType() == currSerie.GetType() && ChartHelper.IsValueEqualsString(serie.stack, currSerie.stack))
- {
- dataList.Add(serie.GetDataList(dataZoom));
- }
- }
- }
-
- /// <summary>
- /// 获得维度X的最大最小值
- /// </summary>
- /// <param name="dataZoom"></param>
- /// <param name="axisIndex"></param>
- /// <param name="minVaule"></param>
- /// <param name="maxValue"></param>
- public static void GetXMinMaxValue(BaseChart chart, int axisIndex, bool isValueAxis,
- bool inverse, out double minVaule, out double maxValue, bool isPolar = false, bool filterByDataZoom = true)
- {
- GetMinMaxValue(chart, axisIndex, isValueAxis, inverse, false, out minVaule, out maxValue, isPolar, filterByDataZoom);
- }
-
- /// <summary>
- /// 获得维度Y的最大最小值
- /// </summary>
- /// <param name="dataZoom"></param>
- /// <param name="axisIndex"></param>
- /// <param name="minVaule"></param>
- /// <param name="maxValue"></param>
- public static void GetYMinMaxValue(BaseChart chart, int axisIndex, bool isValueAxis,
- bool inverse, out double minVaule, out double maxValue, bool isPolar = false, bool filterByDataZoom = true)
- {
- GetMinMaxValue(chart, axisIndex, isValueAxis, inverse, true, out minVaule, out maxValue, isPolar, filterByDataZoom);
- }
-
- private static Dictionary<int, List<Serie>> _stackSeriesForMinMax = new Dictionary<int, List<Serie>>();
- private static Dictionary<int, double> _serieTotalValueForMinMax = new Dictionary<int, double>();
- private static DataZoom xDataZoom, yDataZoom;
- public static void GetMinMaxValue(BaseChart chart, int axisIndex, bool isValueAxis,
- bool inverse, bool yValue, out double minVaule, out double maxValue, bool isPolar = false,
- bool filterByDataZoom = true)
- {
- double min = double.MaxValue;
- double max = double.MinValue;
- var series = chart.series;
- var isPercentStack = SeriesHelper.IsPercentStack<Bar>(series);
- if (!SeriesHelper.IsStack(series))
- {
- for (int i = 0; i < series.Count; i++)
- {
- var serie = series[i];
- if ((isPolar && serie.polarIndex != axisIndex) ||
- (!isPolar && serie.yAxisIndex != axisIndex) ||
- !serie.show) continue;
- var updateDuration = serie.animation.enable ? serie.animation.dataChangeDuration : 0;
- var unscaledTime = serie.animation.unscaledTime;
- if (isPercentStack && SeriesHelper.IsPercentStack<Bar>(series, serie.serieName))
- {
- if (100 > max) max = 100;
- if (0 < min) min = 0;
- }
- else
- {
- var showData = serie.GetDataList(filterByDataZoom ? chart.GetXDataZoomOfSerie(serie) : null);
- if (serie is Candlestick || serie is SimplifiedCandlestick)
- {
- foreach (var data in showData)
- {
- double dataMin, dataMax;
- data.GetMinMaxData(1, inverse, out dataMin, out dataMax);
- if (dataMax > max) max = dataMax;
- if (dataMin < min) min = dataMin;
- }
- }
- else
- {
- var performanceMode = serie.IsPerformanceMode();
- foreach (var data in showData)
- {
- var currData = performanceMode ? data.GetData(yValue ? 1 : 0, inverse) :
- data.GetCurrData(yValue ? 1 : 0, updateDuration, unscaledTime, inverse);
- if (!serie.IsIgnoreValue(data, currData))
- {
- if (currData > max) max = currData;
- if (currData < min) min = currData;
- }
- }
- }
- }
- }
- }
- else
- {
- SeriesHelper.GetStackSeries(series, ref _stackSeriesForMinMax);
- foreach (var ss in _stackSeriesForMinMax)
- {
- _serieTotalValueForMinMax.Clear();
- for (int i = 0; i < ss.Value.Count; i++)
- {
- var serie = ss.Value[i];
- if ((isPolar && serie.polarIndex != axisIndex) ||
- (!isPolar && serie.yAxisIndex != axisIndex) ||
- !serie.show) continue;
- var showData = serie.GetDataList(filterByDataZoom ? chart.GetXDataZoomOfSerie(serie) : null);
- if (SeriesHelper.IsPercentStack<Bar>(series, serie.stack))
- {
- for (int j = 0; j < showData.Count; j++)
- {
- _serieTotalValueForMinMax[j] = 100;
- }
- }
- else
- {
- for (int j = 0; j < showData.Count; j++)
- {
- if (!_serieTotalValueForMinMax.ContainsKey(j))
- _serieTotalValueForMinMax[j] = 0;
- double currData = 0;
- if (serie is Candlestick)
- {
- currData = showData[j].GetMaxData(false);
- }
- else
- {
- currData = yValue ? showData[j].GetData(1) : showData[j].GetData(0);
- }
- if (inverse) currData = -currData;
- if (!serie.IsIgnoreValue(showData[j], currData))
- _serieTotalValueForMinMax[j] = _serieTotalValueForMinMax[j] + currData;
- }
- }
- }
- double tmax = double.MinValue;
- double tmin = double.MaxValue;
- foreach (var tt in _serieTotalValueForMinMax)
- {
- if (tt.Value > tmax) tmax = tt.Value;
- if (tt.Value < tmin) tmin = tt.Value;
- }
- if (tmax > max) max = tmax;
- if (tmin < min) min = tmin;
- }
- }
- if (max == double.MinValue && min == double.MaxValue)
- {
- minVaule = 0;
- maxValue = 0;
- }
- else
- {
- minVaule = min;
- maxValue = max;
- }
- }
-
- public static int GetMaxSerieDataCount(List<Serie> series)
- {
- int max = 0;
- foreach (var serie in series)
- {
- if (serie.dataCount > max) max = serie.dataCount;
- }
- return max;
- }
- }
- }
|