public JsonResult DrawRadar() { 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 = 60; int margin_right = 40; int margin_bottom = 40; int margin_left = 40; //文字大小,單位:px int fontsize = 12; // 扇區名稱預留的位置 顏色框20,與文字間隙5,文字80,距離折線圖10,需要包含邊緣留白 int lineNameWidth = 200 - margin_right; #endregion #region 數據 //最大數量/總數量 int maxCount = 0; string[] radarNameData = 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 } }; 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, 200, 200, 200)); Pen grayPen = new Pen(grayBrush, 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)); //圓心 int centerX = height / 2 + margin_left; int centerY = width / 2 + margin_top; //角的個數,規定是幾邊形 int len = radarNameData.Length; //角度 int angle = 360 / len; // 外圍半徑 int radius = width / 2; // 每次遞減的半徑 int subRadius = radius / 5; for (int k = 0; k < 5; k++) { Point[] points = new Point[len + 1]; for (int i = 0; i < len; i++) { double angleHude = i * angle * Math.PI / 180;/*角度變成弧度*/ int x = (int)((radius - k * subRadius) * Math.Cos(angleHude)) + centerX; int y = (int)((radius - k * subRadius) * Math.Sin(angleHude)) + centerY; Point point = new Point(x, y); points[i] = point; if (i == 0) { RectangleF sumRec = new RectangleF(x - 30, y - 15, 30, 15); g.DrawString((100 - k * 20).ToString(), font, blackBrush, sumRec); } } points[len] = points[0]; if (k == 0) { g.DrawLines(blackPen, points); for (int i = 0; i < points.Length - 1; i++) { g.DrawLine(grayPen, points[i], new Point(centerX, centerY)); //90 270 StringFormat format = new StringFormat(); //兩條豎線 int currAngle = i * angle; int txtX = 0; int txtY = 0; if (currAngle % 180 == 90) { format.Alignment = StringAlignment.Center; txtX = points[i].X; if (currAngle == 90) { txtY = points[i].Y - 20; } else { txtY = points[i].Y + 20; } } else { txtY = points[i].Y; if (currAngle > 90 && currAngle < 270) { format.FormatFlags = StringFormatFlags.DirectionRightToLeft; txtX = points[i].X - 50; } else { format.FormatFlags = 0; txtX = points[i].X + 10; } } // 文字 Rectangle recText = new Rectangle(txtX, txtY, 40, fontsize); g.DrawString(radarNameData[i], font, blackBrush, recText, format); } } else { g.DrawLines(grayPen, points); } } //開始數據 for (int i = 0; i < lineData.Count; i++) { /* * 這個是色塊 */ //顏色快代表的內容 Color tempColor = colors[i];//GetRandomColor(); //文字內容 StringFormat txtFormat = new StringFormat(); //format.Alignment = StringAlignment.Center; //居中 //畫筆 SolidBrush txtBrush = new SolidBrush(tempColor); // 名稱處理 // 顏色塊 Rectangle rectangle = new Rectangle(margin_left + width + 50, margin_top + i * 25, 20, 20); g.FillRectangle(txtBrush, rectangle); // 文字 RectangleF rec = new RectangleF(margin_left + width + 75, margin_top + i * 25 + 4, 80, 20); g.DrawString(lineName[i], font, blackBrush, rec, txtFormat); Point[] points = new Point[lineData[i].Count + 1]; for (int j = 0; j < lineData[i].Count; j++) { int currRadius = Convert.ToInt32(lineData[i][j] / 100.0 * radius); double angleHude = j * angle * Math.PI / 180;/*角度變成弧度*/ int x = (int)(currRadius * Math.Cos(angleHude)) + centerX; int y = (int)(currRadius * Math.Sin(angleHude)) + centerY; Point point = new Point(x, y); points[j] = point; } points[lineData[i].Count] = points[0]; txtBrush.Color = Color.FromArgb(Convert.ToInt32(0.7 * 255), tempColor.R, tempColor.G, tempColor.B); g.FillPolygon(txtBrush, 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); }