From 64b92252e98e08882043e3cce170f46dd7476338 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 1/4] Synchronize the model when InvalidatePlot is called on a plot --- Source/OxyPlot.Avalonia/Plot.cs | 12 +++++------- Source/OxyPlot.Avalonia/PlotBase.cs | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Source/OxyPlot.Avalonia/Plot.cs b/Source/OxyPlot.Avalonia/Plot.cs index abab5a3..a89797f 100644 --- a/Source/OxyPlot.Avalonia/Plot.cs +++ b/Source/OxyPlot.Avalonia/Plot.cs @@ -93,15 +93,16 @@ public override IPlotController ActualController /// Updates the model. If Model==null, an internal model will be created. The ActualModel.Update will be called (updates all series data). /// /// if set to true , all data collections will be updated. - protected new void UpdateModel(bool updateData = true) + protected override void UpdateModel(bool updateData = true) { + // The method is virtual, so InvalidatePlot also ends up here (including from InvalidateFlag + // and from the axes): without synchronization, in-place edits of the wrappers would not reach the model. SynchronizeProperties(); SynchronizeSeries(); SynchronizeAxes(); SynchronizeAnnotations(); SynchronizeLegends(); - // TODO: does this achieve anything? we always call InvalidatePlot after UpdateModel base.UpdateModel(updateData); } @@ -110,22 +111,19 @@ public override IPlotController ActualController /// protected void OnAppearanceChanged() { - UpdateModel(false); InvalidatePlot(false); } void IPlot.ElementAppearanceChanged(object element) { // TODO: determine type of element to perform a more fine-grained update - this.UpdateModel(false); - base.InvalidatePlot(false); + InvalidatePlot(false); } void IPlot.ElementDataChanged(object element) { // TODO: determine type of element to perform a more fine-grained update - this.UpdateModel(true); - base.InvalidatePlot(true); + InvalidatePlot(true); } diff --git a/Source/OxyPlot.Avalonia/PlotBase.cs b/Source/OxyPlot.Avalonia/PlotBase.cs index f9f348e..908d680 100644 --- a/Source/OxyPlot.Avalonia/PlotBase.cs +++ b/Source/OxyPlot.Avalonia/PlotBase.cs @@ -395,7 +395,7 @@ protected override Size ArrangeOverride(Size finalSize) /// Updates the model. /// /// The update Data. - protected void UpdateModel(bool updateData = true) + protected virtual void UpdateModel(bool updateData = true) { if (Width <= 0 || Height <= 0 || ActualModel == null) { From 9c7f639784614689bd8742a9d6868ca61f9cb530 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 2/4] Fix logical tree and model update when plot elements change --- Source/OxyPlot.Avalonia/Plot.cs | 79 ++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/Source/OxyPlot.Avalonia/Plot.cs b/Source/OxyPlot.Avalonia/Plot.cs index a89797f..671a056 100644 --- a/Source/OxyPlot.Avalonia/Plot.cs +++ b/Source/OxyPlot.Avalonia/Plot.cs @@ -14,6 +14,7 @@ namespace OxyPlot.Avalonia using global::Avalonia.Controls; using global::Avalonia.LogicalTree; using global::Avalonia.VisualTree; + using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; @@ -173,6 +174,19 @@ private void OnSeriesChanged(object sender, NotifyCollectionChangedEventArgs e) /// The instance containing the event data. private void SyncLogicalTree(NotifyCollectionChangedEventArgs e) { + // Reset (which is Clear) does not provide OldItems, while Move and Replace send + // the same element in both NewItems and OldItems. In these cases the per-item logic + // below leaves garbage in the tree, clears the parent or fails on re-adding, + // so the tree is rebuilt completely. + if (e.Action == NotifyCollectionChangedAction.Reset + || e.Action == NotifyCollectionChangedAction.Move + || e.Action == NotifyCollectionChangedAction.Replace) + { + RebuildTreeChildren(); + OnElementsChanged(); + return; + } + // In order to get DataContext and binding to work with the series, axes and annotations // we add the items to the logical tree if (e.NewItems != null) @@ -199,7 +213,70 @@ private void SyncLogicalTree(NotifyCollectionChangedEventArgs e) } } - this.OnAppearanceChanged(); + OnElementsChanged(); + } + + /// + /// Updates the model after the set of series, axes, annotations or legends has changed. + /// This is a data update: the core loads the series points and computes the axis ranges + /// only during it, otherwise an added series stays empty and the axes have no range. + /// + private void OnElementsChanged() + { + InvalidatePlot(true); + } + + /// + /// Brings the logical and visual trees in line with the collections of the plot. + /// Elements of the control template are not touched: they are also in the visual tree, + /// and removing them would break rendering permanently. + /// + private void RebuildTreeChildren() + { + var expected = new HashSet( + series.Cast().Concat(axes).Concat(annotations).Concat(legends)); + + foreach (var child in LogicalChildren.ToArray()) + { + if (IsPlotPart(child) && !expected.Contains(child)) + { + (child as ISetLogicalParent)?.SetParent(null); + LogicalChildren.Remove(child); + } + } + + foreach (var child in VisualChildren.ToArray()) + { + if (IsPlotPart(child) && !expected.Contains(child)) + { + VisualChildren.Remove(child); + } + } + + foreach (var item in expected) + { + if (item is ILogical logical && !LogicalChildren.Contains(logical)) + { + (item as ISetLogicalParent)?.SetParent(this); + LogicalChildren.Add(logical); + } + + if (item is Visual visual && !VisualChildren.Contains(visual)) + { + VisualChildren.Add(visual); + } + } + } + + /// + /// Determines whether a tree element belongs to the collections of the plot, + /// and not to the control template. + /// + /// The tree element. + /// true if this is a series, an axis, an annotation or a legend. + private static bool IsPlotPart(object child) + { + return child is Series || child is Axis || child is Annotation || child is Legend; } /// From a175b9b001d323042bb259e913970801c51ca48c Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 3/4] Apply TitleHorizontalAlignment to the model --- Source/OxyPlot.Avalonia/Plot.Properties.cs | 8 ++++---- Source/OxyPlot.Avalonia/Plot.cs | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Source/OxyPlot.Avalonia/Plot.Properties.cs b/Source/OxyPlot.Avalonia/Plot.Properties.cs index 9d900cf..5d30579 100644 --- a/Source/OxyPlot.Avalonia/Plot.Properties.cs +++ b/Source/OxyPlot.Avalonia/Plot.Properties.cs @@ -140,7 +140,7 @@ public partial class Plot /// /// Identifies the dependency property. /// - public static readonly StyledProperty TitleAlignmentProperty = AvaloniaProperty.Register(nameof(TitleHorizontalAlignment), TitleHorizontalAlignment.CenteredWithinPlotArea); + public static readonly StyledProperty TitleHorizontalAlignmentProperty = AvaloniaProperty.Register(nameof(TitleHorizontalAlignment), TitleHorizontalAlignment.CenteredWithinPlotArea); /// /// Identifies the dependency property. @@ -204,7 +204,7 @@ static Plot() SubtitleFontWeightProperty.Changed.AddClassHandler(AppearanceChanged); SubtitleProperty.Changed.AddClassHandler(AppearanceChanged); TextColorProperty.Changed.AddClassHandler(AppearanceChanged); - TitleAlignmentProperty.Changed.AddClassHandler(AppearanceChanged); + TitleHorizontalAlignmentProperty.Changed.AddClassHandler(AppearanceChanged); TitleFontProperty.Changed.AddClassHandler(AppearanceChanged); TitleFontSizeProperty.Changed.AddClassHandler(AppearanceChanged); TitleFontWeightProperty.Changed.AddClassHandler(AppearanceChanged); @@ -640,12 +640,12 @@ public TitleHorizontalAlignment TitleHorizontalAlignment { get { - return GetValue(TitleAlignmentProperty); + return GetValue(TitleHorizontalAlignmentProperty); } set { - SetValue(TitleAlignmentProperty, value); + SetValue(TitleHorizontalAlignmentProperty, value); } } diff --git a/Source/OxyPlot.Avalonia/Plot.cs b/Source/OxyPlot.Avalonia/Plot.cs index 671a056..595292c 100644 --- a/Source/OxyPlot.Avalonia/Plot.cs +++ b/Source/OxyPlot.Avalonia/Plot.cs @@ -304,6 +304,7 @@ private void SynchronizeProperties() m.TitleFontSize = TitleFontSize; m.TitleFontWeight = (int)TitleFontWeight; m.TitleToolTip = TitleToolTip; + m.TitleHorizontalAlignment = TitleHorizontalAlignment; m.Subtitle = Subtitle; m.SubtitleColor = SubtitleColor.ToOxyColor(); From 89d50ba76b340cf6dc5b702faefe56e4bdf9fc97 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:01 +0000 Subject: [PATCH 4/4] Accept any brush as PlotAreaBackground --- Source/OxyPlot.Avalonia/Plot.Properties.cs | 4 ++-- Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/OxyPlot.Avalonia/Plot.Properties.cs b/Source/OxyPlot.Avalonia/Plot.Properties.cs index 5d30579..cec7f79 100644 --- a/Source/OxyPlot.Avalonia/Plot.Properties.cs +++ b/Source/OxyPlot.Avalonia/Plot.Properties.cs @@ -95,7 +95,7 @@ public partial class Plot /// /// Identifies the dependency property. /// - public static readonly StyledProperty PlotAreaBackgroundProperty = AvaloniaProperty.Register(nameof(PlotAreaBackground), null); + public static readonly StyledProperty PlotAreaBackgroundProperty = AvaloniaProperty.Register(nameof(PlotAreaBackground), null); /// /// Identifies the dependency property. @@ -426,7 +426,7 @@ public Color SubtitleColor /// Gets or sets the background brush of the Plot area. /// /// The brush. - public Brush PlotAreaBackground + public IBrush PlotAreaBackground { get { diff --git a/Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs b/Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs index 7eef9a9..4ed114b 100644 --- a/Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs +++ b/Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs @@ -120,7 +120,7 @@ public static OxyColor ToOxyColor(this Color color) } /// - /// Converts a to an . + /// Converts an to an . /// /// The brush. /// An .