最近项目要用C#实现画一个雷达图,搜了搜网上竟然找不到C#画雷达图的解决方案,那么自己实现一个吧

实现效果如下图:

代码如下:

     public static class RadarDemo
{
static float mW = ;
static float mH = ;
static Dictionary<string, float> mData = new Dictionary<string, float>
{
//{ "速度",77},
{ "力量", },
{ "防守", },
{ "射门", },
{ "传球", },
{ "耐力", }
};//维度数据
static float mCount = mData.Count; //边数
static float mCenter = mW * 0.5f; //中心点
static float mRadius = mCenter - ; //半径(减去的值用于给绘制的文本留空间)
static double mAngle = (Math.PI * ) / mCount; //角度
static Graphics graphics = null;
static int mPointRadius = ; // 各个维度分值圆点的半径
static int textFontSize = ; //顶点文字大小 px
const string textFontFamily = "Microsoft Yahei"; //顶点字体
static Color lineColor = Color.Green;
static Color fillColor = Color.FromArgb(, , , );
static Color fontColor = Color.Black; public static void Show()
{
Bitmap img = new Bitmap((int)mW, (int)mH);
graphics = Graphics.FromImage(img);
graphics.Clear(Color.White);
img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/0.png", ImageFormat.Png);
DrawPolygon(graphics);
img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/1.png", ImageFormat.Png);
DrawLines(graphics);
img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/2.png", ImageFormat.Png);
DrawText(graphics);
img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/3.png", ImageFormat.Png);
DrawRegion(graphics);
img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/4.png", ImageFormat.Png);
DrawCircle(graphics);
img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/5.png", ImageFormat.Png);
img.Dispose();
graphics.Dispose(); } // 绘制多边形边
private static void DrawPolygon(Graphics ctx)
{
var r = mRadius / mCount; //单位半径
Pen pen = new Pen(lineColor);
//画6个圈
for (var i = ; i < mCount; i++)
{
var points = new List<PointF>();
var currR = r * (i + ); //当前半径
//画6条边
for (var j = ; j < mCount; j++)
{
var x = (float)(mCenter + currR * Math.Cos(mAngle * j));
var y = (float)(mCenter + currR * Math.Sin(mAngle * j));
points.Add(new PointF { X = x, Y = y });
}
ctx.DrawPolygon(pen, points.ToArray());
//break;
} ctx.Save();
} //顶点连线
private static void DrawLines(Graphics ctx)
{
for (var i = ; i < mCount; i++)
{
var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i));
var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i));
ctx.DrawLine(new Pen(lineColor), new PointF { X = mCenter, Y = mCenter }, new PointF { X = x, Y = y });
//break;
}
ctx.Save();
} //绘制文本
private static void DrawText(Graphics ctx)
{
var fontSize = textFontSize;//mCenter / 12;
Font font = new Font(textFontFamily, fontSize, FontStyle.Regular); int i = ;
foreach (var item in mData)
{
var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i));
var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i) - fontSize); if (mAngle * i > && mAngle * i <= Math.PI / )
{
ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x - ctx.MeasureString(item.Key, font).Width * 0.5f, y + fontSize/* y + fontSize*/);
}
else if (mAngle * i > Math.PI / && mAngle * i <= Math.PI)
{
ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x - ctx.MeasureString(item.Key, font).Width, y /*y + fontSize*/);
}
else if (mAngle * i > Math.PI && mAngle * i <= Math.PI * / )
{
ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x - ctx.MeasureString(item.Key, font).Width, y);
}
else if (mAngle * i > Math.PI * / )
{
ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x - ctx.MeasureString(item.Key, font).Width * 0.5f, y - fontSize * 0.5f);
}
else
{
ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x, y /* y + fontSize*/);
}
i++;
}
ctx.Save();
} //绘制数据区域
private static void DrawRegion(Graphics ctx)
{
int i = ;
List<PointF> points = new List<PointF>();
foreach (var item in mData)
{
var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i) * item.Value / );
var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i) * item.Value / ); points.Add(new PointF { X = x, Y = y }); //ctx.DrawArc(new Pen(lineColor), x, y, r, r, 0, (float)Math.PI * 2);
i++;
} //GraphicsPath path = new GraphicsPath();
//path.AddLines(points.ToArray()); ctx.FillPolygon(new SolidBrush(fillColor), points.ToArray()); ctx.Save();
} //画点
private static void DrawCircle(Graphics ctx)
{
//var r = mCenter / 18;
var r = mPointRadius; int i = ;
foreach (var item in mData)
{
var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i) * item.Value / );
var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i) * item.Value / );
ctx.FillPie(new SolidBrush(fillColor), x - r, y - r, r * , r * , , );
//ctx.DrawArc(new Pen(lineColor), x, y, r, r, 0, (float)Math.PI * 2);
i++;
}
ctx.Save();
} }

把这个类粘贴到你的项目中,执行RadarDemo.Show();就会在你的根目录里生成雷达图了,为了方便理解怎么画出来的,我把画每一个步骤时的图片都保存下来了。可以自行运行查看

C# 使用GDI绘制雷达图的更多相关文章

  1. 【带着canvas去流浪(6)】绘制雷达图

    目录 一. 任务说明 二. 重点提示 三. 示例代码 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文 ...

  2. Emgu-WPF 激光雷达研究-绘制雷达图

    原文:Emgu-WPF 激光雷达研究-绘制雷达图 硬件:Hokuyo URG04LX 环境:VS2017- win10- 64  Emgu_3.2.0.2682 语言:C#  WPF   数据解析参考 ...

  3. 带着canvas去流浪系列之六 绘制雷达图

    [摘要] 用canvas原生API实现百度Echarts基本图表. 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 任务说明 使用原生canvas ...

  4. 利用matlibplot绘制雷达图

    之前在一些数据分析案例中看到用 Go 语言绘制的雷达图,非常的漂亮,就想着用matlibplot.pyplot也照着画一个,遗憾的是matlibplot.pyplot模块中没有直接绘制雷达图的函数,不 ...

  5. 利用d3.js绘制雷达图

    利用d3,js将数据可视化,能够做到数据与代码的分离.方便以后改动数据. 这次利用d3.js绘制了一个五维的雷达图.即将多个对象的五种属性在一张图上对照. 数据写入data.csv.数据类型写入typ ...

  6. Mesh绘制雷达图(UGUI)

    参考资料:http://www.cnblogs.com/jeason1997/p/5130413.html ** 描述:雷达图 刷新 radarDate.SetVerticesDirty(); usi ...

  7. Python绘制雷达图(俗称六芒星)

    原文链接:https://blog.csdn.net/Just_youHG/article/details/83904618 背景 <Python数据分析与挖掘实战> 案例2–航空公司客户 ...

  8. wepy绘制雷达图

    代码如下: <style lang='less'> .radar-canvas2 { width: 690rpx; height: 420rpx; } </style> < ...

  9. R语言绘图:雷达图

    使用fmsb包绘制雷达图 library("fmsb") radarfig <- rbind(rep(90, 4), rep(60, 4), c(86.17, 73.96, ...

随机推荐

  1. Codeforces Round #592 (Div. 2)

    A. Pens and Pencils 题目链接:https://codeforces.com/contest/1244/problem/A 题意: 给定五个数 a , b , c , d , k 求 ...

  2. 记录一次创建.net core 项目 并且发布到docekr【完全新手入门】

    1]环境说明 操作系统:Window 10 专业版 开发工具 Vs2019专业版 Docker:  Docker for Windows  2]创建.net core项目并且发布 2.0先打开并且运行 ...

  3. 使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus

    更多内容,关注公众号:来学云计算 场景: 某设备定时于每天23:00左右将一天的运行日志.devicelogtxt上传到Azure Blob,期待Blob文件上传后, 自动通过Azure Functi ...

  4. MySql数据库之常用数据类型及常用约束简述

    本文呢,主要给大家简述一下数据库中常用的几种数据类型以及约束. 1.数据类型 数据类型,是指数据表中可以存储的数据的种类. 数据库中常用的数据类型有: 1.整型:int.bit 2.小数:decima ...

  5. PMBOK 指南 第三章 项目经理的角色

    项目经理的角色 3.1 概述 项目经理类似于交响乐团的指挥 成员与角色 在团队中的职责 知识和技能:具备项目管理知识.技术知识.理解和经验. 3.2 定义 项目经理是由执行组织委派,领导团队实现项目目 ...

  6. javascript中的对象拷贝

    js中的数据类型 在介绍javascript中的对象的拷贝之前,我先介绍一个基础的东西,javascript中的数据类型. 我们做前端的应该都知到在es6 之前,javascript中的数据类型Boo ...

  7. spring-boot 再添加mysql启动器的时候报错, The driver is automatically registered via the SPI and manual loading of the driver class....

    mysql驱动更新迭代之后驱动,稍微有点变化: com.mysql.jdbc.Driver (变化为) --> driver-class-name: com.mysql.cj.jdbc.Driv ...

  8. Highlight List View Objects 突出显示列表视图对象

    In this lesson, you will learn how to format data that satisfies the specified criteria. For this pu ...

  9. JavaScript 递归遍历json串获取相关数据

    递归遍历json串获取相关数据   by:授客 QQ:1033553122 1.   测试数据 // 导航菜单 [ { id: 1, parentId: 0, parentName: null, na ...

  10. JS 参考手册

    JS 参考手册 JavaScript 对象 HTML DOM 对象