RstForm.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using LiveChartsCore;
  2. using LiveChartsCore.Kernel.Sketches;
  3. using LiveChartsCore.SkiaSharpView;
  4. using LiveChartsCore.SkiaSharpView.Painting;
  5. using LiveChartsCore.SkiaSharpView.VisualElements;
  6. using LiveChartsCore.SkiaSharpView.WinForms;
  7. using SkiaSharp;
  8. using System.Collections.ObjectModel;
  9. using System.Windows.Forms.VisualStyles;
  10. namespace ReatimeSpeedTest.Gui
  11. {
  12. public partial class MainForm : Form
  13. {
  14. public MainForm()
  15. {
  16. InitializeComponent();
  17. LiveCharts.Configure(config =>
  18. config
  19. .AddDarkTheme()
  20. .HasGlobalSKTypeface(SKFontManager.Default.MatchCharacter('ºº'))// <- Chinese
  21. );
  22. }
  23. private CartesianChart lineChart;
  24. private CartesianChart barChart;
  25. private PieChart pieChart;
  26. private NeedleVisual Needle;
  27. public class DateModel
  28. {
  29. public DateTime DateTime { get; set; }
  30. public double Value { get; set; }
  31. }
  32. private void MainForm_Shown(object sender, EventArgs e)
  33. {
  34. var btn = new ToolStripButton("Dbg");
  35. var toolBarBar = new ToolStrip { RenderMode = ToolStripRenderMode.System, BackColor = BackColor, ForeColor = ForeColor };
  36. toolBarBar.Items.Add(btn);
  37. toolBarBar.Items.Add(new ToolStripButton("C") { Alignment = ToolStripItemAlignment.Right, CheckOnClick = true, Checked = true });
  38. toolBarBar.Items.Add(new ToolStripButton("B") { Alignment = ToolStripItemAlignment.Right, CheckOnClick = true, Checked = true });
  39. toolBarBar.Items.Add(new ToolStripButton("A") { Alignment = ToolStripItemAlignment.Right, CheckOnClick = true, Checked = true });
  40. var toolBarLine = new ToolStrip { RenderMode = ToolStripRenderMode.System, BackColor = BackColor, ForeColor = ForeColor };
  41. toolBarLine.Items.Add(new ToolStripButton("C") { Alignment = ToolStripItemAlignment.Right, CheckOnClick = true, Checked = true });
  42. toolBarLine.Items.Add(new ToolStripButton("B") { Alignment = ToolStripItemAlignment.Right, CheckOnClick = true, Checked = true });
  43. toolBarLine.Items.Add(new ToolStripButton("A") { Alignment = ToolStripItemAlignment.Right, CheckOnClick = true, Checked = true });
  44. lineChart = new() { Dock = DockStyle.Fill };
  45. barChart = new() { Dock = DockStyle.Fill };
  46. Needle = new()
  47. {
  48. Value = 23.3,
  49. Fill = new SolidColorPaint(SKColors.Red),
  50. };
  51. pieChart = new()
  52. {
  53. Dock = DockStyle.Fill,
  54. InitialRotation = -225,
  55. MaxAngle = 270,
  56. MinValue = 0,
  57. MaxValue = 100,
  58. // out of livecharts properties...
  59. Location = new Point(0, 0),
  60. Size = new Size(50, 50),
  61. Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom,
  62. VisualElements = [
  63. new AngularTicksVisual
  64. {
  65. Labeler = value => value.ToString("N1"),
  66. LabelsSize = 16,
  67. LabelsOuterOffset = 15,
  68. OuterOffset = 65,
  69. TicksLength = 20
  70. },
  71. Needle
  72. ]
  73. };
  74. var sph = new SplitContainer { Dock = DockStyle.Fill, Orientation = Orientation.Horizontal, BorderStyle = BorderStyle.FixedSingle };
  75. var spv = new SplitContainer { Dock = DockStyle.Fill, Orientation = Orientation.Vertical, BorderStyle = BorderStyle.FixedSingle };
  76. sph.Panel1.Controls.Add(spv);
  77. sph.Panel2.Controls.Add(lineChart);
  78. sph.Panel2.Controls.Add(toolBarLine);
  79. spv.Panel1.Controls.Add(pieChart);
  80. spv.Panel2.Controls.Add(barChart);
  81. spv.Panel2.Controls.Add(toolBarBar);
  82. Controls.Add(sph);
  83. var conValues = new ObservableCollection<double> { 2, 5, 4, 4, 56, 7, 45, 24, 7, 7, 3546, 24, 2345, 236, 3567, 2456 };
  84. var errValues = new ObservableCollection<double> { 3, 1, 6, 4, 56, 7, 45, 24, 7, 7, 3546, 24, 2345, 236, 3567, 2456 };
  85. var barSeries = new ISeries[]
  86. {
  87. new ColumnSeries<double>
  88. {
  89. Name = "Con",
  90. Values = conValues,
  91. XToolTipLabelFormatter = (d)=>$"#{d.Index+1}",
  92. },
  93. new ColumnSeries<double>
  94. {
  95. Name = "Err",
  96. Values = errValues,
  97. }
  98. };
  99. barChart.Series = barSeries;
  100. barChart.XAxes = [new Axis
  101. {
  102. Labeler = (d)=>$"#{d+1}",
  103. }];
  104. var lineDataSource = new ObservableCollection<DateModel>()
  105. {
  106. new() { DateTime = DateTime.Now.AddSeconds(-4), Value = 5 },
  107. new() { DateTime = DateTime.Now.AddSeconds(-3), Value = 9 },
  108. new() { DateTime = DateTime.Now.AddSeconds(-2), Value = 2 },
  109. new() { DateTime = DateTime.Now.AddSeconds(-1), Value = 8 },
  110. new() { DateTime = DateTime.Now, Value = 6 }
  111. };
  112. var lineSeries = new LineSeries<DateModel>
  113. {
  114. Name = "Total",
  115. Values = lineDataSource,
  116. Mapping = (model, i) => new(model.DateTime.Ticks, model.Value),
  117. GeometrySize = 5,
  118. //Fill = new SolidColorPaint(SKColors.Transparent),
  119. //Stroke = new SolidColorPaint(SKColors.LightSkyBlue),
  120. //GeometryFill = new SolidColorPaint(SKColors.LightSkyBlue),
  121. //GeometryStroke = new SolidColorPaint(SKColors.LightSkyBlue),
  122. XToolTipLabelFormatter = point => point.Model.DateTime.ToString("dd HH:mm:ss.fff"),
  123. };
  124. lineChart.Series = [lineSeries];
  125. lineChart.XAxes = [new Axis
  126. {
  127. Name = "Time",
  128. Labeler = value => new DateTime((long)value).ToString("dd HH:mm")
  129. }];
  130. lineChart.YAxes = [new Axis
  131. {
  132. Name = "Speed",
  133. Labeler = value => value.ToString("N2")
  134. }];
  135. btn.Click += delegate
  136. {
  137. Needle.Value = DateTime.Now.Millisecond;
  138. lineSeries.Values.Add(new()
  139. {
  140. DateTime = DateTime.Now,
  141. Value = DateTime.Now.Second
  142. });
  143. conValues[0] = DateTime.Now.Millisecond;
  144. errValues[0] = DateTime.Now.Millisecond;
  145. lineSeries.IsVisible = !lineSeries.IsVisible;
  146. };
  147. }
  148. private void MainForm_Load(object sender, EventArgs e)
  149. {
  150. }
  151. }
  152. }