PlotHepler.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using ScottPlot;
  2. using ScottPlot.Plottables;
  3. using ScottPlot.WPF;
  4. using System.Windows.Media;
  5. namespace ProximaAnalizer.Helpers;
  6. internal class PlotHepler(WpfPlot PlotControl)
  7. {
  8. public void InitPlot()
  9. {
  10. PlotControl.Plot.Grid.XAxisStyle.MajorLineStyle.Width = 1f;
  11. PlotControl.Plot.Grid.YAxisStyle.MajorLineStyle.Width = 1f;
  12. PlotControl.Plot.Axes.Bottom.TickLabelStyle.Alignment = Alignment.MiddleLeft;
  13. PlotControl.Plot.Axes.DateTimeTicksBottom();
  14. PlotControl.Background = (Brush)App.Current.Resources.FindName("BackgroundColor");
  15. PlotControl.Plot.RenderManager.RenderStarting += (s, e) =>
  16. {
  17. Tick[] ticks = PlotControl.Plot.Axes.Bottom.TickGenerator.Ticks;
  18. for (int i = 0; i < ticks.Length; i++)
  19. {
  20. DateTime dt = DateTime.FromOADate(ticks[i].Position);
  21. //string label = $"{dt:MM-dd HH:mm:ss}";
  22. string label = $"{dt:HH:mm:ss}";
  23. ticks[i] = new Tick(ticks[i].Position, label);
  24. }
  25. };
  26. PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
  27. PlotControl.Plot.Axes.Bottom.TickLabelStyle.Rotation = 90;
  28. PlotControl.Plot.Axes.Bottom.TickLabelStyle.Alignment = Alignment.MiddleLeft;
  29. PixelPadding padding = new(48, 48, 56, 40);
  30. PlotControl.Plot.Layout.Fixed(padding);
  31. PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
  32. PlotControl.Plot.Grid.MajorLineColor = ScottPlot.Colors.LightGray.WithOpacity(1);
  33. PlotControl.Plot.Grid.MajorLineWidth = 2;
  34. PlotControl.Plot.Grid.MinorLineColor = ScottPlot.Colors.Black.WithOpacity(.2);
  35. PlotControl.Plot.Grid.MinorLineWidth = 0f;
  36. }
  37. public void AddLeftLine(IEnumerable<DateTime> time, List<float> value, LinePattern linePattern, MarkerStyle markerStyle, float lineWidth, string color)
  38. {
  39. Scatter mainScatter = PlotControl.Plot.Add.Scatter(time.ToList(), value);
  40. mainScatter.MarkerStyle = markerStyle;
  41. mainScatter.LineWidth = lineWidth;
  42. mainScatter.LinePattern = linePattern;
  43. mainScatter.Color = new ScottPlot.Color(color);
  44. mainScatter.Axes.YAxis = PlotControl.Plot.Axes.Left;
  45. }
  46. public void AddRightLine(IEnumerable<DateTime> time, List<float> value, LinePattern linePattern, MarkerStyle markerStyle, float lineWidth, string color)
  47. {
  48. Scatter mainScatter = PlotControl.Plot.Add.Scatter(time.ToList(), value);
  49. mainScatter.MarkerStyle = markerStyle;
  50. mainScatter.LineWidth = lineWidth;
  51. mainScatter.LinePattern = linePattern;
  52. mainScatter.Color = new ScottPlot.Color(color);
  53. mainScatter.Axes.YAxis = PlotControl.Plot.Axes.Right;
  54. }
  55. public bool AddAlarmLine(DateTime dateTime, string text)
  56. {
  57. var line = PlotControl.Plot.Add.VerticalLine(dateTime.ToOADate());
  58. line.Color = new ScottPlot.Color("DC143C");
  59. line.Text = text;
  60. line.LineWidth = 1f;
  61. line.LabelOppositeAxis = true;
  62. return true;
  63. }
  64. public bool AddWarningLine(DateTime dateTime, string text)
  65. {
  66. var line = PlotControl.Plot.Add.VerticalLine(dateTime.ToOADate());
  67. line.Color = new ScottPlot.Color("FFA500");
  68. line.LineWidth = 1f;
  69. line.Text = text;
  70. line.LabelOppositeAxis = true;
  71. return true;
  72. }
  73. }