我一直在使用OxyPlot来完成我目前正在进行的项目之一,并且它运行得非常好。但是,我无法解决在一个窗口中绘制多个图形的问题。我的目标是能够做像this这样的事情。
我目前的单曲情节看起来像this。
这是我的一些代码
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”属性设置为填充的行,因为这导致大小的变化无效。然后,您可以调整窗体的大小和单独的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程序并添加到其中。这可以找到here
我使用TableLayoutPanel在不同的行和列中排列单独的图。我打算以编程方式添加图形,因此我可以添加行并将它们停靠在布局中。