mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-28 12:08:46 +00:00
优化图表性能
This commit is contained in:
@@ -5,6 +5,36 @@ namespace XCharts.Runtime
|
||||
{
|
||||
public static class DataHelper
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public static double DataAverage(ref List<SerieData> showData, SampleType sampleType,
|
||||
int minCount, int maxCount, int rate)
|
||||
{
|
||||
@@ -23,14 +53,84 @@ namespace XCharts.Runtime
|
||||
|
||||
public static double SampleValue(ref List<SerieData> showData, SampleType sampleType, int rate,
|
||||
int minCount, int maxCount, double totalAverage, int index, float dataAddDuration, float dataChangeDuration,
|
||||
ref bool dataChanging, Axis axis, bool unscaledTime)
|
||||
ref bool dataChanging, Axis axis, bool unscaledTime, bool useCurrentData = true,
|
||||
bool checkDataChanging = true, List<double> sampleSumPrefix = null)
|
||||
{
|
||||
var inverse = axis.inverse;
|
||||
var minValue = 0;
|
||||
var maxValue = 0;
|
||||
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);
|
||||
}
|
||||
|
||||
if (rate <= 1 || index == minCount)
|
||||
{
|
||||
if (showData[index].IsDataChanged())
|
||||
if (checkDataChanging && showData[index].IsDataChanged())
|
||||
dataChanging = true;
|
||||
|
||||
return showData[index].GetCurrData(1, dataAddDuration, dataChangeDuration, inverse, minValue, maxValue, unscaledTime);
|
||||
@@ -45,7 +145,7 @@ namespace XCharts.Runtime
|
||||
{
|
||||
count++;
|
||||
total += showData[i].GetCurrData(1, dataAddDuration, dataChangeDuration, inverse, minValue, maxValue, unscaledTime);
|
||||
if (showData[i].IsDataChanged())
|
||||
if (checkDataChanging && showData[i].IsDataChanged())
|
||||
dataChanging = true;
|
||||
}
|
||||
if (sampleType == SampleType.Average)
|
||||
@@ -61,7 +161,7 @@ namespace XCharts.Runtime
|
||||
if (value > max)
|
||||
max = value;
|
||||
|
||||
if (showData[i].IsDataChanged())
|
||||
if (checkDataChanging && showData[i].IsDataChanged())
|
||||
dataChanging = true;
|
||||
}
|
||||
return max;
|
||||
@@ -74,7 +174,7 @@ namespace XCharts.Runtime
|
||||
if (value < min)
|
||||
min = value;
|
||||
|
||||
if (showData[i].IsDataChanged())
|
||||
if (checkDataChanging && showData[i].IsDataChanged())
|
||||
dataChanging = true;
|
||||
}
|
||||
return min;
|
||||
@@ -92,7 +192,7 @@ namespace XCharts.Runtime
|
||||
if (value > max)
|
||||
max = value;
|
||||
|
||||
if (showData[i].IsDataChanged())
|
||||
if (checkDataChanging && showData[i].IsDataChanged())
|
||||
dataChanging = true;
|
||||
}
|
||||
var average = total / rate;
|
||||
@@ -101,7 +201,7 @@ namespace XCharts.Runtime
|
||||
else
|
||||
return min;
|
||||
}
|
||||
if (showData[index].IsDataChanged())
|
||||
if (checkDataChanging && showData[index].IsDataChanged())
|
||||
dataChanging = true;
|
||||
|
||||
return showData[index].GetCurrData(1, dataAddDuration, dataChangeDuration, inverse, minValue, maxValue, unscaledTime);
|
||||
|
||||
Reference in New Issue
Block a user