[英]How to create square plot area with Oxyplot
我正在嘗試創建一個方形圖(X軸寬度與Y軸高度相同)。
我找不到任何關於此的文檔,我看到的所有可能無法訪問的屬性都無法訪問。
我試過了:
<oxy:PlotView Model="{Binding Model}" Width="500" Height="500"/>
這顯然不起作用,因為這設置了整個區域(而不是圖形特定部分)。
我通過掛鈎到解決了這個LayoutUpdated
上的事件PlotView
和更新PlotView.Width
從PlotArea
寬度/高度差。
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;
}
}
}
假設您不希望它可以調整大小,如果您將其包裝在Height
等於Width
加上標題部分高度的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.