我试图创建一个正方形的图表(X轴宽度与Y轴高度相同)。
我找不到任何关于此的文档,而我所看到的所有可能能够做到这一点的属性都无法访问。
我已经尝试过:
<oxy:PlotView Model="{Binding Model}" Width="500" Height="500"/>
这显然行不通,因为它设置的是整个区域而不是图表的特定部分。
我通过在PlotView
的LayoutUpdated
事件中进行钩取,从PlotArea
的宽度/高度差更新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;
}
}
}
宽度
加上标题部分高度的高度
的Border
中,则可以正常工作。例如:
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>
Model对象的代码后台:
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);
}
}
SizeChanged
事件,并在该事件处理程序中重新调整Border
容器的大小。如何使用外部元素来定义绘图区域的宽度和高度,例如网格或边框?
<Border width="500" height="500">
<oxy:PlotView Model="{Binding Model}" />
</Border>