-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlotWindow.cs
More file actions
95 lines (86 loc) · 2.73 KB
/
Copy pathPlotWindow.cs
File metadata and controls
95 lines (86 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FFT
{
/// <summary>
/// Window class for displaying charts and signals
/// </summary>
public class PlotWindow
{
private Task _task;
public Form form { get; set; }
public int[] SignalBuffer { get; set; }
public bool DrawLine { get; set; }
public bool Invert { get; set; }
public PlotWindow(string name, int[] buffer, bool invert = true, bool drawLine = false)
{
DrawLine = drawLine;
Invert = invert;
SignalBuffer = buffer;
MakeWindow(name);
}
public PlotWindow(string name)
{
MakeWindow(name);
}
private void MakeWindow(string name)
{
Action<object> action = (object obj) =>
{
form = new Form();
form.Name = obj.ToString();
form.Text = obj.ToString();
form.Left = 50;
form.Top = 50;
form.Width = 400;
form.Height = 400;
form.Paint += OnPaint;
form.Resize += OnResize;
form.Show();
Application.Run(form);
};
_task = Task.Factory.StartNew(action, name);
}
private void OnResize(object sender, EventArgs e)
{
(sender as Form)?.Refresh();
}
private void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
int y0 = 0;
int y1 = 0;
if (SignalBuffer != null)
{
int deltaHeight = e.ClipRectangle.Height / 2;
e.Graphics.DrawLine(Pens.Red,
0, deltaHeight,
e.ClipRectangle.Width, deltaHeight);
for (var i = 0; i < SignalBuffer.Length - 1; i++)
{
if (DrawLine && (i % 10) == 0)
{
e.Graphics.DrawLine(Pens.Red,
i, deltaHeight,
i, deltaHeight+5);
}
y0 = deltaHeight + SignalBuffer[i];
y1 = deltaHeight + SignalBuffer[i + 1];
if (Invert)
{
y0 = e.ClipRectangle.Height - y0;
y1 = e.ClipRectangle.Height - y1;
}
e.Graphics.DrawLine(Pens.Black,
i, y0,
i + 1, y1);
}
}
}
}
}