在WPF中为OxyPlot显示具有两个Y轴的多个图例

huangapple go评论82阅读模式
英文:

Displaying Multiple Legends for OxyPlot in WPF with Two Y Axes

问题

我在我的WPF项目中使用OxyPlot来创建一个带有两个Y轴的图表。我已经将不同的系列分配给左侧和右侧的Y轴。我想要显示两个单独的图例:一个位于左侧,其中的元素对应于左侧的Y轴,另一个图例用于右侧的Y轴。

我已经通过使用YAxisKey属性正确地实现了两个Y轴并将系列分配给每个轴。然而,在OxyPlot中,我还没有找到内置的方法来分割图例。

是否有方法可以实现图例的视觉分离?如果可以的话,您能否提供实现方法或示例?以下是我用于创建轴和系列分配的当前代码:

var leftAxis = new LinearAxis
{
    Position = AxisPosition.Left,
    Key = "LeftAxis",
    Title = "Left Axis"
};

var rightAxis = new LinearAxis
{
    Position = AxisPosition.Right,
    Key = "RightAxis",
    Title = "Right Axis"
};

plotModel.Axes.Add(leftAxis);
plotModel.Axes.Add(rightAxis);

var series0 = new LineSeries
{
    YAxisKey = rightAxis.Key,
    Title = "Series0",
    Color = OxyColors.Blue,
};

var series1 = new LineSeries
{
    YAxisKey = leftAxis.Key,
    Title = "Series1",
    Color = OxyColors.Red,
};

我欢迎任何关于基于Y轴以视觉方式分割图例的建议或替代方法。提前感谢您的帮助!

英文:

I'm using OxyPlot in my WPF project to create a plot with two Y axes. I have different series assigned to either the left or the right Y axis.
I would like to display two separate legends: one on the left with elements corresponding to the left Y axis, and another legend for the right Y axis.

I have already implemented the two Y axes and assigned the series correctly to each axis using the YAxisKey property. However, I haven't found a built-in way to split the legends in OxyPlot.

Is there a way to achieve this visual separation of legends? If so, could you please provide guidance or an example of how to implement it?

My current code for creating the axes and series assignment looks similar to the following:

    var leftAxis = new LinearAxis
    {
        Position = AxisPosition.Left,
        Key = "LeftAxis",
        Title = "Left Axis"
    };

    var rightAxis = new LinearAxis
    {
        Position = AxisPosition.Right,
        Key = "RightAxis",
        Title = "Right Axis"
    };

    plotModel.Axes.Add(leftAxis);
    plotModel.Axes.Add(rightAxis);

    var series0 = new LineSeries
    {
        YAxisKey = rightAxis.Key,
        Title = "Series0",
        Color = OxyColors.Blue,
    };

    var series1 = new LineSeries
    {
        YAxisKey = leftAxis.Key,
        Title = "Series0",
        Color = OxyColors.Red,
    };

I'm open to any suggestions or alternative approaches to visually split the legends based on the Y axes. Thank you in advance for your help!

答案1

得分: 1

你可以向PlotModel添加任意数量的图例。每个Legend实例可以分配自己的键。然后,你可以设置每个系列的LegendKey属性来控制它们显示在哪个图例中。

public static PlotModel TwoAxesToLegends()
{
    var model = new PlotModel();

    var leftAxis = new LinearAxis
    {
        Position = AxisPosition.Left,
        Key = "LeftAxis",
        Title = "Left Axis"
    };

    var rightAxis = new LinearAxis
    {
        Position = AxisPosition.Right,
        Key = "RightAxis",
        Title = "Right Axis"
    };

    model.Axes.Add(leftAxis);
    model.Axes.Add(rightAxis);

    var leftLegend = new Legend
    {
        LegendTitle = "Left Axis",
        LegendPlacement = LegendPlacement.Inside,
        LegendPosition = LegendPosition.LeftTop,
        LegendBackground = OxyColor.FromAColor(200, OxyColors.White),
        LegendBorder = OxyColors.Black,
        Key = "Legend Left",
    };

    var rightLegend = new Legend
    {
        LegendTitle = "Right Axis",
        LegendPlacement = LegendPlacement.Inside,
        LegendPosition = LegendPosition.RightTop,
        LegendBackground = OxyColor.FromAColor(200, OxyColors.White),
        LegendBorder = OxyColors.Black,
        Key = "Legend Right",
    };

    model.Legends.Add(leftLegend);
    model.Legends.Add(rightLegend);

    var leftSeries = new LineSeries
    {
        YAxisKey = rightAxis.Key,
        LegendKey = leftLegend.Key,
        Title = "Left Series",
        Color = OxyColors.Blue,
    };

    var rightSeries = new LineSeries
    {
        YAxisKey = leftAxis.Key,
        LegendKey = rightLegend.Key,
        Title = "Right Series",
        Color = OxyColors.Red,
    };

    model.Series.Add(leftSeries);
    model.Series.Add(rightSeries);

    return model;
}
英文:

You can add an arbitrary number of legends to a PlotModel. Each instance of Legend can get it's own key assigned. You can then set the LegendKey property on each series to control in which legend they are displayed.

public static PlotModel TwoAxesToLegends()
    {
        var model = new PlotModel();

        var leftAxis = new LinearAxis
        {
            Position = AxisPosition.Left,
            Key = "LeftAxis",
            Title = "Left Axis"
        };

        var rightAxis = new LinearAxis
        {
            Position = AxisPosition.Right,
            Key = "RightAxis",
            Title = "Right Axis"
        };

        model.Axes.Add(leftAxis);
        model.Axes.Add(rightAxis);

        var leftLegend = new Legend
        {
            LegendTitle = "Left Axis",
            LegendPlacement = LegendPlacement.Inside,
            LegendPosition = LegendPosition.LeftTop,
            LegendBackground = OxyColor.FromAColor(200, OxyColors.White),
            LegendBorder = OxyColors.Black,
            Key = "Legend Left",
        };

        var rightLegend = new Legend
        {
            LegendTitle = "Right Axis",
            LegendPlacement = LegendPlacement.Inside,
            LegendPosition = LegendPosition.RightTop,
            LegendBackground = OxyColor.FromAColor(200, OxyColors.White),
            LegendBorder = OxyColors.Black,
            Key = "Legend Right",
        };

        model.Legends.Add(leftLegend);
        model.Legends.Add(rightLegend);

        var leftSeries = new LineSeries
        {
            YAxisKey = rightAxis.Key,
            LegendKey = leftLegend.Key,
            Title = "Left Series",
            Color = OxyColors.Blue,
        };

        var rightSeries = new LineSeries
        {
            YAxisKey = leftAxis.Key,
            LegendKey = rightLegend.Key,
            Title = "Right Series",
            Color = OxyColors.Red,
        };

        model.Series.Add(leftSeries);
        model.Series.Add(rightSeries);

        return model;
    }

huangapple
  • 本文由 发表于 2023年6月1日 19:43:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定