Total.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing.Design;
  6. using FastReport.Utils;
  7. namespace FastReport.Data
  8. {
  9. /// <summary>
  10. /// Specifies the total type.
  11. /// </summary>
  12. public enum TotalType
  13. {
  14. /// <summary>
  15. /// The total returns sum of values.
  16. /// </summary>
  17. Sum,
  18. /// <summary>
  19. /// The total returns minimal value.
  20. /// </summary>
  21. Min,
  22. /// <summary>
  23. /// The total returns maximal value.
  24. /// </summary>
  25. Max,
  26. /// <summary>
  27. /// The total returns average value.
  28. /// </summary>
  29. Avg,
  30. /// <summary>
  31. /// The total returns number of values.
  32. /// </summary>
  33. Count,
  34. /// <summary>
  35. /// The total returns number of distinct values.
  36. /// </summary>
  37. CountDistinct
  38. }
  39. /// <summary>
  40. /// Represents a total that is used to calculate aggregates such as Sum, Min, Max, Avg, Count.
  41. /// </summary>
  42. public partial class Total : Base
  43. {
  44. #region Fields
  45. private TotalType totalType;
  46. private string expression;
  47. private DataBand evaluator;
  48. private BandBase printOn;
  49. private string evaluateCondition;
  50. private bool includeInvisibleRows;
  51. private bool resetAfterPrint;
  52. private bool resetOnReprint;
  53. // engine
  54. private object value;
  55. private int count;
  56. private bool keeping;
  57. private Total keepTotal;
  58. private TotalCollection subTotals;
  59. private TotalCollection parentTotal;
  60. private const string subPrefix = "_sub";
  61. private Hashtable distinctValues;
  62. #endregion
  63. #region Properties
  64. /// <summary>
  65. /// Gets or sets the total type.
  66. /// </summary>
  67. [DefaultValue(TotalType.Sum)]
  68. [Category("Data")]
  69. public TotalType TotalType
  70. {
  71. get { return totalType; }
  72. set { totalType = value; }
  73. }
  74. /// <summary>
  75. /// Gets or sets the expression used to calculate the total.
  76. /// </summary>
  77. [Category("Data")]
  78. [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
  79. public string Expression
  80. {
  81. get { return expression; }
  82. set { expression = value; }
  83. }
  84. /// <summary>
  85. /// Gets or sets the evaluator databand.
  86. /// </summary>
  87. /// <remarks>
  88. /// The total will be calculated for each row of this band.
  89. /// </remarks>
  90. [Category("Data")]
  91. public DataBand Evaluator
  92. {
  93. get { return evaluator; }
  94. set { evaluator = value; }
  95. }
  96. /// <summary>
  97. /// This property is kept for compatibility only.
  98. /// </summary>
  99. [Category("Data")]
  100. [Browsable(false)]
  101. public BandBase Resetter
  102. {
  103. get { return printOn; }
  104. set { printOn = value; }
  105. }
  106. /// <summary>
  107. /// Gets or sets the band to print the total on.
  108. /// </summary>
  109. /// <remarks>
  110. /// The total will be resetted after the specified band has been printed.
  111. /// </remarks>
  112. [Category("Data")]
  113. public BandBase PrintOn
  114. {
  115. get { return printOn; }
  116. set { printOn = value; }
  117. }
  118. /// <summary>
  119. /// Gets or sets a value that determines whether the total should be resetted after print.
  120. /// </summary>
  121. [DefaultValue(true)]
  122. [Category("Behavior")]
  123. public bool ResetAfterPrint
  124. {
  125. get { return resetAfterPrint; }
  126. set { resetAfterPrint = value; }
  127. }
  128. /// <summary>
  129. /// Gets or sets a value that determines whether the total should be resetted if printed
  130. /// on repeated band (i.e. band with "RepeatOnEveryPage" flag).
  131. /// </summary>
  132. [DefaultValue(true)]
  133. [Category("Behavior")]
  134. public bool ResetOnReprint
  135. {
  136. get { return resetOnReprint; }
  137. set { resetOnReprint = value; }
  138. }
  139. /// <summary>
  140. /// Gets or sets the condition which tells the total to evaluate.
  141. /// </summary>
  142. [Category("Data")]
  143. [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
  144. public string EvaluateCondition
  145. {
  146. get { return evaluateCondition; }
  147. set { evaluateCondition = value; }
  148. }
  149. /// <summary>
  150. /// Gets or sets a value that determines if invisible rows of the <b>Evaluator</b> should
  151. /// be included into the total's value.
  152. /// </summary>
  153. [DefaultValue(false)]
  154. [Category("Behavior")]
  155. public bool IncludeInvisibleRows
  156. {
  157. get { return includeInvisibleRows; }
  158. set { includeInvisibleRows = value; }
  159. }
  160. /// <summary>
  161. /// This property is not relevant to this class.
  162. /// </summary>
  163. [Browsable(false)]
  164. public new Restrictions Restrictions
  165. {
  166. get { return base.Restrictions; }
  167. set { base.Restrictions = value; }
  168. }
  169. /// <summary>
  170. /// Gets the value of total.
  171. /// </summary>
  172. [Browsable(false)]
  173. public object Value
  174. {
  175. get { return GetValue(); }
  176. }
  177. private bool IsPageFooter
  178. {
  179. get
  180. {
  181. return PrintOn is PageFooterBand || PrintOn is ColumnFooterBand ||
  182. ((PrintOn is HeaderFooterBandBase) && (PrintOn as HeaderFooterBandBase).RepeatOnEveryPage);
  183. }
  184. }
  185. private bool IsInsideHierarchy
  186. {
  187. get
  188. {
  189. return Report.Engine.HierarchyLevel > 1 &&
  190. !Name.StartsWith(subPrefix) &&
  191. PrintOn != null && PrintOn.ParentDataBand != null && PrintOn.ParentDataBand.IsHierarchical;
  192. }
  193. }
  194. #endregion
  195. #region Private Methods
  196. private object GetValue()
  197. {
  198. if (IsInsideHierarchy)
  199. {
  200. Total subTotal = FindSubTotal(subPrefix + Report.Engine.HierarchyLevel.ToString());
  201. return subTotal.Value;
  202. }
  203. if (TotalType == TotalType.Avg)
  204. {
  205. if (value == null || count == 0)
  206. return null;
  207. return new Variant(value) / count;
  208. }
  209. else if (TotalType == TotalType.Count)
  210. return count;
  211. else if (TotalType == TotalType.CountDistinct)
  212. return distinctValues.Keys.Count;
  213. return value;
  214. }
  215. private void AddValue(object value)
  216. {
  217. if (value == null || value is DBNull)
  218. return;
  219. if (TotalType == TotalType.CountDistinct)
  220. {
  221. distinctValues[value] = 1;
  222. return;
  223. }
  224. if (this.value == null)
  225. this.value = value;
  226. else
  227. {
  228. switch (TotalType)
  229. {
  230. case TotalType.Sum:
  231. case TotalType.Avg:
  232. this.value = (new Variant(this.value) + new Variant(value)).Value;
  233. break;
  234. case TotalType.Min:
  235. IComparable val1 = this.value as IComparable;
  236. IComparable val2 = value as IComparable;
  237. if (val1 != null && val2 != null && val1.CompareTo(val2) > 0)
  238. this.value = value;
  239. break;
  240. case TotalType.Max:
  241. val1 = this.value as IComparable;
  242. val2 = value as IComparable;
  243. if (val1 != null && val2 != null && val1.CompareTo(val2) < 0)
  244. this.value = value;
  245. break;
  246. }
  247. }
  248. }
  249. private Total FindSubTotal(string name)
  250. {
  251. Total result = subTotals.FindByName(name);
  252. if (result == null)
  253. {
  254. result = this.Clone();
  255. result.Name = name;
  256. subTotals.Add(result);
  257. }
  258. return result;
  259. }
  260. #endregion
  261. #region Public Methods
  262. /// <inheritdoc/>
  263. public override void Assign(Base source)
  264. {
  265. BaseAssign(source);
  266. }
  267. /// <inheritdoc/>
  268. public override void Serialize(FRWriter writer)
  269. {
  270. Total c = writer.DiffObject as Total;
  271. base.Serialize(writer);
  272. if (TotalType != c.TotalType)
  273. writer.WriteValue("TotalType", TotalType);
  274. if (Expression != c.Expression)
  275. writer.WriteStr("Expression", Expression);
  276. if (Evaluator != c.Evaluator)
  277. writer.WriteRef("Evaluator", Evaluator);
  278. if (PrintOn != c.PrintOn)
  279. writer.WriteRef("PrintOn", PrintOn);
  280. if (ResetAfterPrint != c.ResetAfterPrint)
  281. writer.WriteBool("ResetAfterPrint", ResetAfterPrint);
  282. if (ResetOnReprint != c.ResetOnReprint)
  283. writer.WriteBool("ResetOnReprint", ResetOnReprint);
  284. if (EvaluateCondition != c.EvaluateCondition)
  285. writer.WriteStr("EvaluateCondition", EvaluateCondition);
  286. if (IncludeInvisibleRows != c.IncludeInvisibleRows)
  287. writer.WriteBool("IncludeInvisibleRows", IncludeInvisibleRows);
  288. }
  289. internal Total Clone()
  290. {
  291. Total total = new Total();
  292. total.SetReport(Report);
  293. total.TotalType = TotalType;
  294. total.Expression = Expression;
  295. total.Evaluator = Evaluator;
  296. total.PrintOn = PrintOn;
  297. total.ResetAfterPrint = ResetAfterPrint;
  298. total.ResetOnReprint = ResetOnReprint;
  299. total.EvaluateCondition = EvaluateCondition;
  300. total.IncludeInvisibleRows = IncludeInvisibleRows;
  301. return total;
  302. }
  303. #endregion
  304. #region Report Engine
  305. /// <inheritdoc/>
  306. public override string[] GetExpressions()
  307. {
  308. List<string> expressions = new List<string>();
  309. if (!String.IsNullOrEmpty(Expression))
  310. expressions.Add(Expression);
  311. if (!String.IsNullOrEmpty(EvaluateCondition))
  312. expressions.Add(EvaluateCondition);
  313. return expressions.ToArray();
  314. }
  315. /// <inheritdoc/>
  316. public override void Clear()
  317. {
  318. base.Clear();
  319. value = null;
  320. count = 0;
  321. distinctValues.Clear();
  322. }
  323. internal void AddValue()
  324. {
  325. if (IsInsideHierarchy)
  326. {
  327. Total subTotal = FindSubTotal(subPrefix + Report.Engine.HierarchyLevel.ToString());
  328. subTotal.AddValue();
  329. return;
  330. }
  331. if (!Evaluator.Visible && !IncludeInvisibleRows)
  332. return;
  333. if (!String.IsNullOrEmpty(EvaluateCondition) && (bool)Report.Calc(EvaluateCondition) == false)
  334. return;
  335. if (TotalType != TotalType.Count && String.IsNullOrEmpty(Expression))
  336. return;
  337. if (keeping)
  338. {
  339. keepTotal.AddValue();
  340. return;
  341. }
  342. // if Total refers to another total
  343. Total total = IsRefersToTotal();
  344. if (total != null)
  345. {
  346. if (!total.parentTotal.Contains(this))
  347. {
  348. total.parentTotal.Add(this);
  349. }
  350. return;
  351. }
  352. object value = TotalType == TotalType.Count ? null : Report.Calc(Expression);
  353. AddValue(value);
  354. if (TotalType != TotalType.Avg || (value != null && !(value is DBNull)))
  355. count++;
  356. }
  357. internal void ResetValue()
  358. {
  359. if (IsInsideHierarchy)
  360. {
  361. Total subTotal = FindSubTotal(subPrefix + Report.Engine.HierarchyLevel.ToString());
  362. subTotal.ResetValue();
  363. return;
  364. }
  365. Clear();
  366. }
  367. internal void ExecuteTotal(object val)
  368. {
  369. foreach (Total total in parentTotal)
  370. total.Execute(val);
  371. }
  372. private void Execute(object val)
  373. {
  374. AddValue(val);
  375. if (value != null && !(value is DBNull))
  376. count++;
  377. }
  378. private Total IsRefersToTotal()
  379. {
  380. string expr = Expression;
  381. if (expr.StartsWith("[") && expr.EndsWith("]"))
  382. expr = expr.Substring(1, expr.Length - 2);
  383. return Report.Dictionary.Totals.FindByName(expr);
  384. }
  385. internal void StartKeep()
  386. {
  387. if (!IsPageFooter || keeping)
  388. return;
  389. keeping = true;
  390. keepTotal = Clone();
  391. }
  392. internal void EndKeep()
  393. {
  394. if (!IsPageFooter || !keeping)
  395. return;
  396. keeping = false;
  397. if (TotalType == TotalType.CountDistinct)
  398. {
  399. foreach (object key in keepTotal.distinctValues)
  400. {
  401. distinctValues[key] = 1;
  402. }
  403. }
  404. else
  405. {
  406. AddValue(keepTotal.value);
  407. count += keepTotal.count;
  408. }
  409. }
  410. #endregion
  411. /// <summary>
  412. /// Initializes a new instance of the <see cref="Total"/> class with default settings.
  413. /// </summary>
  414. public Total()
  415. {
  416. expression = "";
  417. evaluateCondition = "";
  418. resetAfterPrint = true;
  419. subTotals = new TotalCollection(null);
  420. parentTotal = new TotalCollection(null);
  421. distinctValues = new Hashtable();
  422. SetFlags(Flags.CanCopy, false);
  423. }
  424. }
  425. }