diff --git a/maui/src/Charts/Series/BoxAndWhiskerSeries.cs b/maui/src/Charts/Series/BoxAndWhiskerSeries.cs index de7c059..afcb25a 100644 --- a/maui/src/Charts/Series/BoxAndWhiskerSeries.cs +++ b/maui/src/Charts/Series/BoxAndWhiskerSeries.cs @@ -1034,7 +1034,7 @@ internal override void GenerateSegments(SeriesView seriesView) for (int i = 0; i < PointsCount; i++) { - var yList = YDataCollection[i].Where(x => !double.IsNaN(x)).ToArray(); + var yList = FilterNaNValues(YDataCollection[i]); List outliers = []; @@ -1042,7 +1042,13 @@ internal override void GenerateSegments(SeriesView seriesView) { // The quartile, whisker, and min/max calculations below all rely on ascending values. Array.Sort(yList); - average = yList.Average(); + double sum = 0; + for (int j = 0; j < yList.Length; j++) + { + sum += yList[j]; + } + + average = sum / yList.Length; } int yCount = yList.Length; @@ -1489,6 +1495,33 @@ static void GetMinMaxandOutlier(double lowerQuartile, double upperQuartile, doub } } + /// + /// Filters out NaN values from the source list without LINQ allocations. + /// + static double[] FilterNaNValues(IList source) + { + int count = 0; + for (int i = 0; i < source.Count; i++) + { + if (!double.IsNaN(source[i])) + { + count++; + } + } + + var result = new double[count]; + int index = 0; + for (int i = 0; i < source.Count; i++) + { + if (!double.IsNaN(source[i])) + { + result[index++] = source[i]; + } + } + + return result; + } + void GetQuartileValues(double[] yList, int len, out double lowerQuartile, out double upperQuartile) { double[] lowerQuartileArray;