PieChartControl.cs

Description du code

PieChartControl.cs est un fichier du projet BrolDev.
Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.

Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.

Code source ou contenu du fichier

  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6.  
  7. namespace be.gaudry.model.drawing.chart
  8. {
  9. /// <summary>
  10. /// Summary description for PieChartControl.
  11. /// </summary>
  12. public class PieChartControl : System.Windows.Forms.Panel
  13. {
  14. /// <summary>
  15. /// Initializes the <c>PieChartControl</c>.
  16. /// </summary>
  17. public PieChartControl() : base() {
  18. this.SetStyle(ControlStyles.UserPaint, true);
  19. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  20. this.SetStyle(ControlStyles.DoubleBuffer, true);
  21. this.SetStyle(ControlStyles.ResizeRedraw, true);
  22. m_toolTip = new ToolTip();
  23. }
  24.  
  25. /// <summary>
  26. /// Sets the left margin for the chart.
  27. /// </summary>
  28. public float LeftMargin {
  29. set {
  30. Debug.Assert(value >= 0);
  31. m_leftMargin = value;
  32. Invalidate();
  33. }
  34. }
  35.  
  36. /// <summary>
  37. /// Sets the right margin for the chart.
  38. /// </summary>
  39. public float RightMargin {
  40. set {
  41. Debug.Assert(value >= 0);
  42. m_rightMargin = value;
  43. Invalidate();
  44. }
  45. }
  46.  
  47. /// <summary>
  48. /// Sets the top margin for the chart.
  49. /// </summary>
  50. public float TopMargin {
  51. set {
  52. Debug.Assert(value >= 0);
  53. m_topMargin = value;
  54. Invalidate();
  55. }
  56. }
  57.  
  58. /// <summary>
  59. /// Sets the bottom margin for the chart.
  60. /// </summary>
  61. public float BottomMargin {
  62. set {
  63. Debug.Assert(value >= 0);
  64. m_bottomMargin = value;
  65. Invalidate();
  66. }
  67. }
  68.  
  69. /// <summary>
  70. /// Sets the indicator if chart should fit the bounding rectangle
  71. /// exactly.
  72. /// </summary>
  73. public bool FitChart {
  74. set {
  75. m_fitChart = value;
  76. Invalidate();
  77. }
  78. }
  79.  
  80. /// <summary>
  81. /// Sets values to be represented by the chart.
  82. /// </summary>
  83. public double[] Values {
  84. set {
  85. m_values = value;
  86. Invalidate();
  87. }
  88. get { return m_values; }
  89. }
  90.  
  91. /// <summary>
  92. /// Sets colors to be used for rendering pie slices.
  93. /// </summary>
  94. public Color[] Colors {
  95. set {
  96. m_colors = value;
  97. Invalidate();
  98. }
  99. }
  100.  
  101. /// <summary>
  102. /// Flag indicating that the selected part of the pie move on mouse on
  103. /// </summary>
  104. public bool DisplaceOnMouseOn
  105. {
  106. get { return this.displaceOnMouseOn; }
  107. set { this.displaceOnMouseOn = value; }
  108. }
  109.  
  110. /// <summary>
  111. /// Sets values for slice displacements.
  112. /// </summary>
  113. public float[] SliceRelativeDisplacements {
  114. set {
  115. m_relativeSliceDisplacements = value;
  116. //brol modif
  117. tempSliceDisplacements = new float[m_relativeSliceDisplacements.Length];
  118. m_relativeSliceDisplacements.CopyTo(tempSliceDisplacements,0);
  119. Invalidate();
  120. }
  121. }
  122.  
  123. /// <summary>
  124. /// Gets or sets tooltip texts.
  125. /// </summary>
  126. public string[] ToolTips {
  127. set { m_toolTipTexts = value; }
  128. get { return m_toolTipTexts; }
  129. }
  130.  
  131. /// <summary>
  132. /// Sets texts appearing by each pie slice.
  133. /// </summary>
  134. public string[] Texts {
  135. set { m_texts = value; }
  136. get { return m_texts; }
  137. }
  138.  
  139. /// <summary>
  140. /// Sets pie slice reative height.
  141. /// </summary>
  142. public float SliceRelativeHeight {
  143. set {
  144. m_sliceRelativeHeight = value;
  145. Invalidate();
  146. }
  147. }
  148.  
  149. /// <summary>
  150. /// Sets the shadow style.
  151. /// </summary>
  152. public ShadowStyle ShadowStyle {
  153. set {
  154. m_shadowStyle = value;
  155. Invalidate();
  156. }
  157. }
  158.  
  159. /// <summary>
  160. /// Sets the edge color type.
  161. /// </summary>
  162. public EdgeColorType EdgeColorType {
  163. set {
  164. m_edgeColorType = value;
  165. Invalidate();
  166. }
  167. }
  168.  
  169. /// <summary>
  170. /// Sets the edge lines width.
  171. /// </summary>
  172. public float EdgeLineWidth {
  173. set {
  174. m_edgeLineWidth = value;
  175. Invalidate();
  176. }
  177. }
  178.  
  179. /// <summary>
  180. /// Sets the initial angle from which pies are drawn.
  181. /// </summary>
  182. public float InitialAngle {
  183. set {
  184. m_initialAngle = value;
  185. Invalidate();
  186. }
  187. }
  188.  
  189. /// <summary>
  190. /// Increase relativeSliceDisplacement for an item
  191. /// </summary>
  192. /// <param name="partIndex">(int) index of the item</param>
  193. /// <param name="outer">(bool) true to increase, false to decrease</param>
  194. /// <returns>(bool) true if displacement done</returns>
  195. public bool displacePart(int partIndex, bool outer)
  196. {
  197. if (partIndex >= m_relativeSliceDisplacements.Length)
  198. return false;
  199. if (outer)
  200. if ((m_relativeSliceDisplacements[partIndex] + 0.5F) > 1F)
  201. m_relativeSliceDisplacements[partIndex] = 1F;
  202. else
  203. m_relativeSliceDisplacements[partIndex] = m_relativeSliceDisplacements[partIndex] + 0.5F;
  204. else
  205. if ((m_relativeSliceDisplacements[partIndex] - 0.5F) <=0F)
  206. m_relativeSliceDisplacements[partIndex] = 0F;
  207. else
  208. m_relativeSliceDisplacements[partIndex] = m_relativeSliceDisplacements[partIndex] - 0.5F;
  209.  
  210. //m_relativeSliceDisplacements[partIndex] = (outer) ? m_relativeSliceDisplacements[partIndex] + 0.5F : ((m_relativeSliceDisplacements[partIndex] - 0.5F)>0.1F)?m_relativeSliceDisplacements[partIndex] - 0.5F:0.1F;
  211. Invalidate();
  212. return true;
  213. }
  214.  
  215. /// <summary>
  216. /// Handles <c>OnPaint</c> event.
  217. /// </summary>
  218. /// <param name="args">
  219. /// <c>PaintEventArgs</c> object.
  220. /// </param>
  221. protected override void OnPaint(PaintEventArgs args) {
  222. base.OnPaint(args);
  223. if (HasAnyValue) {
  224. DoDraw(args.Graphics);
  225. }
  226. }
  227.  
  228. /// <summary>
  229. /// Sets values for the chart and draws them.
  230. /// </summary>
  231. /// <param name="graphics">
  232. /// Graphics object used for drawing.
  233. /// </param>
  234. protected void DoDraw(Graphics graphics) {
  235. if (m_values != null && m_values.Length > 0) {
  236. graphics.SmoothingMode = SmoothingMode.AntiAlias;
  237. float width = ClientSize.Width - m_leftMargin - m_rightMargin;
  238. float height = ClientSize.Height - m_topMargin - m_bottomMargin;
  239. // if the width or height if <=0 an exception would be thrown -> exit method..
  240. if (width <= 0 || height <= 0)
  241. return;
  242. if (m_pieChart != null)
  243. m_pieChart.Dispose();
  244. if (m_colors != null && m_colors.Length > 0)
  245. m_pieChart = new PieChart3D(m_leftMargin, m_topMargin, width, height, m_values, m_colors, m_sliceRelativeHeight, m_texts);
  246. else
  247. m_pieChart = new PieChart3D(m_leftMargin, m_topMargin, width, height, m_values, m_sliceRelativeHeight, m_texts);
  248. m_pieChart.FitToBoundingRectangle = m_fitChart;
  249. m_pieChart.InitialAngle = m_initialAngle;
  250. m_pieChart.SliceRelativeDisplacements = m_relativeSliceDisplacements;
  251. m_pieChart.EdgeColorType = m_edgeColorType;
  252. m_pieChart.EdgeLineWidth = m_edgeLineWidth;
  253. m_pieChart.ShadowStyle = m_shadowStyle;
  254. m_pieChart.HighlightedIndex = m_highlightedIndex;
  255. m_pieChart.Draw(graphics);
  256. m_pieChart.Font = this.Font;
  257. m_pieChart.ForeColor = this.ForeColor;
  258. m_pieChart.PlaceTexts(graphics);
  259. }
  260. }
  261.  
  262. /// <summary>
  263. /// Handles <c>MouseEnter</c> event to activate the tooltip.
  264. /// </summary>
  265. /// <param name="e"></param>
  266. protected override void OnMouseEnter(System.EventArgs e) {
  267. base.OnMouseEnter(e);
  268. m_defaultToolTipAutoPopDelay = m_toolTip.AutoPopDelay;
  269. m_toolTip.AutoPopDelay = Int16.MaxValue;
  270. }
  271.  
  272. /// <summary>
  273. /// Handles <c>MouseLeave</c> event to disable tooltip.
  274. /// </summary>
  275. /// <param name="e"></param>
  276. protected override void OnMouseLeave(System.EventArgs e) {
  277. base.OnMouseLeave(e);
  278. m_toolTip.RemoveAll();
  279. m_toolTip.AutoPopDelay = m_defaultToolTipAutoPopDelay;
  280. m_highlightedIndex = -1;
  281. Refresh();
  282. }
  283.  
  284. /// <summary>
  285. /// Handles <c>MouseMove</c> event to display tooltip for the pie
  286. /// slice under pointer and to display slice in highlighted color.
  287. /// </summary>
  288. /// <param name="e"></param>
  289. protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) {
  290. base.OnMouseMove(e);
  291. if (m_pieChart != null && m_values != null && m_values.Length > 0) {
  292. int index = m_pieChart.FindPieSliceUnderPoint(new PointF(e.X, e.Y));
  293. if (index != m_highlightedIndex) {
  294. m_highlightedIndex = index;
  295. //brol modif
  296. if (displaceOnMouseOn)
  297. {
  298. if (m_relativeSliceDisplacements.Length > m_highlightedIndex && m_highlightedIndex >= 0)
  299. {
  300. m_relativeSliceDisplacements[m_highlightedIndex] = m_relativeSliceDisplacements[m_highlightedIndex] + 0.5F;
  301. }
  302. else
  303. tempSliceDisplacements.CopyTo(m_relativeSliceDisplacements, 0);
  304. }
  305. Refresh();
  306. }
  307. if (m_highlightedIndex != -1) {
  308. if (m_toolTipTexts == null || m_toolTipTexts.Length <= m_highlightedIndex || m_toolTipTexts[m_highlightedIndex].Length == 0)
  309. m_toolTip.SetToolTip(this, m_values[m_highlightedIndex].ToString());
  310. else
  311. m_toolTip.SetToolTip(this, m_toolTipTexts[m_highlightedIndex]);
  312. }
  313. else {
  314. m_toolTip.RemoveAll();
  315. }
  316. }
  317. }
  318.  
  319. /// <summary>
  320. /// Clean up any resources being used.
  321. /// </summary>
  322. /*protected override void Dispose(bool disposing) {
  323.   if (!m_disposed) {
  324.   try {
  325.   if (disposing) {
  326.   m_pieChart.Dispose();
  327.   m_toolTip.Dispose();
  328.   }
  329.   m_disposed = true;
  330.   }
  331.   finally {
  332.   base.Dispose(disposing);
  333.   }
  334.   }
  335.   }*/
  336.  
  337. /// <summary>
  338. /// Gets a flag indicating if at least one value is nonzero.
  339. /// </summary>
  340. private bool HasAnyValue
  341. {
  342. get {
  343. if (m_values == null)
  344. return false;
  345. foreach (decimal angle in m_values) {
  346. if (angle != 0) {
  347. return true;
  348. }
  349. }
  350. return false;
  351. }
  352. }
  353.  
  354. private PieChart3D m_pieChart = null;
  355. private float m_leftMargin;
  356. private float m_topMargin;
  357. private float m_rightMargin;
  358. private float m_bottomMargin;
  359. private bool m_fitChart = false;
  360.  
  361. private double[] m_values = null;
  362. private Color[] m_colors = null;
  363. private float m_sliceRelativeHeight;
  364. private float[] m_relativeSliceDisplacements = new float[] { 0F };
  365. private string[] m_texts = null;
  366. private string[] m_toolTipTexts = null;
  367. private ShadowStyle m_shadowStyle = ShadowStyle.GradualShadow;
  368. private EdgeColorType m_edgeColorType = EdgeColorType.SystemColor;
  369. private float m_edgeLineWidth = 1F;
  370. private float m_initialAngle;
  371. private int m_highlightedIndex = -1;
  372. private ToolTip m_toolTip = null;
  373. /// <summary>
  374. /// Default AutoPopDelay of the ToolTip control.
  375. /// </summary>
  376. private int m_defaultToolTipAutoPopDelay;
  377. /// <summary>
  378. /// Flag indicating that object has been disposed.
  379. /// </summary>
  380. //private bool m_disposed = false;
  381. /// <summary>
  382. /// Store m_relativeSliceDisplacements values to restore it after OnMouseMove event
  383. /// </summary>
  384. private float[] tempSliceDisplacements = new float[] { 0F };//brol modif
  385. /// <summary>
  386. /// Flag indicating that the selected part of the pie move on mouse on
  387. /// </summary>
  388. private bool displaceOnMouseOn = false;//brol modif
  389. }
  390. }

