C#画图之饼图

2021-05-18 23:29

阅读:393

标签:名称   pie   配置   return   class   com   str   smooth   tin   

        public JsonResult DrawPie()
        {
            // 预置颜色
            List colors = new List()
            {
                Color.FromArgb(255,182,193),
                Color.FromArgb(238,130,238),
                Color.FromArgb(220,20,60),
                Color.FromArgb(153,50,204),
                Color.FromArgb(30,144,255),
                Color.FromArgb(60,179,113),
                Color.FromArgb(255,215,0),
                Color.FromArgb(255,140,0),
                Color.FromArgb(105,105,105)
            };

            #region 允许配置项

            //定义宽高  只定义宽度即可
            int height = 500, width = height;

            //边缘位置留白
            int margin_top = 20;
            int margin_right = 40;
            int margin_bottom = 20;
            int margin_left = 20;

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

            // 扇区名称预留的位置  颜色框20,与文字间隙5,文字80,距离折线图10,需要包含边缘留白
            int lineNameWidth = 140 - margin_right;

            #endregion

            #region 数据

            //最大数量/总数量
            int totalCount = 0;

            string[] pieNameData = new string[] { "第一个", "第二个", "第三个", "第四个", "第五个" };
            int[] pieData = new int[] { 24, 33, 11, 55, 22 };

            totalCount = pieData.Sum();

            #endregion

            //单位转换对象
            Spire.Pdf.Graphics.PdfUnitConvertor unitCvtr = new Spire.Pdf.Graphics.PdfUnitConvertor();

            //生成图像对象
            Bitmap image = new Bitmap(width + margin_left + margin_right + lineNameWidth, 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);

            //填充区域内容--背景
            g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width + margin_left + margin_right + lineNameWidth, height + margin_top + margin_bottom);

            //文字样式
            Font font = new Font("宋体", unitCvtr.ConvertUnits(fontsize, Spire.Pdf.Graphics.PdfGraphicsUnit.Pixel, Spire.Pdf.Graphics.PdfGraphicsUnit.Point));
            

            float tempAngle = 0;

            for (int i = 0; i )
            {
                Color tempColor = colors[i];

                //文字内容
                StringFormat format = new StringFormat();
                //format.Alignment = StringAlignment.Far; //居中
                //format.FormatFlags = StringFormatFlags.DirectionVertical;

                //画笔
                SolidBrush brush = new SolidBrush(tempColor);
                Pen pen = new Pen(brush, 1);

                // 折线名称处理
                // 颜色块
                Rectangle rectangle = new Rectangle(margin_left + width + 10, margin_top + i * 25, 20, 20);
                g.FillRectangle(brush, rectangle);

                // 文字
                RectangleF txtRec = new RectangleF(margin_left + width + 10 + 25, margin_top + i * 25 + 4, 100, 20);

                string txt = pieNameData[i].ToString() + "(" + Math.Round((pieData[i] / Convert.ToDouble(totalCount) * 100), 2).ToString() + "%)";
                g.DrawString(txt, font, blackBrush, txtRec, format);

                /************************
                 * 开始画饼图
                 ************************/
                //饼图
                int radius = (height > width ? width : height);

                Rectangle rec = new Rectangle(margin_left, margin_top, radius, radius);

                if (i 1)
                {
                    float angle = Convert.ToInt32(pieData[i] / Convert.ToDouble(totalCount) * 360);

                    g.FillPie(brush, rec, tempAngle, angle);

                    tempAngle += angle;
                }
                else
                {
                    //防止计算误差导致不足360或者超过360
                    float angle = 360 - tempAngle;

                    g.FillPie(brush, rec, tempAngle, angle);
                }
            }

            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#画图之饼图

标签:名称   pie   配置   return   class   com   str   smooth   tin   

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


评论


亲,登录后才可以留言!