Skip to content

Commit dd9daeb

Browse files
Merge pull request #378 from syncfusion/paulandersons/perf-chart-control-improvements
perf: Chart control performance improvements
2 parents c33d82a + 628b191 commit dd9daeb

5 files changed

Lines changed: 94 additions & 34 deletions

File tree

maui/src/Charts/Area/Partial/CartesianChartArea.cs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.ObjectModel;
1+
using System.Collections.ObjectModel;
22

33
namespace Syncfusion.Maui.Toolkit.Charts
44
{
@@ -103,8 +103,23 @@ internal void CalculateSbsPosition()
103103
{
104104
if (!stackingSeries.IsSbsValueCalculated && _seriesGroup != null)
105105
{
106-
var stackingSeriesType = stackingSeries.GetType();
107-
string groupID = _seriesGroup.FirstOrDefault(x => x.Value.Any(s => s.GroupingLabel == stackingSeries.GroupingLabel && s.GetType() == stackingSeriesType)).Key;
106+
string? groupID = null;
107+
foreach (var group in _seriesGroup)
108+
{
109+
foreach (var s in group.Value)
110+
{
111+
if (s.GroupingLabel == stackingSeries.GroupingLabel && s.GetType() == stackingSeries.GetType())
112+
{
113+
groupID = group.Key;
114+
break;
115+
}
116+
}
117+
118+
if (groupID != null)
119+
{
120+
break;
121+
}
122+
}
108123
StackingSeriesBase stackingSeriesBase;
109124
int size = SideBySideSeriesPosition.Count > 0 && groupingKeys.Count > 0 && groupingKeys.TryGetValue(groupID, out var groupValue)
110125
? SideBySideSeriesPosition[groupValue].Count : 0;
@@ -354,15 +369,23 @@ internal void InvalidateMinWidth()
354369

355370
internal void ResetSBSSegments()
356371
{
357-
var sideBySideSeries = VisibleSeries?.Where(series => series.IsSideBySide).ToList();
358-
359-
if (sideBySideSeries != null && sideBySideSeries.Count > 0)
372+
if (VisibleSeries == null)
360373
{
361-
SideBySideSeriesPosition = null;
374+
return;
375+
}
362376

363-
foreach (var chartSeries in sideBySideSeries)
377+
bool hasSbsSeries = false;
378+
foreach (var series in VisibleSeries)
379+
{
380+
if (series.IsSideBySide)
364381
{
365-
chartSeries.SegmentsCreated = false;
382+
if (!hasSbsSeries)
383+
{
384+
SideBySideSeriesPosition = null;
385+
hasSbsSeries = true;
386+
}
387+
388+
series.SegmentsCreated = false;
366389
}
367390
}
368391
}
@@ -407,7 +430,22 @@ static double GetSBSMaxWidth(List<CartesianSeries> seriesGroup)
407430
internal void UpdateStackingSeries()
408431
{
409432
//if visible series count is 0 or not contain any stacking series then return.
410-
if (VisibleSeries == null || VisibleSeries.Count == 0 || !VisibleSeries.Any(series => series is StackingSeriesBase && !series.SegmentsCreated))
433+
if (VisibleSeries == null || VisibleSeries.Count == 0)
434+
{
435+
return;
436+
}
437+
438+
bool hasStackingToCreate = false;
439+
foreach (var series in VisibleSeries)
440+
{
441+
if (series is StackingSeriesBase && !series.SegmentsCreated)
442+
{
443+
hasStackingToCreate = true;
444+
break;
445+
}
446+
}
447+
448+
if (!hasStackingToCreate)
411449
{
412450
return;
413451
}

maui/src/Charts/Layouts/ChartZoomPanView.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,16 @@ void GenerateSelectionElements(ICanvas canvas, ObservableCollection<ChartAxis> a
115115
{
116116
if (axis.IsVertical)
117117
{
118-
startPoint = new Point(isOpposed ? actualArrangeRect.X : actualArrangeRect.X + actualArrangeRect.Width, selectedRect.Top);
119-
endPoint = new Point(isOpposed ? actualArrangeRect.X : actualArrangeRect.X + actualArrangeRect.Width, selectedRect.Bottom);
118+
double xCoord = isOpposed ? actualArrangeRect.X : actualArrangeRect.X + actualArrangeRect.Width;
119+
startPoint = new Point(xCoord, selectedRect.Top);
120+
endPoint = new Point(xCoord, selectedRect.Bottom);
120121
tooltipPosition = isOpposed ? TooltipPosition.Right : TooltipPosition.Left;
121122
}
122123
else
123124
{
124-
startPoint = new Point(selectedRect.Left, isOpposed ? actualArrangeRect.Y + actualArrangeRect.Height : actualArrangeRect.Y);
125-
endPoint = new Point(selectedRect.Right, isOpposed ? actualArrangeRect.Y + actualArrangeRect.Height : actualArrangeRect.Y);
125+
double yCoord = isOpposed ? actualArrangeRect.Y + actualArrangeRect.Height : actualArrangeRect.Y;
126+
startPoint = new Point(selectedRect.Left, yCoord);
127+
endPoint = new Point(selectedRect.Right, yCoord);
126128
tooltipPosition = isOpposed ? TooltipPosition.Top : TooltipPosition.Bottom;
127129
}
128130

@@ -177,7 +179,7 @@ void GenerateAxisTrackballInfos(PointF startPoint, PointF endPoint, TooltipPosit
177179
ChartZoomPanView.MapChartLabelStyle(chart, axisPointInfo2.Helper, axis.TrackballLabelStyle);
178180
}
179181

180-
Rect actualArrangeRect = new Rect(axis.ArrangeRect.X, axis.ArrangeRect.Y, axis.ArrangeRect.X + axis.ArrangeRect.Width, axis.ArrangeRect.Y + axis.ArrangeRect.Height);
182+
Rect actualArrangeRect = new Rect(axisRect.X, axisRect.Y, axisRect.X + axisRect.Width, axisRect.Y + axisRect.Height);
181183

182184
axisPointInfo1.Helper.Show(actualArrangeRect, new Rect(startPoint.X - 1, startPoint.Y - 1, _dimension, _dimension), false);
183185
axisPointInfo2.Helper.Show(actualArrangeRect, new Rect(endPoint.X - 1, endPoint.Y - 1, _dimension, _dimension), false);

maui/src/Charts/Series/CartesianSeries.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public abstract class CartesianSeries : ChartSeries
1515
double _xAxisMax = double.MinValue;
1616
double _yAxisMin = double.MaxValue;
1717
double _yAxisMax = double.MinValue;
18+
List<double>? _cachedIndexedXValues;
1819

1920
#endregion
2021

@@ -1027,26 +1028,44 @@ internal override void UpdateRange()
10271028
return null;
10281029
}
10291030

1030-
double xIndexValues = 0d;
10311031
var xValues = ActualXValues as List<double>;
10321032

10331033
if (IsIndexed || xValues == null)
10341034
{
10351035
if (ActualXAxis is CategoryAxis categoryAxis && !categoryAxis.ArrangeByIndex || ActualXAxis == null)
10361036
{
1037-
xValues = GroupedXValuesIndexes.Count > 0 ? GroupedXValuesIndexes : (from val in (ActualXValues as List<string>) select (xIndexValues++)).ToList();
1037+
xValues = GroupedXValuesIndexes.Count > 0 ? GroupedXValuesIndexes : GetOrCreateIndexedXValues();
10381038
}
10391039
else
10401040
{
1041-
xValues = xValues != null ? (from val in xValues select (xIndexValues++)).ToList() : (from val in (ActualXValues as List<string>) select (xIndexValues++)).ToList();
1041+
xValues = GetOrCreateIndexedXValues();
10421042
}
10431043
}
10441044

10451045
return xValues;
10461046
}
10471047

1048+
List<double> GetOrCreateIndexedXValues()
1049+
{
1050+
int count = ActualXValues is List<double> dList ? dList.Count : (ActualXValues as List<string>)?.Count ?? 0;
1051+
if (_cachedIndexedXValues != null && _cachedIndexedXValues.Count == count)
1052+
{
1053+
return _cachedIndexedXValues;
1054+
}
1055+
1056+
var indexedValues = new List<double>(count);
1057+
for (int i = 0; i < count; i++)
1058+
{
1059+
indexedValues.Add(i);
1060+
}
1061+
1062+
_cachedIndexedXValues = indexedValues;
1063+
return _cachedIndexedXValues;
1064+
}
1065+
10481066
internal override void OnDataSourceChanged(object oldValue, object newValue)
10491067
{
1068+
_cachedIndexedXValues = null;
10501069
ResetAutoScroll();
10511070
InvalidateSideBySideSeries();
10521071
foreach (var item in EmptyPointIndexes)
@@ -1059,6 +1078,7 @@ internal override void OnDataSourceChanged(object oldValue, object newValue)
10591078

10601079
internal override void OnDataSource_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
10611080
{
1081+
_cachedIndexedXValues = null;
10621082
ResetAutoScroll();
10631083
base.OnDataSource_CollectionChanged(sender, e);
10641084
}

maui/src/Charts/Series/ChartSeriesPartial.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,16 +1411,19 @@ void ResetDataPoint()
14111411

14121412
if (ItemsSource != null)
14131413
{
1414-
var items = ItemsSource is IList ? ItemsSource as IList : null;
1415-
if (items == null)
1414+
bool hasData = false;
1415+
if (ItemsSource is IList list)
14161416
{
1417-
if (ItemsSource is IEnumerable source)
1418-
{
1419-
items = source.Cast<object>().ToList();
1420-
}
1417+
hasData = list.Count > 0;
1418+
}
1419+
else if (ItemsSource is IEnumerable source)
1420+
{
1421+
var enumerator = source.GetEnumerator();
1422+
hasData = enumerator.MoveNext();
1423+
(enumerator as IDisposable)?.Dispose();
14211424
}
14221425

1423-
if (items != null && items.Count > 0)
1426+
if (hasData)
14241427
{
14251428
GenerateDataPoints();
14261429
}

maui/src/Charts/Series/StackingSeriesBase.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,14 @@ void RefreshSeries()
330330

331331
void ResetVisibleSeries()
332332
{
333-
if (ChartArea != null)
333+
if (ChartArea?.VisibleSeries == null)
334334
{
335-
var visibleSeries = ChartArea.VisibleSeries;
336-
var stackingSeries = visibleSeries?.Where(series => series.IsStacking).ToList();
337-
338-
if (stackingSeries == null)
339-
{
340-
return;
341-
}
335+
return;
336+
}
342337

343-
foreach (var chartSeries in stackingSeries)
338+
foreach (var chartSeries in ChartArea.VisibleSeries)
339+
{
340+
if (chartSeries.IsStacking)
344341
{
345342
chartSeries.SegmentsCreated = false;
346343
}

0 commit comments

Comments
 (0)