在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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); }
|
请发表评论