在图表上绘制矩形
本文关键字:绘制 | 更新日期: 2023-09-27 18:03:28
我是c#新手,我想在图表(system . windows . forms . datavisvisualization . charting.chart)上画一些矩形。我试着看了一些教程,尽管大多数都是关于在表格上画画,而不是在图表上。
下面是我根据一些教程编写的一些代码。它不工作,图表只是空白。
public partial class Form1 : Form
{
private RectangleF r1;
private RectangleF r2;
public Form1()
{
r1.X = 10;
r1.Y = 10;
r1.Width = 20;
r1.Height = 20.5F;
r1.X = 100;
r1.Y = 100;
r1.Width = 200;
r1.Height = 300;
System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
InitializeComponent();
this.chart1.PostPaint += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs>(this.PostPaint);
}
private void PostPaint(object sender, System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs e)
{
e.ChartGraphics.GetAbsoluteRectangle(r1);
}
}
在我创建的这个例子中,只绘制了第二个矩形,图表根本没有显示轴。我想用浮点数画多个三角形
public partial class Form1 : Form
{
private Rectangle r1;
private Rectangle r2;
public Form1()
{
r1.X = 10;
r1.Y = 10;
r1.Width = 20;
r1.Height = 20;
r1.X = 100;
r1.Y = 100;
r1.Width = 200;
r1.Height = 300;
System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
InitializeComponent();
this.chart1.PostPaint += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs>(this.PostPaint);
}
private void PostPaint(object sender, System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs e)
{
e.ChartGraphics.Graphics.DrawRectangle(new Pen(Color.Red, 3), r1);
e.ChartGraphics.Graphics.DrawRectangle(new Pen(Color.Black, 5), r2);
}
}
在这两个示例中,您为r1
设置了两次值,而不是r1
和r2
。
在您的第一个示例中,GetAbsoluteRectangle
没有绘制矩形。它用于转换坐标。您应该像在第二个示例中那样使用DrawRectangle
。
修改
r1.X = 100;
r1.Y = 100;
r1.Width = 200;
r1.Height = 300;
By that:
r2.X = 100;
r2.Y = 100;
r2.Width = 200;
r2.Height = 300;