2021-11-23 13:20:07 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2022-02-19 22:37:57 +08:00
|
|
|
namespace XCharts.Runtime
|
2021-11-23 13:20:07 +08:00
|
|
|
{
|
2024-04-22 22:25:12 +08:00
|
|
|
public static class DataHelper
|
2021-11-23 13:20:07 +08:00
|
|
|
{
|
2026-05-20 22:13:04 +08:00
|
|
|
private static List<double> s_SampleSumPrefix = new List<double>();
|
|
|
|
|
|
|
|
|
|
public static bool IsAnyDataChanged(ref List<SerieData> showData, int minCount, int maxCount)
|
|
|
|
|
{
|
|
|
|
|
for (int i = minCount; i < maxCount; i++)
|
|
|
|
|
{
|
|
|
|
|
if (showData[i].IsDataChanged())
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<double> BuildSampleSumPrefix(ref List<SerieData> showData, int maxCount, bool inverse)
|
|
|
|
|
{
|
|
|
|
|
if (maxCount < 0) maxCount = 0;
|
|
|
|
|
var targetCount = maxCount + 1;
|
|
|
|
|
if (s_SampleSumPrefix.Count != targetCount)
|
|
|
|
|
{
|
|
|
|
|
s_SampleSumPrefix.Clear();
|
|
|
|
|
for (int i = 0; i < targetCount; i++)
|
|
|
|
|
s_SampleSumPrefix.Add(0);
|
|
|
|
|
}
|
|
|
|
|
s_SampleSumPrefix[0] = 0;
|
|
|
|
|
for (int i = 0; i < maxCount; i++)
|
|
|
|
|
{
|
|
|
|
|
s_SampleSumPrefix[i + 1] = s_SampleSumPrefix[i] + showData[i].GetData(1, inverse);
|
|
|
|
|
}
|
|
|
|
|
return s_SampleSumPrefix;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 08:31:32 +08:00
|
|
|
public static double DataAverage(ref List<SerieData> showData, SampleType sampleType,
|
|
|
|
|
int minCount, int maxCount, int rate)
|
2021-11-23 13:20:07 +08:00
|
|
|
{
|
|
|
|
|
double totalAverage = 0;
|
|
|
|
|
if (rate > 1 && sampleType == SampleType.Peak)
|
|
|
|
|
{
|
|
|
|
|
double total = 0;
|
|
|
|
|
for (int i = minCount; i < maxCount; i++)
|
|
|
|
|
{
|
|
|
|
|
total += showData[i].data[1];
|
|
|
|
|
}
|
|
|
|
|
totalAverage = total / (maxCount - minCount);
|
|
|
|
|
}
|
|
|
|
|
return totalAverage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static double SampleValue(ref List<SerieData> showData, SampleType sampleType, int rate,
|
2023-07-04 13:07:44 +08:00
|
|
|
int minCount, int maxCount, double totalAverage, int index, float dataAddDuration, float dataChangeDuration,
|
2026-05-20 22:13:04 +08:00
|
|
|
ref bool dataChanging, Axis axis, bool unscaledTime, bool useCurrentData = true,
|
|
|
|
|
bool checkDataChanging = true, List<double> sampleSumPrefix = null)
|
2021-11-23 13:20:07 +08:00
|
|
|
{
|
|
|
|
|
var inverse = axis.inverse;
|
2023-11-24 08:29:55 +08:00
|
|
|
var minValue = 0;
|
|
|
|
|
var maxValue = 0;
|
2026-05-20 22:13:04 +08:00
|
|
|
if (!useCurrentData)
|
|
|
|
|
{
|
|
|
|
|
if (rate <= 1 || index == minCount)
|
|
|
|
|
return showData[index].GetData(1, inverse);
|
|
|
|
|
|
|
|
|
|
switch (sampleType)
|
|
|
|
|
{
|
|
|
|
|
case SampleType.Sum:
|
|
|
|
|
case SampleType.Average:
|
|
|
|
|
if (sampleSumPrefix != null)
|
|
|
|
|
{
|
|
|
|
|
var totalByPrefix = sampleSumPrefix[index + 1] - sampleSumPrefix[index - rate + 1];
|
|
|
|
|
if (sampleType == SampleType.Average)
|
|
|
|
|
return totalByPrefix / rate;
|
|
|
|
|
else
|
|
|
|
|
return totalByPrefix;
|
|
|
|
|
}
|
|
|
|
|
double total = 0;
|
|
|
|
|
for (int i = index; i > index - rate; i--)
|
|
|
|
|
{
|
|
|
|
|
total += showData[i].GetData(1, inverse);
|
|
|
|
|
}
|
|
|
|
|
if (sampleType == SampleType.Average)
|
|
|
|
|
return total / rate;
|
|
|
|
|
else
|
|
|
|
|
return total;
|
|
|
|
|
|
|
|
|
|
case SampleType.Max:
|
|
|
|
|
double max = double.MinValue;
|
|
|
|
|
for (int i = index; i > index - rate; i--)
|
|
|
|
|
{
|
|
|
|
|
var value = showData[i].GetData(1, inverse);
|
|
|
|
|
if (value > max)
|
|
|
|
|
max = value;
|
|
|
|
|
}
|
|
|
|
|
return max;
|
|
|
|
|
|
|
|
|
|
case SampleType.Min:
|
|
|
|
|
double min = double.MaxValue;
|
|
|
|
|
for (int i = index; i > index - rate; i--)
|
|
|
|
|
{
|
|
|
|
|
var value = showData[i].GetData(1, inverse);
|
|
|
|
|
if (value < min)
|
|
|
|
|
min = value;
|
|
|
|
|
}
|
|
|
|
|
return min;
|
|
|
|
|
|
|
|
|
|
case SampleType.Peak:
|
|
|
|
|
max = double.MinValue;
|
|
|
|
|
min = double.MaxValue;
|
|
|
|
|
total = 0;
|
|
|
|
|
for (int i = index; i > index - rate; i--)
|
|
|
|
|
{
|
|
|
|
|
var value = showData[i].GetData(1, inverse);
|
|
|
|
|
total += value;
|
|
|
|
|
if (value < min)
|
|
|
|
|
min = value;
|
|
|
|
|
if (value > max)
|
|
|
|
|
max = value;
|
|
|
|
|
}
|
|
|
|
|
var average = total / rate;
|
|
|
|
|
if (average >= totalAverage)
|
|
|
|
|
return max;
|
|
|
|
|
else
|
|
|
|
|
return min;
|
|
|
|
|
}
|
|
|
|
|
return showData[index].GetData(1, inverse);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 13:20:07 +08:00
|
|
|
if (rate <= 1 || index == minCount)
|
|
|
|
|
{
|
2026-05-20 22:13:04 +08:00
|
|
|
if (checkDataChanging && showData[index].IsDataChanged())
|
2021-12-08 08:31:32 +08:00
|
|
|
dataChanging = true;
|
|
|
|
|
|
2023-07-04 13:07:44 +08:00
|
|
|
return showData[index].GetCurrData(1, dataAddDuration, dataChangeDuration, inverse, minValue, maxValue, unscaledTime);
|
2021-11-23 13:20:07 +08:00
|
|
|
}
|
|
|
|
|
switch (sampleType)
|
|
|
|
|
{
|
|
|
|
|
case SampleType.Sum:
|
|
|
|
|
case SampleType.Average:
|
|
|
|
|
double total = 0;
|
|
|
|
|
var count = 0;
|
|
|
|
|
for (int i = index; i > index - rate; i--)
|
|
|
|
|
{
|
2021-12-08 08:31:32 +08:00
|
|
|
count++;
|
2023-07-04 13:07:44 +08:00
|
|
|
total += showData[i].GetCurrData(1, dataAddDuration, dataChangeDuration, inverse, minValue, maxValue, unscaledTime);
|
2026-05-20 22:13:04 +08:00
|
|
|
if (checkDataChanging && showData[i].IsDataChanged())
|
2021-12-08 08:31:32 +08:00
|
|
|
dataChanging = true;
|
2021-11-23 13:20:07 +08:00
|
|
|
}
|
2021-12-08 08:31:32 +08:00
|
|
|
if (sampleType == SampleType.Average)
|
|
|
|
|
return total / rate;
|
|
|
|
|
else
|
|
|
|
|
return total;
|
|
|
|
|
|
2021-11-23 13:20:07 +08:00
|
|
|
case SampleType.Max:
|
|
|
|
|
double max = double.MinValue;
|
|
|
|
|
for (int i = index; i > index - rate; i--)
|
|
|
|
|
{
|
2023-07-04 13:07:44 +08:00
|
|
|
var value = showData[i].GetCurrData(1, dataAddDuration, dataChangeDuration, inverse, minValue, maxValue, unscaledTime);
|
2021-12-08 08:31:32 +08:00
|
|
|
if (value > max)
|
|
|
|
|
max = value;
|
|
|
|
|
|
2026-05-20 22:13:04 +08:00
|
|
|
if (checkDataChanging && showData[i].IsDataChanged())
|
2021-12-08 08:31:32 +08:00
|
|
|
dataChanging = true;
|
2021-11-23 13:20:07 +08:00
|
|
|
}
|
|
|
|
|
return max;
|
2021-12-08 08:31:32 +08:00
|
|
|
|
2021-11-23 13:20:07 +08:00
|
|
|
case SampleType.Min:
|
|
|
|
|
double min = double.MaxValue;
|
|
|
|
|
for (int i = index; i > index - rate; i--)
|
|
|
|
|
{
|
2023-07-04 13:07:44 +08:00
|
|
|
var value = showData[i].GetCurrData(1, dataAddDuration, dataChangeDuration, inverse, minValue, maxValue, unscaledTime);
|
2021-12-08 08:31:32 +08:00
|
|
|
if (value < min)
|
|
|
|
|
min = value;
|
|
|
|
|
|
2026-05-20 22:13:04 +08:00
|
|
|
if (checkDataChanging && showData[i].IsDataChanged())
|
2021-12-08 08:31:32 +08:00
|
|
|
dataChanging = true;
|
2021-11-23 13:20:07 +08:00
|
|
|
}
|
|
|
|
|
return min;
|
2021-12-08 08:31:32 +08:00
|
|
|
|
2021-11-23 13:20:07 +08:00
|
|
|
case SampleType.Peak:
|
|
|
|
|
max = double.MinValue;
|
|
|
|
|
min = double.MaxValue;
|
|
|
|
|
total = 0;
|
|
|
|
|
for (int i = index; i > index - rate; i--)
|
|
|
|
|
{
|
2023-07-04 13:07:44 +08:00
|
|
|
var value = showData[i].GetCurrData(1, dataAddDuration, dataChangeDuration, inverse, minValue, maxValue, unscaledTime);
|
2021-11-23 13:20:07 +08:00
|
|
|
total += value;
|
2021-12-08 08:31:32 +08:00
|
|
|
if (value < min)
|
|
|
|
|
min = value;
|
|
|
|
|
if (value > max)
|
|
|
|
|
max = value;
|
|
|
|
|
|
2026-05-20 22:13:04 +08:00
|
|
|
if (checkDataChanging && showData[i].IsDataChanged())
|
2021-12-08 08:31:32 +08:00
|
|
|
dataChanging = true;
|
2021-11-23 13:20:07 +08:00
|
|
|
}
|
|
|
|
|
var average = total / rate;
|
2021-12-08 08:31:32 +08:00
|
|
|
if (average >= totalAverage)
|
|
|
|
|
return max;
|
|
|
|
|
else
|
|
|
|
|
return min;
|
2021-11-23 13:20:07 +08:00
|
|
|
}
|
2026-05-20 22:13:04 +08:00
|
|
|
if (checkDataChanging && showData[index].IsDataChanged())
|
2021-12-08 08:31:32 +08:00
|
|
|
dataChanging = true;
|
|
|
|
|
|
2023-07-04 13:07:44 +08:00
|
|
|
return showData[index].GetCurrData(1, dataAddDuration, dataChangeDuration, inverse, minValue, maxValue, unscaledTime);
|
2021-11-23 13:20:07 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|