【ASP.NET】画柱形图生成图片

 public class StatisticsGraph
    {
        public StatisticsGraph() { }

        #region MyRegion
        /// <summary>
        /// 柱形图
        /// </summary>
        /// <param name="bottomData">X横坐标数据</param>
        /// <param name="barData">Y纵坐标数据</param>
        /// <param name="savePath">保存图片路径</param>
        public void DrawBarChart(string[] bottomData, double[] barData, string savePath)
        {
            #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[] { "第一个", "第二个", "第三个", "第四个", "第五个" };
            //float[] barData = new float[] { 24, 33, 11, 55, 22 };

            maxCount = (int)barData.Max() + 1;
            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++)
            {
                Brush blueBrush = new SolidBrush(GetColor(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());


            image.Save(savePath, System.Drawing.Imaging.ImageFormat.Png);
        }
        /// <summary>
        /// 获取颜色
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public Color GetColor(int i)
        {
            if (i % 7 == 0)
            {
                return Color.Red;
            }
            if (i % 7 == 1)
            {
                return Color.Blue;
            }
            if (i % 7 == 2)
            {
                return Color.DarkSlateGray;
            }
            if (i % 7 == 3)
            {
                return Color.Gray;
            }
            if (i % 7 == 4)
            {
                return Color.Green;
            }
            if (i % 7 == 5)
            {
                return Color.Pink;
            }
            if (i % 7 == 6)
            {
                return Color.PapayaWhip;
            }
            else
            {
                return Color.Gold;
            }
        }

        /// <summary>
        /// 柱形图
        /// </summary>
        /// <param name="bottomData">X横坐标数据</param>
        /// <param name="barData">Y纵坐标数据</param>
        /// <param name="height">画布高度</param>
        /// <param name="width">画布宽度</param>
        /// <returns></returns>
        public Stream DrawBarChart(string[] bottomData, double[] barData, int height, int width)
        {
            #region 配置
            int margin_top = 20;
            int margin_right = 40;
            int margin_bottom = 60;
            int margin_left = 60;
            int xsubline = 20;
            int fontsize = 12;
            #endregion

            #region 数据
            int maxCount = 0;
            maxCount = (int)barData.Max() + 1;
            maxCount = maxCount == 0 ? 5 : maxCount;
            #endregion

            #region 画图
            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);
            g.DrawLine(blackPen, margin_left, margin_top, margin_left, (height + margin_top));
            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));
            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;
                RectangleF rec = new RectangleF(10, (height + margin_top - lineHeight * i - fontsize / 2), margin_left - 20, 20);
                StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
                g.DrawString(text.ToString(), font, blackBrush, rec, format);
            }
            int singleWidth = width / barData.Length;
            for (int i = 0; i < barData.Length; i++)
            {
                Brush blueBrush = new SolidBrush(GetColor(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);
                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);
            }
            #endregion

            Stream stream = new MemoryStream();
            image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            return stream;
        }

        #endregion

    }

涉及到一个dll请访问地址下载:

https://download.csdn.net/download/u012949335/87693810

调用画图:

string[] bottomData = new string[] { "<60", "60-69", "70-79", "80-89", "90-100" };
double[] barData = new double[] { 5, 10, 15, 20, 25.4 };
StatisticsGraph sg = new StatisticsGraph();
sg.DrawBarChart(bottomData, barData, Server.MapPath("~/Columnchart/xx.png"));

效果图如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值