c#畫圖之折線圖


public JsonResult DrawLineChart()
        {
            // 預置顏色
            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 = 700;

            //邊緣位置留白
            int margin_top = 20;
            int margin_right = 40;
            int margin_bottom = 60;
            int margin_left = 60;

            //輔助線距離頂部的距離
            int xsubline = 20;

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

            // 折線名稱預留的位置  顏色框20,與文字間隙5,文字80,距離折線圖10,需要包含邊緣留白
            int lineNameWidth = 120 - margin_right;

            #endregion

            #region 數據

            //最大數量/總數量--生成y軸時,顯示數字需要
            int maxCount = 0;

            //x軸底部顯示的名稱
            string[] bottomData = new string[] { "第一個", "第二個", "第三個", "第四個", "第五個" };

            //折線名稱
            string[] lineName = new string[] { "折線1", "折線2" };

            //折線數據
            List<List<int>> lineData = new List<List<int>> {
                new List<int>(){ 12,23,15,44,32 },
                new List<int>(){ 9,33,6,21,22 }
            };

            //maxCount = xCount.Max();
            for (int i = 0; i < lineData.Count; i++)
            {
                int tempMaxCount = lineData[i].Max();

                if (tempMaxCount > maxCount)
                {
                    maxCount = tempMaxCount;
                }
            }

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

            //灰色畫筆--輔助線條顏色
            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 + lineNameWidth, 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));

            // 為了適應后面的計算
            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 / (bottomData.Length - 1);

            for (int i = 0; i < bottomData.Length; i++)
            {
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center; //居中

                //x軸下的文字
                //指向線
                g.DrawLine(blackPen, margin_left + i * singleWidth, (height + margin_top), margin_left + i * singleWidth, (height + margin_top + 5));
                //文字
                RectangleF rec = new RectangleF(margin_left + (i * singleWidth) - singleWidth / 2, (height + margin_top + 15), singleWidth, (margin_bottom - 20));
                g.DrawString(bottomData[i].ToString(), font, blackBrush, rec, format);
            }

            //預定顏色
            for (int i = 0; i < lineName.Length; i++)
            {
                //隨機顏色
                Color tempColor = colors[i];//GetRandomColor();

                //文字內容
                StringFormat format = new StringFormat(StringFormatFlags.DirectionVertical);
                //format.Alignment = StringAlignment.Center; //居中

                //畫筆
                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 rec = new RectangleF(margin_left + width + 10 + 25, margin_top + i * 25, 80, 20);
                g.DrawString(bottomData[i].ToString(), font, blackBrush, rec, format);

                //這里要開始畫折線了
                Point[] points = new Point[lineData[i].Count];

                for (int j = 0; j < lineData[i].Count; j++)
                {
                    int x = j * singleWidth + margin_left;
                    int y = height + margin_top - Convert.ToInt32(lineData[i][j] / Convert.ToDouble(maxCount) * (height - xsubline));

                    Point point = new Point(x,y);

                    points[j] = point;
                }

                g.DrawLines(pen, points);
            }

            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