日期:2014-05-18  浏览次数:20824 次

求帮忙,如何用C#绘制曲线图?
我想设计一个程序,用C#代码来读取数据库中的血压数值,然后把血压以曲线图的形式画出来
纵轴上的血压分为sbp(收缩压),dbp(舒张压)
横轴是日期时间(time)

着是我自己写的代码,调试的时候,没有提示语法错误,但是,picturebox控件上不显示图形,不知道为什么

C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
            Db db = new Db();
            textBox1.Text = "1";
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Db db = new Db();
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Black, 2);
            SolidBrush sb = new SolidBrush(Color.Green);
            Point[] xpts = new Point[3]{
                new Point(pictureBox1.Width-35,pictureBox1.Height-32),
                new Point(pictureBox1.Width-35,pictureBox1.Height-28),
                new Point(pictureBox1.Width-30,pictureBox1.Height-30)
                                       };
            g.DrawLine(pen, 50, pictureBox1.Height - 30, pictureBox1.Width - 30, pictureBox1.Height - 30);
            g.DrawPolygon(pen, xpts);
            g.DrawString("时间", new Font("宋体", 9), sb, pictureBox1.Width - 30, pictureBox1.Height - 30);
            Point[] ypts = new Point[3]{
                  new Point(48,55),
                  new Point(50,50),
                  new Point(52,55)   
            };
            g.DrawLine(pen, 50, pictureBox1.Height - 30, 50, 50);
            g.DrawPolygon(pen, ypts);
            g.DrawString("/mmHg", new Font("宋体", 9), sb, 50, 50);
            g.DrawString("50", new Font("宋体", 9), sb, 30, pictureBox1.Height - 35);
            for (int j = 0; j < 9; j++)
            {
                g.DrawLine(pen, 50, pictureBox1.Height - 30 - (j + 1) * (pictureBox1.Height - 80) / 10, 55, pictureBox1.Height - 30 - (j + 1) * (pictureBox1.Height - 80) / 10);
                g.DrawString(((j + 1) * 13 + 50).ToString(), new Font("宋体", 9), sb, 30, pictureBox1.Height - 35 - (j + 1) * (pictureBox1.Height - 80) / 10);

            }
            string sql = "select sbp,dbp,time from bp where pnum='" + textBox1.Text + "'";
            string[,] a = db.doSelect(sql);
            int n = 0;
            n = a.GetLength(0);
            //取出的数据的行数为n;
            int i = 0;
            for (i = 0; i < n - 1; i++)
            {
                g.DrawLine(new Pen(Color.Blue), 50 + (i + 1) * (pictureBox1.Width - 80) / (n + 1), pictureBox1.Height - 30 - (int.Parse(a[i, 0]) - 50) * (pictureBox1.Height - 80) / 130, 50 + (i + 2) * (pictureBox1.Width - 80) / (n + 1), pictureBox1.Height - 30 - (int.Parse(a[i + 1, 0]) - 50) * (pictureBox1.Height - 80) / 130);
                g.DrawLine(new Pen(Color.Red), 50 + (i + 1) * (pictureBox1.Width - 80) / (n + 1), pictureBox1.Height - 30 - (int.Parse(a[i, 1]) - 50) * (pictureBox1.Height - 80) / 130, 50 + (i + 2) * (pictureBox1.Width - 80) / (n + 1), pictureBox1.Height - 30 - (int.Parse(a[i + 1, 1]) - 50) * (pictureBox1.Height - 80) / 130);
                g.DrawLine(new Pen(Color.Black), 50 + (i + 1) * (pictureBox1.Width - 80) / (n + 1), pictureBox1.Height - 35, 50 + (i + 1) * (pictureBox1.Width - 80) / (n + 1), pictureBox1.Height - 30);
                g.DrawString(a[i, 2], new Font("宋体", 9), sb, 20 + (i + 1) * (pictureBox1.Width - 80) / (n + 1), pictureBox1.Height - 28);

            }
            g.DrawLine(new Pen(Color.Blue)