1. using System;
  2. using System.Drawing;
  3. //HttpUtility.UrlEncode
  4.  
  5. /// <summary>
  6. ///Curve 的摘要说明
  7. /// </summary>
  8. public class Curve
  9. {
  10. public Curve()
  11. {
  12. //
  13. //TODO: 在此处添加构造函数逻辑
  14. //
  15. }
  16.  
  17. /*******************************************************属性设置**************************************************************/
  18. /// <summary>
  19. /// 房价数组
  20. /// </summary>
  21. private string[] _CurveData;
  22. /// <summary>
  23. /// 房价时间
  24. /// </summary>
  25. //private string[] _CurveDate;
  26. public string[] CurveData
  27. {
  28. get { return _CurveData; }
  29. set { _CurveData = value; }
  30. }
  31. //public string[] CurveDate
  32. //{
  33. // get { return _CurveDate; }
  34. // set { _CurveDate = value; }
  35. //}
  36.  
  37. /// <summary>
  38. /// 是否显示曲线
  39. /// </summary>
  40. private bool _IsDemonstrationCurve=true;
  41. public bool IsDemonstrationCurve
  42. {
  43. get { return _IsDemonstrationCurve;}
  44. set {
  45. if(value==false)
  46. {
  47. _IsDemonstrationCurve = false;
  48. }else
  49. {
  50. _IsDemonstrationCurve = true;
  51. }
  52.  
  53. }
  54. }
  55. /// <summary>
  56. /// 是否显示 矩形图
  57. /// </summary>
  58. private bool _IsDemonstrationRectangle=true;
  59. public bool IsDemonstrationRectangle
  60. {
  61. get { return _IsDemonstrationRectangle; }
  62. set
  63. {
  64. if(value==false)
  65. {
  66. _IsDemonstrationRectangle = false;
  67. }else
  68. {
  69. _IsDemonstrationRectangle = true;
  70. }
  71. }
  72.  
  73. }
  74. /*****************************************属性设置结束***********************************************************************************/
  75.  
  76. public bool getCurve(string FileName)
  77. {
  78.  
  79. try
  80. {
  81. /***********************大图的参数*****************************/
  82. /*自写编码*/
  83. int ImgWidth = ; //1.[总宽度] ***图宽度
  84. int ImgHeight = ; //2.[总高度] ***图高度
  85. int ItemNum = ; //3.[项目数量] ***图表划分的块
  86. //int ChildSpace=15; //6.[各块间的间距]
  87. int ChartLeft = ; //7.[图表左边距] ***图表距图的左边距离
  88. int ChartRight = ; //8.[图表右边距] ***图表距图的右边距离
  89. int ChartTop = ; //9.[图表顶边距] ***图表距图顶边距离
  90. int ChartBottom = ; //10.[图表底边距] ***图表距图底边距离
  91.  
  92. /*************************坐标位置调整******************************************/
  93. int YMaxValue = ; //11.[纵坐标标尺最大值] ***纵坐标标尺的最大值
  94. int YItemNum = ; //12.[纵坐标标尺段数] ***纵坐标标尺的段数
  95. int YTop = ; //13.[距纵轴顶端间隔]
  96. int YStrStart = ; //14.[纵坐标标尺文字起始X坐标]
  97. int XRight = ; //15.[距横轴右端间隔]
  98. int XStrStart = ; //16.[横坐标标尺文字起始Y坐标]
  99.  
  100. /*************************小刻度位置调整*********************************/
  101. int ChildNum = ; //4.[块数] ***大块中划分的子项的数量
  102. float ChildRate = 0.6f; //5.[各块总占空间比率]
  103. int xiaoSpacing = ; //小标示和小刻度之间的距离
  104. int Xxiaospace_between = ; //矩形距离小刻度的位置
  105. int intjiange = ; //顶部名称说明距离 左边的距离
  106.  
  107. string[] strGIFName = new string[ItemNum]; //大标示 注意和项目个数有联系
  108. //string strGIFName = "2009年价格走势图";
  109. /*因为 最近要换 所以 只有今年的*/
  110. for (int intGIF = ; intGIF <= ItemNum; intGIF++)
  111. {
  112.  
  113. strGIFName[intGIF - ] = + intGIF + "年价格走势图";
  114. // arrValueNames[1] = "2010年价格走势图";
  115. // arrValueNames[2] = "2011年价格走势图";
  116. }
  117.  
  118. /*****************************各项数值的计算****************************************/
  119. //[图表总宽度]=[总宽度]-[图表左边距]-[图表右边距]-[距横轴右端间隔]
  120. int chartwidth = ImgWidth - ChartLeft - ChartRight - XRight;
  121. //[项目宽度]=[图表总宽度]/[项目数量]
  122. int itemwidth = chartwidth/ItemNum;
  123. //[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
  124. int factwidth = Convert.ToInt32(Math.Floor(itemwidth*ChildRate));
  125. //[各块矩形宽度]=[各块总占空间比率的实际宽度]/[块数]
  126. int rectanglewidth = factwidth/ChildNum;
  127. //[各块间的间距]=([项目宽度]-[各块总占空间比率的实际宽度])/([块数]+1)
  128. int falge = ((itemwidth - factwidth)/(ChildNum + ));
  129. decimal myFale = falge;
  130. int childspace = Convert.ToInt32(Math.Floor(myFale));
  131. //(itemwidth-factwidth)/(ChildNum+1)
  132.  
  133. /***********************************开始画图******************************************************/
  134. Graphics objGps; //建立画板对象
  135. Bitmap objBitMap = new Bitmap(ImgWidth, ImgHeight); //建立位图对象
  136. objGps = Graphics.FromImage(objBitMap); //根据位图对象建立画板对象
  137. objGps.Clear(Color.White); //设置画板对象的背景色
  138. /***************************横坐标设置*************************************************/
  139. string[] strArrValueName = new string[ChildNum]; //标示名称(月份) 注意长度要和ChildNum 相同 //横坐标设置
  140. for (int intquarter = ; intquarter <= strArrValueName.Length; intquarter++)
  141. {
  142. if (intquarter% == )
  143. {
  144. strArrValueName[intquarter - ] = "1~3月";
  145. }
  146. if (intquarter% == )
  147. {
  148. strArrValueName[intquarter - ] = "4~6月";
  149. }
  150. if (intquarter% == )
  151. {
  152. strArrValueName[intquarter - ] = "7~9月";
  153. }
  154. if (intquarter% == )
  155. {
  156. strArrValueName[intquarter - ] = "10~12月";
  157. }
  158. }
  159.  
  160. /****************************数据的赋值*******************************/
  161. /*********************************************************************/
  162. //显示的数据
  163. int[] arrValues = {, , , , , , , , , , , }; //数据数组
  164. //CurveData
  165.  
  166. for (int inttheNum = ; inttheNum <ChildNum; inttheNum++)
  167. {
  168.  
  169. /* (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i 纵坐标的计算方法 */
  170. //ImgHeight - ChartBottom - ChartTop - YTop 总高度-距离顶部高度-距离底部高度-y坐标距离顶部高度/标示个数个数=单个坐标的高度
  171. // ImgHeight - ChartBottom-单个坐标的高度 总高度-距离底部高度-单个项目的高度
  172. double dange = (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum; //单个高度的计算
  173.  
  174. double floatfalge = (double.Parse(CurveData[inttheNum])) * dange;
  175. // double floatfalge = (ImgHeight - ChartBottom - ChartTop - YTop)*(inttheNum + 1)*400/YMaxValue;
  176. arrValues[inttheNum] = Convert.ToInt32(Math.Floor(floatfalge));
  177. }
  178.  
  179. /***************************************纵坐标显示标示设置*******************************************/
  180. //string[] arrValueNames = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};
  181. string[] arrValueNames = new string[YItemNum]; //这里表示所有的纵坐标的标示度名称
  182. for (int intValueName = ; intValueName < arrValueNames.Length; intValueName++)
  183. {
  184. arrValueNames[intValueName] = ((intValueName*) + ).ToString();
  185. }
  186.  
  187. //得出矩形宽度,和画图X轴位置
  188.  
  189. //[项目宽度]=[总宽度]/[项目数量]
  190. //======[各块总占空间比率]=([各块矩形宽度]+[各块间的间距])/[项目宽度]
  191. //[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
  192. //[各块矩形宽度]=([各块总占空间比率的实际宽度]-[各块间的间距]*([块数]))/[块数]
  193. //[一边空余空间宽度]=([项目宽度]-[各块所占空间比率的总宽度])/2
  194.  
  195. System.Drawing.Point[] pi = new Point[arrValues.Length]; //定义折线点的对象数组
  196. System.Drawing.Point[] pit = new Point[]; //定义坐标三角点的对象数组
  197. System.Drawing.Pen pe = new Pen(new SolidBrush(GetColor()), 1f); //定义画直线的对象
  198. //画纵轴
  199. objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom), new Point(ChartLeft, ChartTop));
  200. //画纵轴终点箭头
  201. pit[].X = ImgWidth - ChartRight; //确定三角形三点的位置
  202. pit[].Y = ImgHeight - ChartBottom - ;
  203. pit[].X = ImgWidth - ChartRight;
  204. pit[].Y = ImgHeight - ChartBottom + ;
  205. pit[].X = ImgWidth - ChartRight + ;
  206. pit[].Y = ImgHeight - ChartBottom;
  207. objGps.FillPolygon(new SolidBrush(GetColor()), pit);
  208. //画纵轴标尺和标尺描述
  209. for (int i = ; i <= YItemNum; i++)
  210. {
  211. //画标尺
  212. objGps.DrawLine(pe,
  213. new PointF(ChartLeft,
  214. ImgHeight - ChartBottom -
  215. (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i),
  216. new PointF(ChartLeft - ,
  217. ImgHeight - ChartBottom -
  218. (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i));
  219. //画描述
  220. objGps.DrawString(arrValueNames[i - ].ToString(), new Font("宋体", ), Brushes.Black,
  221. new Point(YStrStart,
  222. ImgHeight - ChartBottom -
  223. (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i - ));
  224. }
  225. //画横轴
  226. objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom),
  227. new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom));
  228. //画横轴终点箭头
  229. pit[].X = ChartLeft - ; //确定三角形三点的位置
  230. pit[].Y = ChartTop;
  231. pit[].X = ChartLeft + ;
  232. pit[].Y = ChartTop;
  233. pit[].X = ChartLeft;
  234. pit[].Y = ChartTop - ;
  235. objGps.FillPolygon(new SolidBrush(GetColor()), pit);
  236. //画横轴标尺和标尺描述
  237.  
  238. arrValueNames = strGIFName;
  239.  
  240. for (int i = ; i <= ItemNum; i++)
  241. {
  242. //i表示画的 第几个 ImgHeight - ChartBottom + 5 表示线的长度
  243. //pe 表示pen 定义的一只笔
  244. // ChartLeft + itemwidth * i, ImgHeight - ChartBottom 横坐标=左边距+显示个数 ImgHeight - ChartBottom纵坐标=图片总高度-底边距离
  245.  
  246. objGps.DrawLine(pe, new PointF(ChartLeft + itemwidth*i, ImgHeight - ChartBottom),
  247. new PointF(ChartLeft + itemwidth*i, ImgHeight - ChartBottom + )); //画横坐标大刻度
  248.  
  249. //画小刻度和标示
  250. for (int j = ; j <= ChildNum; j++)
  251. {
  252. //
  253. float widthX; //定义起点和重点的横坐标
  254. widthX = ChartLeft + itemwidth*j/ChildNum + (i - )*itemwidth;
  255. //画小的刻度
  256. objGps.DrawLine(pe, new PointF(widthX, ImgHeight - ChartBottom),
  257. new PointF(widthX, ImgHeight - ChartBottom + ));
  258. //画小标示
  259.  
  260. //arrValueNames[]
  261. // ChartLeft + childspace + itemwidth * (i - 1) 横坐标=左边距+间隔+个数
  262. int widthxiao;
  263. widthxiao = ChartLeft + xiaoSpacing + itemwidth*(j - )/ChildNum + itemwidth*(i - );
  264. objGps.DrawString(strArrValueName[j - ].ToString(), new Font("宋体", ), Brushes.Blue,
  265. new Point(widthxiao, ImgHeight - ChartBottom + XStrStart - ));
  266. }
  267.  
  268. objGps.DrawString(arrValueNames[i - ].ToString(), new Font("宋体", ), Brushes.Black,
  269. new Point(ChartLeft + intjiange + itemwidth*(i - ),
  270. ImgHeight - ChartBottom + XStrStart));
  271. }
  272.  
  273. //框架完成 开始画图
  274. for (int intfalge = ; intfalge <= ItemNum; intfalge++)
  275. {
  276.  
  277. for (int j = ; j < ChildNum; j++) //画矩形图和折线图
  278. {
  279. int theintwidth = ;
  280. //左边距+各块间距
  281. theintwidth = ChartLeft + itemwidth * j / ChildNum + Xxiaospace_between + (intfalge - ) * itemwidth;
  282.  
  283. if(IsDemonstrationRectangle==true)
  284. {
  285. //画矩形
  286. //5表示距离小刻度的位置
  287. //j * (childspace + rectanglewidth)) + childspace + ChartLeft
  288. //画矩形里面的颜色
  289. objGps.FillRectangle(new SolidBrush(GetColor(j)), theintwidth,
  290. ImgHeight - ChartBottom - arrValues[j],
  291. rectanglewidth, arrValues[j]);
  292. //画矩形外框
  293. objGps.DrawRectangle(Pens.Black, theintwidth, ImgHeight - ChartBottom - arrValues[j], rectanglewidth,
  294. arrValues[j]);
  295. }
  296.  
  297. //********************************画折线图****************************************************
  298. if(IsDemonstrationCurve==true)
  299. {
  300. // pi[j].X = (j * (childspace + rectanglewidth)) + childspace + ChartLeft;
  301. pi[j].X = theintwidth; //定义折线的横坐标
  302. pi[j].Y = ImgHeight - ChartBottom - arrValues[j]; //纵坐标=图片总高度-底部高度-实际数值
  303.  
  304. pe.SetLineCap(System.Drawing.Drawing2D.LineCap.RoundAnchor,
  305. System.Drawing.Drawing2D.LineCap.RoundAnchor,
  306. System.Drawing.Drawing2D.DashCap.Round);
  307. if (j > ) //进行连线
  308. {
  309. objGps.DrawLine(pe, pi[j - ], pi[j]);
  310. }
  311. }
  312. }
  313. }
  314.  
  315. //objBitMap.Save(Response.OutputStream, ImageFormat.Gif);//该位图对象以"GIF"格式输出
  316. //objBitMap.Save();//进行保存
  317. System.Web.HttpServerUtility myServer = System.Web.HttpContext.Current.Server;
  318. string strPath= myServer.MapPath("~/CurveGIF/");
  319.  
  320. //string strPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath.ToString() +
  321. //"/CurveGIF/"+FileName+".gif";
  322.  
  323. objBitMap.Save(strPath+FileName+".gif");
  324. pe.Dispose();
  325. objBitMap.Dispose();
  326. GC.Collect(); //资源释放 垃圾回收
  327. return true;
  328. }catch(Exception ex)
  329. {
  330. return false;
  331. }
  332. }
  333.  
  334. /**/
  335. /// <param name="itemIndex">系统定义的颜色,有效值0到7,分别为(Blue,Yellow,Red,Orange,Purple,Brown,Pink,Black)</param>
  336. /// <returns></returns>
  337. public static Color GetColor(int itemIndex)
  338. {
  339. Color objColor = new Color();
  340. switch (itemIndex)
  341. {
  342. case :
  343. objColor = Color.Blue;
  344. break;
  345. case :
  346. objColor = Color.Yellow;
  347. break;
  348. case :
  349. objColor = Color.Red;
  350. break;
  351. case :
  352. objColor = Color.Orange;
  353. break;
  354. case :
  355. objColor = Color.Purple;
  356. break;
  357. case :
  358. objColor = Color.Brown;
  359. break;
  360. case :
  361. objColor = Color.Pink;
  362. break;
  363. case :
  364. objColor = Color.Peru;
  365. break;
  366. case :
  367. objColor = Color.PowderBlue;
  368. break;
  369. case :
  370. objColor = Color.RosyBrown;
  371. break;
  372. case :
  373. objColor = Color.RoyalBlue;
  374. break;
  375. case :
  376. objColor = Color.SaddleBrown;
  377. break;
  378. case :
  379. objColor = Color.SeaGreen;
  380. break;
  381. default:
  382. objColor = Color.Black;
  383. break;
  384. }
  385. return objColor;
  386. }
  387.  
  388. /**/
  389. /// <param name="red">自定义颜色红色分量值,有效值0到255</param>
  390. /// <param name="green">自定义颜色绿色分量值,有效值0到255</param>
  391. /// <param name="blue">自定义颜色蓝色分量值,有效值0到255</param>
  392. /// <returns></returns>
  393. public static Color GetColor(int red, int green, int blue)
  394. {
  395. Color objColor = new Color();
  396. objColor = Color.FromArgb(red, green, blue);
  397. return objColor;
  398. }
  399.  
  400. }

