[英]OxyPlot: Plotting several graphs on one window using OxyPlot and WindowsForms
我一直在將 OxyPlot 用於我目前正在進行的項目之一,並且效果很好。 但是,我一直無法解決在一個窗口中繪制多個圖形的問題。 我的目標是能夠做到像這樣。
我目前的單一情節是這樣的。
這是我的一些代碼
var myModel = new PlotModel { Title = "REBA Score" };
myModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Minimum = 0, Maximum = a_count, Title = "Frame" });
myModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Minimum = 0, Maximum = 15, Title = "Score" });
var series1 = new LineSeries
{
StrokeThickness = 1,
MarkerSize = 1,
};
for (int i = 0; i < a_count; i++)
{
int x_val = i;
int y_val = scores[0, i]; //"scores" is an array with my data
series1.Points.Add(new DataPoint(x_val, y_val));
}
myModel.Series.Add(series1);
this.plot1.Model = myModel //plot1 is an object of PlotView
我想要做的是對幾個不同的數據數組使用相同的“創建線性系列 -> 填充 -> 繪圖”方法,並使其顯示如前面提到的那樣。 我在這個項目中使用 WindowsForms 和 OxyPlot。
這是我使用 bgura 的推薦所做的。
我首先創建了多個 PlotView 並設置了它們的屬性。 我改變了它們的大小,這樣它們就不會占據整個窗口,並且我確保注釋掉“Dock”屬性設置為填充的行,因為這會導致大小的變化不起作用。 然后,您可以調整 Form 和單個 PlotView 的大小以實現多圖效果。
private OxyPlot.WindowsForms.PlotView plot1;
private OxyPlot.WindowsForms.PlotView plot2;
private OxyPlot.WindowsForms.PlotView plot3;
private void InitializeComponent()
{
this.plot1 = new OxyPlot.WindowsForms.PlotView();
this.plot2 = new OxyPlot.WindowsForms.PlotView();
this.plot3 = new OxyPlot.WindowsForms.PlotView();
this.SuspendLayout();
//
// plot1
//
//this.plot1.Dock = System.Windows.Forms.DockStyle.Fill;
this.plot1.Location = new System.Drawing.Point(0, 0);
this.plot1.Name = "plot1";
this.plot1.PanCursor = System.Windows.Forms.Cursors.Hand;
this.plot1.Size = new System.Drawing.Size(300, 300);
this.plot1.TabIndex = 0;
this.plot1.Text = "plot1";
this.plot1.ZoomHorizontalCursor = System.Windows.Forms.Cursors.SizeWE;
this.plot1.ZoomRectangleCursor = System.Windows.Forms.Cursors.SizeNWSE;
this.plot1.ZoomVerticalCursor = System.Windows.Forms.Cursors.SizeNS;
//
// plot2
//
//this.plot2.Dock = System.Windows.Forms.DockStyle.Fill;
this.plot2.Location = new System.Drawing.Point(300, 0);
this.plot2.Name = "plot2";
this.plot2.PanCursor = System.Windows.Forms.Cursors.Hand;
this.plot2.Size = new System.Drawing.Size(300, 300);
this.plot2.TabIndex = 0;
this.plot2.Text = "plot2";
this.plot2.ZoomHorizontalCursor = System.Windows.Forms.Cursors.SizeWE;
this.plot2.ZoomRectangleCursor = System.Windows.Forms.Cursors.SizeNWSE;
this.plot2.ZoomVerticalCursor = System.Windows.Forms.Cursors.SizeNS;
//
// plot3
//
//this.plot3.Dock = System.Windows.Forms.DockStyle.Fill;
this.plot3.Location = new System.Drawing.Point(900, 600 );
this.plot3.Name = "plot3";
this.plot3.PanCursor = System.Windows.Forms.Cursors.Hand;
this.plot3.Size = new System.Drawing.Size(300,300);
this.plot3.TabIndex = 0;
this.plot3.Text = "plot3";
this.plot3.ZoomHorizontalCursor = System.Windows.Forms.Cursors.SizeWE;
this.plot3.ZoomRectangleCursor = System.Windows.Forms.Cursors.SizeNWSE;
this.plot3.ZoomVerticalCursor = System.Windows.Forms.Cursors.SizeNS;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1225, 900);
this.Controls.Add(this.plot3);
this.Controls.Add(this.plot1);
this.Controls.Add(this.plot2);
this.Name = "Form1";
this.Text = "plot3 Score";
this.ResumeLayout(false);
}
然后我創建了多個 PlotModel 並用數據填充它們,並將它們分配給相應的 PlotView:
this.InitializeComponent();
//Setup plot1
var plot1_model = new PlotModel { Title = "plot1 Score" };
plot1_model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Minimum = 0, Maximum = a_count, Title = "Frame" });
plot1_model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Minimum = 0, Maximum = 15, Title = "Score" });
var plot1_Series = new LineSeries { StrokeThickness = 1, MarkerSize = 1 };
for (int i = 0; i < a_count; i++)
{
int x_val = i;
int y_val = your_data[0, i];
plot1_Series.Points.Add(new DataPoint(x_val, y_val));
}
plot1_model.Series.Add(plot1_Series);
//Setup plot2
var plot2_Model = new PlotModel { Title = "plot2 Score" };
plot2_Model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Minimum = 0, Maximum = a_count, Title = "Frame" });
plot2_Model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Minimum = 0, Maximum = 15, Title = "Score" });
var plot2_Series = new LineSeries { StrokeThickness = 1, MarkerSize = 1 };
for (int i = 0; i < a_count; i++)
{
int x_val = i;
int y_val = your_data[1, i];
plot2_Series.Points.Add(new DataPoint(x_val, y_val));
}
plot2_Model.Series.Add(plot2_Series);
//Setup plot3
var plot3_Model = new PlotModel { Title = "plot3 Score" };
plot3_Model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Minimum = 0, Maximum = a_count, Title = "Frame" });
plot3_Model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Minimum = 0, Maximum = 15, Title = "Score" });
var plot3_Series = new LineSeries{ StrokeThickness = 1, MarkerSize = 1};
for (int i = 0; i < a_count; i++)
{
int x_val = i;
int y_val = your_data[3, i];
plot3_Series.Points.Add(new DataPoint(x_val, y_val));
}
this.plot1.Model = plot1_Model;
this.plot2.Model = plot2_model;
this.plot3.Model = plot3_Model;
希望這可以幫助需要實現相同功能的人。 如果有人想知道,我幾乎使用了 OxyPlot 文檔中的 HelloWorld 程序並將其添加到其中。 可以在這里找到
我使用TableLayoutPanel在不同的行和列中排列單獨的圖。 我計划以編程方式添加圖形,以便我可以添加行並將它們停靠在布局中。
當我遇到同樣的問題時,我想建議我對此主題的解決方案。 如果您想在一個 Plotview 中繪制多個圖形(具有不同的比例),您不需要每次都創建一個新的 Plotview。 只需為要添加新圖形的軸分配一個“AxisKey”。
例如:這些是軸:
Dim xAxes As New OxyPlot.Axes.DateTimeAxis()
With xAxes
.Key = "x_first_Time"
.Position = Axes.AxisPosition.Bottom
.StringFormat = "HH:mm:ss"
.Unit = "ms"
.Title = "Zeitintervall"
.TitleFont = "Arial"
.TitleFontSize = 18.0!
.TitleFontWeight = FontWeights.Bold
.MajorGridlineStyle = LineStyle.Dot
.MajorGridlineThickness = 0.5
End With
model.Axes.Add(xAxes)
Dim yAxestemp As New OxyPlot.Axes.LinearAxis()
With yAxestemp
.Position = Axes.AxisPosition.Left
.Unit = "deg Celsius"
.Key = "y_first_Temp"
.TitlePosition = 0.5
.AxisTitleDistance = 10
.Title = "Temperatur "
.TitleFont = "Arial"
.TitleFontSize = 18.0!
.TitleFontWeight = FontWeights.Bold
.MajorGridlineStyle = LineStyle.Dot
.MajorGridlineThickness = 0.5
End With
model.Axes.Add(yAxestemp)
Dim yAxesRPM As New OxyPlot.Axes.LinearAxis()
With yAxesRPM
.Position = Axes.AxisPosition.Right
.Unit = "1/min"
.Key = "y_second_RPM"
.TitlePosition = 0.5
.AxisTitleDistance = 10
.Title = "Drehzahl "
.TitleFont = "Arial"
.TitleFontSize = 18.0!
.TitleFontWeight = FontWeights.Bold
End With
model.Axes.Add(yAxesRPM)
Plot.Model = model
系列看起來像這樣:
With Sensor1series
.Title = "Sensor_1"
.YAxisKey = "y_first_Temp"
End With
model.Series.Add(Sensor1series)
With Sensor2series
.Title = "Sensor_2"
.YAxisKey = "y_first_Temp"
End With
model.Series.Add(Sensor2series)
With Sensor3series
.Title = "Sensor_3"
.YAxisKey = "y_first_Temp"
End With
model.Series.Add(Sensor3series)
With Sensor4series
.Title = "Sensor_4"
.YAxisKey = "y_first_Temp"
End With
model.Series.Add(Sensor4series)
With drehzahlseries
.Title = "Drehzahl [rpm]"
.LineStyle = LineStyle.Dot
.YAxisKey = "y_second_RPM"
End With
model.Series.Add(drehzahlseries)
為了用一些數據填充這些系列,我在另一個類中創建了一個包含數據的列表。 這是填充系列的示例:
Sensor1series.Points.Add(New OxyPlot.DataPoint(Time, Datalist.Last.tempvalue1))
Sensor2series.Points.Add(New OxyPlot.DataPoint(Time, Datalist.Last.tempvalue2))
Sensor3series.Points.Add(New OxyPlot.DataPoint(Time, Datalist.Last.tempvalue3))
Sensor4series.Points.Add(New OxyPlot.DataPoint(Time, Datalist.Last.tempvalue4))
drehzahlseries.Points.Add(New OxyPlot.DataPoint(Time, Datalist.Last.RPM))
TextBox_Time.Text = Datalist.Last.time.ToString()
這應該是這樣的,Rpm 位於輔助軸上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.