c # drawing of a bar graph

public a JsonResult DrawBarChart () 
        { 
            #region allows CI // define the width and height int height = 500 , width = 700 ; // edge positions blank int margin_top = 20 is ;
             int margin_right = 40 ;
             int margin_bottom = 60 ;
             int margin_left = 60 ; // auxiliary line distance from the top of the int xsubline = 20 is ; // text size, unit: PX int fontSize = 12 is ;

            
            

            
            

            
            

            
            

            #endregion 

            #region data // Maximum number / total number int maxCount = 0 ; String [] = bottomData new new String [] { " first " , " second " , " third " , " fourth " , " fifth " };
             int [] = barData new new int [] { 24 , 33 is , . 11 , 55 , 22 is }; 
            maxCount

            
            

              
= BarData.Max (); 
            maxCount = maxCount == 0 ? . 5 : maxCount; 

            #endregion 

            // unit conversion target 
            Spire.Pdf.Graphics.PdfUnitConvertor unitCvtr = new new Spire.Pdf.Graphics.PdfUnitConvertor (); 

            // generates an image object 
            Image = Bitmap new new Bitmap (width + margin_left margin_right +, + height + margin_top margin_bottom); 

            // Create canvas 
            Graphics G = Graphics.FromImage (Image); 

            // black brush - color spindle 
            brush blackBrush = new new SolidBrush with (Color.FromArgb ( 255 ,102 , 102 , 102 )); 
            Pen blackPen = new new Pen (blackBrush, . 1 ); 

            // gray Brush - auxiliary line color 
            Brush grayBrush = new new SolidBrush with (Color.FromArgb ( 255 , 224 , 224 , 224 )); 
            Pen grayPen = new new Pen (grayBrush, . 1 ); 

            // filled areas content 
            g.FillRectangle (Brushes.WhiteSmoke, 0 , 0 , + margin_left + margin_right width, height margin_top + + margin_bottom);

            // Y axis 
            g.drawLine (blackPen, margin_left, margin_top, margin_left, (+ height margin_top)); 

            // X-axis 
            g.DrawLine (blackPen, margin_left, (height + margin_top), (width + margin_left), (height + margin_top)); 

            the font font = new new the font ( " Arial " , unitCvtr.ConvertUnits (fontSize, Spire.Pdf.Graphics.PdfGraphicsUnit.Pixel, Spire.Pdf.Graphics.PdfGraphicsUnit.Point)); 

            // X axis - auxiliary line 

            // Videos auxiliary lines 5, regardless of the size of the figures 
            int avgCount = Convert.ToInt32 (Math.Ceiling (maxCount / 5.0 )); 

            int the lineHeight = (height - xsubline) / . 5;

            //画辅助线与文字
            for (int i = 0; i <= 5; i++)
            {
                //辅助线
                if (i > 0)
                {
                    g.DrawLine(grayPen, margin_left, (height + margin_top - lineHeight * i), (width + margin_left), (height + margin_top - lineHeight * i));
                }

                //指向文字的线
                g.DrawLine(blackPen, (margin_left - 5), (height + margin_top - lineHeight * i), margin_left, (height + margin_top - lineHeight * i));
                //文字
                int text = avgCount * i;

                if (i == 5)
                {
                    if (maxCount - text > 0)
                    {
                        text = text + (maxCount - text);
                    }
                }

                RectangleF rec = new RectangleF(10, (height + margin_top - lineHeight * i - fontsize / 2), margin_left - 20, 20);
                //public void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle);
                //g.DrawString(text.ToString(), font, blackBrush, 10, (height + margin_top - lineHeight * i));
                StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
                g.DrawString(text.ToString(), font, blackBrush, rec, format);
            }

            //蓝色画笔--柱子的颜色
            Brush blueBrush = new SolidBrush(Color.FromArgb(255, 63, 167, 220));
            Pen bluePen = new Pen(blueBrush, . 1 ); 

            int singleWidth = width / barData.Length; 

            for ( int I = 0 ; I <barData.Length; I ++ ) 
            { 
                the StringFormat the format = new new the StringFormat (); 
                format.Alignment = StringAlignment.Center; // centered 

                // Calculation column 
                int pillarHeight = Convert.ToInt32 (barData [I] / Convert.ToDouble (maxCount) * (height - xsubline)); 

                // this is the code column Videos 
                the Rectangle Rectangle = new new the Rectangle (margin_left + (I + singleWidth singleWidth * / 4), height + margin_top - pillarHeight, singleWidth / 2, pillarHeight);
                g.FillRectangle(blueBrush, rectangle);

                //柱子上的文字
                RectangleF recText = new RectangleF(margin_left + (i * singleWidth), (height + margin_top - pillarHeight - 20), singleWidth, 20);
                g.DrawString(barData[i].ToString(), font, blackBrush, recText, format);

                //x轴下的文字
                //指向线
                g.DrawLine(blackPen, margin_left + (i * singleWidth + singleWidth / 2), (height + margin_top), margin_left + (i * singleWidth + singleWidth / 2), (+ Height + margin_top . 5 ));
                 // Text 
                RectangleF REC = new new RectangleF (margin_left + (I * singleWidth), (height margin_top + + 15 ), singleWidth, (margin_bottom - 20 is )); 
                g.drawString (bottomData [I] .ToString (), font, blackBrush, REC, the format); 
            } 

            // save the image to the specified stream, adapted to direct the output image stream manner
             // System.IO.MemoryStream new new MS = the System. IO.MemoryStream ();
             // Image.Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);
             // Response.ClearContent ();
             //Response.ContentType = "image/Jpeg";
            //Response.BinaryWrite(ms.ToArray());

            string relativePath = @"\draw-image\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
            string path = Server.MapPath(relativePath);
            image.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);

            //return relativePath;
            return Json(relativePath, JsonRequestBehavior.AllowGet);
        }

Renderings

 

Guess you like

Origin www.cnblogs.com/zhoushangwu/p/11725445.html