這篇文章給大家介紹C#中怎么利用GDI酷繪制一個(gè)雷達(dá)圖,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
堅(jiān)守“ 做人真誠(chéng) · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專(zhuān)業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都成都酒樓設(shè)計(jì)小微創(chuàng)業(yè)公司專(zhuān)業(yè)提供企業(yè)網(wǎng)站制作營(yíng)銷(xiāo)網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺(jué)設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁(yè)布局、功能開(kāi)發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。
代碼如下:
public static class RadarDemo { static float mW = 1200; static float mH = 1200; static Dictionary mData = new Dictionary { //{ "速度",77}, { "力量", 72}, { "防守", 110}, { "射門(mén)", 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); //畫(huà)6個(gè)圈 for (var i = 0; i < mCount; i++) { var points = new List(); var currR = r * (i + 1); //當(dāng)前半徑 //畫(huà)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 points = new List(); 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(); } //畫(huà)點(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(); } }
關(guān)于C#中怎么利用GDI酷繪制一個(gè)雷達(dá)圖就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
標(biāo)題名稱(chēng):C#中怎么利用GDI酷繪制一個(gè)雷達(dá)圖
URL鏈接:
http://weahome.cn/article/gcsepd.html