我是C#的新手,我想在图表上绘制一些矩形(System.Windows.Forms.DataVisualization.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);
}
}
答案 0 :(得分:1)
在这两个示例中,您要将r1
的值设置为两次,而不是r1
和r2
。
在第一个示例中,GetAbsoluteRectangle
不会绘制矩形。它用于转换坐标。您应该像第二个示例中那样使用DrawRectangle
。
答案 1 :(得分:0)
只需更改
r1.X = 100;
r1.Y = 100;
r1.Width = 200;
r1.Height = 300;
由此:
r2.X = 100;
r2.Y = 100;
r2.Width = 200;
r2.Height = 300;
它会 - 正常 - 工作正常。