/MetaBuilders.WebControls/Polling/PollResultsList.cs

# · C# · 306 lines · 228 code · 32 blank · 46 comment · 39 complexity · 3273cbd04d87499e6e7e6fb51ade38a1 MD5 · raw file

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.Web.UI.Design;
  5. using System.ComponentModel;
  6. using System.ComponentModel.Design;
  7. namespace MetaBuilders.WebControls
  8. {
  9. /// <summary>
  10. /// Displays the results of a poll as a bar graph list.
  11. /// </summary>
  12. internal class PollResultsList : System.Web.UI.WebControls.ListControl
  13. {
  14. #region Properties
  15. /// <summary>
  16. /// Gets the style of bars in the list.
  17. /// </summary>
  18. [
  19. Category( "Style" ),
  20. DesignerSerializationVisibility( DesignerSerializationVisibility.Content ),
  21. NotifyParentProperty( true ),
  22. PersistenceMode( PersistenceMode.InnerProperty ),
  23. ]
  24. public virtual BarStyle BarStyle
  25. {
  26. get
  27. {
  28. if ( barStyle == null )
  29. {
  30. barStyle = new BarStyle();
  31. // barStyle.BorderWidth = Unit.Pixel( 1 );
  32. // barStyle.Height = Unit.Pixel( 10 );
  33. if ( IsTrackingViewState )
  34. {
  35. ( (IStateManager)barStyle ).TrackViewState();
  36. }
  37. }
  38. return barStyle;
  39. }
  40. }
  41. private BarStyle barStyle;
  42. /// <summary>
  43. /// Gets or sets the <see cref="ResultDisplayType"/> of the bars in the list.
  44. /// </summary>
  45. /// <remarks>
  46. /// With <see cref="ResultDisplayType.Percentage"/>, the widths of the bars will add up to width of the control as a whole.
  47. /// With <see cref="ResultDisplayType.Count"/>, the item with the highest count will fill the width of the control.
  48. /// </remarks>
  49. [
  50. Category( "Appearance" ),
  51. Description( "Gets or sets the ResultDisplayType of the bars in the list." ),
  52. Bindable( true ),
  53. DefaultValue( ResultDisplayType.Count ),
  54. ]
  55. public virtual ResultDisplayType BarDisplay
  56. {
  57. get
  58. {
  59. Object savedState = this.ViewState["BarDisplay"];
  60. if ( savedState != null )
  61. {
  62. return (ResultDisplayType)savedState;
  63. }
  64. return ResultDisplayType.Count;
  65. }
  66. set
  67. {
  68. if ( !Enum.IsDefined( typeof( ResultDisplayType ), value ) )
  69. {
  70. throw new InvalidEnumArgumentException( "value", (Int32)value, typeof( ResultDisplayType ) );
  71. }
  72. this.ViewState["BarDisplay"] = value;
  73. }
  74. }
  75. /// <summary>
  76. /// Gets or sets the <see cref="ResultDisplayType"/> of the vote count display in the list.
  77. /// </summary>
  78. /// <remarks>
  79. /// With <see cref="ResultDisplayType.Percentage"/>, the vote count of each item will be the percentage of votes which that answer received.
  80. /// With <see cref="ResultDisplayType.Count"/>, the vote count will show the number of votes which that answer received.
  81. /// </remarks>
  82. [
  83. Category( "Appearance" ),
  84. Description( "Gets or sets the ResultDisplayType of the vote count display in the list." ),
  85. Bindable( true ),
  86. DefaultValue( ResultDisplayType.Percentage ),
  87. ]
  88. public virtual ResultDisplayType VoteCountDisplay
  89. {
  90. get
  91. {
  92. Object savedState = this.ViewState["VoteCountDisplay"];
  93. if ( savedState != null )
  94. {
  95. return (ResultDisplayType)savedState;
  96. }
  97. return ResultDisplayType.Percentage;
  98. }
  99. set
  100. {
  101. if ( !Enum.IsDefined( typeof( ResultDisplayType ), value ) )
  102. {
  103. throw new InvalidEnumArgumentException( "value", (Int32)value, typeof( ResultDisplayType ) );
  104. }
  105. this.ViewState["VoteCountDisplay"] = value;
  106. }
  107. }
  108. #endregion
  109. #region ViewState
  110. /// <summary>
  111. /// Overrides <see cref="Control.LoadViewState"/>.
  112. /// </summary>
  113. /// <param name="savedState"></param>
  114. protected override void LoadViewState( object savedState )
  115. {
  116. if ( savedState == null )
  117. {
  118. base.LoadViewState( null );
  119. return;
  120. }
  121. Pair pair = savedState as Pair;
  122. if ( pair != null )
  123. {
  124. base.LoadViewState( pair.First );
  125. if ( pair.Second != null )
  126. {
  127. ( (IStateManager)this.BarStyle ).LoadViewState( pair.Second );
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// Overrides <see cref="Control.SaveViewState"/>.
  133. /// </summary>
  134. /// <returns></returns>
  135. protected override object SaveViewState()
  136. {
  137. Pair pair = new Pair();
  138. pair.First = base.SaveViewState();
  139. if ( this.barStyle != null )
  140. {
  141. pair.Second = ( (IStateManager)this.barStyle ).SaveViewState();
  142. }
  143. if ( pair.First == null && pair.Second == null )
  144. {
  145. return null;
  146. }
  147. return pair;
  148. }
  149. /// <summary>
  150. /// Overrides <see cref="TrackViewState"/>.
  151. /// </summary>
  152. protected override void TrackViewState()
  153. {
  154. base.TrackViewState();
  155. if ( this.barStyle != null )
  156. {
  157. ( (IStateManager)barStyle ).TrackViewState();
  158. }
  159. }
  160. #endregion
  161. #region Life Cycle
  162. public override void DataBind()
  163. {
  164. base.DataBind( true );
  165. }
  166. /// <summary>
  167. /// Overrides <see cref="WebControl.RenderBeginTag"/>.
  168. /// </summary>
  169. public override void RenderBeginTag( HtmlTextWriter writer )
  170. {
  171. // No Opening Tag
  172. }
  173. /// <summary>
  174. /// Overrides <see cref="WebControl.RenderContents"/>.
  175. /// </summary>
  176. protected override void RenderContents( HtmlTextWriter writer )
  177. {
  178. Table outerTable = new Table();
  179. outerTable.ControlStyle.CopyFrom( this.ControlStyle );
  180. if ( outerTable.Font.Size == FontUnit.Empty )
  181. {
  182. outerTable.Font.Size = FontUnit.Parse( "1em", System.Globalization.CultureInfo.InvariantCulture );
  183. }
  184. outerTable.Width = Unit.Percentage( 100 );
  185. Double totalVotes = 0;
  186. Double barDisplayMax = 0;
  187. // First I need to get totals and percentages information
  188. foreach ( ListItem item in Items )
  189. {
  190. Int32 itemVoteCount = Int32.Parse( item.Value, System.Globalization.CultureInfo.InvariantCulture );
  191. totalVotes += itemVoteCount;
  192. switch ( this.BarDisplay )
  193. {
  194. case ResultDisplayType.Percentage:
  195. barDisplayMax = totalVotes;
  196. break;
  197. case ResultDisplayType.Count:
  198. if ( itemVoteCount > barDisplayMax )
  199. {
  200. barDisplayMax = itemVoteCount;
  201. }
  202. break;
  203. }
  204. }
  205. // Then I cycle thru the items and display the bar and vote count
  206. foreach ( ListItem item in Items )
  207. {
  208. TableRow rRow = new TableRow();
  209. outerTable.Rows.Add( rRow );
  210. TableCell barHolder = new TableCell();
  211. barHolder.Width = Unit.Percentage( 100 );
  212. rRow.Cells.Add( barHolder );
  213. Label answerName = new Label();
  214. answerName.Text = item.Text + "<br>";
  215. barHolder.Controls.Add( answerName );
  216. HorizontalBar bar = new HorizontalBar();
  217. bar.EnableViewState = false;
  218. bar.Percentage = ( barDisplayMax != 0 ) ? ( Double.Parse( item.Value, System.Globalization.CultureInfo.InvariantCulture ) / barDisplayMax ) * 100 : 0;
  219. ;
  220. bar.ControlStyle.CopyFrom( this.BarStyle );
  221. if ( bar.Height == Unit.Empty )
  222. {
  223. bar.Height = Unit.Pixel( 10 );
  224. }
  225. if ( bar.BorderWidth == Unit.Empty )
  226. {
  227. bar.BorderWidth = Unit.Pixel( 1 );
  228. }
  229. if ( bar.ForeColor.Equals( bar.BackColor ) )
  230. {
  231. PollView parent = this.NamingContainer as PollView;
  232. if ( parent != null && !parent.ForeColor.Equals( bar.BackColor ) )
  233. {
  234. bar.ForeColor = parent.ForeColor;
  235. }
  236. else
  237. {
  238. if ( bar.BackColor.Equals( System.Drawing.Color.Black ) )
  239. {
  240. bar.ForeColor = System.Drawing.Color.White;
  241. }
  242. else
  243. {
  244. bar.ForeColor = System.Drawing.Color.Black;
  245. }
  246. }
  247. }
  248. barHolder.Controls.Add( bar );
  249. TableCell numericDisplay = new TableCell();
  250. switch ( this.VoteCountDisplay )
  251. {
  252. case ResultDisplayType.Percentage:
  253. Double votePercentage = ( totalVotes != 0 ) ? ( Double.Parse( item.Value, System.Globalization.CultureInfo.InvariantCulture ) / totalVotes ) * 100 : 0;
  254. numericDisplay.Text = votePercentage.ToString( "##0.0", System.Globalization.CultureInfo.InvariantCulture ) + "%";
  255. break;
  256. case ResultDisplayType.Count:
  257. numericDisplay.Text = item.Value;
  258. break;
  259. }
  260. numericDisplay.VerticalAlign = VerticalAlign.Bottom;
  261. rRow.Cells.Add( numericDisplay );
  262. }
  263. outerTable.RenderControl( writer );
  264. }
  265. /// <summary>
  266. /// Overrides <see cref="WebControl.RenderEndTag"/>.
  267. /// </summary>
  268. public override void RenderEndTag( HtmlTextWriter writer )
  269. {
  270. // No End Tag
  271. }
  272. #endregion
  273. }
  274. }