(七十四)c#Winform自定義控制項-金字塔圖表

来源:https://www.cnblogs.com/bfyx/archive/2019/09/26/11590722.html
-Advertisement-
Play Games

前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...


前提

入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果覺得寫的還行,請點個 star 支持一下吧

歡迎前來交流探討: 企鵝群568015492 企鵝群568015492

麻煩博客下方點個【推薦】,謝謝

NuGet

Install-Package HZH_Controls

目錄

https://www.cnblogs.com/bfyx/p/11364884.html

用處及效果

準備工作

依然使用GDI+畫圖,不懂的先百度瞭解下

開始

添加一些枚舉

 1  public enum FunelChartAlignment
 2     {
 3         /// <summary>
 4         /// The left
 5         /// </summary>
 6         Left,
 7         /// <summary>
 8         /// The center
 9         /// </summary>
10         Center,
11         /// <summary>
12         /// The right
13         /// </summary>
14         Right
15     }
16 
17  public enum FunelChartDirection
18     {
19         /// <summary>
20         /// Up
21         /// </summary>
22         UP,
23         /// <summary>
24         /// Down
25         /// </summary>
26         Down
27     }

添加一個項實體

 1   public class FunelChartItem
 2     {
 3         /// <summary>
 4         /// Gets or sets the text.
 5         /// </summary>
 6         /// <value>The text.</value>
 7         public string Text { get; set; }
 8         /// <summary>
 9         /// Gets or sets the value.
10         /// </summary>
11         /// <value>The value.</value>
12         public float Value { get; set; }
13         /// <summary>
14         /// Gets or sets the color of the value.
15         /// </summary>
16         /// <value>The color of the value.</value>
17         public System.Drawing.Color? ValueColor { get; set; }
18         /// <summary>
19         /// Gets or sets the color of the text fore.
20         /// </summary>
21         /// <value>The color of the text fore.</value>
22         public System.Drawing.Color? TextForeColor { get; set; }
23     }

添加一個類UCFunnelChart ,繼承UserControl

