Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Source/OxyPlot.Avalonia/Plot.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public partial class Plot
/// <summary>
/// Identifies the <see cref="PlotAreaBackground"/> dependency property.
/// </summary>
public static readonly StyledProperty<Brush> PlotAreaBackgroundProperty = AvaloniaProperty.Register<Plot, Brush>(nameof(PlotAreaBackground), null);
public static readonly StyledProperty<IBrush> PlotAreaBackgroundProperty = AvaloniaProperty.Register<Plot, IBrush>(nameof(PlotAreaBackground), null);

/// <summary>
/// Identifies the <see cref="PlotAreaBorderColor"/> dependency property.
Expand Down Expand Up @@ -140,7 +140,7 @@ public partial class Plot
/// <summary>
/// Identifies the <see cref="TitleHorizontalAlignment"/> dependency property.
/// </summary>
public static readonly StyledProperty<TitleHorizontalAlignment> TitleAlignmentProperty = AvaloniaProperty.Register<Plot, TitleHorizontalAlignment>(nameof(TitleHorizontalAlignment), TitleHorizontalAlignment.CenteredWithinPlotArea);
public static readonly StyledProperty<TitleHorizontalAlignment> TitleHorizontalAlignmentProperty = AvaloniaProperty.Register<Plot, TitleHorizontalAlignment>(nameof(TitleHorizontalAlignment), TitleHorizontalAlignment.CenteredWithinPlotArea);

/// <summary>
/// Identifies the <see cref="TitleFont"/> dependency property.
Expand Down Expand Up @@ -204,7 +204,7 @@ static Plot()
SubtitleFontWeightProperty.Changed.AddClassHandler<Plot>(AppearanceChanged);
SubtitleProperty.Changed.AddClassHandler<Plot>(AppearanceChanged);
TextColorProperty.Changed.AddClassHandler<Plot>(AppearanceChanged);
TitleAlignmentProperty.Changed.AddClassHandler<Plot>(AppearanceChanged);
TitleHorizontalAlignmentProperty.Changed.AddClassHandler<Plot>(AppearanceChanged);
TitleFontProperty.Changed.AddClassHandler<Plot>(AppearanceChanged);
TitleFontSizeProperty.Changed.AddClassHandler<Plot>(AppearanceChanged);
TitleFontWeightProperty.Changed.AddClassHandler<Plot>(AppearanceChanged);
Expand Down Expand Up @@ -426,7 +426,7 @@ public Color SubtitleColor
/// Gets or sets the background brush of the Plot area.
/// </summary>
/// <value>The brush.</value>
public Brush PlotAreaBackground
public IBrush PlotAreaBackground
{
get
{
Expand Down Expand Up @@ -640,12 +640,12 @@ public TitleHorizontalAlignment TitleHorizontalAlignment
{
get
{
return GetValue(TitleAlignmentProperty);
return GetValue(TitleHorizontalAlignmentProperty);
}

set
{
SetValue(TitleAlignmentProperty, value);
SetValue(TitleHorizontalAlignmentProperty, value);
}
}

Expand Down
92 changes: 84 additions & 8 deletions Source/OxyPlot.Avalonia/Plot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,15 +94,16 @@ public override IPlotController ActualController
/// Updates the model. If Model==<c>null</c>, an internal model will be created. The ActualModel.Update will be called (updates all series data).
/// </summary>
/// <param name="updateData">if set to <c>true</c> , all data collections will be updated.</param>
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);
}

Expand All @@ -110,22 +112,19 @@ public override IPlotController ActualController
/// </summary>
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);
}


Expand Down Expand Up @@ -175,6 +174,19 @@ private void OnSeriesChanged(object sender, NotifyCollectionChangedEventArgs e)
/// <param name="e">The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs" /> instance containing the event data.</param>
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)
Expand All @@ -201,7 +213,70 @@ private void SyncLogicalTree(NotifyCollectionChangedEventArgs e)
}
}

this.OnAppearanceChanged();
OnElementsChanged();
}

/// <summary>
/// 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.
/// </summary>
private void OnElementsChanged()
{
InvalidatePlot(true);
}

/// <summary>
/// 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.
/// </summary>
private void RebuildTreeChildren()
{
var expected = new HashSet<object>(
series.Cast<object>().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);
}
}
}

/// <summary>
/// Determines whether a tree element belongs to the collections of the plot,
/// and not to the control template.
/// </summary>
/// <param name="child">The tree element.</param>
/// <returns><c>true</c> if this is a series, an axis, an annotation or a legend.</returns>
private static bool IsPlotPart(object child)
{
return child is Series || child is Axis || child is Annotation || child is Legend;
}

/// <summary>
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Source/OxyPlot.Avalonia/PlotBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ protected override Size ArrangeOverride(Size finalSize)
/// Updates the model.
/// </summary>
/// <param name="updateData">The update Data.</param>
protected void UpdateModel(bool updateData = true)
protected virtual void UpdateModel(bool updateData = true)
{
if (Width <= 0 || Height <= 0 || ActualModel == null)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static OxyColor ToOxyColor(this Color color)
}

/// <summary>
/// Converts a <see cref="Brush" /> to an <see cref="OxyColor" />.
/// Converts an <see cref="IBrush" /> to an <see cref="OxyColor" />.
/// </summary>
/// <param name="brush">The brush.</param>
/// <returns>An <see cref="OxyColor" />.</returns>
Expand Down