Structure et Fichiers du projet

Afficher/masquer...


Répertoires contenus dans /var/www/bin/sniplets/bibliobrol/broldev/src/model/drawing/chart/ 
IcôneNomTailleModification
Pas de sous-répertoires.
IcôneNomTailleModification
| _ Répertoire parent0 octets1732348372 23/11/2024 08:52:52
Fichiers contenus dans /var/www/bin/sniplets/bibliobrol/broldev/src/model/drawing/chart/ 
IcôneNomTailleModificationAction
IcôneNomTailleModificationAction
Afficher le fichier .cs|.csShadowStyle.cs620 octets31/10/2018 18:33:22-refusé-
Afficher le fichier .cs|.csEdgeColor.cs2.92 Ko31/10/2018 18:33:21-refusé-
Afficher le fichier .cs|.csPieChart.cs44.97 Ko31/10/2018 18:33:22-refusé-
Afficher le fichier .cs|.csColorUtil.cs2.05 Ko31/10/2018 18:33:21-refusé-
Afficher le fichier .resx|.resxPieChartControl.resx1.69 Ko31/10/2018 18:33:22-refusé-
Afficher le fichier .cs|.csPieChartControl.cs14.04 Ko31/10/2018 18:33:22-refusé-
Afficher le fichier .cs|.csPieSlice.cs52.86 Ko31/10/2018 18:33:22-refusé-
Afficher le fichier .cs|.csGraphicsUtil.cs2.33 Ko31/10/2018 18:33:21-refusé-
Afficher le fichier .cs|.csQuadrilateral.cs6.42 Ko31/10/2018 18:33:22-refusé-
Afficher le fichier .cs|.csEdgeColorType.cs1.64 Ko31/10/2018 18:33:21-refusé-

Utilisation de l'explorateur de code

  • Navigation :
    • Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
    • Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
    • La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
    • Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
  • Affichage :
    • Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
  • Actions :
    • Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 16/10/2009, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/cs-broldev-source-rf-model/drawing/chart//PieChartControl.cs.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.