添加一些控制屬性

  1 /// <summary>
  2         /// The title
  3         /// </summary>
  4         private string title;
  5         /// <summary>
  6         /// Gets or sets the title.
  7         /// </summary>
  8         /// <value>The title.</value>
  9         [Browsable(true)]
 10         [Category("自定義")]
 11         [Description("獲取或設置標題")]
 12         public string Title
 13         {
 14             get { return title; }
 15             set
 16             {
 17                 title = value;
 18                 ResetTitleSize();
 19                 Invalidate();
 20             }
 21         }
 22 
 23         /// <summary>
 24         /// The title font
 25         /// </summary>
 26         private Font titleFont = new Font("微軟雅黑", 12);
 27         /// <summary>
 28         /// Gets or sets the title font.
 29         /// </summary>
 30         /// <value>The title font.</value>
 31         [Browsable(true)]
 32         [Category("自定義")]
 33         [Description("獲取或設置標題字體")]
 34         public Font TitleFont
 35         {
 36             get { return titleFont; }
 37             set
 38             {
 39                 titleFont = value;
 40                 ResetTitleSize();
 41                 Invalidate();
 42             }
 43         }
 44 
 45         /// <summary>
 46         /// The title fore color
 47         /// </summary>
 48         private Color titleForeColor = Color.Black;
 49         /// <summary>
 50         /// Gets or sets the color of the title fore.
 51         /// </summary>
 52         /// <value>The color of the title fore.</value>
 53         [Browsable(true)]
 54         [Category("自定義")]
 55         [Description("獲取或設置標題文字顏色")]
 56         public Color TitleForeColor
 57         {
 58             get { return titleForeColor; }
 59             set
 60             {
 61                 titleForeColor = value;
 62                 Invalidate();
 63             }
 64         }
 65         /// <summary>
 66         /// The items
 67         /// </summary>
 68         private FunelChartItem[] items;
 69         /// <summary>
 70         /// Gets or sets the items.
 71         /// </summary>
 72         /// <value>The items.</value>
 73         [Browsable(true)]
 74         [Category("自定義")]
 75         [Description("獲取或設置項目")]
 76         public FunelChartItem[] Items
 77         {
 78             get { return items; }
 79             set
 80             {
 81                 items = value;
 82                 Invalidate();
 83             }
 84         }
 85 
 86         /// <summary>
 87         /// The direction
 88         /// </summary>
 89         private FunelChartDirection direction = FunelChartDirection.UP;
 90         /// <summary>
 91         /// Gets or sets the direction.
 92         /// </summary>
 93         /// <value>The direction.</value>
 94         [Browsable(true)]
 95         [Category("自定義")]
 96         [Description("獲取或設置方向")]
 97         public FunelChartDirection Direction
 98         {
 99             get { return direction; }
100             set
101             {
102                 direction = value;
103                 Invalidate();
104             }
105         }
106 
107         /// <summary>
108         /// The alignment
109         /// </summary>
110         private FunelChartAlignment alignment = FunelChartAlignment.Center;
111         /// <summary>
112         /// Gets or sets the alignment.
113         /// </summary>
114         /// <value>The alignment.</value>
115         [Browsable(true)]
116         [Category("自定義")]
117         [Description("獲取或設置對齊方式")]
118         public FunelChartAlignment Alignment
119         {
120             get { return alignment; }
121             set
122             {
123                 alignment = value;
124                 Invalidate();
125             }
126         }
127 
128         /// <summary>
129         /// The item text align
130         /// </summary>
131         private FunelChartAlignment itemTextAlign = FunelChartAlignment.Center;
132         /// <summary>
133         /// Gets or sets the item text align.
134         /// </summary>
135         /// <value>The item text align.</value>
136         [Browsable(true)]
137         [Category("自定義")]
138         [Description("獲取或設置文字位置")]
139         public FunelChartAlignment ItemTextAlign
140         {
141             get { return itemTextAlign; }
142             set
143             {
144                 itemTextAlign = value;
145                 ResetWorkingRect();
146                 Invalidate();
147             }
148         }
149         /// <summary>
150         /// The show value
151         /// </summary>
152         private bool showValue = false;
153         /// <summary>
154         /// Gets or sets a value indicating whether [show value].
155         /// </summary>
156         /// <value><c>true</c> if [show value]; otherwise, <c>false</c>.</value>
157         [Browsable(true)]
158         [Category("自定義")]
159         [Description("獲取或設置是否顯示值")]
160         public bool ShowValue
161         {
162             get { return showValue; }
163             set
164             {
165                 showValue = value;
166                 Invalidate();
167             }
168         }
169 
170 
171         /// <summary>
172         /// The value format
173         /// </summary>
174         private string valueFormat = "0.##";
175         /// <summary>
176         /// Gets or sets the value format.
177         /// </summary>
178         /// <value>The value format.</value>
179         [Browsable(true)]
180         [Category("自定義")]
181         [Description("獲取或設置值格式化")]
182         public string ValueFormat
183         {
184             get { return valueFormat; }
185             set
186             {
187                 valueFormat = value;
188                 Invalidate();
189             }
190         }
191 
192         /// <summary>
193         /// The m rect working
194         /// </summary>
195         RectangleF m_rectWorking;
196         /// <summary>
197         /// The m title size
198         /// </summary>
199         SizeF m_titleSize = SizeF.Empty;
200         /// <summary>
201         /// The int split width
202         /// </summary>
203         int intSplitWidth = 1;

