覆盖OxyPlot默认颜色板

11

我最近一直在使用OxyPlot,想知道是否有一种方法可以覆盖PlotSeries/PlotModel的默认颜色调色板。

我知道我可以单独为每个系列设置颜色,但如果有一个颜色数组,然后将其应用于模型/系列会很好。

3个回答

18
你可以在 PlotViewModel 中更改 DefaultColors 列表:
plotView.Model.DefaultColors =  new List<OxyColor>
        {
            OxyColors.Red,
            OxyColors.Green,
            OxyColors.Blue,
            OxyColor.FromRgb(0x20, 0x4A, 0x87)
        };
为了使其更加简单,可以使用OxyPalettes的类方法:
plotView.Model.DefaultColors = OxyPalettes.Jet(plotView.Model.Series.Count).Colors;

我创建了一个 FromRgb 的颜色,如何引用它? - DevDave
@DevDave 为什么不将它保存到本地变量中?或者你是什么意思? - forallepsilon

6

除了其他答案所述的内容之外,如果您想在应用程序中使用相同的颜色集合来绘制所有图表,可以在xaml资源中设置这些颜色。例如,如果您想使用默认的Seaborn调色板,则可以将以下内容添加到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>

0

仅适用于OxyPlot 2.1及以上版本

从OxyPlot版本2.1.0及以上版本开始,Plot类及其相关组件已被移动到OxyPlot.Contrib.Wpf,详见发布信息

因此,App.xaml示例应写为:

<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>

不要忘记从NuGet管理器中添加Oxyplot.Contrib*包到你的C#解决方案中。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接