如何使用 Oxyplot 创建正方形绘图区域

How to create square plot area with Oxyplot

我正在尝试创建一个方形图(X 轴宽度与 Y 轴高度相同)。

我找不到关于此的任何文档,而且我所看到的可能能够执行此操作的所有属性都无法访问。

我试过:

<oxy:PlotView Model="{Binding Model}" Width="500" Height="500"/>

这显然是行不通的,因为它设置了整个区域(不是图形特定部分)。

如何使用外部元素来定义绘图区域的宽度和高度,例如GridBorder

<Border width="500" height="500">
    <oxy:PlotView Model="{Binding Model}" />
</Border>

假设您不希望它可以调整大小,如果您将它包裹在 Border 中并且 Height 等于 Width 加上高度标题部分。例如:

XAML:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:oxy="http://oxyplot.org/wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Border Width="200" Height="224">
            <oxy:PlotView x:Name="plot" Model="{Binding Path=PlotModel}"/>
        </Border>
    </Grid>
</Window>

Code-behind 型号 object:

using OxyPlot;
using OxyPlot.Series;
using System.Windows;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public PlotModel Model
    {
        get; set;
    }

    public MainWindow()
    {
        DataContext = this;

        Model = new PlotModel
        {
            Title = "Test",
            TitlePadding = 0,
            TitleFontSize = 24
        };
        LineSeries line = new LineSeries();
        line.Points.Add(new DataPoint(0, 0));
        line.Points.Add(new DataPoint(1, 1));
        line.Points.Add(new DataPoint(2, 2));
        Model.Series.Add(line);
    }
}

这就是它的样子:

如果你想做一个可调整大小的版本,然后使用包含 window 的 SizeChanged 事件,re-adjust Border 容器的大小事件处理程序。

我通过挂接到 PlotView 上的 LayoutUpdated 事件并从 PlotArea width/height 差异更新 PlotView.Width 来解决这个问题。

XAML:

<Window x:Class="Temp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:oxy="http://oxyplot.org/wpf"
        Title="MainWindow" Width="500" Height="500">
    <Grid>
        <oxy:PlotView Model="{Binding PlotModel}" x:Name="PlotView"/>
    </Grid>
</Window>

隐藏代码:

public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new MainViewModel();
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var plotView = (PlotView) this.FindName("PlotView");

            plotView.LayoutUpdated += OnLayoutUpdated;
        }

        private void OnLayoutUpdated(object sender, EventArgs e)
        {
            var plotView = (PlotView) this.FindName("PlotView") ;

            if (plotView.Model != null)
            {
                var widthAdjustment = plotView.Model.PlotArea.Width - plotView.Model.PlotArea.Height;

                plotView.Width = plotView.ActualWidth - widthAdjustment;
            }
        }
    }