Skip to content
Open
2 changes: 0 additions & 2 deletions Source/OxyPlot.Avalonia/Annotations/TextAnnotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,9 @@ public override void SynchronizeProperties()
{
base.SynchronizeProperties();
var a = (Annotations.TextAnnotation)InternalAnnotation;
a.TextHorizontalAlignment = HorizontalAlignment.ToHorizontalAlignment();
a.Background = Background.ToOxyColor();

a.Offset = Offset.ToScreenVector();
a.TextVerticalAlignment = VerticalAlignment.ToVerticalAlignment();
a.Padding = Padding.ToOxyThickness();

a.Stroke = Stroke.ToOxyColor();
Expand Down
6 changes: 3 additions & 3 deletions Source/OxyPlot.Avalonia/Axes/Axis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public abstract class Axis : Control
/// <summary>
/// Identifies the <see cref="ExtraGridlines"/> dependency property.
/// </summary>
public static readonly StyledProperty<double[]> ExtraGridlinesProperty = AvaloniaProperty.Register<Axis, double[]>("ExtraGridLines", null);
public static readonly StyledProperty<double[]> ExtraGridlinesProperty = AvaloniaProperty.Register<Axis, double[]>(nameof(ExtraGridlines), null);

/// <summary>
/// Identifies the <see cref="FilterFunction"/> dependency property.
Expand Down Expand Up @@ -589,7 +589,7 @@ public double ExtraGridlineThickness
}

