C# WPF OxyPlot x, y 应该有相同的比例

C# WPF OxyPlot x, y should have the same scaling

不幸的是,我不知道如何调整坐标轴缩放比例。 x 和 y 轴应具有相同的缩放比例。例如,您在下面看到两张图片。一次使用蓝色笔触(相同缩放比例),一次使用红色笔触(不同缩放比例)。我怎样才能调整只出现相同的缩放比例(请参见带有蓝色笔触的图片)。 x 和 y 具有相同的距离,例如从 0(开始)到 10(结束)。它应该 而不是 与带有红色笔划的图片具有不同的缩放比例。后面应该可以说x轴和y轴正好等于点的距离

有人可以帮我做同样的缩放吗?

我可以自己定义值范围吗?既然说从 -12 到 + 12?

XAML

<UserControl x:Class="Vorschau.UCVorschau"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Vorschau"
             xmlns:oxy="http://oxyplot.org/wpf"
             mc:Ignorable="d" Height="461" Width="624">
    <Canvas HorizontalAlignment="Left" Height="461" VerticalAlignment="Top" Width="624">
        <Button x:Name="btGenerate" Content="Generiere Koordinaten" Canvas.Left="25" Canvas.Top="409" Click="btGenerate_Click"/>
        <oxy:Plot x:Name="oxyPlot" Title="{Binding Title}" Height="350" Canvas.Left="224" Width="350" Background="#FFD1CFD0">
            <oxy:Plot.Axes>
                <oxy:LinearAxis Position="Bottom" MinimumPadding="0.1" MaximumPadding="0.1"/>
                <oxy:LinearAxis Position="Left" MinimumPadding="0.1" MaximumPadding="0.1"/>
            </oxy:Plot.Axes>

            <oxy:Plot.Series>
                <oxy:LineSeries x:Name="ls" ItemsSource="{Binding Points}" LineStyle="None"  MarkerType="Circle" MarkerSize="5" MarkerFill="Transparent" MarkerStroke="Black" MarkerStrokeThickness="2"/>
            </oxy:Plot.Series>
        </oxy:Plot>
        <Button x:Name="btClear" Content="Koordianten löschen" Canvas.Left="181" Canvas.Top="409" Click="btClear_Click"/>
        <Button x:Name="btRead" Content="Koordianten einlesen" Canvas.Left="473" Canvas.Top="409" Click="btRead_Click"/>
        <Canvas HorizontalAlignment="Left" Height="579" VerticalAlignment="Top" Width="5" Background="#FFB8B8B8" RenderTransformOrigin="0.5,0.5" Canvas.Left="311" Canvas.Top="106">
            <Canvas.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="-1"/>
                    <SkewTransform/>
                    <RotateTransform Angle="90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Canvas.RenderTransform>
        </Canvas>
    </Canvas>
</UserControl>

用户控件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OxyPlot;
using OxyPlot.Series;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Globalization;
using OxyPlot.Wpf;
using System.Diagnostics;

