ChartExamples.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using QuestPDF.Examples.Engine;
  5. using QuestPDF.Fluent;
  6. using QuestPDF.Infrastructure;
  7. using Microcharts;
  8. using ScottPlot;
  9. using SkiaSharp;
  10. using Colors = QuestPDF.Helpers.Colors;
  11. using Orientation = Microcharts.Orientation;
  12. namespace QuestPDF.Examples
  13. {
  14. public class ChartExample
  15. {
  16. [Test]
  17. public void MicrochartChart()
  18. {
  19. var entries = new[]
  20. {
  21. new ChartEntry(212)
  22. {
  23. Label = "UWP",
  24. ValueLabel = "112",
  25. Color = SKColor.Parse("#2c3e50")
  26. },
  27. new ChartEntry(248)
  28. {
  29. Label = "Android",
  30. ValueLabel = "648",
  31. Color = SKColor.Parse("#77d065")
  32. },
  33. new ChartEntry(128)
  34. {
  35. Label = "iOS",
  36. ValueLabel = "428",
  37. Color = SKColor.Parse("#b455b6")
  38. },
  39. new ChartEntry(514)
  40. {
  41. Label = "Forms",
  42. ValueLabel = "214",
  43. Color = SKColor.Parse("#3498db")
  44. }
  45. };
  46. RenderingTest
  47. .Create()
  48. .PageSize(400, 600)
  49. .ProduceImages()
  50. .ShowResults()
  51. .Render(container =>
  52. {
  53. container
  54. .Background(Colors.White)
  55. .Padding(25)
  56. .Column(column =>
  57. {
  58. column
  59. .Item()
  60. .PaddingBottom(10)
  61. .Text("Chart example")
  62. .FontSize(20)
  63. .SemiBold()
  64. .FontColor(Colors.Blue.Medium);
  65. column
  66. .Item()
  67. .Border(1)
  68. .ExtendHorizontal()
  69. .Height(300)
  70. .SkiaSharpCanvas((canvas, size) =>
  71. {
  72. var chart = new BarChart
  73. {
  74. Entries = entries,
  75. LabelOrientation = Orientation.Horizontal,
  76. ValueLabelOrientation = Orientation.Horizontal,
  77. IsAnimated = false
  78. };
  79. chart.DrawContent(canvas, (int)size.Width, (int)size.Height);
  80. });
  81. });
  82. });
  83. }
  84. [Test]
  85. public void ScottPlotChart()
  86. {
  87. RenderingTest
  88. .Create()
  89. .PageSize(400, 300)
  90. .ProduceImages()
  91. .ShowResults()
  92. .Render(container =>
  93. {
  94. container
  95. .Background(Colors.White)
  96. .Padding(25)
  97. .SkiaSharpCanvas((canvas, availableSpace) =>
  98. {
  99. var points = Enumerable
  100. .Range(0, 100)
  101. .Select(x => new Coordinates(x, Math.Sin(x / 10f)))
  102. .ToArray();
  103. using var plot = new Plot();
  104. plot.Add.Scatter(points, ScottPlot.Color.FromHex(Colors.Teal.Medium));
  105. canvas.ClipRect(new SKRect(0, 0, availableSpace.Width, availableSpace.Height));
  106. plot.Render(canvas, (int)availableSpace.Width, (int)availableSpace.Height);
  107. });
  108. });
  109. }
  110. }
  111. }