-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogBrowser.cs
419 lines (349 loc) · 14.5 KB
/
LogBrowser.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ZedGraph; // Graphs
using System.Xml;
namespace MultiWiiWinGUI
{
public partial class LogBrowser : Form
{
int m_iColumnCount = 0;
DataTable m_dtCSV = new DataTable();
PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
PointPairList list3 = new PointPairList();
PointPairList list4 = new PointPairList();
PointPairList list5 = new PointPairList();
int graphs = 0;
public string sInitialDirectory;
public LogBrowser()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = sInitialDirectory;
openFileDialog1.Filter = "Log Files|*.log";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + "logs";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
Stream stream = File.Open(openFileDialog1.FileName, FileMode.Open);
PopulateDataTableFromUploadedFile(stream);
stream.Close();
dataGridView1.DataSource = m_dtCSV;
}
catch (Exception ex) { MessageBox.Show("Failed to read File: " + ex.ToString()); }
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
CreateChart(zg1);
int a = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//Commands.Rows[a].HeaderCell.Value
row.HeaderCell.Value = a.ToString();
a++;
}
}
else
{
//this.Close();
return;
}
}
private void PopulateDataTableFromUploadedFile(System.IO.Stream strm)
{
System.IO.StreamReader srdr = new System.IO.StreamReader(strm);
String strLine = String.Empty;
Int32 iLineCount = 0;
do
{
strLine = srdr.ReadLine();
if (strLine == null)
{
break;
}
if (0 == iLineCount++)
{
m_dtCSV = new DataTable("CSVTable");
//m_dtCSV = this.CreateDataTableForCSVData(strLine);
}
this.AddDataRowToTable(strLine, m_dtCSV);
} while (true);
}
private DataTable CreateDataTableForCSVData(String strLine)
{
DataTable dt = new DataTable("CSVTable");
String[] strVals = strLine.Split(new char[] { ',' });
m_iColumnCount = strVals.Length;
int idx = 0;
foreach (String strVal in strVals)
{
String strColumnName = String.Format("{0}", idx++);
dt.Columns.Add(strColumnName, Type.GetType("System.String"));
}
return dt;
}
private DataRow AddDataRowToTable(String strCSVLine, DataTable dt)
{
String[] strVals = strCSVLine.Split(new char[] { ','});
Int32 iTotalNumberOfValues = strVals.Length;
// If number of values in this line are more than the columns
// currently in table, then we need to add more columns to table.
if (iTotalNumberOfValues > m_iColumnCount)
{
Int32 iDiff = iTotalNumberOfValues - m_iColumnCount;
for (Int32 i = 0; i < iDiff; i++)
{
String strColumnName = String.Format("{0}", (m_iColumnCount + i));
dt.Columns.Add(strColumnName, Type.GetType("System.String"));
}
m_iColumnCount = iTotalNumberOfValues;
}
int idx = 0;
DataRow drow = dt.NewRow();
foreach (String strVal in strVals)
{
String strColumnName = String.Format("{0}", idx++);
drow[strColumnName] = strVal.Trim();
}
dt.Rows.Add(drow);
return drow;
}
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
int a = 0;
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.HeaderText = a.ToString();
a++;
}
}
catch { }
try
{
string option = dataGridView1[0, e.RowIndex].EditedFormattedValue.ToString();
// using (XmlReader reader = XmlReader.Create(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + "dataflashlog.xml"))
using (XmlReader reader = XmlReader.Create("logfields.xml"))
{
reader.Read();
reader.ReadStartElement("LOGFORMAT");
reader.ReadToFollowing("MULTIWII");
reader.ReadToFollowing(option);
dataGridView1.Columns[0].HeaderText = "";
XmlReader inner = reader.ReadSubtree();
inner.MoveToElement();
int a = 1;
while (inner.Read())
{
inner.MoveToElement();
if (inner.IsStartElement())
{
if (inner.Name.StartsWith("F"))
{
dataGridView1.Columns[a].HeaderText = inner.ReadString();
Console.WriteLine(a + " " + dataGridView1.Columns[a].HeaderText);
a++;
}
}
}
for (; a < dataGridView1.Columns.Count; a++)
{
dataGridView1.Columns[a].HeaderText = "";
}
}
}
catch { Console.WriteLine("DGV logbrowse error"); }
}
public void CreateChart(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
// Set the titles and axis labels
myPane.Title.Text = "";
myPane.XAxis.Title.Text = "Logfile time";
myPane.YAxis.Title.Text = "Logged value";
myPane.XAxis.Type = AxisType.Date;
myPane.XAxis.Scale.Format = "HH:mm:ss.fff";
LineItem myCurve;
myCurve = myPane.AddCurve("Value", list1, Color.Red, SymbolType.None);
myCurve = myPane.AddCurve("Value", list2, Color.LightGreen, SymbolType.None);
myCurve = myPane.AddCurve("Value", list3, Color.LightBlue, SymbolType.None);
myCurve = myPane.AddCurve("Value", list4, Color.Pink, SymbolType.None);
myCurve = myPane.AddCurve("Value", list5, Color.Yellow, SymbolType.None);
// Show the x axis grid
myPane.XAxis.MajorGrid.IsVisible = true;
//myPane.XAxis.Scale.Min = 0;
//myPane.XAxis.Scale.Max = -1;
// Make the Y axis scale red
myPane.YAxis.Scale.FontSpec.FontColor = Color.White;
myPane.YAxis.Title.FontSpec.FontColor = Color.White;
// turn off the opposite tics so the Y tics don't show up on the Y2 axis
myPane.YAxis.MajorTic.IsOpposite = false;
myPane.YAxis.MinorTic.IsOpposite = false;
// Don't display the Y zero line
myPane.YAxis.MajorGrid.IsZeroLine = true;
// Align the Y axis labels so they are flush to the axis
myPane.YAxis.Scale.Align = AlignP.Inside;
// Manually set the axis range
//myPane.YAxis.Scale.Min = -1;
//myPane.YAxis.Scale.Max = 1;
// Fill the axis background with a gradient
myPane.Chart.Fill = new Fill(Color.DimGray, Color.DarkGray, 45.0f);
myPane.Fill = new Fill(Color.DimGray, Color.DimGray, 45.0f);
myPane.Legend.Fill = new Fill(Color.DimGray, Color.DimGray, 0);
myPane.Legend.FontSpec.FontColor = Color.White;
foreach (ZedGraph.LineItem li in myPane.CurveList)
{
li.Line.Width = 2;
}
myPane.XAxis.MajorTic.Color = Color.White;
myPane.XAxis.MinorTic.Color = Color.White;
myPane.YAxis.MajorTic.Color = Color.White;
myPane.YAxis.MinorTic.Color = Color.White;
myPane.XAxis.MajorGrid.Color = Color.White;
myPane.YAxis.MajorGrid.Color = Color.White;
myPane.YAxis.Scale.FontSpec.FontColor = Color.White;
myPane.YAxis.Title.FontSpec.FontColor = Color.White;
myPane.XAxis.Scale.FontSpec.FontColor = Color.White;
myPane.XAxis.Title.FontSpec.FontColor = Color.White;
myPane.Legend.Fill = new ZedGraph.Fill(Color.FromArgb(0x85, 0x84, 0x83));
myPane.Legend.Position = LegendPos.TopCenter;
// Calculate the Axis Scale Ranges
try
{
zg1.AxisChange();
}
catch { }
}
private void Graphit_Click(object sender, EventArgs e)
{
double prvTimestamp = 0;
if (dataGridView1.RowCount == 0 || dataGridView1.ColumnCount == 0)
{
MessageBox.Show("Please load a valid file");
return;
}
GraphPane myPane = zg1.GraphPane;
int col = dataGridView1.CurrentCell.ColumnIndex;
int row = dataGridView1.CurrentCell.RowIndex;
string type = dataGridView1[0, row].Value.ToString();
double a = 0; // row counter
if (col == 0)
{
MessageBox.Show("Please pick another column, Highlight the cell you wish to graph");
return;
}
int error = 0;
foreach (DataGridViewRow datarow in dataGridView1.Rows)
{
//get the date and convert it to a double
DateTime dt;
try
{
dt = Convert.ToDateTime(datarow.Cells[1].Value.ToString());
}
catch
{
dt = Convert.ToDateTime("00:00:00.000");
}
double timestamp = dt.ToOADate();
if (datarow.Cells[0].Value.ToString() == type)
{
prvTimestamp = timestamp;
try
{
double value = double.Parse(datarow.Cells[col].Value.ToString(), new System.Globalization.CultureInfo("en-US"));
if (graphs == 0)
{
zg1.GraphPane.CurveList[0].Label.Text = dataGridView1.Columns[col].HeaderText;
list1.Add(timestamp, value);
}
else if (graphs == 1)
{
zg1.GraphPane.CurveList[1].Label.Text = dataGridView1.Columns[col].HeaderText;
list2.Add(timestamp, value);
}
else if (graphs == 2)
{
zg1.GraphPane.CurveList[2].Label.Text = dataGridView1.Columns[col].HeaderText;
list3.Add(timestamp, value);
}
else if (graphs == 3)
{
zg1.GraphPane.CurveList[3].Label.Text = dataGridView1.Columns[col].HeaderText;
list4.Add(timestamp, value);
}
else if (graphs == 4)
{
zg1.GraphPane.CurveList[4].Label.Text = dataGridView1.Columns[col].HeaderText;
list5.Add(timestamp, value);
}
else
{
MessageBox.Show("Max of 5");
break;
}
}
catch { error++; Console.WriteLine("Bad Data : " + type + " " + col + " " + a); if (error >= 500) { MessageBox.Show("There is to much bad data - failing"); break; } }
}
/*
if (datarow.Cells[0].Value.ToString() == "GMOD")
{
int i = myPane.AddYAxis("");
myPane.YAxisList[i].Color = Color.Orange;
myPane.YAxisList[i].Scale.IsVisible = true;
myPane.YAxisList[i].MajorTic.IsAllTics = true;
myPane.YAxisList[i].MinorTic.IsAllTics = true;
myPane.YAxisList[i].Cross = prvTimestamp;
myPane.YAxisList[i].IsVisible = true;
myPane.YAxisList[i].Scale.Align = AlignP.Inside;
}
*/
a++;
}
// Make sure the Y axis is rescaled to accommodate actual data
try
{
zg1.AxisChange();
}
catch { }
// Zoom all
zg1.ZoomOutAll(zg1.GraphPane);
// Force a redraw
zg1.Invalidate();
graphs++;
}
private void BUT_cleargraph_Click(object sender, EventArgs e)
{
graphs = 0;
foreach (LineItem line in zg1.GraphPane.CurveList)
{
line.Clear();
line.Label.Text = "Value";
}
zg1.Invalidate();
}
private void BUT_loadlog_Click(object sender, EventArgs e)
{
// reset column count
m_iColumnCount = 0;
// clear existing lists
zg1.GraphPane.CurveList.Clear();
// reload
Form1_Load(sender, e);
}
}
}