ChartExamples.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using QuestPDF.Fluent;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Infrastructure;
  4. using ScottPlot;
  5. using Colors = QuestPDF.Helpers.Colors;
  6. using ImageFormat = QuestPDF.Infrastructure.ImageFormat;
  7. namespace QuestPDF.DocumentationExamples;
  8. public class ChartExamples
  9. {
  10. [Test]
  11. public void PieChartExample()
  12. {
  13. Settings.UseEnvironmentFonts = true;
  14. Document
  15. .Create(document =>
  16. {
  17. document.Page(page =>
  18. {
  19. page.MinSize(new PageSize(350, 0));
  20. page.MaxSize(new PageSize(350, 1000));
  21. page.DefaultTextStyle(x => x.FontSize(20));
  22. page.Margin(25);
  23. page.Content()
  24. .Column(column =>
  25. {
  26. column.Spacing(10);
  27. column.Item().Text("US energy consumption [%]\nby source in 2021").AlignCenter().Bold();
  28. column.Item()
  29. .AspectRatio(1)
  30. .Svg(size =>
  31. {
  32. using ScottPlot.Plot plot = new();
  33. var slices = new PieSlice[]
  34. {
  35. new() { Value = 8, FillColor = new ScottPlot.Color(Colors.Yellow.Medium.Hex), Label = "Nuclear" },
  36. new() { Value = 12, FillColor = new ScottPlot.Color(Colors.Green.Medium.Hex), Label = "Renewable" },
  37. new() { Value = 32, FillColor = new ScottPlot.Color(Colors.Blue.Medium.Hex), Label = "Natural gas" },
  38. new() { Value = 11, FillColor = new ScottPlot.Color(Colors.Grey.Medium.Hex), Label = "Coal" },
  39. new() { Value = 36, FillColor = new ScottPlot.Color(Colors.Brown.Medium.Hex), Label = "Petroleum" }
  40. };
  41. var pie = plot.Add.Pie(slices);
  42. pie.DonutFraction = 0.5;
  43. pie.SliceLabelDistance = 1.5;
  44. pie.LineColor = ScottPlot.Colors.White;
  45. pie.LineWidth = 3;
  46. foreach (var pieSlice in pie.Slices)
  47. {
  48. pieSlice.LabelStyle.FontName = "Lato";
  49. pieSlice.LabelStyle.FontSize = 16;
  50. }
  51. plot.Axes.Frameless();
  52. plot.HideGrid();
  53. return plot.GetSvgXml((int)size.Width, (int)size.Height);
  54. });
  55. });
  56. });
  57. })
  58. .GenerateImages(x => "chart-pie.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.Best, RasterDpi = 144 });
  59. }
  60. [Test]
  61. public void BarExample()
  62. {
  63. Settings.UseEnvironmentFonts = true;
  64. Document
  65. .Create(document =>
  66. {
  67. document.Page(page =>
  68. {
  69. page.MinSize(new PageSize(650, 0));
  70. page.MaxSize(new PageSize(650, 1000));
  71. page.DefaultTextStyle(x => x.FontSize(20));
  72. page.Margin(25);
  73. page.Content()
  74. .Column(column =>
  75. {
  76. column.Spacing(10);
  77. column.Item().Text("Popularity of C# versions in 2023").AlignCenter().Bold();
  78. column.Item()
  79. .AspectRatio(2)
  80. .Svg(size =>
  81. {
  82. ScottPlot.Plot plot = new();
  83. var bars = new Bar[]
  84. {
  85. new() { Position = 1, Value = 2 },
  86. new() { Position = 2, Value = 3 },
  87. new() { Position = 3, Value = 8 },
  88. new() { Position = 4, Value = 13 },
  89. new() { Position = 5, Value = 17 },
  90. new() { Position = 6, Value = 17 },
  91. new() { Position = 7, Value = 32 },
  92. new() { Position = 8, Value = 42 }
  93. };
  94. foreach (var bar in bars)
  95. {
  96. bar.FillColor = new ScottPlot.Color(Colors.Grey.Medium.Hex);
  97. bar.LineWidth = 0;
  98. bar.Size = 0.5;
  99. }
  100. plot.Add.Bars(bars);
  101. Tick[] ticks = [
  102. new(1, "Other"),
  103. new(2, "C# 5"),
  104. new(3, "C# 6"),
  105. new(4, "C# 7"),
  106. new(5, "C# 8"),
  107. new(6, "C# 9"),
  108. new(7, "C# 10"),
  109. new(8, "C# 11")
  110. ];
  111. plot.Axes.Bottom.TickGenerator = new ScottPlot.TickGenerators.NumericManual(ticks);
  112. plot.Axes.Bottom.MajorTickStyle.Length = 0;
  113. plot.Axes.Bottom.TickLabelStyle.FontName = "Lato";
  114. plot.Axes.Bottom.TickLabelStyle.FontSize = 16;
  115. plot.Axes.Bottom.TickLabelStyle.OffsetY = 8;
  116. plot.Grid.XAxisStyle.IsVisible = false;
  117. plot.Axes.Margins(bottom: 0, top: 0.25f);
  118. return plot.GetSvgXml((int)size.Width, (int)size.Height);
  119. });
  120. });
  121. });
  122. })
  123. .GenerateImages(x => "chart-bars.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.Best, RasterDpi = 144 });
  124. }
  125. }