C#畫圖之餅圖


        public JsonResult DrawPie()
        {
            // 預置顏色
            List<Color> colors = new List<Color>()
            {
                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 < pieData.Length; 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 < pieData.Length - 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);
        }

示例圖

 

那種帶把,把的末尾帶文字的圖,不會算啊,哪個會算的,有幸看到了能不能告訴我怎么算啊,跪謝!!!

帶把的餅圖(不會算的餅圖)如下:

 

 


免責聲明!

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



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