/// <summary>
/// Gets or sets ExtraGridLines.
/// Gets or sets ExtraGridlines.
/// </summary>
public double[] ExtraGridlines
{
Expand Down Expand Up @@ -1548,7 +1548,7 @@ protected virtual void SynchronizeProperties()
a.MinimumPadding = MinimumPadding;
a.MaximumPadding = MaximumPadding;
a.MinimumMargin = MinimumMargin;
a.MaximumMargin = MinimumMargin;
a.MaximumMargin = MaximumMargin;
a.MinimumDataMargin = MinimumDataMargin;
a.MaximumDataMargin = MaximumDataMargin;
a.Position = Position;
Expand Down
74 changes: 53 additions & 21 deletions Source/OxyPlot.Avalonia/Axes/LinearColorAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Diagnostics.Contracts;
using Avalonia;

namespace OxyPlot.Avalonia
Expand Down Expand Up @@ -44,13 +43,21 @@ public class LinearColorAxis : Axis
/// </summary>
public static readonly StyledProperty<int> PaletteSizeProperty = AvaloniaProperty.Register<LinearColorAxis, int>(nameof(PaletteSize), 20, validate: ValidatePaletteSize);

/// <summary>
/// The default palette of the core. It is returned when there are no gradient stops.
/// </summary>
private readonly OxyPalette defaultPalette;

/// <summary>
/// Initializes a new instance of the <see cref="LinearColorAxis"/> class.
/// </summary>
public LinearColorAxis()
{
InternalAxis = new Axes.LinearColorAxis();
GradientStops = new List<GradientStop>();
defaultPalette = ((Axes.LinearColorAxis)InternalAxis).Palette;

// The value is set as the current one so that it does not override style setters.
SetCurrentValue(GradientStopsProperty, new List<GradientStop>());
}

/// <summary>
Expand Down Expand Up @@ -136,13 +143,25 @@ public override Axes.Axis CreateModel()
protected override void SynchronizeProperties()
{
base.SynchronizeProperties();
var axis = InternalAxis as Axes.LinearColorAxis;
Contract.Requires<InvalidOperationException>(axis != null);
if (GradientStops != null)
if (InternalAxis is not Axes.LinearColorAxis axis)
{
axis.Palette = GradientStops.Count > 2
? Interpolate(GradientStops.ToList(), PaletteSize)
: new OxyPalette();
throw new InvalidOperationException("The internal axis must be of type LinearColorAxis.");
}

if (GradientStops != null && GradientStops.Count >= 2)
{
axis.Palette = Interpolate(GradientStops.ToList(), PaletteSize);
}
else if (GradientStops != null && GradientStops.Count == 1)
{
// A single stop: a palette of one color, but of the requested size.
axis.Palette = new OxyPalette(
Enumerable.Repeat(GradientStops[0].Color.ToOxyColor(), Math.Max(1, PaletteSize)).ToList());
}
else
{
// No stops: return the palette of the core, otherwise the map would become monochrome.
axis.Palette = defaultPalette;
}

axis.HighColor = HighColor.ToOxyColor();
Expand All @@ -168,26 +187,39 @@ private static OxyPalette Interpolate(List<GradientStop> stops, int paletteSize)
Debug.Assert(stops.Count >= 2, "Can't interpolate less than 2 gradient stops.");
Debug.Assert(paletteSize > 0, "Palette size must be non-zero positive number.");

var palette = new List<OxyColor>();
stops.Sort((x1, x2) => x1.Offset.CompareTo(x2.Offset));

var palettePositions = stops[0].Offset;
var step = (double)stops.Count / paletteSize;
var first = stops[0].Offset;
var last = stops[stops.Count - 1].Offset;
var range = last - first;

// All stops are at one point or a single color is needed: there is nothing to interpolate,
// but the requested palette size must still be honored.
if (range <= 0 || paletteSize < 2)
{
return new OxyPalette(
Enumerable.Repeat(stops[0].Color.ToOxyColor(), Math.Max(1, paletteSize)).ToList());
}

var palette = new List<OxyColor>(paletteSize);
var segment = 0;

for (int i = 0; i < stops.Count - 1; i++)
for (int i = 0; i < paletteSize; i++)
{
var start = stops[i];
var end = stops[i + 1];
var position = first + (range * i / (paletteSize - 1));

while (palettePositions <= end.Offset)
while (segment < stops.Count - 2 && position > stops[segment + 1].Offset)
{
palette.Add(
OxyColor.Interpolate(
start.Color.ToOxyColor(),
end.Color.ToOxyColor(),
(palettePositions - start.Offset) / (end.Offset - start.Offset)));
palettePositions += step;
segment++;
}

var start = stops[segment];
var end = stops[segment + 1];
var span = end.Offset - start.Offset;
// Stops with the same offset: take the start color, no division by zero.
var t = span > 0 ? (position - start.Offset) / span : 0.0;

palette.Add(OxyColor.Interpolate(start.Color.ToOxyColor(), end.Color.ToOxyColor(), t));
}

return new OxyPalette(palette);
Expand Down
2 changes: 1 addition & 1 deletion Source/OxyPlot.Avalonia/Series/AreaSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AreaSeries : LineSeries
/// <summary>
/// Identifies the <see cref="Color2"/> dependency property.
/// </summary>
public static readonly StyledProperty<Color> Color2Property = AvaloniaProperty.Register<Series, Color>(nameof(Color2), MoreColors.Automatic);
public static readonly StyledProperty<Color> Color2Property = AvaloniaProperty.Register<AreaSeries, Color>(nameof(Color2), MoreColors.Automatic);

/// <summary>
/// Identifies the <see cref="ConstantY2"/> dependency property.
Expand Down
12 changes: 7 additions & 5 deletions Source/OxyPlot.Avalonia/Series/BarSeries/BarSeriesBase{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public class BarSeriesBase<T> : BarSeriesBase
protected override void SynchronizeProperties(OxyPlot.Series.Series series)
{
base.SynchronizeProperties(series);
if (Items != null)
var s = (OxyPlot.Series.BarSeriesBase<T>)series;
s.Items.Clear();

// Items can contain foreign objects if ItemsSource is set together with ValueField or ColorField
foreach (var item in Items)
{
var s = (OxyPlot.Series.BarSeriesBase<T>)series;
s.Items.Clear();
foreach (T item in Items)
if (item is T typed)
{
s.Items.Add(item);
s.Items.Add(typed);
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions Source/OxyPlot.Avalonia/Series/BoxPlotSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace OxyPlot.Avalonia
{
using global::Avalonia.Media;
using System.Linq;

/// <summary>
/// This is a Avalonia wrapper of OxyPlot.BoxPlotSeries
Expand Down Expand Up @@ -262,24 +261,28 @@ protected override void SynchronizeProperties(OxyPlot.Series.Series series)
base.SynchronizeProperties(series);

var s = (OxyPlot.Series.BoxPlotSeries)series;
s.BoxWidth = BoxWidth;
s.Fill = Fill.ToOxyColor();
s.LineStyle = LineStyle;
s.MedianPointSize = MedianPointSize;
s.MedianThickness = MedianThickness;
s.OutlierSize = OutlierSize;
s.OutlierType = OutlierType;
s.OutlierOutline = (OutlierOutline ?? Enumerable.Empty<Point>()).Select(point => point.ToScreenPoint()).ToArray();
s.OutlierOutline = OutlierOutline.ToScreenPointArray();
s.ShowBox = ShowBox;
s.ShowMedianAsDot = ShowMedianAsDot;
s.Stroke = Stroke.ToOxyColor();
s.StrokeThickness = StrokeThickness;
s.WhiskerWidth = WhiskerWidth;

if (Items != null)
s.Items.Clear();

// Items can contain foreign objects if ItemsSource is set
foreach (var item in Items)
{
s.Items.Clear();
foreach (var item in Items)
if (item is OxyPlot.Series.BoxPlotItem boxItem)
{
s.Items.Add((OxyPlot.Series.BoxPlotItem)item);
s.Items.Add(boxItem);
}
}
}
Expand Down
45 changes: 1 addition & 44 deletions Source/OxyPlot.Avalonia/Series/HeatMapSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace OxyPlot.Avalonia
{
using global::Avalonia.Media;

/// <summary>
/// HeatMapSeries Avalonia wrapper
/// </summary>
Expand Down Expand Up @@ -48,16 +46,6 @@ public class HeatMapSeries : XYAxisSeries
/// </summary>
public static readonly StyledProperty<string> ColorAxisKeyProperty = AvaloniaProperty.Register<HeatMapSeries, string>(nameof(ColorAxisKey), default);

/// <summary>
/// Identifies this <see cref="LowColorProperty"/> dependency property.
/// </summary>
public static readonly StyledProperty<Color> LowColorProperty = AvaloniaProperty.Register<HeatMapSeries, Color>(nameof(LowColor), default);

/// <summary>
/// Identifies this <see cref="HighColorProperty"/> dependency property.
/// </summary>
public static readonly StyledProperty<Color> HighColorProperty = AvaloniaProperty.Register<HeatMapSeries, Color>(nameof(HighColor), default);

/// <summary>
/// Initializes static members of the <see cref="HeatMapSeries"/> class.
/// </summary>
Expand All @@ -81,38 +69,6 @@ public HeatMapSeries()
InternalSeries = new OxyPlot.Series.HeatMapSeries { Data = Data };
}

/// <summary>
/// Gets or sets LowColor
/// </summary>
public Color LowColor
{
get
{
return GetValue(LowColorProperty);
}

set
{
SetValue(LowColorProperty, value);
}
}

/// <summary>
/// Gets or sets HighColor
/// </summary>
public Color HighColor
{
get
{
return GetValue(LowColorProperty);
}

set
{
SetValue(LowColorProperty, value);
}
}

/// <summary>
/// Gets or sets ColorAxisKey property.
/// </summary>
Expand Down Expand Up @@ -237,6 +193,7 @@ protected override void SynchronizeProperties(OxyPlot.Series.Series series)
s.X1 = X1;
s.Y0 = Y0;
s.Y1 = Y1;
s.ColorAxisKey = ColorAxisKey;
}
}
}
3 changes: 1 addition & 2 deletions Source/OxyPlot.Avalonia/Series/LineSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace OxyPlot.Avalonia
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// This is a Avalonia wrapper of OxyPlot.LineSeries
Expand Down Expand Up @@ -497,7 +496,7 @@ protected override void SynchronizeProperties(OxyPlot.Series.Series series)
s.Dashes = Dashes;
s.LineJoin = LineJoin;
s.MarkerFill = MarkerFill.ToOxyColor();
s.MarkerOutline = (MarkerOutline ?? Enumerable.Empty<Point>()).Select(point => point.ToScreenPoint()).ToArray();
s.MarkerOutline = MarkerOutline.ToScreenPointArray();
s.MinimumSegmentLength = MinimumSegmentLength;
s.LabelFormatString = LabelFormatString;
s.LabelMargin = LabelMargin;
Expand Down
23 changes: 20 additions & 3 deletions Source/OxyPlot.Avalonia/Series/ScatterSeries{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public abstract class ScatterSeries<T> : XYAxisSeries where T : ScatterPoint
/// <summary>
/// Identifies the <see cref="Mapping"/> dependency property.
/// </summary>
public static readonly StyledProperty<Func<object, ScatterPoint>> MappingProperty = AvaloniaProperty.Register<ScatterSeries<T>, Func<object, ScatterPoint>>(nameof(Mapping), null);
public static readonly StyledProperty<Func<object, T>> MappingProperty = AvaloniaProperty.Register<ScatterSeries<T>, Func<object, T>>(nameof(Mapping), null);

/// <summary>
/// Identifies the <see cref="MarkerFill"/> dependency property.
Expand Down Expand Up @@ -86,6 +86,11 @@ public abstract class ScatterSeries<T> : XYAxisSeries where T : ScatterPoint
/// </summary>
public static readonly StyledProperty<MarkerType> MarkerTypeProperty = AvaloniaProperty.Register<ScatterSeries<T>, MarkerType>(nameof(MarkerType), MarkerType.Square);

/// <summary>
/// Identifies the <see cref="ColorAxisKey"/> dependency property.
/// </summary>
public static readonly StyledProperty<string> ColorAxisKeyProperty = AvaloniaProperty.Register<ScatterSeries<T>, string>(nameof(ColorAxisKey), null);

/// <summary>
/// Initializes a new instance of the <see cref = "ScatterSeries{T}" /> class.
/// </summary>
Expand Down Expand Up @@ -197,7 +202,7 @@ public Func<object, T> Mapping
{
get
{
return (Func<object, T>)GetValue(MappingProperty);
return GetValue(MappingProperty);
}

set
Expand Down Expand Up @@ -306,7 +311,18 @@ public MarkerType MarkerType
/// Gets or sets the color axis key.
/// </summary>
/// <value>The color axis key.</value>
public string ColorAxisKey { get; set; }
public string ColorAxisKey
{
get
{
return GetValue(ColorAxisKeyProperty);
}

set
{
SetValue(ColorAxisKeyProperty, value);
}
}

/// <summary>
/// Creates the internal series.
Expand Down Expand Up @@ -358,6 +374,7 @@ static ScatterSeries()
MarkerStrokeProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
MarkerStrokeThicknessProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
MarkerTypeProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
ColorAxisKeyProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
}
}
}
2 changes: 2 additions & 0 deletions Source/OxyPlot.Avalonia/Series/ThreeColorLineSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ protected override void SynchronizeProperties(OxyPlot.Series.Series series)
s.ColorLo = ColorLo.ToOxyColor();
s.LimitHi = LimitHi;
s.ColorHi = ColorHi.ToOxyColor();
s.LineStyleLo = LineStyleLo;
s.LineStyleHi = LineStyleHi;
}

static ThreeColorLineSeries()
Expand Down
Loading