NET简单的一个画图程序
- using System;
- using System.Drawing;
- //HttpUtility.UrlEncode
- /// <summary>
- ///Curve 的摘要说明
- /// </summary>
- public class Curve
- {
- public Curve()
- {
- //
- //TODO: 在此处添加构造函数逻辑
- //
- }
- /*******************************************************属性设置**************************************************************/
- /// <summary>
- /// 房价数组
- /// </summary>
- private string[] _CurveData;
- /// <summary>
- /// 房价时间
- /// </summary>
- //private string[] _CurveDate;
- public string[] CurveData
- {
- get { return _CurveData; }
- set { _CurveData = value; }
- }
- //public string[] CurveDate
- //{
- // get { return _CurveDate; }
- // set { _CurveDate = value; }
- //}
- /// <summary>
- /// 是否显示曲线
- /// </summary>
- private bool _IsDemonstrationCurve=true;
- public bool IsDemonstrationCurve
- {
- get { return _IsDemonstrationCurve;}
- set {
- if(value==false)
- {
- _IsDemonstrationCurve = false;
- }else
- {
- _IsDemonstrationCurve = true;
- }
- }
- }
- /// <summary>
- /// 是否显示 矩形图
- /// </summary>
- private bool _IsDemonstrationRectangle=true;
- public bool IsDemonstrationRectangle
- {
- get { return _IsDemonstrationRectangle; }
- set
- {
- if(value==false)
- {
- _IsDemonstrationRectangle = false;
- }else
- {
- _IsDemonstrationRectangle = true;
- }
- }
- }
- /*****************************************属性设置结束***********************************************************************************/
- public bool getCurve(string FileName)
- {
- try
- {
- /***********************大图的参数*****************************/
- /*自写编码*/
- int ImgWidth = ; //1.[总宽度] ***图宽度
- int ImgHeight = ; //2.[总高度] ***图高度
- int ItemNum = ; //3.[项目数量] ***图表划分的块
- //int ChildSpace=15; //6.[各块间的间距]
- int ChartLeft = ; //7.[图表左边距] ***图表距图的左边距离
- int ChartRight = ; //8.[图表右边距] ***图表距图的右边距离
- int ChartTop = ; //9.[图表顶边距] ***图表距图顶边距离
- int ChartBottom = ; //10.[图表底边距] ***图表距图底边距离
- /*************************坐标位置调整******************************************/
- int YMaxValue = ; //11.[纵坐标标尺最大值] ***纵坐标标尺的最大值
- int YItemNum = ; //12.[纵坐标标尺段数] ***纵坐标标尺的段数
- int YTop = ; //13.[距纵轴顶端间隔]
- int YStrStart = ; //14.[纵坐标标尺文字起始X坐标]
- int XRight = ; //15.[距横轴右端间隔]
- int XStrStart = ; //16.[横坐标标尺文字起始Y坐标]
- /*************************小刻度位置调整*********************************/
- int ChildNum = ; //4.[块数] ***大块中划分的子项的数量
- float ChildRate = 0.6f; //5.[各块总占空间比率]
- int xiaoSpacing = ; //小标示和小刻度之间的距离
- int Xxiaospace_between = ; //矩形距离小刻度的位置
- int intjiange = ; //顶部名称说明距离 左边的距离
- string[] strGIFName = new string[ItemNum]; //大标示 注意和项目个数有联系
- //string strGIFName = "2009年价格走势图";
- /*因为 最近要换 所以 只有今年的*/
- for (int intGIF = ; intGIF <= ItemNum; intGIF++)
- {
- strGIFName[intGIF - ] = + intGIF + "年价格走势图";
- // arrValueNames[1] = "2010年价格走势图";
- // arrValueNames[2] = "2011年价格走势图";
- }
- /*****************************各项数值的计算****************************************/
- //[图表总宽度]=[总宽度]-[图表左边距]-[图表右边距]-[距横轴右端间隔]
- int chartwidth = ImgWidth - ChartLeft - ChartRight - XRight;
- //[项目宽度]=[图表总宽度]/[项目数量]
- int itemwidth = chartwidth/ItemNum;
- //[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
- int factwidth = Convert.ToInt32(Math.Floor(itemwidth*ChildRate));
- //[各块矩形宽度]=[各块总占空间比率的实际宽度]/[块数]
- int rectanglewidth = factwidth/ChildNum;
- //[各块间的间距]=([项目宽度]-[各块总占空间比率的实际宽度])/([块数]+1)
- int falge = ((itemwidth - factwidth)/(ChildNum + ));
- decimal myFale = falge;
- int childspace = Convert.ToInt32(Math.Floor(myFale));
- //(itemwidth-factwidth)/(ChildNum+1)
- /***********************************开始画图******************************************************/
- Graphics objGps; //建立画板对象
- Bitmap objBitMap = new Bitmap(ImgWidth, ImgHeight); //建立位图对象
- objGps = Graphics.FromImage(objBitMap); //根据位图对象建立画板对象
- objGps.Clear(Color.White); //设置画板对象的背景色
- /***************************横坐标设置*************************************************/
- string[] strArrValueName = new string[ChildNum]; //标示名称(月份) 注意长度要和ChildNum 相同 //横坐标设置
- for (int intquarter = ; intquarter <= strArrValueName.Length; intquarter++)
- {
- if (intquarter% == )
- {
- strArrValueName[intquarter - ] = "1~3月";
- }
- if (intquarter% == )
- {
- strArrValueName[intquarter - ] = "4~6月";
- }
- if (intquarter% == )
- {
- strArrValueName[intquarter - ] = "7~9月";
- }
- if (intquarter% == )
- {
- strArrValueName[intquarter - ] = "10~12月";
- }
- }
- /****************************数据的赋值*******************************/
- /*********************************************************************/
- //显示的数据
- int[] arrValues = {, , , , , , , , , , , }; //数据数组
- //CurveData
- for (int inttheNum = ; inttheNum <ChildNum; inttheNum++)
- {
- /* (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i 纵坐标的计算方法 */
- //ImgHeight - ChartBottom - ChartTop - YTop 总高度-距离顶部高度-距离底部高度-y坐标距离顶部高度/标示个数个数=单个坐标的高度
- // ImgHeight - ChartBottom-单个坐标的高度 总高度-距离底部高度-单个项目的高度
- double dange = (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum; //单个高度的计算
- double floatfalge = (double.Parse(CurveData[inttheNum])) * dange;
- // double floatfalge = (ImgHeight - ChartBottom - ChartTop - YTop)*(inttheNum + 1)*400/YMaxValue;
- arrValues[inttheNum] = Convert.ToInt32(Math.Floor(floatfalge));
- }
- /***************************************纵坐标显示标示设置*******************************************/
- //string[] arrValueNames = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};
- string[] arrValueNames = new string[YItemNum]; //这里表示所有的纵坐标的标示度名称
- for (int intValueName = ; intValueName < arrValueNames.Length; intValueName++)
- {
- arrValueNames[intValueName] = ((intValueName*) + ).ToString();
- }
- //得出矩形宽度,和画图X轴位置
- //[项目宽度]=[总宽度]/[项目数量]
- //======[各块总占空间比率]=([各块矩形宽度]+[各块间的间距])/[项目宽度]
- //[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
- //[各块矩形宽度]=([各块总占空间比率的实际宽度]-[各块间的间距]*([块数]))/[块数]
- //[一边空余空间宽度]=([项目宽度]-[各块所占空间比率的总宽度])/2
- System.Drawing.Point[] pi = new Point[arrValues.Length]; //定义折线点的对象数组
- System.Drawing.Point[] pit = new Point[]; //定义坐标三角点的对象数组
- System.Drawing.Pen pe = new Pen(new SolidBrush(GetColor()), 1f); //定义画直线的对象
- //画纵轴
- objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom), new Point(ChartLeft, ChartTop));
- //画纵轴终点箭头
- pit[].X = ImgWidth - ChartRight; //确定三角形三点的位置
- pit[].Y = ImgHeight - ChartBottom - ;
- pit[].X = ImgWidth - ChartRight;
- pit[].Y = ImgHeight - ChartBottom + ;
- pit[].X = ImgWidth - ChartRight + ;
- pit[].Y = ImgHeight - ChartBottom;
- objGps.FillPolygon(new SolidBrush(GetColor()), pit);
- //画纵轴标尺和标尺描述
- for (int i = ; i <= YItemNum; i++)
- {
- //画标尺
- objGps.DrawLine(pe,
- new PointF(ChartLeft,
- ImgHeight - ChartBottom -
- (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i),
- new PointF(ChartLeft - ,
- ImgHeight - ChartBottom -
- (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i));
- //画描述
- objGps.DrawString(arrValueNames[i - ].ToString(), new Font("宋体", ), Brushes.Black,
- new Point(YStrStart,
- ImgHeight - ChartBottom -
- (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i - ));
- }
- //画横轴
- objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom),
- new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom));
- //画横轴终点箭头
- pit[].X = ChartLeft - ; //确定三角形三点的位置
- pit[].Y = ChartTop;
- pit[].X = ChartLeft + ;
- pit[].Y = ChartTop;
- pit[].X = ChartLeft;
- pit[].Y = ChartTop - ;
- objGps.FillPolygon(new SolidBrush(GetColor()), pit);
- //画横轴标尺和标尺描述
- arrValueNames = strGIFName;
- for (int i = ; i <= ItemNum; i++)
- {
- //i表示画的 第几个 ImgHeight - ChartBottom + 5 表示线的长度
- //pe 表示pen 定义的一只笔
- // ChartLeft + itemwidth * i, ImgHeight - ChartBottom 横坐标=左边距+显示个数 ImgHeight - ChartBottom纵坐标=图片总高度-底边距离
- objGps.DrawLine(pe, new PointF(ChartLeft + itemwidth*i, ImgHeight - ChartBottom),
- new PointF(ChartLeft + itemwidth*i, ImgHeight - ChartBottom + )); //画横坐标大刻度
- //画小刻度和标示
- for (int j = ; j <= ChildNum; j++)
- {
- //
- float widthX; //定义起点和重点的横坐标
- widthX = ChartLeft + itemwidth*j/ChildNum + (i - )*itemwidth;
- //画小的刻度
- objGps.DrawLine(pe, new PointF(widthX, ImgHeight - ChartBottom),
- new PointF(widthX, ImgHeight - ChartBottom + ));
- //画小标示
- //arrValueNames[]
- // ChartLeft + childspace + itemwidth * (i - 1) 横坐标=左边距+间隔+个数
- int widthxiao;
- widthxiao = ChartLeft + xiaoSpacing + itemwidth*(j - )/ChildNum + itemwidth*(i - );
- objGps.DrawString(strArrValueName[j - ].ToString(), new Font("宋体", ), Brushes.Blue,
- new Point(widthxiao, ImgHeight - ChartBottom + XStrStart - ));
- }
- objGps.DrawString(arrValueNames[i - ].ToString(), new Font("宋体", ), Brushes.Black,
- new Point(ChartLeft + intjiange + itemwidth*(i - ),
- ImgHeight - ChartBottom + XStrStart));
- }
- //框架完成 开始画图
- for (int intfalge = ; intfalge <= ItemNum; intfalge++)
- {
- for (int j = ; j < ChildNum; j++) //画矩形图和折线图
- {
- int theintwidth = ;
- //左边距+各块间距
- theintwidth = ChartLeft + itemwidth * j / ChildNum + Xxiaospace_between + (intfalge - ) * itemwidth;
- if(IsDemonstrationRectangle==true)
- {
- //画矩形
- //5表示距离小刻度的位置
- //j * (childspace + rectanglewidth)) + childspace + ChartLeft
- //画矩形里面的颜色
- objGps.FillRectangle(new SolidBrush(GetColor(j)), theintwidth,
- ImgHeight - ChartBottom - arrValues[j],
- rectanglewidth, arrValues[j]);
- //画矩形外框
- objGps.DrawRectangle(Pens.Black, theintwidth, ImgHeight - ChartBottom - arrValues[j], rectanglewidth,
- arrValues[j]);
- }
- //********************************画折线图****************************************************
- if(IsDemonstrationCurve==true)
- {
- // pi[j].X = (j * (childspace + rectanglewidth)) + childspace + ChartLeft;
- pi[j].X = theintwidth; //定义折线的横坐标
- pi[j].Y = ImgHeight - ChartBottom - arrValues[j]; //纵坐标=图片总高度-底部高度-实际数值
- pe.SetLineCap(System.Drawing.Drawing2D.LineCap.RoundAnchor,
- System.Drawing.Drawing2D.LineCap.RoundAnchor,
- System.Drawing.Drawing2D.DashCap.Round);
- if (j > ) //进行连线
- {
- objGps.DrawLine(pe, pi[j - ], pi[j]);
- }
- }
- }
- }
- //objBitMap.Save(Response.OutputStream, ImageFormat.Gif);//该位图对象以"GIF"格式输出
- //objBitMap.Save();//进行保存
- System.Web.HttpServerUtility myServer = System.Web.HttpContext.Current.Server;
- string strPath= myServer.MapPath("~/CurveGIF/");
- //string strPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath.ToString() +
- //"/CurveGIF/"+FileName+".gif";
- objBitMap.Save(strPath+FileName+".gif");
- pe.Dispose();
- objBitMap.Dispose();
- GC.Collect(); //资源释放 垃圾回收
- return true;
- }catch(Exception ex)
- {
- return false;
- }
- }
- /**/
- /// <param name="itemIndex">系统定义的颜色,有效值0到7,分别为(Blue,Yellow,Red,Orange,Purple,Brown,Pink,Black)</param>
- /// <returns></returns>
- public static Color GetColor(int itemIndex)
- {
- Color objColor = new Color();
- switch (itemIndex)
- {
- case :
- objColor = Color.Blue;
- break;
- case :
- objColor = Color.Yellow;
- break;
- case :
- objColor = Color.Red;
- break;
- case :
- objColor = Color.Orange;
- break;
- case :
- objColor = Color.Purple;
- break;
- case :
- objColor = Color.Brown;
- break;
- case :
- objColor = Color.Pink;
- break;
- case :
- objColor = Color.Peru;
- break;
- case :
- objColor = Color.PowderBlue;
- break;
- case :
- objColor = Color.RosyBrown;
- break;
- case :
- objColor = Color.RoyalBlue;
- break;
- case :
- objColor = Color.SaddleBrown;
- break;
- case :
- objColor = Color.SeaGreen;
- break;
- default:
- objColor = Color.Black;
- break;
- }
- return objColor;
- }
- /**/
- /// <param name="red">自定义颜色红色分量值,有效值0到255</param>
- /// <param name="green">自定义颜色绿色分量值,有效值0到255</param>
- /// <param name="blue">自定义颜色蓝色分量值,有效值0到255</param>
- /// <returns></returns>
- public static Color GetColor(int red, int green, int blue)
- {
- Color objColor = new Color();
- objColor = Color.FromArgb(red, green, blue);
- return objColor;
- }
- }
然后这里是 调用的方法:
- Curve myCurve = new Curve();
- /**设置参数**/
- myCurve.IsDemonstrationCurve = true; //是否显示曲折线
- myCurve.IsDemonstrationRectangle = false;//是否显示矩形
- string[] strValue = {"","","","1.5"};//显示的数据 现在只能显示4 个
- myCurve.CurveData = strValue; //赋值
- myCurve.getCurve(Guid.NewGuid().ToString());//创建GIF 保存
- Response.Write("GIF生成成功!");
不过要注意的是 需要建立文件夹
出处:http://blog.csdn.net/jacky4955/article/details/4061658
NET简单的一个画图程序的更多相关文章
- 最简单的一个win32程序
#include <windows.h> HINSTANCE g_hInst = NULL; //2 窗口处理函数 LRESULT CALLBACK WndProc( HWND hWnd, ...
- Xamarin开发的一个简单画图程序分享
最近Xamarin比较火,于是稍微看了下,感觉接触过MVC的都应该能很快上手,还挺有意思,于是忍不住写了个简单的画图程序,之前看帖子有人说装不上或者无法部署,估计我比较幸运,编译完了一次就安装成功了, ...
- 一个简单的P2P传输程序
写了一个简单的P2P传输程序,在P2P的圈子中传输文件,不过为了简便,这个程序没有真正的传输文件,只是简单的判断一下文件的位置在哪里.这个程序可以处理当有一个peer闪退的情况,在这种情况下,剩下的p ...
- IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序
前言:本篇博文是本人阅读国外的IOS Programming Tutorial的一篇入门文章的学习过程总结,难度不大,因为是入门.主要是入门UIImagePickerController这个控制器,那 ...
- iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...
- 写一个ajax程序就是如此简单
写一个ajax程序就是如此简单 ajax介绍: 1:AJAX全称为Asynchronous JavaScript and XML(异步JavaScript和XML),指一种创建交互式网页应用的网页开发 ...
- 一个简单的MDI示范程序(Delphi)
http://www.cnblogs.com/pchmonster/archive/2012/01/07/2316012.html 最为一个巩固之前有关窗体和对象的有关知识,下面就建立一个简单的MDI ...
- 一个简单的DDraw应用程序2
//------------------------------------------------------------------------- // 文件名 : 6_1.cpp// 创建者 : ...
- 一个简单的DDraw应用程序
阅读排行榜 1. C/C++ 笔试.面试题目大汇总(72915) 2. [STL]list基础(21718) 3. COM笔记-CoCreateInstance(14842) 4. C/C++ ...
随机推荐
- VS异常--未找到与约束 ContractName Microsoft.VisualStudio.Language.Intellisense.IGlyphService RequiredTypeIdentity
早上打开项目的时候突然遇到这么个错误: ======================= 未找到与约束 ContractName Microsoft.VisualStudio.Language.Inte ...
- Broken pipe错误原因
这个异常是由于以下几个原因造成. 1.客户端再发起请求后没有等服务器端相应完,点击了stop按钮,导致服务器端接收到取消请求. 通常情况下是不会有这么无聊的用户,出现这种情况可能是由于用户提交了 ...
- wamp server 3.0.0 修改默认浏览器,软件语言和配置文件编辑器
改默认IE浏览器为Chrome: wampmanager.conf : navigator ="C:\Program Files (x86)\Google\Chrome\Applicatio ...
- 51Nod 1686 第K大区间(离散化+尺取法)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 思路: 第K大值,所以可以考虑二分法,然后用尺取法去扫描, ...
- shell 判断字符串长度是否不为0
test.sh #!/bin/bash s1="" if test $s1 ;then echo "length is not zero" else echo ...
- c++ 字符串拷贝以及合并
#include<iostream> #include<string> using namespace std; class stringfun { ]; public: vo ...
- 在Angular中定义共享的Providers
转自:https://segmentfault.com/a/1190000010700308 有时,你需要在 Angular 应用中创建一个共享模块,该模块定义了功能模块和lazy-loaded模块可 ...
- 2-14 MySQL初步认识,及CentOS6.8环境,源码方式安装MySQL
什么是数据库: 存放数据的仓库RDBMS-->(Relational Database Management System) 关系型数据库管理系统DBMS--->(Database Man ...
- UVA-10369 Arctic Network (最小生成树)
题目大意:n个村庄的坐标已知,现在要架光纤使所有的村庄都能上网,但受光纤的参数d所限,每根光纤只能给距离不超过d的村庄之间连接.但是有s个信号机,信号机之间能无限畅连.考虑到光纤的价格和参数d有关,现 ...
- java中进行四舍五入
在oracle中有一个很好的函数进行四舍五入,round(), select round(111112.23248987,6) from dual; 但是java的Number本身不提供四舍五入的方法 ...