Files
XCharts/Runtime/Internal/Utilities/DataHelper.cs

110 lines
4.4 KiB
C#
Raw Normal View History

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
{
internal static class DataHelper
{
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,
ref bool dataChanging, Axis axis, bool unscaledTime)
2021-11-23 13:20:07 +08:00
{
var inverse = axis.inverse;
2021-12-08 08:31:32 +08:00
var minValue = axis.context.minValue;
var maxValue = axis.context.maxValue;
2021-11-23 13:20:07 +08:00
if (rate <= 1 || index == minCount)
{
2021-12-08 08:31:32 +08:00
if (showData[index].IsDataChanged())
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);
2021-12-08 08:31:32 +08:00
if (showData[i].IsDataChanged())
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;
if (showData[i].IsDataChanged())
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;
if (showData[i].IsDataChanged())
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;
if (showData[i].IsDataChanged())
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
}
2021-12-08 08:31:32 +08:00
if (showData[index].IsDataChanged())
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
}
}
}