• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#画图之雷达图

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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);
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++里大写TRUE和小写true区别发布时间:2022-07-13
下一篇:
C++异常发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap