RedNegativesTable.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 System.Collections.Generic;
  10. using GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13. using GrapeCity.Documents.Layout;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsImagingWeb.Demos
  18. {
  19. // This example shows how to draw a simple table containing
  20. // some numeric data, with negative values displayed in red,
  21. // and with differently shaded odd and even rows,
  22. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  23. public class RedNegativesTable
  24. {
  25. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  26. {
  27. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  28. using var g = bmp.CreateGraphics(Color.White);
  29. DrawTable(g, pixelSize.Width, pixelSize.Height);
  30. return bmp;
  31. }
  32.  
  33. class ItemLine
  34. {
  35. public ItemLine(string text, double sales, double marginNumber, double marginPercent, int totalCustomers)
  36. {
  37. Text = text;
  38. Sales = sales;
  39. MarginNumber = marginNumber;
  40. MarginPercent = marginPercent;
  41. TotalCustomers = totalCustomers;
  42. }
  43. public string Text { get; }
  44. public double Sales { get; }
  45. public double MarginNumber { get; }
  46. public double MarginPercent { get; }
  47. public int TotalCustomers { get; }
  48. }
  49.  
  50. static void DrawTable(GcGraphics g, int pageWidth, int pageHeight)
  51. {
  52. var items = new ItemLine[]
  53. {
  54. new ItemLine("Washers & Dryers", 1580, 196.82, 12.46, 247),
  55. new ItemLine("Projectors & Screens", 973, 211.26, 21.72, 382),
  56. new ItemLine("Refrigerators", 953, -350.57, -36.77, 331),
  57. new ItemLine("Laptops", 668, 181.17, 27.11, 301),
  58. new ItemLine("Coffee Machines", 476, -11.15, -2.34, 251),
  59. new ItemLine("Lamps", 471, 340.31, 72.28, 548),
  60. new ItemLine("Water Heaters", 443, 94.59, 21.34, 152),
  61. new ItemLine("Monitors", 424, -0.77, -0.18, 327),
  62. new ItemLine("Microwaves", 319, 14.30, 4.48, 375),
  63. new ItemLine("Desktops", 266, 23.22, 8.72, 184),
  64. new ItemLine("Air Conditioners", 253, 137.82, 54.54, 216),
  65. new ItemLine("Printers & Scanners", 219, 0.59, 0.27, 379),
  66. new ItemLine("Computers Accessories", 128, -18.23, -14.27, 654),
  67. new ItemLine("Fans", 83, -38.64, -46.35, 287)
  68. };
  69.  
  70. var host = new LayoutHost();
  71. var view = host.CreateView(pageWidth, pageHeight);
  72.  
  73. var rt = view.CreateRect();
  74. rt.AnchorTopLeft(null, 72, 72);
  75.  
  76. var ta = new TableRenderer(g,
  77. rt, FixedTableSides.TopLeft,
  78. rowCount: items.Length + 1,
  79. columnCount: 5,
  80. gridLineColor: Color.Transparent,
  81. gridLineWidth: 0);
  82.  
  83. var columns = ta.ColumnRects;
  84. columns[0].SetWidth(270);
  85. columns[1].SetWidth(150);
  86. columns[2].SetWidth(150);
  87. columns[3].SetWidth(150);
  88. columns[4].SetWidth(150);
  89.  
  90. var fmt = new TextFormat
  91. {
  92. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "consola.ttf")),
  93. ForeColor = Color.FromArgb(38, 38, 38),
  94. FontSize = 20,
  95. FontSizeInGraphicUnits = true,
  96. };
  97. var fmtSmall = new TextFormat(fmt)
  98. {
  99. FontSize = 14
  100. };
  101.  
  102. var csCaption = new CellStyle
  103. {
  104. TextAlignment = TextAlignment.Trailing,
  105. ParagraphAlignment = ParagraphAlignment.Center,
  106. TextFormat = fmt,
  107. PaddingBottom = 10,
  108. PaddingRight = 4
  109. };
  110. ta.AddCell(new CellStyle(csCaption)
  111. {
  112. CustomDraw = (g, tc) =>
  113. {
  114. float y = tc.Height * 0.5f;
  115. float x = 10;
  116. var pen = new GCDRAW.Pen(Color.DimGray, 1);
  117. g.DrawLine(new PointF(x, y - 7), new PointF(x, y + 7), pen);
  118. g.DrawLine(new PointF(x - 3, y + 3), new PointF(x, y + 7), pen);
  119. g.DrawLine(new PointF(x + 3, y + 3), new PointF(x, y + 7), pen);
  120. },
  121. CreateTextLayout = (g, cs, data) =>
  122. {
  123. var tl = g.CreateTextLayout();
  124. tl.AppendLine("Sales", fmt);
  125. tl.Append("in Thousands", fmtSmall);
  126. return tl;
  127. }
  128. }, 0, 1, null);
  129. ta.AddCell(new CellStyle(csCaption)
  130. {
  131. CreateTextLayout = (g, cs, data) =>
  132. {
  133. var tl = g.CreateTextLayout();
  134. tl.AppendLine("Margin", fmt);
  135. tl.Append("in Thousands", fmtSmall);
  136. return tl;
  137. }
  138. }, 0, 2, null);
  139. ta.AddCell(csCaption, 0, 3, "Margin %");
  140. ta.AddCell(csCaption, 0, 4, "Total\nCustomers");
  141.  
  142. var csName = new CellStyle
  143. {
  144. TextFormat = fmt,
  145. PaddingTopBottom = 5,
  146. PaddingLeft = 4
  147. };
  148. var csNum = new CellStyle
  149. {
  150. TextFormat = fmt,
  151. TextAlignment = TextAlignment.Trailing,
  152. PaddingTopBottom = 5,
  153. PaddingRight = 4
  154. };
  155. var csNumNegative = new CellStyle(csNum)
  156. {
  157. TextFormat = new TextFormat(fmt)
  158. {
  159. ForeColor = Color.Red
  160. }
  161. };
  162. var csBand = new CellStyle
  163. {
  164. FillColor = Color.FromArgb(244, 241, 250),
  165. Background = true
  166. };
  167.  
  168. var ci = new System.Globalization.CultureInfo("en-US");
  169.  
  170. for (int i = 0; i < items.Length; i++)
  171. {
  172. var item = items[i];
  173. int rowIndex = i + 1;
  174.  
  175. if ((i & 1) != 0)
  176. {
  177. ta.AddCell(csBand, rowIndex, 0, 1, 5);
  178. }
  179. ta.AddCell(csName, rowIndex, 0, item.Text);
  180. ta.AddCell(csNum, rowIndex, 1, item.Sales.ToString("c0", ci));
  181. var v = item.MarginNumber;
  182. ta.AddCell(v >= 0.0 ? csNum : csNumNegative, rowIndex, 2, v.ToString("n2"));
  183. v = item.MarginPercent;
  184. ta.AddCell(v >= 0.0 ? csNum : csNumNegative, rowIndex, 3, v.ToString("n2"));
  185. ta.AddCell(csNum, rowIndex, 4, item.TotalCustomers.ToString("n0"));
  186. }
  187.  
  188. ta.SetVerticalGridLineWidth(1, 2);
  189. ta.SetHorizontalGridLineWidth(1, 2);
  190.  
  191. var pen = new GCDRAW.Pen(Color.FromArgb(159, 146, 213), 2)
  192. {
  193. DashStyle = DashStyle.Solid,
  194. LineCap = PenLineCap.Round
  195. };
  196. ta.AddCell(new CellStyle
  197. {
  198. Background = true,
  199. Borders = FrameBorders.LeftBorder | FrameBorders.TopBorder,
  200. Pen = pen,
  201. LinePaddingLeft = -2,
  202. LinePaddingTop = -2,
  203. }, 1, 1, items.Length, 4);
  204.  
  205. ta.Render();
  206. }
  207. }
  208. }
  209.