求帮忙,如何用C#绘制曲线图?

wangnadh 2012-03-19 10:34:35
我想设计一个程序,用C#代码来读取数据库中的血压数值,然后把血压以曲线图的形式画出来
纵轴上的血压分为sbp(收缩压),dbp(舒张压)
横轴是日期时间(time)

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


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是其中的方法

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;
}

}




...全文
281 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zldy0438 2013-04-01
  • 打赏
  • 举报
回复
放在哪个public下呢?
「已注销」 2012-03-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xmzhaoyong 的回复:]

你这段代码中没显示图的原因是你把注册事件的代码给注释了!
[/Quote]

找到原因了,的确是少了注册事件,不过不是放在load下的,在public下就好了……
囧啊囧
zincy 2012-03-19
  • 打赏
  • 举报
回复
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();
Pillar_Leung 2012-03-19
  • 打赏
  • 举报
回复
用 mschart 将Type选择Line
深海之蓝 2012-03-19
  • 打赏
  • 举报
回复
还是用 第三方控件吧,mschart,teechart都可以,效果都很好的
「已注销」 2012-03-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xmzhaoyong 的回复:]

你这段代码中没显示图的原因是你把注册事件的代码给注释了!
[/Quote]
??
那个如果说的是这句话的话
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

我调试的时候,没有//掉,照样不行啊
xmzhaoyong 2012-03-19
  • 打赏
  • 举报
回复
你这段代码中没显示图的原因是你把注册事件的代码给注释了!
「已注销」 2012-03-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fdh120 的回复:]

貌似没问题。。。。。。能显出坐标
[/Quote]

囧为什么我这里连坐标都没有……
有人和我说可能是load事件关联的问题……我也不知道该如何弄,有没有可能是这方面的?
xmzhaoyong 2012-03-19
  • 打赏
  • 举报
回复
建议你做成用户控件的形式,你这样做的会存在重绘太频繁。给你个思路不知道适合不适合你!
两个Bitmap,一个画背景、一个画数据图。
不懂装懂 2012-03-19
  • 打赏
  • 举报
回复
貌似没问题。。。。。。能显出坐标
xiaokang559 2012-03-19
  • 打赏
  • 举报
回复
可以用第三方控件嘛,做起来不用这么麻烦的,ACTIVE REPORT还有DUNCHAR都可以很方便的实现的。SILVERLIGHT的CHART只能web使用吧?
莫忘初心svip 2012-03-19
  • 打赏
  • 举报
回复
看代码似乎木有问题,,,,调试一下看一下你的二维数值
欢乐的小猪 2012-03-19
  • 打赏
  • 举报
回复
何不用MSChart
或者Silverlight的Chart控件呢?

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