HierarchicalTable.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Numerics;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Common;
  12. using GrapeCity.Documents.Drawing;
  13. using GrapeCity.Documents.Layout;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // This example shows how to draw a table with hierarchical data,
  20. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  21. public class HierarchicalTable
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. var doc = new GcPdfDocument();
  26. var p = doc.Pages.Add(new SizeF(doc.PageSize.Height, doc.PageSize.Width));
  27. var g = p.Graphics;
  28.  
  29. DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);
  30.  
  31. // Save the PDF:
  32. doc.Save(stream);
  33. return doc.Pages.Count;
  34. }
  35.  
  36. class Range
  37. {
  38. public Range(double valueLow, double deltaLow, double valueHigh, double deltaHigh)
  39. {
  40. ValueLow = valueLow;
  41. DeltaLow = deltaLow;
  42. ValueHigh = valueHigh;
  43. DeltaHigh = deltaHigh;
  44. }
  45. public double ValueLow { get; }
  46. public double DeltaLow { get; }
  47. public double ValueHigh { get; }
  48. public double DeltaHigh { get; }
  49. }
  50.  
  51. class Scope
  52. {
  53. public Scope(int ntrials, Range ad, Range @as, Range nr, Range ra, Range av)
  54. {
  55. Ntrials = ntrials;
  56. AD = ad;
  57. AS = @as;
  58. NR = nr;
  59. RA = ra;
  60. AV = av;
  61. }
  62. public int Ntrials { get; }
  63. public Range AD { get; }
  64. public Range AS { get; }
  65. public Range NR { get; }
  66. public Range RA { get; }
  67. public Range AV { get; }
  68. }
  69.  
  70. static readonly Scope[] Data = new Scope[]
  71. {
  72. new Scope(20, new Range(50.5, 3.2, 74.8, 4.9), new Range(50.4, 3.6, 74.9, 6.8),
  73. new Range(50.3, 2.7, 75.2, 6.5), new Range(49.9, 2.6, 74.8, 6.6), new Range(50.3, 3.0, 74.9, 6.2)),
  74. new Scope(60, new Range(50.3, 4.5, 75.1, 4.5), new Range(50.8, 4.1, 75.6, 6.5),
  75. new Range(50.7, 3.4, 75.7, 5.7), new Range(49.5, 2.9, 74.7, 5.9), new Range(50.3, 3.7, 75.3, 5.6)),
  76. new Scope(100, new Range(52.6, 4.7, 75.9, 4.8), new Range(50.3, 4.6, 75.6, 5.9),
  77. new Range(51.1, 4.1, 75.3, 5.4), new Range(49.8, 3.3, 74.9, 5.3), new Range(51.9, 4.2, 75.4, 5.4)),
  78. new Scope(140, new Range(51.7, 5.3, 75.8, 7.0), new Range(49.8, 5.7, 75.4, 6.8),
  79. new Range(51.4, 4.4, 75.6, 5.6), new Range(49.5, 3.5, 74.6, 6.2), new Range(50.6, 4.7, 75.4, 6.4)),
  80. new Scope(180, new Range(51.5, 8.4, 76.2, 9.5), new Range(52.2, 6.6, 74.9, 7.0),
  81. new Range(52.0, 4.9, 75.7, 6.4), new Range(50.7, 4.2, 74.8, 6.3), new Range(51.6, 6.0, 75.4, 7.3))
  82. };
  83.  
  84. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  85. {
  86. var host = new LayoutHost();
  87. var view = host.CreateView(pageWidth, pageHeight);
  88.  
  89. var rt = view.CreateRect();
  90. rt.AnchorTopLeft(null, 36, 36);
  91.  
  92. int scopeCount = Data.Length;
  93.  
  94. var ta = new TableRenderer(g,
  95. rt, FixedTableSides.TopLeft,
  96. rowCount: 12,
  97. columnCount: scopeCount + 2,
  98. gridLineColor: Color.Black,
  99. gridLineWidth: 1,
  100. rowMinHeight: 20);
  101.  
  102. // Thicker width for some grid lines:
  103. ta.SetVerticalGridLineWidth(2, 3f);
  104. for (int i = 0; i < 13; i += 2)
  105. {
  106. ta.SetHorizontalGridLineWidth(i, 3f);
  107. }
  108.  
  109. var fmt = new TextFormat
  110. {
  111. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cambria.ttc")),
  112. FontSize = 16,
  113. FontSizeInGraphicUnits = true
  114. };
  115.  
  116. ta.AddCell(0, 0, 2, 2);
  117. ta.AddCell(new CellStyle
  118. {
  119. TextAlignment = TextAlignment.Center,
  120. CreateTextLayout = (g, cs, data) =>
  121. {
  122. var tl = g.CreateTextLayout();
  123. tl.Append("|", fmt);
  124. tl.Append("N", new TextFormat(fmt) { FontItalic = true });
  125. tl.Append("trials", new TextFormat(fmt) { FontItalic = true, Subscript = true });
  126. tl.Append("|", fmt);
  127. return tl;
  128. }
  129. }, 0, 2, 1, scopeCount, null);
  130.  
  131. var cs = new CellStyle
  132. {
  133. PaddingLeftRight = 10,
  134. TextAlignment = TextAlignment.Center,
  135. ParagraphAlignment = ParagraphAlignment.Center,
  136. FixedWidth = false,
  137. TextFormat = fmt
  138. };
  139.  
  140. // Add a rotated cell:
  141. ta.AddCell(new CellStyle(cs)
  142. {
  143. PaddingTopBottom = 10,
  144. RotationAngle = 270
  145. }, 2, 0, 8, 1, "Subject");
  146.  
  147. // Assign a style to DefaultCellStyle to avoid passing
  148. // it to each call of the AddCell method:
  149. ta.DefaultCellStyle = cs;
  150.  
  151. ta.AddCell(2, 1, 2, 1, "AD");
  152. ta.AddCell(4, 1, 2, 1, "AS");
  153. ta.AddCell(6, 1, 2, 1, "NR");
  154. ta.AddCell(8, 1, 2, 1, "RA");
  155. ta.AddCell(10, 0, 2, 2, "Average:");
  156.  
  157. for (int i = 0; i < scopeCount; i++)
  158. {
  159. var sc = Data[i];
  160. int ci = i + 2;
  161. ta.AddCell(1, ci, sc.Ntrials.ToString());
  162. ta.AddCell(2, ci, $"{sc.AD.ValueLow:n1} ± {sc.AD.DeltaLow:n1}");
  163. ta.AddCell(3, ci, $"{sc.AD.ValueHigh:n1} ± {sc.AD.DeltaHigh:n1}");
  164. ta.AddCell(4, ci, $"{sc.AS.ValueLow:n1} ± {sc.AS.DeltaLow:n1}");
  165. ta.AddCell(5, ci, $"{sc.AS.ValueHigh:n1} ± {sc.AS.DeltaHigh:n1}");
  166. ta.AddCell(6, ci, $"{sc.NR.ValueLow:n1} ± {sc.NR.DeltaLow:n1}");
  167. ta.AddCell(7, ci, $"{sc.NR.ValueHigh:n1} ± {sc.NR.DeltaHigh:n1}");
  168. ta.AddCell(8, ci, $"{sc.RA.ValueLow:n1} ± {sc.RA.DeltaLow:n1}");
  169. ta.AddCell(9, ci, $"{sc.RA.ValueHigh:n1} ± {sc.RA.DeltaHigh:n1}");
  170. ta.AddCell(10, ci, $"{sc.AV.ValueLow:n1} ± {sc.AV.DeltaLow:n1}");
  171. ta.AddCell(11, ci, $"{sc.AV.ValueHigh:n1} ± {sc.AV.DeltaHigh:n1}");
  172. }
  173.  
  174. // Add a special background to every other row:
  175. ta.DefaultCellStyle = new CellStyle
  176. {
  177. FillColor = Color.FromArgb(233, 240, 222),
  178. Background = true
  179. };
  180. for (int i = 3; i < 12; i += 2)
  181. {
  182. ta.AddCell(i, 2, 1, scopeCount);
  183. }
  184.  
  185. ta.Render();
  186. }
  187. }
  188. }
  189.