[英]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;
}
}
}
將兩個軸的MajorStep
設置為相同:
<oxy:Plot.Axes>
<oxy:LinearAxis ... MajorStep="1"/>
<oxy:LinearAxis ... MajorStep="1"/>
</oxy:Plot.Axes>
請注意,您可能想要控制其他類似的屬性: MinorStep
、 MajorTickSize
和MinorTickSize
。
另請注意,標簽Major Ticks
在Major Ticks
。
我認為原始文檔將解決您的問題。
1) 要設置軸Maximum\\Minimum
在 Xaml 中為LinearAxis
設置相同的命名屬性。
2) 要定義特定的軸標記步驟,請使用LinearAxis
Major/minor intervals
在現代 OxyPlot 2.0+ 中,您應該設置
plotModel.PlotType = PlotType.Cartesian;
它會限制比例,但可能會出現輕微的縮放和填充問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.