How to create a square plot area using Oxyplot

I am trying to create a square graph (the width of the X axis is the same as the height of the Y axis).

I can not find the documentation on this, and all the properties that I saw that could do this are not available.

I tried:

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

This clearly does not work, because it defines the entire area (and not a specific part of the graph).

+6
source share
3 answers

I solved this by connecting to the event LayoutUpdatedon PlotViewand updating PlotView.Widthfrom the width / height difference 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>

Code for:

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;
            }
        }
    }
+2
source

, , , , 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>

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);
    }
}

: the window

, window SizeChanged Border .

0

How to use an external element to determine the width and height of the plot area, for example, Grid or Border

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

All Articles