C# 使用GDI繪制雷達(dá)圖的實(shí)例
最近項(xiàng)目要用C#實(shí)現(xiàn)畫一個(gè)雷達(dá)圖,搜了搜網(wǎng)上竟然找不到C#畫雷達(dá)圖的解決方案,那么自己實(shí)現(xiàn)一個(gè)吧
實(shí)現(xiàn)效果如下圖:
代碼如下:
public static class RadarDemo { static float mW = 1200; static float mH = 1200; static Dictionary<string, float> mData = new Dictionary<string, float> { //{ "速度",77}, { "力量", 72}, { "防守", 110}, { "射門", 50}, { "傳球", 80}, { "耐力", 60 } };//維度數(shù)據(jù) static float mCount = mData.Count; //邊數(shù) static float mCenter = mW * 0.5f; //中心點(diǎn) static float mRadius = mCenter - 100; //半徑(減去的值用于給繪制的文本留空間) static double mAngle = (Math.PI * 2) / mCount; //角度 static Graphics graphics = null; static int mPointRadius = 5; // 各個(gè)維度分值圓點(diǎn)的半徑 static int textFontSize = 18; //頂點(diǎn)文字大小 px const string textFontFamily = "Microsoft Yahei"; //頂點(diǎn)字體 static Color lineColor = Color.Green; static Color fillColor = Color.FromArgb(128, 255, 0, 0); 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個(gè)圈 for (var i = 0; i < mCount; i++) { var points = new List<PointF>(); var currR = r * (i + 1); //當(dāng)前半徑 //畫6條邊 for (var j = 0; 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(); } //頂點(diǎn)連線 private static void DrawLines(Graphics ctx) { for (var i = 0; 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 = 0; 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 > 0 && mAngle * i <= Math.PI / 2) { 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 / 2 && 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 * 3 / 2) { ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x - ctx.MeasureString(item.Key, font).Width, y); } else if (mAngle * i > Math.PI * 3 / 2) { 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(); } //繪制數(shù)據(jù)區(qū)域 private static void DrawRegion(Graphics ctx) { int i = 0; List<PointF> points = new List<PointF>(); foreach (var item in mData) { var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i) * item.Value / 100); var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i) * item.Value / 100); 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(); } //畫點(diǎn) private static void DrawCircle(Graphics ctx) { //var r = mCenter / 18; var r = mPointRadius; int i = 0; foreach (var item in mData) { var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i) * item.Value / 100); var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i) * item.Value / 100); ctx.FillPie(new SolidBrush(fillColor), x - r, y - r, r * 2, r * 2, 0, 360); //ctx.DrawArc(new Pen(lineColor), x, y, r, r, 0, (float)Math.PI * 2); i++; } ctx.Save(); } }
把這個(gè)類粘貼到你的項(xiàng)目中,執(zhí)行RadarDemo.Show();就會(huì)在你的根目錄里生成雷達(dá)圖了,為了方便理解怎么畫出來(lái)的,我把畫每一個(gè)步驟時(shí)的圖片都保存下來(lái)了??梢宰孕羞\(yùn)行查看
總結(jié)
以上所述是小編給大家介紹的C# 使用GDI繪制雷達(dá)圖的實(shí)例,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
C# 對(duì)文件與文件夾的操作包括刪除、移動(dòng)與復(fù)制
在.Net中,對(duì)文件(File)和文件夾(Folder)的操作可以使用File類和Directory類,也可以使用FileInfo類和DirectoryInfo類,本文將詳細(xì)介紹,需要的朋友可以參考2012-11-11基于C#實(shí)現(xiàn)的輕量級(jí)多線程隊(duì)列圖文詳解
這篇文章主要給大家介紹了關(guān)于基于C#實(shí)現(xiàn)的輕量級(jí)多線程隊(duì)列的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08用NPOI創(chuàng)建Excel、合并單元格、設(shè)置單元格樣式、邊框的方法
本篇文章小編為大家介紹,用NPOI創(chuàng)建Excel、合并單元格、設(shè)置單元格樣式、邊框的方法。需要的朋友參考下2013-04-04使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能
這篇文章主要為大家介紹了使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08WPF實(shí)現(xiàn)環(huán)(圓)形菜單的示例代碼
這篇文章主要介紹了如何利用WPF繪制一個(gè)簡(jiǎn)單的環(huán)形菜單,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-07-07通過(guò)容器擴(kuò)展屬性IExtenderProvider實(shí)現(xiàn)WinForm通用數(shù)據(jù)驗(yàn)證組件
這篇文章介紹了通過(guò)容器擴(kuò)展屬性IExtenderProvider實(shí)現(xiàn)WinForm通用數(shù)據(jù)驗(yàn)證組件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12C#開發(fā)Windows服務(wù)實(shí)例之實(shí)現(xiàn)禁止QQ運(yùn)行
這篇文章主要介紹了通過(guò)C#開發(fā)Windows服務(wù),查殺qq進(jìn)程的服務(wù)功能,需要的朋友可以參考下2013-10-10C#從文件或標(biāo)準(zhǔn)輸入設(shè)備讀取指定行的方法
這篇文章主要介紹了C#從文件或標(biāo)準(zhǔn)輸入設(shè)備讀取指定行的方法,涉及C#文件及IO操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04