GDI + C#绘制图形 [英] GDI+ C# Drawing a graph

查看:62
本文介绍了GDI + C#绘制图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在GDI +中绘制图形,我在图形中使用了一个学生对象,我有使用模块名称填充图形x轴的代码,但是在映射方面遇到了挑战y轴对应于正确的x轴.我在下面附加了一个我正在使用的OnPaint事件.如何正确绘制y轴.

I am trying to draw a graph in GDI+, I am using a student object in the graph, I have the code to populate the x-axis of the graph with the names of the modules, but I am having challenges with mapping the y-axis to correspond to the correct x-axis. I have attached an OnPaint event that I am using below. How can I map the y-axis to be correct.

protected override void OnPaint(PaintEventArgs e)
       {

           studentResults = StudentViewer.SelectedStudeMarks;
           base.OnPaint(e);

           //HorizontalLine
           e.Graphics.DrawLine(Pens.Black, new Point(20, 330), new Point(330, 330));
           //Vertical Line
           e.Graphics.DrawLine(Pens.Black, new Point(20, 330), new Point(20, 20));

           FontFamily f = new FontFamily("Arial");
           Font font = new Font(f, 8f);


           //Vertical line for the graph
           int marks = 100;
           for (int i = 30; i < 350; i += 30)
           {
               e.Graphics.DrawEllipse(Pens.Black, new Rectangle(new Point(20, i), new Size(5, 5)));
               e.Graphics.FillEllipse(Brushes.Red, new Rectangle(new Point(20, i), new Size(5, 5)));
               e.Graphics.DrawString(marks.ToString(), font, Brushes.Black, new PointF(0, i));
               marks-=10;
           }

           ////Horizontal line
           int j = 0;
           for (int i = 20; i < 390; i += 30)
           {

               e.Graphics.DrawEllipse(Pens.Black, new Rectangle(new Point(i, 330), new Size(5, 5)));
               e.Graphics.FillEllipse(Brushes.Red, new Rectangle(new Point(i, 330), new Size(5, 5)));


           }

           for (int i = 50; i < 390; i+=30)
           {
               if (j <= 5)
                   {
                        e.Graphics.DrawString(studentResults.Courses[j].Name.Substring(0, 3), font, Brushes.Black, new PointF(i, 340));
                       j++;
                   }
                   else
                   {
                       continue;
                   }
           }
           j = 0;

           int z = 390;
           for (int i = 50; i < 390; i += 30)
           {


               if (j <= 5)
               {

                   e.Graphics.DrawEllipse(Pens.Black, new Rectangle(new Point(i, Convert.ToInt32(studentResults.Courses[j].Marks +z)), new Size(5, 5)));
                   e.Graphics.FillEllipse(Brushes.Red, new Rectangle(new Point(i,Convert.ToInt32(studentResults.Courses[j].Marks + z)), new Size(5, 5)));
                   j++;

               }
               else
               {
                   continue;
               }
           }
           e.Graphics.Flush();


       }

推荐答案

刚开始:如果您在方法中使用了硬编码的立即常量,那么所有这20个, 330,5,您将永远不会获得良好的结果,甚至不要尝试.您应该结合使用一些显式声明的常量来参数化所有内容.并且您还应该根据数据计算其中的大多数:图形数据和控件的当前大小或Graphics边界.

另外,不要试图用一种方法来全部编写.使用适当的参数创建一些方法.您显示的处理程序的代码应成为一行或两行代码.

还有什么?另外,无需Flush图形.渲染是通过System.Windows.Forms.Control.Invalidate方法触发的.

您不应该继续支持此代码.您将需要尽快开始对其进行重新编写,否则,您将一事无成.是的,我确定.

—SA
Just to start with: if you use those immediate constants hard-coded in your method, all those 20, 330, 5, you will never get good results, don''t even try. You should parametrize everything in combination with using some explicitly declared constants; and you also should calculate most of them based on data: both graphical data and the current size of the control or Graphics bounds.

Also, don''t try to write it all in one method. Create some methods with appropriate parameters. The code of the handler you show should become a line or two of code.

What else? Also, no need to Flush graphics. The rendering is is triggered through System.Windows.Forms.Control.Invalidate methods.

You should not continue supporting this very code; you will need to start re-writing it as soon as possible, otherwise you will go nowhere. Yes, I''m sure.

—SA


这篇关于GDI + C#绘制图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