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