[英]How to create square plot area with Oxyplot
I'm trying to create a square graph (X axis width is the same as the Y axis height). 我正在尝试创建一个方形图(X轴宽度与Y轴高度相同)。
I can't find any documentation on this and all of the properties I've seen which might be able to do this are inaccessible. 我找不到任何关于此的文档,我看到的所有可能无法访问的属性都无法访问。
I've tried: 我试过了:
<oxy:PlotView Model="{Binding Model}" Width="500" Height="500"/>
This obviously doesn't work because this sets the entire area (not the graph specific portion). 这显然不起作用,因为这设置了整个区域(而不是图形特定部分)。
I solved this by hooking into the LayoutUpdated
event on the PlotView
and updating the PlotView.Width
from the PlotArea
width/height difference. 我通过挂钩到解决了这个
LayoutUpdated
上的事件PlotView
和更新PlotView.Width
从PlotArea
宽度/高度差。
XAML: 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>
Code Behind: 代码背后:
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;
}
}
}
Assuming you don't want it to be resizable, it works if you wrap it in a Border
with a Height
that is equal to the Width
plus the height of the title section. 假设您不希望它可以调整大小,如果您将其包装在
Height
等于Width
加上标题部分高度的Border
,则它可以工作。 For example: 例如:
XAML: 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 for the Model object: 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);
}
}
And this is what it looks like: 这就是它的样子:
If you want to do a resizable version, then use the containing window's SizeChanged
event, and re-adjust the size of the Border
container in that event handler. 如果要执行可调整大小的版本,请使用包含窗口的
SizeChanged
事件,并在该事件处理程序中重新调整Border
容器的大小。
How about using an element outside to define the width and height of plotting area, like Grid or Border 如何使用外部元素来定义绘图区域的宽度和高度,如网格或边框
<Border width="500" height="500">
<oxy:PlotView Model="{Binding Model}" />
</Border>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.