码迷,mamicode.com
首页 > Windows程序 > 详细

c#画图之柱形图

时间:2019-10-23 13:27:04      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:lin   ack   res   创建   http   保存   ring   white   输出   

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

            //黑色画笔--主轴颜色
            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条辅助线,不管数字大小
            int avgCount = Convert.ToInt32(Math.Ceiling(maxCount / 5.0));

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

效果图

技术图片

 

c#画图之柱形图

标签:lin   ack   res   创建   http   保存   ring   white   输出   

原文地址:https://www.cnblogs.com/zhoushangwu/p/11725445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!