C#如何c 绘制折线图如下的填色图?

查看: 5359|回复: 8|关注: 0
【求助】C#如何用PictureBox显示matlab中plot绘制的图形
我用C#调用matlab函数,plot绘制的波形图会在我的C#程序里面弹出一个窗口,我现在不想弹出窗口,想让这个绘制好的图像显示在C#的控件PictureBox里面,请问如何做?我的程序
function test()
s=sin(0.03*t);
axis([0 ]);
谁帮我解决一下,查到好多C++的类似代码,没见有c#的
我也关心这个问题,高手出来啊
期待高手出现!!
我也关心这个问题,高手出来啊
回复 1# bugzpro 的帖子
请问最后找到办法了吗?
:lol :lol :lol :lol
目前没有办法把MATLAB中的绘图成果直接显示在PIC中,只能保存为图片再加载到PIC中
8楼正解,目前没办法
站长推荐 /1
Powered byC#绘制曲线图_百度知道
C#绘制曲线图
-0.0250 ······请教在C#中怎样把这些点绘制成曲线图.0083
0有一系列点.0270
0?十分感谢,如下
提问者采纳
chart类可以实现这个图标功能,使用 DataBindings 属性绑定到数据源。你可以参考微软帮助文档中的chart类中的属性和方法。
提问者评价
按照你说的,真的成功了,好开心,谢谢你!
其他类似问题
您可能关注的推广回答者:
曲线图的相关知识
按默认排序
其他2条回答
& & &i&nbsp.Rows[0][j& & & k& & &; & & &=& &-& &14.DrawString(n[i]; & &nbsp.X&nbsp,&nbsp.Blue); & & &16; &&DataBase=db_TomeO=&=&& & && &nbsp.Width&=& int&/& {&&& & & & &=& & & & &nbsp,& for& &=&nbsp.Y& &z& & //绘制矩形&& && &void&nbsp,& &(int&/& &points[9]; for& & && & &=&( & &k&=&14;new&;(int& &&nbsp.DdrowPic()//绘制图表的方法& &points[11]:<img class="ikqb_img" src="http.DrawLines( &nbsp,&this,& &&-&0; &k&; &=& & &&k& &-& && &14; &points[1]; }& & &&nbsp.Width&nbsp,&x; SqlConnection&& & &nbsp.Y& &=& }&nbsp.X&Uid=-&34)&//Fill方法填充DataSet对象//以上数据库的操作是读取出每个点的数值& String[]& &34);&x;=&Count[9].com/zhidao/pic/item/91ef76c6a7efce1ba5fba827af51f3deb58f650e; &2月&& & &nbsp,& &nbsp,&nbsp,&nbsp://a;int[12]; Brush& & & //创建笔刷&34)& & & &=& &/&m&new&/& & & & & & && &nbsp,&-&nbsp,&x; & & & &nbsp,&-&points& &points[3]; & &(y& & //X轴&*&z& && points[9]; & &2;& &Count[3]; & & & &n& && &nbsp.Red.baidu,&new&&fontO;Arial& & & &1500&; &*&points[0];( & & & &nbsp.Width&& & & & &-& for&& &&=& &nbsp,& }&nbsp.ToString())& & int[]& & & x& &&1000&& & &&0; Pen&nbsp.Y&FontS//绘制垂直线条&nbsp.X&&& & //绘制边框与显示字体& &Con&k,& & &2000&=& x&10.ToString().X&16;34)& &nbsp,&nbsp.Clear(C &nbsp.Width&12)/16因为起始为500&500; &nbsp.Width& &&/& & &;+& SqlDataAdapter& &&nbsp.Width& & &nbsp,&(int&& && & &网站人气指数曲线分析&14;*&; &nbsp.ToString(),& & && &34)& &&Server=WRET-MOSY688YVW\\MRGLL;i& &nbsp. &14; & &/zhidao/wh%3D450%2C600/sign=bf79f11f7d1ed21b799c26e/91ef76c6a7efce1ba5fba827af51f3deb58f650e;3500&&new&nbsp,& points[11]; & &x;=& & & &nbsp.Y&+&=& & &&nbsp.Regular).Regular); & && &&+&B &x;//创建SqlCommand对象&&&j++)&=&x& & & Point[]&&&34)& & //与Y轴数生成有关(y&34)&&; &rect); &*& //创建Size对象&& &=&& &nbsp.SelectCommand&&& &Count[8];34)&& &=& &nbsp.Width&&14; & k&x&& x&& &9; & &12)& & &12月&12; & &nbsp,&nbsp.Width& &=& & & &.Width& & & x& &nbsp,& da.Width&nbsp.00f); & &-&=&& & &points[7];{& &i&&//绘制折线& & Size&& & & &35; &34)& &nbsp.X&=& string&-&Count[11];( x& & &=& &/zhidao/wh%3D600%2C800/sign=fb10a9debaa294fc6a7efce1ba5fba827af51f3deb58f650e; points[4];points[2];x; &;=&cmdtxt2& &&//设置文字内容及输出位置& &nbsp.Y& & & & & &x; &&&&this.Width& & x&&quot,&points[4]; & &nbsp,& &&nbsp.WhiteSmoke); x&=&=&da&=&+&14;=&k&-&/&B-& &=&=& &&/&/& & & Point&nbsp.X&j& && & & points[2]; &-&10;可以省略&nbsp,& & &&&y); &&&+& {& & &&FontSx;/& points[5].Width&0;*& {&};(y& &nbsp.Font(&*&SELECT&12;points); {& & & & &nbsp,&SqlCommand(cmdtxt2; &/&=&-&=& & &&nbsp,&nbsp.X& &12.ToInt32(&DataSet(); & & x&34)&nbsp,&16; & &Com1&.Width& &Count[6];=&Count& & &&nbsp.& &//创建SqlDataAdapter对象&*&S &=&x&nbsp,& & & &=&(j&=&&x; & &-& & &i++)&& &&nbsp.DrawString(m[10&x& & &看你具体情况而定& }&nbsp,& && points[1].Y&nbsp,&9月& & &x& & &( &nbsp.Height&-&=&k&p&=& &nbsp,& &-& &i&-& & && }最后的结果如下; & x&; &x& &=& & x&y& & //创建Rectangle对象&+&4月&x&ds& & &34)&nbsp.DrawString(&Count[4];points[10]; & &Pen(Cx&& & & & &fontT& &nbsp.Width& &12;/& &Rectangle(pointS//声明SQL语句& & & & & &//绘制水平线条&6月& && }&=&14;& & &4500&=&Count[1],&i++)& & &x& & int& &k&=&-& DataSet& & String[]& x&i&7月&+&0).Width&nbsp,&10;=& &14; &nbsp.Red);x& & & & & & &//绘制月份& & & & &=&& //Y&*&/& & & &&10月& //创建画笔&; &nbsp.00f& &+& &new& &Pwd=& &12)&x&-&&nbsp.Width&16&&&&/&Point[12]; && &(Count[10];& for& & & & &5000& & & &-&*&/&14;=& {&=&=&12;+& k&0.R & &new&nbsp,& &=&; & &x; &nbsp.Tables[0];8;k& & & &&x; Graphics&;&-& & &34)&/&nbsp,&华文新魏& & //创建DataSet对象&nbsp.Width&new&); //X& & &g& &=&x;(y& &16; int& & &nbsp.X& &k&k&nbsp.CreateGraphics().DrawLine(p,& & &*&nbsp.hiphotos,& && &=& & &k&&nbsp.Y& &12.Width& &+&10&& & & &points[8];-& Count[j]&=& & &i&nbsp,&&z;(2);10; &=& &points[5]; &=& & &S((brus& x&;&=& & &new&10; & x&nbsp,& &nbsp.D+& SqlCommand&=& &=&& & &x&8月&-&new& &k& &=& & &//创建Graphics对象& & && &points[6];(y& &i&Com1;pointStart& &/&=&/& &SqlConnection(&k& int& & & &=&=&*&nbsp,& & 500& & &Count[2];& points[8]; & & Rectangle& for& &k&14;;&(; &&(y& &nbsp.Y&5月&/& & {& & & & &把你的数据输入进去& & & &-&&2500& &*& &-& & & & &-& & //设置绘制曲线的坐标数组&& &14;C &x& &10; & & &=&12); &i& & //Y轴&& & &nbsp.Y& & &nbsp.Y& &4000&1月& & & & & & &=&new& & int&x; &-&;0; //设置用到的字体&tb_reticulation&quot.X& &i++)&-& & & & &2]; & & &=&34)& & points[7];&/& & & & & & & && &-& &+& k&x&nbsp.jpg" esrc=" & &+&; & &nbsp.DrawRectangle(p; &y& & & && & & &nbsp我只能说给你个代码参考下吧& & & &new& &nbsp://a;=&& &=& & &y& & &-&fontO; & & & & //绘制网格线& & && &-&&nbsp,&&;=&y&nbsp
不明白你想要干嘛啊。。能绘图的软件多得很,干嘛非要用C#编程实现。用Matlab不就马上出来了吗?
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁c#中如何实现在picturebox控件中画一个矩形并填充颜色点击时判断是不是该矩形框并改变填充颜色?_百度知道
c#中如何实现在picturebox控件中画一个矩形并填充颜色点击时判断是不是该矩形框并改变填充颜色?
追问一下,如何判断已经填充的矩形是什么颜色?
提问者采纳
Dispose();&#47.Contains(p))
Graphics g = pictureBox1;
Y=0;比如在 picturebox 有一个矩形 rectA(X=0.FillRectangle(B
Height=200;
private void pictureBox1_Paint(获取鼠标点下的位置
Point p = new Point(e;绘制矩形)public partial class Form1 .Y).CreateGraphics(): Form
public Form1()
InitializeComponent(),这里随你选择
if (e;&#47,以此判断是否选中某个矩形
if (判断鼠标点下的位置是否包含在矩形里面; &#47, 200);&#47, rect);填充颜色
g.Button == System, 0.Left)
Width=100.DrawRectangle(new Pen(Brushes.MouseB
private void pictureBox1_MouseDown(判断按下的是鼠标右键还是左键.Graphics, 2f);&#47.Windows, PaintEventArgs e)
{/&#47, rect);
Rectangle rect = new Rectangle(0.B矩形
private void Form1_Load(&#47.X.Red, e.F&#47, 100;/&#47, MouseEventArgs e)
提问者评价
其他类似问题
picturebox的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁帐号:密码:下次自动登录{url:/nForum/slist.json?uid=guest&root=list-section}{url:/nForum/nlist.json?uid=guest&root=list-section}
贴数:9&分页:thuer发信人: thuer (thuer), 信区: DotNET
标&&题: C# chart控件如何动态绘制多图表区
发信站: 水木社区 (Wed Mar 13 11:03:41 2013), 站内 && 我在网上看到这样添加chart1.ChartAreas.Add("ANOTHER" + i);
但是程序一直有问题,请高手指教啊,多谢啦
画类似下面的图
-- && ※ 修改:·thuer 于 Mar 13 11:04:20 2013 修改本文·[FROM: 221.223.56.*]
※ 来源:·水木社区 ·[FROM: 221.223.56.*]
活着就为改变世界发信人: oldtwo (活着就为改变世界), 信区: DotNET
标&&题: Re: C# chart控件如何动态绘制多图表区
发信站: 水木社区 (Wed Mar 13 11:31:12 2013), 站内 && 实时刷新?
【 在 thuer (thuer) 的大作中提到: 】
: 我在网上看到这样添加chart1.ChartAreas.Add("ANOTHER" + i);
: 但是程序一直有问题,请高手指教啊,多谢啦
: 画类似下面的图
: ...................
&& -- &&&& ---===唯有偏执狂才能成功!===---- &&&& ※ 来源:·水木社区 newsmth.net·[FROM: 222.128.47.*]
thuer发信人: thuer (thuer), 信区: DotNET
标&&题: Re: C# chart控件如何动态绘制多图表区
发信站: 水木社区 (Wed Mar 13 11:46:57 2013), 站内 && 刷新问题不大,就是问如何实现动态绘制如下的图?通过选择,画如下的两个图,或者3个
【 在 oldtwo (活着就为改变世界) 的大作中提到: 】
: 实时刷新?
&& -- && ※ 来源:·水木社区 ·[FROM: 220.181.118.*]
不折腾发信人: lilnelse (不折腾), 信区: DotNET
标&&题: Re: C# chart控件如何动态绘制多图表区
发信站: 水木社区 (Wed Mar 13 11:58:55 2013), 站内 && 你可以下载个dundas chart安装上 里面有demo
能看到源码的 你想要什么效果手动设好了 然后看一下代码就明白了
MS chart据说就是从dundas收购来的 &&&&&& 【 在 thuer (thuer) 的大作中提到: 】
: 我在网上看到这样添加chart1.ChartAreas.Add("ANOTHER" + i);
: 但是程序一直有问题,请高手指教啊,多谢啦
: 画类似下面的图
: ...................
有时,仿佛看见那生路就像一条灰白的长蛇,自己蜿蜒地向我奔来,我等着,等着,看看临
近,但忽然便消失在黑暗里了。 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&----《伤逝》 &&&& ※ 来源:·水木社区 newsmth.net·[FROM: 118.195.65.232]
thuer发信人: thuer (thuer), 信区: DotNET
标&&题: Re: C# chart控件如何动态绘制多图表区
发信站: 水木社区 (Wed Mar 13 14:12:05 2013), 站内 && 我想做的和这个网页是一样的,但是他的代码有问题?
【 在 lilnelse (不折腾) 的大作中提到: 】
: 你可以下载个dundas chart安装上 里面有demo
: 能看到源码的 你想要什么效果手动设好了 然后看一下代码就明白了
: MS chart据说就是从dundas收购来的
-- && ※ 来源:·水木社区 ·[FROM: 220.181.118.*]
不折腾发信人: lilnelse (不折腾), 信区: DotNET
标&&题: Re: C# chart控件如何动态绘制多图表区
发信站: 水木社区 (Wed Mar 13 15:50:52 2013), 站内 && 看了下 觉得思路是对的 就是:
1 chart1.ChartAreas.Add(...)
2 set areas
3 set chart1.Series
4 chart1.Series[i].ChartArea = area[i] && 代码具体没看 手边没环境&&&& 还有 你每次都说有问题 能不能描述具体一点 这样别人也好帮助你
你不说是啥问题 难道让大家猜么? &&&&&& 【 在 thuer (thuer) 的大作中提到: 】
: 我想做的和这个网页是一样的,但是他的代码有问题?
慌乱 && 我怎么才能坐的塌实
我心里揣着危险的种子
我可以点燃一根香烟
也可以点燃一座房子 &&&& ※ 来源:·水木社区 newsmth.net·[FROM: 118.195.65.227]
thuer发信人: thuer (thuer), 信区: DotNET
标&&题: Re: C# chart控件如何动态绘制多图表区
发信站: 水木社区 (Wed Mar 13 16:22:06 2013), 站内 && 不好意思,我以后一定把问题说清楚。
我运行了一下那个例子,程序出现问题,有下面这个提示:
说这一句chart1.ChartAreas.Add("ANOTHER" + i);有问题,说是
“ChartAreaCollection”中已存在名为“ANOTHER0”的图表元素。
【 在 lilnelse (不折腾) 的大作中提到: 】
: 看了下 觉得思路是对的 就是:
: 1 chart1.ChartAreas.Add(...)
: 2 set areas
: ...................
※ 修改:·thuer 于 Mar 13 16:26:44 2013 修改本文·[FROM: 220.181.118.*]
※ 来源:·水木社区 ·[FROM: 220.181.118.*]
不折腾发信人: lilnelse (不折腾), 信区: DotNET
标&&题: Re: C# chart控件如何动态绘制多图表区
发信站: 水木社区 (Wed Mar 13 16:25:04 2013), 站内 && 晕 这种问题自己debug一下啦 何必啥都拿来问。。。
难道是你没有i++?
另外 不知道他initialSeries是咋写的 也可能是这里的问题 &&&&&& 【 在 thuer (thuer) 的大作中提到: 】
: 不好意思,我以后一定把问题说清楚。
: 我运行了一下那个例子,程序出现问题,有下面这个提示:
: “ChartAreaCollection”中已存在名为“ANOTHER0”的图表元素。
: ...................
&& -- && 如果有一天我走了
请忘记我曾经来过 &&&& ※ 来源:·水木社区 newsmth.net·[FROM: 118.195.65.227]
福星狮子发信人: Peugeot888 (福星狮子), 信区: DotNET
标&&题: Re: C# chart控件如何动态绘制多图表区
发信站: 水木社区 (Wed Mar 13 18:54:59 2013), 站内 && 这个比teechart哪个更好更强大些? &&&& 【 在 lilnelse (不折腾) 的大作中提到: 】
: 你可以下载个dundas chart安装上 里面有demo
: 能看到源码的 你想要什么效果手动设好了 然后看一下代码就明白了
: MS chart据说就是从dundas收购来的
: ...................
&& -- && ※ 来源:·水木社区 newsmth.net·[FROM: 120.128.6.*]
文章数:9&分页:评论- 539&
&&&&&&&&&&&
统计图形种类繁多,&有柱状图,&折线图,&扇形图等等,&而统计图形的绘制方法也有很多,&有Flash制作的统计图形,&有水晶报表生成统计图形,&有专门制图软件制作,&也有编程语言自己制作的;这里我们用就C#&制作三款最经典的统计图:&柱状图,&折线图和扇形图;既然是统计,&当然需要数据,&这里演示的数据存于Sql&Server2000中,&三款统计图形都是动态生成.&其中柱状图我会附上制作步骤,&其他两款统计图直接附源码.
说明:&需求不一样,&统计图形绘制后的显示效果也不一样,&比如这里柱状图的主要需求是为了比较每一期报名人数与通过人数的差,&因此会把两根柱子放在一起会使比较结果一目了然.&因此大家可以根据需要灵活绘制.
一.&柱状图的绘制.
绘制步骤如下:
1.&定义绘图用到的类.
定义绘图类int height = 500, width = 700;Bitmap image = new Bitmap(width, height);Graphics g = Graphics.FromImage(image);Pen mypen = new Pen(brush, 1);
2.&绘制图框.
绘制图框g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
3.&绘制横向坐标线
绘制横向坐标线for (int i = 0; i & 14; i++) {g.DrawLine(mypen, x, 80, x, 340);x = x + 40;}
4.&绘制纵向坐标线
绘制纵向坐标线for (int i = 0; i & 9; i++) {g.DrawLine(mypen, 60, y, 620, y);y = y + 26;}
5.&绘制横坐标值
绘制横坐标值String[] n = { "第一期", "第二期", "第三期", "第四期", "全年" };for (int i = 0; i & 7; i++) {g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 348); x = x + 78;}
6.&绘制纵坐标值
绘制纵坐标String[] m = {"250","225", "200", "175", "150", "125", "100&};for (int i = 0; i & 10; i++) {g.DrawString(m[i].ToString(), font, Brushes.Blue, 25, y);y = y + 26;}
7.&定义数组存储数据库中统计的数据
定义存储统计数据的数组int[] Count1 = new int[7]; //存储从数据库读取的报名人数int[] Count2 = new int[7]; //存储从数据库读取的通过人数
8.&从数据库中读取报名人数与通过人数
读取数据SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeT");Con.Open();string cmdtxt2 = "SELECT * FROM ##Count where Company='" + ****+ "'";SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);DataSet ds = new DataSet();da.Fill(ds);
9.&将读取的数据存储到数组中
将数据存储到数组中Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0][&count1&].ToString()); Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0][&count3&].ToString()); Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0][&count2&].ToString()); Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());
10.定义画笔和画刷准备绘图
准备绘制柱状图x = 80; Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);SolidBrush mybrush = new SolidBrush(Color.Red);SolidBrush mybrush2 = new SolidBrush(Color.Green);
11.&根据数组中的值绘制柱状图
绘制柱状图(1)第一期报名人数g.FillRectangle(mybrush, x, 340 - Count1[0], 20, Count1[0]);g.DrawString(Count1[0].ToString(), font2, Brushes.Red, x, 340 - Count1[0] - 15);(2) 第一期通过人数x = x + 20;g.FillRectangle(mybrush2, x, 340 - Count2[0], 20, Count2[0]);g.DrawString(Count2[0].ToString(), font2, Brushes.Green, x, 340 - Count2[0] - 15);
12.&将图形输出到页面.
将页面输出到页中System.IO.MemoryStream ms = new System.IO.MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);Response.ClearContent();Response.ContentType = "image/Jpeg";Response.BinaryWrite(ms.ToArray());
最终柱状图的效果图:
柱状图的完整代码:
绘制柱状统计图的完整代码private void CreateImage(){int height = 500, width = 700;Bitmap image = new Bitmap(width, height);//创建Graphics类对象Graphics g = Graphics.FromImage(image);try{//清空图片背景色g.Clear(Color.White);Font font = new Font("Arial", 10, FontStyle.Regular);Font font1 = new Font("宋体", 20, FontStyle.Bold);LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.BlueViolet, 1.2f, true);g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);// Brush brush1 = new SolidBrush(Color.Blue);g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text + " 成绩统计柱状图", font1, brush, new PointF(70, 30));//画图片的边框线g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);Pen mypen = new Pen(brush, 1);//绘制线条//绘制横向线条int x = 100;for (int i = 0; i & 14; i++){g.DrawLine(mypen, x, 80, x, 340);x = x + 40;}Pen mypen1 = new Pen(Color.Blue, 2);x = 60;g.DrawLine(mypen1, x, 80, x, 340);//绘制纵向线条int y = 106;for (int i = 0; i & 9; i++){g.DrawLine(mypen, 60, y, 620, y);y = y + 26;}g.DrawLine(mypen1, 60, y, 620, y);//x轴String[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" };x = 78;for (int i = 0; i & 7; i++){g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 348); //设置文字内容及输出位置x = x + 78;}//y轴String[] m = {"250","225", "200", "175", "150", "125", "100", " 75"," 50", " 25", " 0"};y = 72;for (int i = 0; i & 10; i++){g.DrawString(m[i].ToString(), font, Brushes.Blue, 25, y); //设置文字内容及输出位置y = y + 26;}int[] Count1 = new int[7];int[] Count2 = new int[7];SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTUid=Pwd=**");Con.Open();string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'";SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);DataSet ds = new DataSet();da.Fill(ds);Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString());Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString());Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString());Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString());Count1[4] = Count1[0] + Count1[1];Count1[5] = Count1[2] + Count1[3];Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString());Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString());Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString());Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString());Count2[4] = Count2[0] + Count2[1];Count2[5] = Count2[2] + Count2[3];Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString());//绘制柱状图.x = 80;Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);SolidBrush mybrush = new SolidBrush(Color.Red);SolidBrush mybrush2 = new SolidBrush(Color.Green);//第一期g.FillRectangle(mybrush, x, 340 - Count1[0], 20, Count1[0]);g.DrawString(Count1[0].ToString(), font2, Brushes.Red, x, 340 - Count1[0] - 15);x = x + 20;g.FillRectangle(mybrush2, x, 340 - Count2[0], 20, Count2[0]);g.DrawString(Count2[0].ToString(), font2, Brushes.Green, x, 340 - Count2[0] - 15);//第二期x = x + 60;g.FillRectangle(mybrush, x, 340 - Count1[1], 20, Count1[1]);g.DrawString(Count1[1].ToString(), font2, Brushes.Red, x, 340 - Count1[1] - 15);x = x + 20;g.FillRectangle(mybrush2, x, 340 - Count2[1], 20, Count2[1]);g.DrawString(Count2[1].ToString(), font2, Brushes.Green, x, 340 - Count2[1] - 15);//第三期x = x + 60;g.FillRectangle(mybrush, x, 340 - Count1[2], 20, Count1[2]);g.DrawString(Count1[2].ToString(), font2, Brushes.Red, x, 340 - Count1[2] - 15);x = x + 20;g.FillRectangle(mybrush2, x, 340 - Count2[2], 20, Count2[2]);g.DrawString(Count2[2].ToString(), font2, Brushes.Green, x, 340 - Count2[2] - 15);//第四期x = x + 60;g.FillRectangle(mybrush, x, 340 - Count1[3], 20, Count1[3]);g.DrawString(Count1[3].ToString(), font2, Brushes.Red, x, 340 - Count1[3] - 15);x = x + 20;g.FillRectangle(mybrush2, x, 340 - Count2[3], 20, Count2[3]);g.DrawString(Count2[3].ToString(), font2, Brushes.Green, x, 340 - Count2[3] - 15);//上半年x = x + 60;g.FillRectangle(mybrush, x, 340 - Count1[4], 20, Count1[4]);g.DrawString(Count1[4].ToString(), font2, Brushes.Red, x, 340 - Count1[4] - 15);x = x + 20;g.FillRectangle(mybrush2, x, 340 - Count2[4], 20, Count2[4]);g.DrawString(Count2[4].ToString(), font2, Brushes.Green, x, 340 - Count2[4] - 15);//下半年x = x + 60;g.FillRectangle(mybrush, x, 340 - Count1[5], 20, Count1[5]);g.DrawString(Count1[5].ToString(), font2, Brushes.Red, x, 340 - Count1[5] - 15);x = x + 20;g.FillRectangle(mybrush2, x, 340 - Count2[5], 20, Count2[5]);g.DrawString(Count2[5].ToString(), font2, Brushes.Green, x, 340 - Count2[5] - 15);//全年x = x + 60;g.FillRectangle(mybrush, x, 340 - Count1[6], 20, Count1[6]);g.DrawString(Count1[6].ToString(), font2, Brushes.Red, x, 340 - Count1[6] - 15);x = x + 20;g.FillRectangle(mybrush2, x, 340 - Count2[6], 20, Count2[6]);g.DrawString(Count2[6].ToString(), font2, Brushes.Green, x, 340 - Count2[6] - 15);//绘制标识Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular);g.DrawRectangle(new Pen(Brushes.Blue), 170, 400, 250, 50); //绘制范围框g.FillRectangle(Brushes.Red, 270, 410, 20, 10); //绘制小矩形g.DrawString("报名人数", font3, Brushes.Red, 292, 408);g.FillRectangle(Brushes.Green, 270, 430, 20, 10);g.DrawString("通过人数", font3, Brushes.Green, 292, 428);System.IO.MemoryStream ms = new System.IO.MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);Response.ClearContent();Response.ContentType = "image/Jpeg";Response.BinaryWrite(ms.ToArray());}finally{g.Dispose();image.Dispose();}}
二. 折线统计图的绘制
折线图的完整代码:
折线图的完整代码private void CreateImage(){int height = 480, width = 700;Bitmap image = new Bitmap(width, height);Graphics g = Graphics.FromImage(image);try{//清空图片背景色g.Clear(Color.White);Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular);Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height);Brush brush1 = new SolidBrush(Color.Blue);Brush brush2 = new SolidBrush(Color.SaddleBrown);g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text + " 成绩统计折线图", font1, brush1, new PointF(85, 30));//画图片的边框线g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);Pen mypen = new Pen(brush, 1);Pen mypen2 = new Pen(Color.Red, 2);//绘制线条//绘制纵向线条int x = 60;for (int i = 0; i & 8; i++){g.DrawLine(mypen, x, 80, x, 340);x = x + 80;}Pen mypen1 = new Pen(Color.Blue, 3);x = 60;g.DrawLine(mypen1, x, 82, x, 340);//绘制横向线条int y = 106;for (int i = 0; i & 10; i++){g.DrawLine(mypen, 60, y, 620, y);y = y + 26;}// y = 106;g.DrawLine(mypen1, 60, y - 26, 620, y - 26);//x轴String[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" };x = 45;for (int i = 0; i & 7; i++){g.DrawString(n[i].ToString(), font, Brushes.Red, x, 348); //设置文字内容及输出位置x = x + 77;}//y轴String[] m = { "220人", " 200人", " 175人", "150人", " 125人", " 100人", " 75人", " 50人"," 25人"};y = 100;for (int i = 0; i & 9; i++){g.DrawString(m[i].ToString(), font, Brushes.Red, 10, y); //设置文字内容及输出位置y = y + 26;}int[] Count1 = new int[7];int[] Count2 = new int[7];SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTUid=Pwd=eesoft");Con.Open();string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'";SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);DataSet ds = new DataSet();da.Fill(ds);//报名人数Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString());Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString());Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString());Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString());Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString()); //全年Count1[4] = Count1[0] + Count1[1];Count1[5] = Count1[2] + Count1[3];Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString());Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString());Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString());Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString()); //全年Count2[4] = Count2[0] + Count2[1];Count2[5] = Count2[2] + Count2[3];//显示折线效果Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);SolidBrush mybrush = new SolidBrush(Color.Red);Point[] points1 = new Point[7];points1[0].X = 60; points1[0].Y = 340 - Count1[0]; //从106纵坐标开始, 到(0, 0)坐标时points1[1].X = 140; points1[1].Y = 340 - Count1[1];points1[2].X = 220; points1[2].Y = 340 - Count1[2];points1[3].X = 300; points1[3].Y = 340 - Count1[3];points1[4].X = 380; points1[4].Y = 340 - Count1[4];points1[5].X = 460; points1[5].Y = 340 - Count1[5];points1[6].X = 540; points1[6].Y = 340 - Count1[6];g.DrawLines(mypen2, points1); //绘制折线//绘制数字g.DrawString(Count1[0].ToString(), font3, Brushes.Red, 58, points1[0].Y - 20);g.DrawString(Count1[1].ToString(), font3, Brushes.Red, 138, points1[1].Y - 20);g.DrawString(Count1[2].ToString(), font3, Brushes.Red, 218, points1[2].Y - 20);g.DrawString(Count1[3].ToString(), font3, Brushes.Red, 298, points1[3].Y - 20);g.DrawString(Count1[4].ToString(), font3, Brushes.Red, 378, points1[4].Y - 20);g.DrawString(Count1[5].ToString(), font3, Brushes.Red, 458, points1[5].Y - 20);g.DrawString(Count1[6].ToString(), font3, Brushes.Red, 538, points1[6].Y - 20);Pen mypen3 = new Pen(Color.Green, 2);Point[] points2 = new Point[7];points2[0].X = 60; points2[0].Y = 340 - Count2[0];points2[1].X = 140; points2[1].Y = 340 - Count2[1];points2[2].X = 220; points2[2].Y = 340 - Count2[2];points2[3].X = 300; points2[3].Y = 340 - Count2[3];points2[4].X = 380; points2[4].Y = 340 - Count2[4];points2[5].X = 460; points2[5].Y = 340 - Count2[5];points2[6].X = 540; points2[6].Y = 340 - Count2[6];g.DrawLines(mypen3, points2); //绘制折线//绘制通过人数g.DrawString(Count2[0].ToString(), font3, Brushes.Green, 61, points2[0].Y - 15);g.DrawString(Count2[1].ToString(), font3, Brushes.Green, 131, points2[1].Y - 15);g.DrawString(Count2[2].ToString(), font3, Brushes.Green, 221, points2[2].Y - 15);g.DrawString(Count2[3].ToString(), font3, Brushes.Green, 301, points2[3].Y - 15);g.DrawString(Count2[4].ToString(), font3, Brushes.Green, 381, points2[4].Y - 15);g.DrawString(Count2[5].ToString(), font3, Brushes.Green, 461, points2[5].Y - 15);g.DrawString(Count2[6].ToString(), font3, Brushes.Green, 541, points2[6].Y - 15);//绘制标识g.DrawRectangle(new Pen(Brushes.Red), 180, 390, 250, 50); //绘制范围框g.FillRectangle(Brushes.Red, 270, 402, 20, 10); //绘制小矩形g.DrawString("报名人数", font2, Brushes.Red, 292, 400);g.FillRectangle(Brushes.Green, 270, 422, 20, 10);g.DrawString("通过人数", font2, Brushes.Green, 292, 420);System.IO.MemoryStream ms = new System.IO.MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);Response.ClearContent();Response.ContentType = "image/Jpeg";Response.BinaryWrite(ms.ToArray());}finally{g.Dispose();image.Dispose();}}
三. 扇形统计图的绘制
扇形统计图的绘制private void CreateImage(){//把连接字串指定为一个常量SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTUid=Pwd=**");Con.Open();string cmdtxt = selectS // "select * from ##Count"; ////SqlCommand Com = new SqlCommand(cmdtxt, Con);DataSet ds = new DataSet();SqlDataAdapter Da = new SqlDataAdapter(cmdtxt, Con);Da.Fill(ds);Con.Close();float Total = 0.0f, T//转换成单精度。也可写成Convert.ToInt32Total = Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);// Total=Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);//设置字体,fonttitle为主标题的字体Font fontlegend = new Font("verdana", 9);Font fonttitle = new Font("verdana", 10, FontStyle.Bold);//背景宽int width = 350;int bufferspace = 15;int legendheight = fontlegend.Height * 10 + //高度int titleheight = fonttitle.Height +int height = width + legendheight + titleheight +//白色背景高int pieheight =Rectangle pierect = new Rectangle(0, titleheight, width, pieheight);//加上各种随机色ArrayList colors = new ArrayList();Random rnd = new Random();for (int i = 0; i & 2; i++)colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));//创建一个bitmap实例Bitmap objbitmap = new Bitmap(width, height);Graphics objgraphics = Graphics.FromImage(objbitmap);//画一个白色背景objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);//画一个亮黄色背景 objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);//以下为画饼图(有几行row画几个)float currentdegree = 0.0f;//画通过人数objgraphics.FillPie((SolidBrush)colors[1], pierect, currentdegree,Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360);currentdegree += Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360;//未通过人数饼状图objgraphics.FillPie((SolidBrush)colors[0], pierect, currentdegree,((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]))-(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360);currentdegree += ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) - (Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360;//以下为生成主标题SolidBrush blackbrush = new SolidBrush(Color.Black);SolidBrush bluebrush = new SolidBrush(Color.Blue);string title = " 机关单位成绩统计饼状图: "+ "\n \n\n";StringFormat stringFormat = new StringFormat();stringFormat.Alignment = StringAlignment.CstringFormat.LineAlignment = StringAlignment.Cobjgraphics.DrawString(title, fonttitle, blackbrush,new Rectangle(0, 0, width, titleheight), stringFormat);//列出各字段与得数目objgraphics.DrawRectangle(new Pen(Color.Red, 2), 0, height + 10 - legendheight, width, legendheight + 50);objgraphics.DrawString("----------------统计信息------------------", fontlegend, bluebrush, 20, height - legendheight + fontlegend.Height * 1 + 1);objgraphics.DrawString("统计单位: " + this.ddlTaget.SelectedItem.Text, fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 3 + 1);objgraphics.DrawString("统计年份: " + this.ddlYear.SelectedItem.Text, fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 4 + 1);objgraphics.DrawString("统计期数: " + this.ddlSpan.SelectedItem.Text, fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 5 + 1);objgraphics.FillRectangle((SolidBrush)colors[1], 5,height - legendheight + fontlegend.Height * 8 + 1, 10, 10);objgraphics.DrawString("报名总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])), fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 7 + 1);objgraphics.FillRectangle((SolidBrush)colors[0], 5, height - legendheight + fontlegend.Height * 9 + 1, 10, 10);objgraphics.DrawString("通过总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]])), fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 8 + 1);objgraphics.DrawString("未通过人数: " + ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) - (Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))), fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 9 + 1);objgraphics.DrawString("通过率: " + Convert.ToString((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) * 100)+ " %", fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 10 + 1);Response.ContentType = "image/Jpeg";objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);objgraphics.Dispose();objbitmap.Dispose();}
&这里的统计图直接输出到网页, 如果大家需要制作 winForm 上需要显示的统计图。可参考我之前写的:
阅读(...) 评论()}

我要回帖

更多关于 c语言绘制函数图像 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信