構造函數初始化

 1  public UCFunnelChart()
 2         {
 3             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
 4             this.SetStyle(ControlStyles.DoubleBuffer, true);
 5             this.SetStyle(ControlStyles.ResizeRedraw, true);
 6             this.SetStyle(ControlStyles.Selectable, true);
 7             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
 8             this.SetStyle(ControlStyles.UserPaint, true);
 9             this.FontChanged += UCFunnelChart_FontChanged;
10             Font = new Font("微軟雅黑", 8);
11 
12             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
13             this.SizeChanged += UCFunnelChart_SizeChanged;
14             Size = new System.Drawing.Size(150, 150);
15             items = new FunelChartItem[0];
16             if (ControlHelper.IsDesignMode())
17             {
18                 items = new FunelChartItem[5];
19                 for (int i = 0; i < 5; i++)
20                 {
21                     items[i] = new FunelChartItem()
22                     {
23                         Text = "item" + i,
24                         Value = 10 * (i + 1)
25                     };
26                 }
27             }
28         }

當大小及狀態改變時 重新計算工作區域

 1   void UCFunnelChart_FontChanged(object sender, EventArgs e)
 2         {
 3             ResetWorkingRect();
 4         }
 5 
 6         /// <summary>
 7         /// Handles the SizeChanged event of the UCFunnelChart control.
 8         /// </summary>
 9         /// <param name="sender">The source of the event.</param>
10         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
11         void UCFunnelChart_SizeChanged(object sender, EventArgs e)
12         {
13             ResetWorkingRect();
14         }
15 
16         /// <summary>
17         /// Resets the working rect.
18         /// </summary>
19         private void ResetWorkingRect()
20         {
21             if (itemTextAlign == FunelChartAlignment.Center)
22             {
23                 m_rectWorking = new RectangleF(0, m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10), this.Width, this.Height - (m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10)));
24             }
25             else if (itemTextAlign == FunelChartAlignment.Left)
26             {
27                 float fltMax = 0;
28                 if (items != null && items.Length > 0)
29                 {
30                     using (Graphics g = this.CreateGraphics())
31                     {
32                         fltMax = items.Max(p => g.MeasureString(p.Text, Font).Width);
33                     }
34                 }
35                 m_rectWorking = new RectangleF(fltMax, m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10), this.Width - fltMax, this.Height - (m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10)));
36             }
37             else
38             {
39                 float fltMax = 0;
40                 if (items != null && items.Length > 0)
41                 {
42                     using (Graphics g = this.CreateGraphics())
43                     {
44                         fltMax = items.Max(p => g.MeasureString(p.Text, Font).Width);
45                     }
46                 }
47                 m_rectWorking = new RectangleF(0, m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10), this.Width - fltMax, this.Height - (m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10)));
48             }
49         }
50 
51         /// <summary>
52         /// Resets the size of the title.
53         /// </summary>
54         private void ResetTitleSize()
55         {
56             if (string.IsNullOrEmpty(title))
57             {
58                 m_titleSize = SizeF.Empty;
59             }
60             else
61             {
62                 using (Graphics g = this.CreateGraphics())
63                 {
64                     m_titleSize = g.MeasureString(title, titleFont);
65                     m_titleSize.Height += 20;
66                 }
67             }
68             ResetWorkingRect();
69         }

