Jonathan Twite
Jonathan Twite

Reputation: 952

Oxyplot annotation with brush

I wish to colour my OxyPlot's RectangleAnnotation with something more pretty than just a plain colour, i.e. some type of brush (GradientBrush etc.). I am working in WPF.

This question (PlotAreaBackground can be defined using gradient ?) on their GitHub site suggests that an annotation can be given a gradient, but I cannot find the suggested example.

Is there a way of applying a brush to a RectangleAnnotation? or any other way of creating coloured areas on a graph that are linked to the values on the axis?

Upvotes: 3

Views: 1832

Answers (1)

Jose
Jose

Reputation: 1897

I think that this is the suggested example.

I've modified it to look like a rectangle annotation, because the example fills the entire background. Hope it helps.

Efect:

enter image description here

Code:

// create a gradient image of height n
int n = 256;
var imageData1 = new OxyColor[n, 1];
for (int i = 0; i < n; i++)
{
    imageData1[i, 0] = OxyColor.Interpolate(OxyColors.Red, OxyColors.Blue, i / (n - 1.0));
}

PngBitmapEncoder encoder = new PngBitmapEncoder();
PngEncoder encode = new PngEncoder(new PngEncoderOptions());
var image1 = new OxyImage(encode.Encode(imageData1));

ImageAnnotation anotation = new ImageAnnotation
{
    ImageSource = image1,
    Interpolate = true,
    Layer = AnnotationLayer.BelowAxes,
    X = new PlotLength(0.5, PlotLengthUnit.RelativeToPlotArea),
    Y = new PlotLength(0.5, PlotLengthUnit.RelativeToPlotArea),
    Width = new PlotLength(0.1, PlotLengthUnit.RelativeToPlotArea),
    Height = new PlotLength(0.5, PlotLengthUnit.RelativeToPlotArea),
    HorizontalAlignment = HorizontalAlignment.Left,
    VerticalAlignment = VerticalAlignment.Top
};
plotModel.Annotations.Add(anotation);

Upvotes: 2

Related Questions