c#畫圖之柱形圖


public JsonResult DrawBarChart()
        {
            #region 允許配置項

            //定義寬高
            int height = 500, width = 700;

            //邊緣位置留白
            int margin_top = 20;
            int margin_right = 40;
            int margin_bottom = 60;
            int margin_left = 60;

            //輔助線距離頂部的距離
            int xsubline = 20;

            //文字大小,單位:px
            int fontsize = 12;

            #endregion

            #region 數據

            //最大數量/總數量
            int maxCount = 0;

            string[] bottomData = new string[] { "第一個", "第二個", "第三個", "第四個", "第五個" };
            int[] barData = new int[] { 24, 33, 11, 55, 22 };

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

            #endregion

            //單位轉換對象
            Spire.Pdf.Graphics.PdfUnitConvertor unitCvtr = new Spire.Pdf.Graphics.PdfUnitConvertor();

            //生成圖像對象
            Bitmap image = new Bitmap(width + margin_left + margin_right, height + margin_top + margin_bottom);

            //創建畫布
            Graphics g = Graphics.FromImage(image);
            //消除鋸齒
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //質量
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;

            //黑色畫筆--主軸顏色
            Brush blackBrush = new SolidBrush(Color.FromArgb(255, 102, 102, 102));
            Pen blackPen = new Pen(blackBrush, 1);

            //灰色畫筆--輔助線條顏色
            Brush grayBrush = new SolidBrush(Color.FromArgb(255, 224, 224, 224));
            Pen grayPen = new Pen(grayBrush, 1);

            //填充區域內容
            g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width + margin_left + margin_right, height + margin_top + margin_bottom);

            //y軸
            g.DrawLine(blackPen, margin_left, margin_top, margin_left, (height + margin_top));

            //x軸
            g.DrawLine(blackPen, margin_left, (height + margin_top), (width + margin_left), (height + margin_top));

            Font font = new Font("宋體", unitCvtr.ConvertUnits(fontsize, Spire.Pdf.Graphics.PdfGraphicsUnit.Pixel, Spire.Pdf.Graphics.PdfGraphicsUnit.Point));

            //x軸--輔助線

            //畫5條輔助線,不管數字大小..這里數字變化后,maxCount也繼續變化,以適應后面的計算
            int avgCount = Convert.ToInt32(Math.Ceiling(maxCount / 5.0));

            maxCount = avgCount * 5;

            int 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++)
            {
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center; //居中

                //計算柱子
                int pillarHeight = Convert.ToInt32(barData[i] / Convert.ToDouble(maxCount) * (height - xsubline));

                //這里是畫柱子的代碼
                Rectangle rectangle = new 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));
                //文字
                RectangleF rec = new RectangleF(margin_left + (i * singleWidth), (height + margin_top + 15), singleWidth, (margin_bottom - 20));
                g.DrawString(bottomData[i].ToString(), font, blackBrush, rec, format);
            }

            //將圖片保存到指定的流中,適用於直接以流的方式輸出圖片
            //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());

            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);
        }

 

效果圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM