C# 使用GDI绘制雷达图
最近项目要用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绘制雷达图的更多相关文章
- 【带着canvas去流浪(6)】绘制雷达图
目录 一. 任务说明 二. 重点提示 三. 示例代码 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文 ...
- Emgu-WPF 激光雷达研究-绘制雷达图
原文:Emgu-WPF 激光雷达研究-绘制雷达图 硬件:Hokuyo URG04LX 环境:VS2017- win10- 64 Emgu_3.2.0.2682 语言:C# WPF 数据解析参考 ...
- 带着canvas去流浪系列之六 绘制雷达图
[摘要] 用canvas原生API实现百度Echarts基本图表. 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 任务说明 使用原生canvas ...
- 利用matlibplot绘制雷达图
之前在一些数据分析案例中看到用 Go 语言绘制的雷达图,非常的漂亮,就想着用matlibplot.pyplot也照着画一个,遗憾的是matlibplot.pyplot模块中没有直接绘制雷达图的函数,不 ...
- 利用d3.js绘制雷达图
利用d3,js将数据可视化,能够做到数据与代码的分离.方便以后改动数据. 这次利用d3.js绘制了一个五维的雷达图.即将多个对象的五种属性在一张图上对照. 数据写入data.csv.数据类型写入typ ...
- Mesh绘制雷达图(UGUI)
参考资料:http://www.cnblogs.com/jeason1997/p/5130413.html ** 描述:雷达图 刷新 radarDate.SetVerticesDirty(); usi ...
- Python绘制雷达图(俗称六芒星)
原文链接:https://blog.csdn.net/Just_youHG/article/details/83904618 背景 <Python数据分析与挖掘实战> 案例2–航空公司客户 ...
- wepy绘制雷达图
代码如下: <style lang='less'> .radar-canvas2 { width: 690rpx; height: 420rpx; } </style> < ...
- R语言绘图:雷达图
使用fmsb包绘制雷达图 library("fmsb") radarfig <- rbind(rep(90, 4), rep(60, 4), c(86.17, 73.96, ...
随机推荐
- abp模块化开发之通用树1:基本使用
一.概述 有些功能在单个项目或多个项目被重复使用,比如:附件,同一个系统中的多个模块都可能使用到,不同项目也有需要.再比如:有无限级分类的树形功能,区域.产品分类.数据字典等.最简单粗暴的办法是直接复 ...
- 想要入行web前端要知道web前端的的基本工作职责
入一行,要先知一行 ”:我们来看看web前端开发职位 无论什么门派都要做到的一些基本工作职责 首先,你必须是一个合格的“页面仔”,这个叫法不好听,但很生动: 我们都知道,所有呈现的内容都是基于HTML ...
- SpringMVC使用Redis共享session
在使用之前,请确认项目已经整合了Redis 一.加入依赖 <dependency> <groupId>org.springframework.session</group ...
- 使用VMware安装CentOS 7
环境:Windows10 , VMware Workstation 15 Player, CentOS 7 为什么选择CentOS ? 主流: 目前的Linux操作系统主要应用于生产环境,主流企业级L ...
- 第421期 Python 周刊
新闻 感谢 Guido 链接: https://blog.dropbox.com/topics/company/thank-you--guido Python之父 Guido van Rossum 即 ...
- 源码分析RocketMQ ACL实现机制
目录 1.BrokerController#initialAcl 2.PlainAccessValidator 2.1 类图 2.2 构造方法 2.3 parse方法 2.4 validate 方法 ...
- 【系统之音】SystemUI篇(二)SysytemUI功能一览--草稿
Main Menu > Navigate > Type Hierarchy(Ctrl + H)
- rsync高级同步工具
1.什么是rsync rsync 是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份的优秀工具,rsync软件使用于 unix/linux/windows等多种操作系统平台. 2 ...
- 文字转语音?我只用十行Python代码就搞定了!
详细使用教程 1.没安装Python的小伙伴需要先安装一下 2.win+r输入cmd打开命令行,输入:pip install baidu-aip,如下安装百度AI的模块. 3.新建文本文档,copy如 ...
- CF977D Divide by three, multiply by two
题目链接 我同学在旁边做者道题,我也看了一下 真的好水难 一看这道题,直接搜索 剪枝是不可能剪枝的一辈子不可能 Code #include <cstdio> #include <io ...