TimeChartTable.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 representing a time chart,
  19. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  20. public class TimeChartTable
  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. DrawTable(g, pixelSize.Width, pixelSize.Height);
  27. return bmp;
  28. }
  29.  
  30. class Range
  31. {
  32. public Range(double valueLow, double deltaLow, double valueHigh, double deltaHigh)
  33. {
  34. ValueLow = valueLow;
  35. DeltaLow = deltaLow;
  36. ValueHigh = valueHigh;
  37. DeltaHigh = deltaHigh;
  38. }
  39. public double ValueLow { get; }
  40. public double DeltaLow { get; }
  41. public double ValueHigh { get; }
  42. public double DeltaHigh { get; }
  43. }
  44.  
  45. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  46. {
  47. var host = new LayoutHost();
  48. var view = host.CreateView(pageWidth, pageHeight * 0.7f);
  49.  
  50. var rt = view.CreateRect();
  51.  
  52. // Pad the table rectangle equally from left, right, top and bottom:
  53. rt.AnchorDeflate(null, 10);
  54.  
  55. // All sides of the table are fixed, so we can apply
  56. // star widths to columns and star heights to rows:
  57. var ta = new TableRenderer(g,
  58. rt, FixedTableSides.All,
  59. rowCount: 10, columnCount: 32,
  60. gridLineColor: Color.DimGray,
  61. gridLineWidth: 1);
  62.  
  63. var columns = ta.ColumnRects;
  64. columns[0].SetWidth(120);
  65. for (int i = 1; i < 32; i++)
  66. {
  67. columns[i].SetStarWidth(1f);
  68. }
  69.  
  70. // Table header is a part of the table;
  71. // the first row (rows[0]) is for the table header:
  72. var rows = ta.RowRects;
  73. rows[0].SetHeight(40);
  74. rows[1].SetHeight(30);
  75. for (int i = 2; i < 10; i++)
  76. {
  77. rows[i].SetStarHeight(1f);
  78. }
  79.  
  80. var fmt = new TextFormat
  81. {
  82. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
  83. FontSize = 16,
  84. FontSizeInGraphicUnits = true
  85. };
  86. var cs = new CellStyle
  87. {
  88. TextFormat = fmt,
  89. TextAlignment = TextAlignment.Center
  90. };
  91.  
  92. var csTitle = new CellStyle(cs)
  93. {
  94. Background = true,
  95. TextFormat = new TextFormat(fmt)
  96. {
  97. FontSize = 16,
  98. FontBold = true
  99. }
  100. };
  101. ta.AddCell(csTitle, 0, 0, 1, 32, "Small Business Marketing Plan");
  102.  
  103. ta.DefaultCellStyle = new CellStyle(cs)
  104. {
  105. ParagraphAlignment = ParagraphAlignment.Center
  106. };
  107. for (int i = 1; i < 32; i++)
  108. {
  109. ta.AddCell(1, i, i.ToString());
  110. }
  111.  
  112. ta.DefaultCellStyle = new CellStyle(cs)
  113. {
  114. TextAlignment = TextAlignment.Leading,
  115. PaddingLeft = 3
  116. };
  117. ta.AddCell(2, 0, "Business Overview");
  118. ta.AddCell(3, 0, "Market Analysis");
  119. ta.AddCell(4, 0, "Marketing Strategy");
  120. ta.AddCell(5, 0, "Operations Plan");
  121. ta.AddCell(6, 0, "Organization");
  122. ta.AddCell(7, 0, "Management");
  123. ta.AddCell(8, 0, "Legal aspects");
  124. ta.AddCell(9, 0, "Financial Plan");
  125.  
  126. // Add merged background cells for color highlighting:
  127. AddBand(2, 1, 6, Color.FromArgb(247, 202, 171));
  128. AddBand(3, 2, 7, Color.FromArgb(179, 198, 231));
  129. AddBand(4, 5, 6, Color.FromArgb(255, 229, 154));
  130. AddBand(5, 11, 6, Color.FromArgb(45, 116, 182));
  131. AddBand(6, 17, 6, Color.FromArgb(255, 155, 155));
  132. AddBand(7, 18, 6, Color.FromArgb(197, 224, 179));
  133. AddBand(8, 24, 4, Color.FromArgb(3, 174, 80));
  134. AddBand(9, 28, 4, Color.FromArgb(1, 176, 241));
  135.  
  136. void AddBand(int rowIndex, int columnIndex, int columnCount, Color color)
  137. {
  138. // The band has the background cell style. So, it is displayed behind the table grid.
  139. ta.AddCell(new CellStyle
  140. {
  141. Background = true,
  142. FillColor = color
  143. }, rowIndex, columnIndex, 1, columnCount);
  144. }
  145.  
  146. // We exclude the first row when we add missing cells
  147. // to avoid drawing grid lines for the table header:
  148. ta.AddMissingCells(1, 0, 9, 32);
  149.  
  150. ta.Render();
  151. }
  152. }
  153. }
  154.