namespace Vorschau
{
    /// <summary>
    /// Interaktionslogik für UCVorschau.xaml
    /// </summary>
    public partial class UCVorschau : UserControl
    {
        public UCVorschau()
        {
            InitializeComponent();
            try
            {
                DataContext = this;
                this.Title = "Vorschaubild";
                oxyPlot.Width = Vorschau.Properties.Settings.Default.BreiteBitmap;
                oxyPlot.Height = Vorschau.Properties.Settings.Default.HoeheBitmap;
                if (Vorschau.Properties.Settings.Default.MarkerTyp == 0)
                {
                    ls.MarkerType = MarkerType.Circle;
                    ls.MarkerFill = System.Windows.Media.Colors.Transparent;
                    ls.MarkerStrokeThickness = Vorschau.Properties.Settings.Default.Kreisdurchmesser;
                }
                else
                {
                    ls.MarkerType = MarkerType.Square;
                    ls.MarkerFill = System.Windows.Media.Colors.Black;
                    ls.MarkerStroke = System.Windows.Media.Colors.Black;
                    ls.MarkerStrokeThickness = 2;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("Es trat ein unerwarteter Fehler auf. \nBitte Starten Sie die Anwendung neu." + error.ToString());
            }

        }
        public string Title { get; set; }
        public IList<DataPoint> Points { get; private set; }

        /// <summary>
        /// Einstelungs-Fenster wird geöffnet
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //btEinstellung


        private void btGenerate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DateTime startZeit = DateTime.Now;
                Cursor = Cursors.Wait;
                if (Vorschau.Properties.Settings.Default.MarkerTyp == 0)
                {
                    ls.MarkerType = MarkerType.Circle;
                    ls.MarkerFill = System.Windows.Media.Colors.Transparent;
                    ls.MarkerStrokeThickness = Vorschau.Properties.Settings.Default.Kreisdurchmesser;
                }
                else
                {
                    ls.MarkerType = MarkerType.Square;
                    ls.MarkerFill = System.Windows.Media.Colors.Black;
                    ls.MarkerStroke = System.Windows.Media.Colors.Black;
                    ls.MarkerStrokeThickness = 2;
                }
                double zufallszahlX;
                double zufallszahlY;

                double XMax = 10;
                double XMin = 0;
                double YMax = 10;
                double YMin = 0;
                // Zur Erstellung des Seeds
                int h = DateTime.Now.Hour;
                int m = DateTime.Now.Minute;
                int s = DateTime.Now.Second;
                String u = h.ToString() + m.ToString() + s.ToString();
                int iu = Int32.Parse(u);
                Random zufall = new Random(iu);
                Console.WriteLine("______________");
                CultureInfo en = new CultureInfo("en-US", false); // Damit ein Punkt ist anstatt ein Komma
                DataContext = this;
                this.Points = new List<DataPoint>();
                System.IO.File.WriteAllText(((Environment.CurrentDirectory + @"\files\koordinaten.txt")), string.Empty);
                using (var fileStream = new FileStream(String.Format(Environment.CurrentDirectory + @"\files\koordinaten.txt"), FileMode.OpenOrCreate))
                {
                    using (var streamWriter = new StreamWriter(fileStream))
                    {
                        for (int i = 0; i < Vorschau.Properties.Settings.Default.AnzahlKoordinaten; i++)
                        {
                            zufallszahlX = zufall.NextDouble() * (XMax - XMin) + XMin;
                            zufallszahlY = zufall.NextDouble() * (YMax - YMin) + YMin;
                            //Console.WriteLine("( " + zufallszahlX + " / " + zufallszahlY + " )" + " |" + i);
                            streamWriter.WriteLine("( " + zufallszahlX.ToString(en.NumberFormat) + " / " + zufallszahlY.ToString(en.NumberFormat) + " )" + " |" + (i + 1));
                            //ls.addPoint(zufallszahlX, zufallszahlY);
                            Points.Add(new DataPoint(zufallszahlX, zufallszahlY));
                        }
                        ls.ItemsSource = Points;
                    }
                }
                Cursor = Cursors.Arrow;
                DateTime endZeit = DateTime.Now;
                TimeSpan gemesseneZeit = endZeit - startZeit;
                // statusbar.Text = "Gemessen Zeit für den Durchlauf: " + gemesseneZeit;
            }
            catch (Exception)
            {
                MessageBox.Show("Hoppla, da lief etwas schief.\nLeider konnten keine Koordinaten generiert werden.\nVersuchen Sie es bitte erneut.", "", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btClear_Click(object sender, RoutedEventArgs e)
        {
            //botAxis.Minimum = botAxis.InternalAxis.ActualMinimum;
            //botAxis.Maximum = botAxis.InternalAxis.ActualMaximum;
            //lefAxis.Minimum = lefAxis.InternalAxis.ActualMinimum;
            //lefAxis.Maximum = lefAxis.InternalAxis.ActualMaximum;
            ls.ItemsSource = null;
        }

        private void btRead_Click(object sender, RoutedEventArgs e)
        {
            DateTime startZeit = DateTime.Now;
            Cursor = Cursors.Wait;
            String line;

            Console.WriteLine("___________");
            Console.WriteLine("Koordinaten: ");
            using (System.IO.StreamReader file = new System.IO.StreamReader(Environment.CurrentDirectory + @"\files\koordinaten.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    Regex myRegexKF = new Regex(@"^(?<koordinaet>\S\s*\S*\d+\s*\S\s*\S*\d+\s*\S)\s*\S\d+", RegexOptions.IgnoreCase);
                    Match matchSuccess = myRegexKF.Match(line);
                    if (matchSuccess.Success)
                    {
                        String koordinaten = matchSuccess.Groups["koordinate"].Value;
                        System.Console.WriteLine(koordinaten);
                    }
                }
            }
            System.Console.ReadLine();
            Cursor = Cursors.Arrow;
            DateTime endZeit = DateTime.Now;
            TimeSpan gemesseneZeit = endZeit - startZeit;
        }
    }
}

我认为原创 documentation 可以解决您的问题。

1) 要设置轴 Maximum\Minimum,请为 LinearAxis.

设置相同的命名属性 Xaml

2) 要定义特定的轴标记步骤,请使用 Major/minor intervals of LinearAxis

设置两个轴的MajorStep相同:

<oxy:Plot.Axes>
    <oxy:LinearAxis ... MajorStep="1"/>
    <oxy:LinearAxis ... MajorStep="1"/>
</oxy:Plot.Axes>

请注意,您可能还想控制其他类似属性:MinorStepMajorTickSizeMinorTickSize

另请注意,标签 放在 Major Ticks 上。

在现代 OxyPlot 2.0+ 中你应该设置

plotModel.PlotType = PlotType.Cartesian;

它会限制比例,但可能会出现较小的缩放和填充问题。