C#绘制曲线图

[图片] 曲线图效果

[代码] [C#]代码

01/// <summary>
02/// 自动根据参数调整图像大小
03/// </summary>
04public void Fit()
05{
06   //计算字体距离
07   intFontSpace = FontSize + 5;
08   //计算图像边距
09   float fltSpace = Math.Min(Width / 6, Height / 6);
10   XSpace = fltSpace;
11   YSpace = fltSpace;
12   //计算X轴刻度宽度
13   XSlice = (Width - 2 * XSpace) / (Keys.Length - 1);
14   //计算Y轴刻度宽度和Y轴刻度开始值
15   float fltMinValue = 0;
16   float fltMaxValue = 0;
17   for (int i = 0; i < Values.Length; i++)
18   {
19       if (Values[i] < fltMinValue)
20       {
21       fltMinValue = Values[i];
22       }
23       else if (Values[i] > fltMaxValue)
24       {
25       fltMaxValue = Values[i];
26       }
27   }
28   if (YSliceBegin > fltMinValue)
29   {
30       YSliceBegin = fltMinValue;
31   }
32   int intYSliceCount = (int)(fltMaxValue / YSliceValue);
33   if (fltMaxValue % YSliceValue != 0)
34   {
35       intYSliceCount++;
36   }
37   YSlice = (Height - 2 * YSpace) / intYSliceCount;
38}

[图片] 数据缩小一个级别的效果

[代码] 完整代码 DrawingCurve.cs

001using System;
002using System.Collections.Generic;
003using System.Text;
004using System.Drawing;
005using System.Data;
006using System.Drawing.Drawing2D;
007  
008namespace SarchPMS.Business.Draw
009{
010    public class DrawingCurve : DrawingChart
011    {
012        /// <summary>
013        /// 画曲线图
014        /// </summary>
015        /// <param name="dsParameter"></param>
016        /// <returns></returns>
017        public override Bitmap DrawImage(DataSet dsParameter)
018        {
019            Curve2D cuv2D = new Curve2D();
020            cuv2D.Fit();
021            return cuv2D.CreateImage();
022        }
023    }
024  
025    public class Curve2D
026    {
027        private Graphics objGraphics; //Graphics 类提供将对象绘制到显示设备的方法
028        private Bitmap objBitmap; //位图对象
029  
030        private float fltWidth = 480; //图像宽度
031        private float fltHeight = 248; //图像高度
032        private float fltXSlice = 50; //X轴刻度宽度
033        private float fltYSlice = 50; //Y轴刻度宽度
034        private float fltYSliceValue = 20; //Y轴刻度的数值宽度
035        private float fltYSliceBegin = 0; //Y轴刻度开始值
036        private float fltTension = 0.5f;
037        private string strTitle = "曲线图"; //标题
038        private string strXAxisText = "月份"; //X轴说明文字
039        private string strYAxisText = "万元"; //Y轴说明文字
040        private string[] strsKeys = new string[] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" }; //键
041        private float[] fltsValues = new float[] { 20.0f, 30.0f, 50.0f, 55.4f, 21.6f, 12.8f, 99.5f, 36.4f, 78.2f, 56.4f, 45.8f, 66.5f, 99.5f, 36.4f, 78.2f, 56.4f, 45.8f, 66.5f, 20.0f, 30.0f, 50.0f, 55.4f, 21.6f, 12.8f }; //值
042        private Color clrBgColor = Color.Snow; //背景色
043        private Color clrTextColor = Color.Black; //文字颜色
044        private Color clrBorderColor = Color.Black; //整体边框颜色
045        private Color clrAxisColor = Color.Black; //轴线颜色
046        private Color clrAxisTextColor = Color.Black; //轴说明文字颜色
047        private Color clrSliceTextColor = Color.Black; //刻度文字颜色
048        private Color clrSliceColor = Color.Black; //刻度颜色
049        private Color[] clrsCurveColors = new Color[] { Color.Red, Color.Blue }; //曲线颜色
050        private float fltXSpace = 100f; //图像左右距离边缘距离
051        private float fltYSpace = 100f; //图像上下距离边缘距离
052        private int intFontSize = 9; //字体大小号数
053        private float fltXRotateAngle = 30f; //X轴文字旋转角度
054        private float fltYRotateAngle = 0f; //Y轴文字旋转角度
055        private int intCurveSize = 2; //曲线线条大小
056        private int intFontSpace = 0; //intFontSpace 是字体大小和距离调整出来的一个比较适合的数字
057  
058        #region 公共属性
059  
060        /// <summary>
061        /// 图像的宽度
062        /// </summary>
063        public float Width
064        {
065            set
066            {
067                if (value < 100)
068                {
069                    fltWidth = 100;
070                }
071                else
072                {
073                    fltWidth = value;
074                }
075            }
076            get
077            {
078                if (fltWidth <= 100)
079                {
080                    return 100;
081                }
082                else
083                {
084                    return fltWidth;
085                }
086            }
087        }
088  
089        /// <summary>
090        /// 图像的高度
091        /// </summary>
092        public float Height
093        {
094            set
095            {
096                if (value < 100)
097                {
098                    fltHeight = 100;
099                }
100                else
101                {
102                    fltHeight = value;
103                }
104            }
105            get
106            {
107                if (fltHeight <= 100)
108                {
109                    return 100;
110                }
111                else
112                {
113                    return fltHeight;
114                }
115            }
116        }
117  
118        /// <summary>
119        /// X轴刻度宽度
120        /// </summary>
121        public float XSlice
122        {
123            set { fltXSlice = value; }
124            get { return fltXSlice; }
125        }
126  
127        /// <summary>
128        /// Y轴刻度宽度
129        /// </summary>
130        public float YSlice
131        {
132            set { fltYSlice = value; }
133            get { return fltYSlice; }
134        }
135  
136        /// <summary>
137        /// Y轴刻度的数值宽度
138        /// </summary>
139        public float YSliceValue
140        {
141            set { fltYSliceValue = value; }
142            get { return fltYSliceValue; }
143        }
144  
145        /// <summary>
146        /// Y轴刻度开始值
147        /// </summary>
148        public float YSliceBegin
149        {
150            set { fltYSliceBegin = value; }
151            get { return fltYSliceBegin; }
152        }
153  
154        /// <summary>
155        /// 张力系数
156        /// </summary>
157        public float Tension
158        {
159            set
160            {
161                if (value < 0.0f && value > 1.0f)
162                {
163                    fltTension = 0.5f;
164                }
165                else
166                {
167                    fltTension = value;
168                }
169            }
170            get
171            {
172                return fltTension;
173            }
174        }
175  
176        /// <summary>
177        /// 标题
178        /// </summary>
179        public string Title
180        {
181            set { strTitle = value; }
182            get { return strTitle; }
183        }
184  
185        /// <summary>
186        /// 键,X轴数据
187        /// </summary>
188        public string[] Keys
189        {
190            set { strsKeys = value; }
191            get { return strsKeys; }
192        }
193  
194        /// <summary>
195        /// 值,Y轴数据
196        /// </summary>
197        public float[] Values
198        {
199            set { fltsValues = value; }
200            get { return fltsValues; }
201        }
202  
203        /// <summary>
204        /// 背景色
205        /// </summary>
206        public Color BgColor
207        {
208            set { clrBgColor = value; }
209            get { return clrBgColor; }
210        }
211  
212        /// <summary>
213        /// 文字颜色
214        /// </summary>
215        public Color TextColor
216        {
217            set { clrTextColor = value; }
218            get { return clrTextColor; }
219        }
220  
221        /// <summary>
222        /// 整体边框颜色
223        /// </summary>
224        public Color BorderColor
225        {
226            set { clrBorderColor = value; }
227            get { return clrBorderColor; }
228        }
229  
230        /// <summary>
231        /// 轴线颜色
232        /// </summary>
233        public Color AxisColor
234        {
235            set { clrAxisColor = value; }
236            get { return clrAxisColor; }
237        }
238  
239        /// <summary>
240        /// X轴说明文字
241        /// </summary>
242        public string XAxisText
243        {
244            set { strXAxisText = value; }
245            get { return strXAxisText; }
246        }
247  
248        /// <summary>
249        /// Y轴说明文字
250        /// </summary>
251        public string YAxisText
252        {
253            set { strYAxisText = value; }
254            get { return strYAxisText; }
255        }
256  
257        /// <summary>
258        /// 轴说明文字颜色
259        /// </summary>
260        public Color AxisTextColor
261        {
262            set { clrAxisTextColor = value; }
263            get { return clrAxisTextColor; }
264        }
265  
266        /// <summary>
267        /// 刻度文字颜色
268        /// </summary>
269        public Color SliceTextColor
270        {
271            set { clrSliceTextColor = value; }
272            get { return clrSliceTextColor; }
273        }
274  
275        /// <summary>
276        /// 刻度颜色
277        /// </summary>
278        public Color SliceColor
279        {
280            set { clrSliceColor = value; }
281            get { return clrSliceColor; }
282        }
283  
284        /// <summary>
285        /// 曲线颜色
286        /// </summary>
287        public Color[] CurveColors
288        {
289            set { clrsCurveColors = value; }
290            get { return clrsCurveColors; }
291        }
292  
293        /// <summary>
294        /// X轴文字旋转角度
295        /// </summary>
296        public float XRotateAngle
297        {
298            get { return fltXRotateAngle; }
299            set { fltXRotateAngle = value; }
300        }
301  
302        /// <summary>
303        /// Y轴文字旋转角度
304        /// </summary>
305        public float YRotateAngle
306        {
307            get { return fltYRotateAngle; }
308            set { fltYRotateAngle = value; }
309        }
310  
311        /// <summary>
312        /// 图像左右距离边缘距离
313        /// </summary>
314        public float XSpace
315        {
316            get { return fltXSpace; }
317            set { fltXSpace = value; }
318        }
319  
320        /// <summary>
321        /// 图像上下距离边缘距离
322        /// </summary>
323        public float YSpace
324        {
325            get { return fltYSpace; }
326            set { fltYSpace = value; }
327        }
328  
329        /// <summary>
330        /// 字体大小号数
331        /// </summary>
332        public int FontSize
333        {
334            get { return intFontSize; }
335            set { intFontSize = value; }
336        }
337  
338        /// <summary>
339        /// 曲线线条大小
340        /// </summary>
341        public int CurveSize
342        {
343            get { return intCurveSize; }
344            set { intCurveSize = value; }
345        }
346  
347        #endregion
348  
349        /// <summary>
350        /// 自动根据参数调整图像大小
351        /// </summary>
352        public void Fit()
353        {
354            //计算字体距离
355            intFontSpace = FontSize + 5;
356            //计算图像边距
357            float fltSpace = Math.Min(Width / 6, Height / 6);
358            XSpace = fltSpace;
359            YSpace = fltSpace;
360            //计算X轴刻度宽度
361            XSlice = (Width - 2 * XSpace) / (Keys.Length - 1);
362            //计算Y轴刻度宽度和Y轴刻度开始值
363            float fltMinValue = 0;
364            float fltMaxValue = 0;
365            for (int i = 0; i < Values.Length; i++)
366            {
367                if (Values[i] < fltMinValue)
368                {
369                    fltMinValue = Values[i];
370                }
371                else if (Values[i] > fltMaxValue)
372                {
373                    fltMaxValue = Values[i];
374                }
375            }
376            if (YSliceBegin > fltMinValue)
377            {
378                YSliceBegin = fltMinValue;
379            }
380            int intYSliceCount = (int)(fltMaxValue / YSliceValue);
381            if (fltMaxValue % YSliceValue != 0)
382            {
383                intYSliceCount++;
384            }
385            YSlice = (Height - 2 * YSpace) / intYSliceCount;
386        }
387  
388        /// <summary>
389        /// 生成图像并返回bmp图像对象
390        /// </summary>
391        /// <returns></returns>
392        public Bitmap CreateImage()
393        {
394            InitializeGraph();
395  
396            int intKeysCount = Keys.Length;
397            int intValuesCount = Values.Length;
398            if (intValuesCount % intKeysCount == 0)
399            {
400                int intCurvesCount = intValuesCount / intKeysCount;
401                for (int i = 0; i < intCurvesCount; i++)
402                {
403                    float[] fltCurrentValues = new float[intKeysCount];
404                    for (int j = 0; j < intKeysCount; j++)
405                    {
406                        fltCurrentValues[j] = Values[i * intKeysCount + j];
407                    }
408                    DrawContent(ref objGraphics, fltCurrentValues, clrsCurveColors[i]);
409                }
410            }
411            else
412            {
413                objGraphics.DrawString("发生错误,Values的长度必须是Keys的整数倍!", new Font("宋体", FontSize + 5), new SolidBrush(TextColor), new Point((int)XSpace, (int)(Height / 2)));
414            }
415  
416            return objBitmap;
417        }
418  
419        /// <summary>
420        /// 初始化和填充图像区域,画出边框,初始标题
421        /// </summary>
422        private void InitializeGraph()
423        {
424  
425            //根据给定的高度和宽度创建一个位图图像
426            objBitmap = new Bitmap((int)Width, (int)Height);
427  
428            //从指定的 objBitmap 对象创建 objGraphics 对象 (即在objBitmap对象中画图)
429            objGraphics = Graphics.FromImage(objBitmap);
430  
431            //根据给定颜色(LightGray)填充图像的矩形区域 (背景)
432            objGraphics.DrawRectangle(new Pen(BorderColor, 1), 0, 0, Width - 1, Height - 1); //画边框
433            objGraphics.FillRectangle(new SolidBrush(BgColor), 1, 1, Width - 2, Height - 2); //填充边框
434  
435            //画X轴,注意图像的原始X轴和Y轴计算是以左上角为原点,向右和向下计算的
436            float fltX1 = XSpace;
437            float fltY1 = Height - YSpace;
438            float fltX2 = Width - XSpace + XSlice / 2;
439            float fltY2 = fltY1;
440            objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), fltX1, fltY1, fltX2, fltY2);
441  
442            //画Y轴
443            fltX1 = XSpace;
444            fltY1 = Height - YSpace;
445            fltX2 = XSpace;
446            fltY2 = YSpace - YSlice / 2;
447            objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), fltX1, fltY1, fltX2, fltY2);
448  
449            //初始化轴线说明文字
450            SetAxisText(ref objGraphics);
451  
452            //初始化X轴上的刻度和文字
453            SetXAxis(ref objGraphics);
454  
455            //初始化Y轴上的刻度和文字
456            SetYAxis(ref objGraphics);
457  
458            //初始化标题
459            CreateTitle(ref objGraphics);
460        }
461  
462        /// <summary>
463        /// 初始化轴线说明文字
464        /// </summary>
465        /// <param name="objGraphics"></param>
466        private void SetAxisText(ref Graphics objGraphics)
467        {
468            float fltX = Width - XSpace + XSlice / 2 - (XAxisText.Length - 1) * intFontSpace;
469            float fltY = Height - YSpace - intFontSpace;
470            objGraphics.DrawString(XAxisText, new Font("宋体", FontSize), new SolidBrush(AxisTextColor), fltX, fltY);
471  
472            fltX = XSpace + 5;
473            fltY = YSpace - YSlice / 2 - intFontSpace;
474            for (int i = 0; i < YAxisText.Length; i++)
475            {
476                objGraphics.DrawString(YAxisText[i].ToString(), new Font("宋体", FontSize), new SolidBrush(AxisTextColor), fltX, fltY);
477                fltY += intFontSpace; //字体上下距离
478            }
479        }
480  
481        /// <summary>
482        /// 初始化X轴上的刻度和文字
483        /// </summary>
484        /// <param name="objGraphics"></param>
485        private void SetXAxis(ref Graphics objGraphics)
486        {
487            float fltX1 = XSpace;
488            float fltY1 = Height - YSpace;
489            float fltX2 = XSpace;
490            float fltY2 = Height - YSpace;
491            int iCount = 0;
492            int iSliceCount = 1;
493            float Scale = 0;
494            float iWidth = ((Width - 2 * XSpace) / XSlice) * 50; //将要画刻度的长度分段,并乘以50,以10为单位画刻度线。
495            float fltSliceHeight = XSlice / 10; //刻度线的高度
496  
497            objGraphics.TranslateTransform(fltX1, fltY1); //平移图像(原点)
498            objGraphics.RotateTransform(XRotateAngle, MatrixOrder.Prepend); //旋转图像
499            objGraphics.DrawString(Keys[0].ToString(), new Font("宋体", FontSize), new SolidBrush(SliceTextColor), 0, 0);
500            objGraphics.ResetTransform(); //重置图像
501  
502            for (int i = 0; i <= iWidth; i += 10) //以10为单位
503            {
504                Scale = i * XSlice / 50;//即(i / 10) * (XSlice / 5),将每个刻度分五部分画,但因为i以10为单位,得除以10
505  
506                if (iCount == 5)
507                {
508                    objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX1 + Scale, fltY1 + fltSliceHeight * 1.5f, fltX2 + Scale, fltY2 - fltSliceHeight * 1.5f);
509                    //画网格虚线
510                    Pen penDashed = new Pen(new SolidBrush(AxisColor));
511                    penDashed.DashStyle = DashStyle.Dash;
512                    objGraphics.DrawLine(penDashed, fltX1 + Scale, fltY1, fltX2 + Scale, YSpace - YSlice / 2);
513                    //这里显示X轴刻度
514                    if (iSliceCount <= Keys.Length - 1)
515                    {
516                        objGraphics.TranslateTransform(fltX1 + Scale, fltY1);
517                        objGraphics.RotateTransform(XRotateAngle, MatrixOrder.Prepend);
518                        objGraphics.DrawString(Keys[iSliceCount].ToString(), new Font("宋体", FontSize), new SolidBrush(SliceTextColor), 0, 0);
519                        objGraphics.ResetTransform();
520                    }
521                    else
522                    {
523                        //超过范围,不画任何刻度文字
524                    }
525                    iCount = 0;
526                    iSliceCount++;
527                    if (fltX1 + Scale > Width - XSpace)
528                    {
529                        break;
530                    }
531                }
532                else
533                {
534                    objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), fltX1 + Scale, fltY1 + fltSliceHeight, fltX2 + Scale, fltY2 - fltSliceHeight);
535                }
536                iCount++;
537            }
538        }
539  
540        /// <summary>
541        /// 初始化Y轴上的刻度和文字
542        /// </summary>
543        /// <param name="objGraphics"></param>
544        private void SetYAxis(ref Graphics objGraphics)
545        {
546            float fltX1 = XSpace;
547            float fltY1 = Height - YSpace;
548            float fltX2 = XSpace;
549            float fltY2 = Height - YSpace;
550            int iCount = 0;
551            float Scale = 0;
552            int iSliceCount = 1;
553            float iHeight = ((Height - 2 * YSpace) / YSlice) * 50; //将要画刻度的长度分段,并乘以50,以10为单位画刻度线。
554            float fltSliceWidth = YSlice / 10; //刻度线的宽度
555            string strSliceText = string.Empty;
556  
557            objGraphics.TranslateTransform(XSpace - intFontSpace * YSliceBegin.ToString().Length, Height - YSpace); //平移图像(原点)
558            objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //旋转图像
559            objGraphics.DrawString(YSliceBegin.ToString(), new Font("宋体", FontSize), new SolidBrush(SliceTextColor), 0, 0);
560            objGraphics.ResetTransform(); //重置图像
561  
562            for (int i = 0; i < iHeight; i += 10)
563            {
564                Scale = i * YSlice / 50; //即(i / 10) * (YSlice / 5),将每个刻度分五部分画,但因为i以10为单位,得除以10
565  
566                if (iCount == 5)
567                {
568                    objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX1 - fltSliceWidth * 1.5f, fltY1 - Scale, fltX2 + fltSliceWidth * 1.5f, fltY2 - Scale);
569                    //画网格虚线
570                    Pen penDashed = new Pen(new SolidBrush(AxisColor));
571                    penDashed.DashStyle = DashStyle.Dash;
572                    objGraphics.DrawLine(penDashed, XSpace, fltY1 - Scale, Width - XSpace + XSlice / 2, fltY2 - Scale);
573                    //这里显示Y轴刻度
574                    strSliceText = Convert.ToString(YSliceValue * iSliceCount + YSliceBegin);
575                    objGraphics.TranslateTransform(XSpace - intFontSize * strSliceText.Length, fltY1 - Scale); //平移图像(原点)
576                    objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //旋转图像
577                    objGraphics.DrawString(strSliceText, new Font("宋体", FontSize), new SolidBrush(SliceTextColor), 0, 0);
578                    objGraphics.ResetTransform(); //重置图像
579  
580                    iCount = 0;
581                    iSliceCount++;
582                }
583                else
584                {
585                    objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), fltX1 - fltSliceWidth, fltY1 - Scale, fltX2 + fltSliceWidth, fltY2 - Scale);
586                }
587                iCount++;
588            }
589        }
590  
591        /// <summary>
592        /// 画曲线
593        /// </summary>
594        /// <param name="objGraphics"></param>
595        private void DrawContent(ref Graphics objGraphics, float[] fltCurrentValues, Color clrCurrentColor)
596        {
597            Pen CurvePen = new Pen(clrCurrentColor, CurveSize);
598            PointF[] CurvePointF = new PointF[Keys.Length];
599            float keys = 0;
600            float values = 0;
601  
602            for (int i = 0; i < Keys.Length; i++)
603            {
604                keys = XSlice * i + XSpace;
605                values = (Height - YSpace) + YSliceBegin - YSlice * (fltCurrentValues[i] / YSliceValue);
606                CurvePointF[i] = new PointF(keys, values);
607            }
608            objGraphics.DrawCurve(CurvePen, CurvePointF, Tension);
609        }
610  
611        /// <summary>
612        /// 初始化标题
613        /// </summary>
614        /// <param name="objGraphics"></param>
615        private void CreateTitle(ref Graphics objGraphics)
616        {
617            objGraphics.DrawString(Title, new Font("宋体", FontSize), new SolidBrush(TextColor), new Point((int)(Width - XSpace) - intFontSize * Title.Length, (int)(YSpace - YSlice / 2 - intFontSpace)));
618        }
619    }
620}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值