然后这里是 调用的方法:

  1. Curve myCurve = new Curve();
  2.  
  3. /**设置参数**/
  4. myCurve.IsDemonstrationCurve = true; //是否显示曲折线
  5. myCurve.IsDemonstrationRectangle = false;//是否显示矩形
  6. string[] strValue = {"","","","1.5"};//显示的数据 现在只能显示4 个
  7. myCurve.CurveData = strValue; //赋值
  8. myCurve.getCurve(Guid.NewGuid().ToString());//创建GIF 保存
  9. Response.Write("GIF生成成功!");

不过要注意的是  需要建立文件夹

出处:http://blog.csdn.net/jacky4955/article/details/4061658

NET简单的一个画图程序的更多相关文章

  1. 最简单的一个win32程序

    #include <windows.h> HINSTANCE g_hInst = NULL; //2 窗口处理函数 LRESULT CALLBACK WndProc( HWND hWnd, ...

  2. Xamarin开发的一个简单画图程序分享

    最近Xamarin比较火,于是稍微看了下,感觉接触过MVC的都应该能很快上手,还挺有意思,于是忍不住写了个简单的画图程序,之前看帖子有人说装不上或者无法部署,估计我比较幸运,编译完了一次就安装成功了, ...

  3. 一个简单的P2P传输程序

    写了一个简单的P2P传输程序,在P2P的圈子中传输文件,不过为了简便,这个程序没有真正的传输文件,只是简单的判断一下文件的位置在哪里.这个程序可以处理当有一个peer闪退的情况,在这种情况下,剩下的p ...

  4. IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序

    前言:本篇博文是本人阅读国外的IOS Programming Tutorial的一篇入门文章的学习过程总结,难度不大,因为是入门.主要是入门UIImagePickerController这个控制器,那 ...

  5. iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序

    iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...

  6. 写一个ajax程序就是如此简单

    写一个ajax程序就是如此简单 ajax介绍: 1:AJAX全称为Asynchronous JavaScript and XML(异步JavaScript和XML),指一种创建交互式网页应用的网页开发 ...

  7. 一个简单的MDI示范程序(Delphi)

    http://www.cnblogs.com/pchmonster/archive/2012/01/07/2316012.html 最为一个巩固之前有关窗体和对象的有关知识,下面就建立一个简单的MDI ...

  8. 一个简单的DDraw应用程序2

    //------------------------------------------------------------------------- // 文件名 : 6_1.cpp// 创建者 : ...

  9. 一个简单的DDraw应用程序

        阅读排行榜 1. C/C++ 笔试.面试题目大汇总(72915) 2. [STL]list基础(21718) 3. COM笔记-CoCreateInstance(14842) 4. C/C++ ...

