首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求帮忙,怎么用C#绘制曲线图

2012-04-12 
求帮忙,如何用C#绘制曲线图?我想设计一个程序,用C#代码来读取数据库中的血压数值,然后把血压以曲线图的形

求帮忙,如何用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), 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);        }    }   } 



其中db是我实现申明的类
doselect是其中的方法
C# code
public string[,] doSelect(string sql) {            SqlConnection conn = new SqlConnection();//建立连接            conn.ConnectionString = cnString;            //设置数据库连接的参数,包括登录的服务器名,和数据库文件所在的位置            conn.Open();//打开数据库            SqlCommand cmd = new SqlCommand();//新建一个数据库命令参数            cmd.Connection = conn;//这个参数将作用于conn所连接的数据库            cmd.CommandType = CommandType.Text;            cmd.CommandText = sql;//执行数据库命令,得到数据            DataSet shuju = new DataSet();//新建一个数据库            SqlDataAdapter adt = new SqlDataAdapter();            adt.SelectCommand = cmd;//            adt.Fill(shuju, "shuju");//将取出来到的数据放到userinfo表中            conn.Close();            if (shuju.Tables["shuju"].Rows.Count > 0)            {                string[,] x = new string[shuju.Tables["shuju"].Rows.Count, shuju.Tables["shuju"].Columns.Count];                for (int i = 0; i < shuju.Tables["shuju"].Rows.Count; i++)                {                    for (int j = 0; j < shuju.Tables["shuju"].Columns.Count; j++)                    {                        x[i, j] = shuju.Tables["shuju"].Rows[i][j].ToString();                    }                }                return x;            }            else            {                string[,] x = new string[1, 1];                x[0, 0] = "";                return x;            }                }


[解决办法]
何不用MSChart
或者Silverlight的Chart控件呢?
[解决办法]
看代码似乎木有问题,,,,调试一下看一下你的二维数值
[解决办法]
你这段代码中没显示图的原因是你把注册事件的代码给注释了!
[解决办法]
vs2010只要用Chart控件 
using System.Web.UI.DataVisualization.Charting;


 DataSet dsHis =GetData();

//if (dsHis.Tables[0].Rows.Count == 0)
//{
// return;

//}
Chart1.ImageLocation = "GaugeImages";
//Chart1.
//
Title title1 = new Title();
title1.Name = "Title1";
title1.Text = "历史曲线"; 
Chart1.Titles.Add(title1);


Legend legend = new Legend();
legend.Name = "Legend1";
Chart1.Legends.Add(legend);

//////////////
//qqq

Chart1.Series.Clear();
Chart1.ChartAreas[0].Position.Auto = true;
Chart1.ChartAreas[0].InnerPlotPosition.Auto = true;
//GETTIME,AREAID,SF6VALUE,O2VALUE
Series series;
foreach (DataColumn column in dsHis.Tables[0].Columns)
{
series = null;
switch (column.ColumnName)
{
case "TVALUE":
series = Chart1.Series.Add("温度(℃)");
series.YValueMembers = "TVALUE";
series.ToolTip = "温度:#VAL \n采集时间:#VALX{MM-dd HH:mm}";
//WeatherChart2.Series.Add(series);
break;


default:
break;
}//end switch (column.ColumnName)
if (series != null)
{
series.ChartType = SeriesChartType.Spline;
series.MarkerStyle = MarkerStyle.Circle;


series.MarkerSize = 5;
series.BorderWidth = 2;
series.XValueMember = "GETTIME";
series.XValueType = ChartValueType.DateTimeOffset;
}
}//end foreach DataColumn column in ds.Tables[0].Columns


Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MM-dd HH:mm";
Chart1.DataSource = dsHis;
Chart1.DataBind();

热点排行