diff --git a/Source/OxyPlot.Avalonia/Plot.Properties.cs b/Source/OxyPlot.Avalonia/Plot.Properties.cs index 9d900cf..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. @@ -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); @@ -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 { @@ -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 abab5a3..595292c 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; @@ -93,15 +94,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 +112,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); } @@ -175,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) @@ -201,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; } /// @@ -229,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(); 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) { 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 .