重繪

  1 protected override void OnPaint(PaintEventArgs e)
  2         {
  3             base.OnPaint(e);
  4             var g = e.Graphics;
  5             g.SetGDIHigh();
  6 
  7             if (!string.IsNullOrEmpty(title))
  8             {
  9                 g.DrawString(title, titleFont, new SolidBrush(titleForeColor), new RectangleF(0, 0, this.Width, m_titleSize.Height), new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
 10             }
 11 
 12             if (items == null || items.Length <= 0)
 13             {
 14                 g.DrawString("沒有數據", Font, new SolidBrush(Color.Black), this.m_rectWorking, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
 15                 return;
 16             }
 17 
 18             List<FunelChartItem> lstItems;
 19             if (direction == FunelChartDirection.UP)
 20             {
 21                 lstItems = items.OrderBy(p => p.Value).ToList();
 22             }
 23             else
 24             {
 25                 lstItems = items.OrderByDescending(p => p.Value).ToList();
 26             }
 27 
 28             List<RectangleF> lstRects = new List<RectangleF>();
 29             List<GraphicsPath> lstPaths = new List<GraphicsPath>();
 30             float maxValue = lstItems.Max(p => p.Value);
 31             float dblSplitHeight = m_rectWorking.Height / lstItems.Count;
 32             for (int i = 0; i < lstItems.Count; i++)
 33             {
 34                 FunelChartItem item = lstItems[i];
 35                 if (item.ValueColor == null || item.ValueColor == Color.Empty || item.ValueColor == Color.Transparent)
 36                     item.ValueColor = ControlHelper.Colors[i];
 37 
 38                 switch (alignment)
 39                 {
 40                     case FunelChartAlignment.Left:
 41                         lstRects.Add(new RectangleF(m_rectWorking.Left, m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
 42                         break;
 43                     case FunelChartAlignment.Center:
 44                         lstRects.Add(new RectangleF(m_rectWorking.Left + (m_rectWorking.Width - (item.Value / maxValue * m_rectWorking.Width)) / 2, m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
 45                         break;
 46                     case FunelChartAlignment.Right:
 47                         lstRects.Add(new RectangleF(m_rectWorking.Right - (item.Value / maxValue * m_rectWorking.Width), m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
 48                         break;
 49                 }
 50             }
 51 
 52             for (int i = 0; i < lstRects.Count; i++)
 53             {
 54                 var rect = lstRects[i];
 55                 GraphicsPath path = new GraphicsPath();
 56                 List<PointF> lstPoints = new List<PointF>();
 57                 if (direction == FunelChartDirection.UP)
 58                 {
 59                     switch (alignment)
 60                     {
 61                         case FunelChartAlignment.Left:
 62                             lstPoints.Add(new PointF(rect.Left, rect.Top));
 63                             if (i != 0)
 64                             {
 65                                 lstPoints.Add(new PointF(lstRects[i - 1].Right, rect.Top));
 66                             }
 67                             break;
 68                         case FunelChartAlignment.Center:
 69                             if (i == 0)
 70                             {
 71                    

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一:Appltools下載: 新建: .py文件 ...
  • MATLAB實例:PCA降維 作者:凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/ 1. iris數據 2. MATLAB程式 3. 結果 iris_pca:前兩個主成分 累計貢獻率 可見:前兩個主成分已經占了95%的貢獻程度。這兩個主成分可以近似表示整個數 ...
  • 最近在開發.Net MVC程式時,突然出現未能載入文件或程式集的錯誤, 錯誤1 錯誤2 猜測時由於引用了Swagger,導致Swagger依賴的組件版本和現有版本衝突(現在仍未確定是這個原因),浪費了好長時間去尋找原因。最終查找資料的時候看到有人提及到Web.config,於時試著將現有5.2.3. ...
  • 在身份認證中,如果某個Action需要許可權才能訪問,最開始的想法就是,哪個Action需要許可權才能訪問,我們寫個特性標註到上面即可,[TypeFilter(typeof(CustomAuthorizeActionFilterAttribute))] 當然了,要先在服務裡面使用Session的服務== ...
  • 在.Net Framework MVC 中有四種過濾器,授權過濾器(Authorize)、Action 過濾器、結果過濾器(Result)、異常過濾器(Exception)四種過濾器。在.Net Core MVC中,有五種過濾器,授權過濾器、Action過濾器、異常過濾器、結果過濾器、資源過濾器,新 ...
  • c# sharepoint client object model 客戶端如何創建中英文站點 ClientContext ClientValidate = tools.GetContext(OnlineSiteUrl, User, Pass, true); Web oWebSite = Client ...
  • [TOC] .NET Conf 2019 2019 9.23 9.25召開了 ".NET Conf 2019" 大會,大會宣佈了 ".Net Core 3.0" 正式版。這兩天我也開始試著將自己Github上的項目從 .Net Core 2.2升級到 .Net Core 3.0 。其中有一個項目,是 ...
  • 場景 Winforn中設置ZedGraph曲線圖的屬性、坐標軸屬性、刻度屬性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100112573 Winform中實現ZedGraph的多條Y軸(附源碼下載): https://bl ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...