Override OxyPlot default color palette
Asked Answered
K

3

11

I've been making use of OxyPlot lately, and was wondering if there is a way of overriding the default Color Palette of PlotSeries / PlotModel?

I know i can set the color individually for each series, but it would be nice to have an array of color and then apply it to the model / series.

Ketcham answered 23/3, 2015 at 15:54 Comment(0)
H
19

You can change the DefaultColors list in PlotView's Model:

plotView.Model.DefaultColors =  new List<OxyColor>
        {
            OxyColors.Red,
            OxyColors.Green,
            OxyColors.Blue,
            OxyColor.FromRgb(0x20, 0x4A, 0x87)
        };

To make it even more easy, OxyPalettes's class methods can be used:

plotView.Model.DefaultColors = OxyPalettes.Jet(plotView.Model.Series.Count).Colors;
Higbee answered 24/4, 2015 at 21:0 Comment(2)
How would I reference the color FromRgb after creating it?Vicarious
@Vicarious Why not to save it to local variable? Or what do you mean?Higbee
D
6

Adding to the other answer, if you want to use the same set of colors for every plot in your application, you could set those in your xaml resources. If, for instance, you want to use the default Seaborn palette, you could add the following to your App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <x:Array Type="Color" x:Key="SeabornColors">
            <Color>#4c72b0</Color>
            <Color>#55a868</Color>
            <Color>#c44e52</Color>
            <Color>#8172b2</Color>
            <Color>#ccb974</Color>
            <Color>#64b5cd</Color>
        </x:Array>
        <Style TargetType="oxy:Plot">
            <Setter Property="DefaultColors" Value="{StaticResource SeabornColors}"/>
        </Style>
    </ResourceDictionary>
</Application.Resources>
Dragonnade answered 24/3, 2017 at 17:42 Comment(0)
E
0

Valid for OxyPlot 2.1 and above

From OxyPlot version 2.1.0 and above, the Plot class and its associated components have been moved to OxyPlot.Contrib.Wpf, as described in the release information.

Hence the App.xaml example shall be written as:

<Application x:Class="Viewer.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:oxycontrib="http://oxyplot.org/wpf/contrib"
             StartupUri="View/MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <x:Array Type="Color" x:Key="SeabornColors">
                <Color>#4c72b0</Color>
                <Color>#55a868</Color>
                <Color>#c44e52</Color>
                <Color>#8172b2</Color>
                <Color>#ccb974</Color>
                <Color>#64b5cd</Color>
            </x:Array>
            <Style TargetType="oxycontrib:Plot">
                <Setter Property="DefaultColors" Value="{StaticResource SeabornColors}"/>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Don't forget to add the Oxyplot.Contrib* package from NuGet manager to your C# solution:

enter image description here

Ehrman answered 3/8, 2023 at 7:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.