zoukankan      html  css  js  c++  java
  • 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);
            }

    效果图

  • 相关阅读:
    人人都是架构师:分布式系统架构落地与瓶颈突破
    Node.js区块链开发
    MDD:使用模型驱动开发方式进行快速开发(多图预警)
    MDSF:模型驱动开发(MDD)介绍 | 一群共同享有幸福生活的成长伙伴
    SLF4J with Logback in a Maven Project | Mograblog
    Spring MVC集成slf4j-logback
    SLF4J和Logback日志框架详解
    Python中国社区
    致远互联官网_致远软件_协同 _OA_OA系统_OA办公系统_协同管理软件及云服务领导供应商-致远软件官网
    新精英博客_公司博客_官方公告
  • 原文地址:https://www.cnblogs.com/zhoushangwu/p/11725445.html
Copyright © 2011-2022 走看看