学习了GDI+ 做了个简单的折线图。

本想做个窗体,然后打开来的时候有一个表传进去作为参数,,目前只实现了画图。所以构造函数中出现了尴尬的

public Form1_quxian(DataTable table)
        {
            InitializeComponent();
        }

还有个问题暂时没解决的就是发现打开窗体的时候并没有出现坐标轴,当鼠标移动到按钮的时候才出现,而且当窗体失去焦点或者被其他窗体盖住后再获得焦点,那么画的曲线就消失了。

 

View Code
 1 namespace zhexiantu{
2 public partial class Form1_quxian : Form
3 {
4 public Form1_quxian()
5 {
6 InitializeComponent();
7
8 }
9 string strCon = @"Data Source=pig\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;password=12345;";//;Integrated Security=True
10
11 public Form1_quxian(DataTable table)
12 {
13 InitializeComponent();
14 }
15
16 private void Form1_quxian_Load(object sender, EventArgs e)
17 {
18
19 }
20
21 private void Form1_quxian_Paint(object sender, PaintEventArgs e)
22 {
23 int height = panel1.Height;
24 int width = panel1.Width;
25 Graphics g = panel1.CreateGraphics();
26 Pen pblack = new Pen(Color.Black,2);
27 pblack.EndCap = LineCap.ArrowAnchor;//箭头
28 g.DrawLine(pblack, 50, 8 * height / 9, 50, 0);//y轴
29 Pen p1 = new Pen(Color.Brown);
30 p1.DashStyle = DashStyle.Dash;
31
32 for (int i = 0; i < 8; i++)
33 {//虚线
34 g.DrawLine(p1, 50, i * height / 9, width - 10, i * height / 9);
35 }
36 g.DrawLine(pblack, 50, 8 * height / 9, width - 10, 8 * height / 9);//x轴
37 }
38
39 private void button1_Click(object sender, EventArgs e)
40 {
41 int height = panel1.Height;
42 dbhelp helper = new dbhelp(strCon);
43 DataTable table = helper.ExecuteDataTable("select ProductID,UnitPrice from Products");
44
45 int rowNum = table.Rows.Count;
46 int maxvalue = 0;
47 int[] x1 = new int[rowNum];//类型or时间
48 int[] x2 = new int[rowNum];//
49 int Pheight = panel1.Height;
50 int width = (panel1.Width) / rowNum;
51
52 Graphics g = panel1.CreateGraphics();
53
54 for (int i = 0; i < rowNum; i++)
55 {
56 x1[i] = Convert.ToInt32(table.Rows[i][0]);
57 x2[i] = Convert.ToInt32(table.Rows[i][1]);
58 if (x2[i] > maxvalue)
59 { maxvalue = x2[i]; }
60 }
61 Pen p2 = new Pen(Color.Red, 2);
62 Font f1 = new Font("Times New Roman",12,FontStyle.Bold,GraphicsUnit.Pixel);
63 SolidBrush sb1 =new SolidBrush(Color.Brown);
64 for (int i = 0; i < 9; i++ )
65 {
66
67 g.DrawString(Convert.ToString((9-i) * maxvalue / 9), f1, sb1, 20, (i * height / 9)-5);
68 }
69 for (int i = 1; i < rowNum; i++)
70 {
71
72 g.DrawLine(p2, 50+(i - 1) * width, Pheight - x2[i - 1], 50+(i) * width, Pheight - x2[i]);
73 Thread.Sleep(50);
74 }
75 }

 


 

你可能感兴趣的:(学习)