随机推荐

  1. VS异常--未找到与约束 ContractName Microsoft.VisualStudio.Language.Intellisense.IGlyphService RequiredTypeIdentity

    早上打开项目的时候突然遇到这么个错误: ======================= 未找到与约束 ContractName Microsoft.VisualStudio.Language.Inte ...

  2. Broken pipe错误原因

      这个异常是由于以下几个原因造成. 1.客户端再发起请求后没有等服务器端相应完,点击了stop按钮,导致服务器端接收到取消请求.  通常情况下是不会有这么无聊的用户,出现这种情况可能是由于用户提交了 ...

  3. wamp server 3.0.0 修改默认浏览器,软件语言和配置文件编辑器

    改默认IE浏览器为Chrome: wampmanager.conf : navigator ="C:\Program Files (x86)\Google\Chrome\Applicatio ...

  4. 51Nod 1686 第K大区间(离散化+尺取法)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 思路: 第K大值,所以可以考虑二分法,然后用尺取法去扫描, ...

  5. shell 判断字符串长度是否不为0

    test.sh #!/bin/bash s1="" if test $s1 ;then echo "length is not zero" else echo ...

  6. c++ 字符串拷贝以及合并

    #include<iostream> #include<string> using namespace std; class stringfun { ]; public: vo ...

  7. 在Angular中定义共享的Providers

    转自:https://segmentfault.com/a/1190000010700308 有时,你需要在 Angular 应用中创建一个共享模块,该模块定义了功能模块和lazy-loaded模块可 ...

  8. 2-14 MySQL初步认识,及CentOS6.8环境,源码方式安装MySQL

    什么是数据库: 存放数据的仓库RDBMS-->(Relational Database Management System) 关系型数据库管理系统DBMS--->(Database Man ...

  9. UVA-10369 Arctic Network (最小生成树)

    题目大意:n个村庄的坐标已知,现在要架光纤使所有的村庄都能上网,但受光纤的参数d所限,每根光纤只能给距离不超过d的村庄之间连接.但是有s个信号机,信号机之间能无限畅连.考虑到光纤的价格和参数d有关,现 ...

  10. java中进行四舍五入

    在oracle中有一个很好的函数进行四舍五入,round(), select round(111112.23248987,6) from dual; 但是java的Number本身不提供四舍五入的方法 ...