MatrixData.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using FastReport.Utils;
  5. using System.Collections;
  6. namespace FastReport.Matrix
  7. {
  8. /// <summary>
  9. /// Contains a set of properties and methods to hold and manipulate the matrix descriptors.
  10. /// </summary>
  11. /// <remarks>
  12. /// This class contains three collections of descriptors such as <see cref="Columns"/>,
  13. /// <see cref="Rows"/> and <see cref="Cells"/>. Use collections' methods to add/remove
  14. /// descriptors. When you are done, call the <see cref="MatrixObject.BuildTemplate"/>
  15. /// method to refresh the matrix.
  16. /// <para/>To fill a matrix in code, use the <b>AddValue</b> method.
  17. /// </remarks>
  18. public class MatrixData
  19. {
  20. #region Fields
  21. private MatrixHeader columns;
  22. private MatrixHeader rows;
  23. private MatrixCells cells;
  24. #endregion
  25. #region Properties
  26. /// <summary>
  27. /// Gets a collection of column descriptors.
  28. /// </summary>
  29. /// <remarks>
  30. /// Note: after you change something in this collection, call the
  31. /// <see cref="MatrixObject.BuildTemplate"/> method to refresh the matrix.
  32. /// </remarks>
  33. public MatrixHeader Columns
  34. {
  35. get { return columns; }
  36. }
  37. /// <summary>
  38. /// Gets a collection of row descriptors.
  39. /// </summary>
  40. /// <remarks>
  41. /// Note: after you change something in this collection, call the
  42. /// <see cref="MatrixObject.BuildTemplate"/> method to refresh the matrix.
  43. /// </remarks>
  44. public MatrixHeader Rows
  45. {
  46. get { return rows; }
  47. }
  48. /// <summary>
  49. /// Gets a collection of data cell descriptors.
  50. /// </summary>
  51. /// <remarks>
  52. /// Note: after you change something in this collection, call the
  53. /// <see cref="MatrixObject.BuildTemplate"/> method to refresh the matrix.
  54. /// </remarks>
  55. public MatrixCells Cells
  56. {
  57. get { return cells; }
  58. }
  59. internal bool IsEmpty
  60. {
  61. get { return Cells.IsEmpty; }
  62. }
  63. #endregion
  64. #region Public Methods
  65. /// <summary>
  66. /// Clears all descriptors.
  67. /// </summary>
  68. public void Clear()
  69. {
  70. Columns.Reset();
  71. Rows.Reset();
  72. Cells.Reset();
  73. }
  74. /// <summary>
  75. /// Adds a value in the matrix.
  76. /// </summary>
  77. /// <param name="columnValues">Array of column values.</param>
  78. /// <param name="rowValues">Array of row values.</param>
  79. /// <param name="cellValues">Array of data values.</param>
  80. /// <remarks>
  81. /// The number of elements in an array passed to this method must be the same as
  82. /// a number of descriptors in the appropriate collection. That is, if your matrix
  83. /// has one column, two row and one cell descriptors (in <b>Columns</b>, <b>Rows</b> and
  84. /// <b>Cells</b> collections respectively), you have to pass one-element array for the
  85. /// <b>columnValues</b> param, two-element array for the <b>rowValues</b> and one-element
  86. /// array for the <b>cellValues</b>.
  87. /// </remarks>
  88. /// <example>This example demonstrates how to fill a simple matrix that contains one column,
  89. /// row and cell.
  90. /// <code>
  91. /// MatrixObject matrix;
  92. /// matrix.Data.AddValue(
  93. /// new object[] { 1996 },
  94. /// new object[] { "Andrew Fuller" },
  95. /// new object[] { 123.45f });
  96. ///
  97. /// // this will produce the following result:
  98. /// // | 1996 |
  99. /// // --------------+----------+
  100. /// // Andrew Fuller | 123.45|
  101. /// // --------------+----------+
  102. /// </code>
  103. /// </example>
  104. public void AddValue(object[] columnValues, object[] rowValues, object[] cellValues)
  105. {
  106. AddValue(columnValues, rowValues, cellValues, 0);
  107. }
  108. /// <summary>
  109. /// Adds a value in the matrix.
  110. /// </summary>
  111. /// <param name="columnValues">Array of column values.</param>
  112. /// <param name="rowValues">Array of row values.</param>
  113. /// <param name="cellValues">Array of data values.</param>
  114. /// <param name="dataRowNo">Datasource row index.</param>
  115. /// <remarks>
  116. /// See the <see cref="AddValue(object[],object[],object[])"/> method for more details.
  117. /// </remarks>
  118. public void AddValue(object[] columnValues, object[] rowValues, object[] cellValues, int dataRowNo)
  119. {
  120. MatrixHeaderItem column = Columns.Find(columnValues, true, dataRowNo);
  121. MatrixHeaderItem row = Rows.Find(rowValues, true, dataRowNo);
  122. Cells.AddValue(column.Index, row.Index, cellValues);
  123. }
  124. internal ArrayList GetValues(int columnIndex, int rowIndex, int cellIndex)
  125. {
  126. return Cells.GetValues(columnIndex, rowIndex, cellIndex);
  127. }
  128. internal void SetValues(int columnIndex, int rowIndex, object[] cellValues)
  129. {
  130. Cells.SetValues(columnIndex, rowIndex, cellValues);
  131. }
  132. /// <summary>
  133. /// Gets a value with specified column, row and cell indicies.
  134. /// </summary>
  135. /// <param name="columnIndex">Index of a column.</param>
  136. /// <param name="rowIndex">Index of a row.</param>
  137. /// <param name="cellIndex">Index of a cell.</param>
  138. /// <returns>The value of a cell.</returns>
  139. public object GetValue(int columnIndex, int rowIndex, int cellIndex)
  140. {
  141. return Cells.GetValue(columnIndex, rowIndex, cellIndex);
  142. }
  143. /// <summary>
  144. /// Sets the cell's value.
  145. /// </summary>
  146. /// <param name="columnIndex">Index of a column.</param>
  147. /// <param name="rowIndex">Index of a row.</param>
  148. /// <param name="cellValue">The new value.</param>
  149. public void SetValue(int columnIndex, int rowIndex, object cellValue)
  150. {
  151. SetValues(columnIndex, rowIndex, new object[] { cellValue });
  152. }
  153. #endregion
  154. internal MatrixData()
  155. {
  156. columns = new MatrixHeader();
  157. columns.Name = "MatrixColumns";
  158. rows = new MatrixHeader();
  159. rows.Name = "MatrixRows";
  160. cells = new MatrixCells();
  161. cells.Name = "MatrixCells";
  162. }
  163. }
  164. }