-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMyPlotModel.cs
More file actions
534 lines (445 loc) · 16.7 KB
/
MyPlotModel.cs
File metadata and controls
534 lines (445 loc) · 16.7 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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
using OxyPlot;
using OxyPlot.Annotations;
using OxyPlot.Axes;
using OxyPlot.Series;
using OxyPlot.Legends;
using System.Collections.Generic;
namespace FFBitrateViewer
{
/*
public class CustomDataPoint : IDataPointProvider
{
public double X { get; set; }
public double Y { get; set; }
public string FrameType { get; set; }
public DataPoint GetDataPoint() => new(X, Y);
public CustomDataPoint(double x, double y)
{
X = x;
Y = y;
FrameType = "X";
}
}
*/
public class SerieStyle
{
public OxyColor Color = OxyColors.Black;
public LineStyle LineStyle = LineStyle.Solid;
public MarkerType MarkerType = MarkerType.None;
public SerieStyle() { }
public SerieStyle(OxyColor color, LineStyle? style = null)
{
Color = color;
if(style != null) LineStyle = (LineStyle)style;
}
public SerieStyle(string color, string? style = null)
{
Color = OxyColor.Parse(color);
if(style != null) LineStyleSet(style);
}
public string ColorToString()
{
return Color.ToString();
}
public void ColorSet(string color)
{
Color = OxyColor.Parse(color);
}
public string LineStyleToString()
{
return LineStyle.ToString();
}
public void LineStyleSet(string style)
{
switch (style.ToUpper())
{
case "DASH":
LineStyle = LineStyle.Dash;
break;
case "DOT":
LineStyle = LineStyle.Dot;
break;
case "SOLID":
LineStyle = LineStyle.Solid;
break;
default:
LineStyle = LineStyle.Solid; // todo@ exception
break;
}
}
}
public class SeriesParams
{
public SerieStyle SerieStyle = new();
public string Color {
get { return SerieStyle.ColorToString(); }
set { SerieStyle.ColorSet(value); }
}
public string Style {
get { return SerieStyle.LineStyleToString(); }
set { SerieStyle.LineStyleSet(value); }
}
}
public class PlotParams
{
public List<SeriesParams> Series { get; set; } = new List<SeriesParams>();
}
public class MyPlotModel : PlotModel
{
/* Named colors to consider:
000000 Black
0000FF Blue
8A2BE2 BlueViolet
A52A2A Brown
7FFF00 Charteuse
D2691E Chocolate
DC143C Crimson
00FFFF Aqua/Cyan
00008B DarkBlue
008B8B DarkCyan
B8860B DarkGoldenrod
A9A9A9 DarkGray
006400 DarkGreen
8B008B DarkMagenta
FF8C00 DarkOrange
9932CC DarkOrchid
8B0000 DarkRed
00CED1 DarkTurquoise -
9400D3 DarkViolet
FF1493 DeepPink
00BFFF DeepSkyBlue
696969 DimGray
1E90FF DodgerBlue
B22222 Firebrick
228B22 ForestGreen
FF00FF Magenta/Fuchsia
FFD700 Gold
DAA520 Goldenrod
808080 Gray
008000 Green
4B0082 Indigo
7CFC00 LawnGreen
D3D3D3 LightGray
20B2AA LightSeaGreen
00FF00 Lime
32CD32 LimeGreen
0000CD MediumBlue
C71585 MediumVioletRed
000080 Navy
808000 Olive
FFA500 Orange
FF4500 OrangeRed
CD853F Peru
800080 Purple
FF0000 Red
4169E1 RoyalBlue
8B4513 SaddleBrown
2E8B57 SeaGreen
A0522D Sienna
00FF7F SpringGreen
D2B48C Tan
008080 Teal
40E0D0 Turquoise
EE82EE Violet
FFFF00 Yellow
9ACD32 YellowGreen
*/
public static List<SerieStyle> PlotStyles = new() {
new SerieStyle(OxyColors.ForestGreen),
new SerieStyle(OxyColors.Red),
new SerieStyle(OxyColors.Blue),
new SerieStyle(OxyColors.Orange),
new SerieStyle(OxyColors.Magenta),
new SerieStyle(OxyColors.DarkTurquoise),
new SerieStyle(OxyColors.Chocolate),
new SerieStyle(OxyColors.DarkViolet),
new SerieStyle(OxyColors.DodgerBlue),
new SerieStyle(OxyColors.Lime),
new SerieStyle(OxyColors.DarkGray),
new SerieStyle(OxyColors.Black),
/*
new SerieStyle(OxyColors.ForestGreen, LineStyle.Dot),
new SerieStyle(OxyColors.Red, LineStyle.Dot),
new SerieStyle(OxyColors.Blue, LineStyle.Dot),
new SerieStyle(OxyColors.Orange, LineStyle.Dot),
new SerieStyle(OxyColors.Magenta, LineStyle.Dot),
new SerieStyle(OxyColors.DarkTurquoise, LineStyle.Dot),
new SerieStyle(OxyColors.Chocolate, LineStyle.Dot),
new SerieStyle(OxyColors.DarkViolet, LineStyle.Dot),
new SerieStyle(OxyColors.DodgerBlue, LineStyle.Dot),
new SerieStyle(OxyColors.Lime, LineStyle.Dot),
new SerieStyle(OxyColors.DarkGray, LineStyle.Dot),
new SerieStyle(OxyColors.Black, LineStyle.Dot)
*/
};
public string Name { get; set; }
public PlotController Controller { get; private set; }
public string PlotViewType { get; set; }
public MyPlotModel(string name) : base()
{
Name = name;
PlotViewType = "frame";
PlotMargins = new OxyThickness(45, 10, 8, 35);
// Setting white background as with transparent (default) image cannot be copied to Clipboard bacause of WPF bug: https://github.com/oxyplot/oxyplot/issues/17
Background = OxyColors.White;
Legends.Add(new Legend
{
LegendPosition = LegendPosition.BottomRight,
LegendPlacement = LegendPlacement.Outside,
LegendOrientation = LegendOrientation.Horizontal,
LegendMargin = 0,
LegendPadding = 0,
LegendBackground = OxyColor.FromAColor(200, OxyColors.White),
LegendBorder = OxyColors.Black,
LegendBorderThickness = 0,
ShowInvisibleSeries = false,
//LegendFont = "Arial" // todo@ Chineese characters not rendered correctly
});
// 0 -- X (time)
Axes.Add(new TimeSpanAxis
{
AbsoluteMaximum = 1, // Will be adjusted automatically while getting data
AbsoluteMinimum = 0,
Maximum = 1, // Will be adjusted automatically while getting data
Minimum = 0,
MaximumPadding = 0,
MinimumPadding = 0,
MinimumRange = 0.5,
MajorGridlineColor = OxyColor.FromArgb(40, 0, 0, 139),
MajorGridlineStyle = LineStyle.Solid,
MinorGridlineColor = OxyColor.FromArgb(20, 0, 0, 139),
MinorGridlineStyle = LineStyle.Dot,
Position = AxisPosition.Bottom,
StringFormat = "h:mm:ss"
});
// 1 -- Y (size/bitrate)
Axes.Add(new LinearAxis
{
AbsoluteMaximum = 1, // Will be adjusted automatically while getting data
AbsoluteMinimum = 0,
Angle = -90,
Maximum = 1, // Will be adjusted automatically while getting data
Minimum = 0,
MaximumPadding = 0,
MinimumPadding = 0,
MaximumDataMargin = 5, // Distance above max Y-value to plot's edge
MinimumRange = 1,
MajorGridlineColor = OxyColor.FromArgb(40, 0, 0, 139),
MajorGridlineStyle = LineStyle.Solid,
MinorGridlineColor = OxyColor.FromArgb(20, 0, 0, 139),
MinorGridlineStyle = LineStyle.Dot,
Position = AxisPosition.Left
});
// Customizing controller to show Graph ToolTip on mouse hover instead of mouse click
// https://stackoverflow.com/a/34899746/4655944
var controller = new PlotController();
controller.UnbindMouseDown(OxyMouseButton.Left);
controller.BindMouseEnter(PlotCommands.HoverSnapTrack);
Controller = controller;
}
public void Redraw()
{
InvalidatePlot(true);
}
public void LineAnnotationAdd(double maxX, int y, string text, OxyColor color)
{
var annotation = new LineAnnotation();
annotation.Type = LineAnnotationType.Horizontal;
annotation.TextVerticalAlignment = VerticalAlignment.Bottom;
annotation.TextLinePosition = 0.04;
annotation.Y = y;
annotation.MaximumX = maxX;
annotation.Color = color;
annotation.Text = text;
Annotations.Add(annotation);
}
public void LineAnnotationsClear()
{
Annotations.Clear();
}
public bool AxisMaximumSet(int index, double? value = null)
{
if (index < 0 || index >= Axes.Count) return false;
if (value == null)
{
Axes[index].Maximum = (index == 0 ? 10 : 1);
Axes[index].AbsoluteMaximum = Axes[index].Maximum;
if (index == 0) AxisXStringFormatSet();
return true;
}
else
{
if (value > Axes[index].Maximum)
{
Axes[index].Maximum = (double)value;
Axes[index].AbsoluteMaximum = Axes[index].Maximum;
if (index == 0) AxisXStringFormatSet();
return true;
}
}
return false;
}
public static string AxisXStringFormatBuild(double? duration)
{
return (duration == null || (double)duration < 60) ? "m:ss" : (((double)duration < 60 * 60) ? "mm:ss" : "h:mm:ss");
}
public void AxisXStringFormatSet()
{
Axes[0].StringFormat = AxisXStringFormatBuild(Axes[0].Maximum);
}
public bool AxisYTitleAndUnitSet(string? plotViewType)
{
if (Axes.Count < 2) return false;
string? title = AxisYTitleBuild(plotViewType);
string? unit = AxisYUnitBuild(plotViewType);
var result = false;
if (title != Axes[1].Title) {
Axes[1].Title = title;
result = true;
}
if (unit != Axes[1].Unit)
{
Axes[1].Unit = unit;
result = true;
}
return result;
}
public static string? AxisYTitleBuild(string? plotType)
{
return (plotType?.ToUpper() ?? "") switch
{
"FRAME" => "Frame size",
"GOP" => "Bit rate",
"SECOND" => "Bit rate",
_ => ""
};
}
public static string? AxisYUnitBuild(string? plotType)
{
return (plotType?.ToUpper() ?? "") switch
{
"FRAME" => "kB",
"GOP" => "kb/GOP",
"SECOND" => "kb/s",
_ => null
};
}
public static string? AxisYAdditionalInfo(string? plotType)
{
switch (plotType?.ToUpper())
{
case "FRAME":
{
return System.Environment.NewLine + "Frame type={FrameType}";
}
}
return null;
}
//public void AxesRedraw()
//{
// for (int axisIndex = 0; axisIndex < Axes.Count; ++axisIndex) AxisRedraw(axisIndex);
//}
//public void AxisRedraw(int idx)
//{
// ((MyPlotModel)Axes[idx].PlotModel).Redraw();
//}
public bool IsEmpty()
{
foreach (var serie in Series) if (!IsSerieEmpty(serie)) return false;
return true;
}
private bool IsSerieEmpty(Series serie)
{
return ((StairStepSeries)serie).Points.Count == 0;
}
public StairStepSeries SerieCreate(FileItem file, int? idx)
{
var serie = new StairStepSeries
{
IsVisible = file.IsExists && file.IsEnabled,
StrokeThickness = 1.5,
Title = file.FN,
TrackerFormatString = TrackerFormatStringBuild(),
Decimator = Decimator.Decimate,
LineJoin = LineJoin.Miter,
VerticalStrokeThickness = 0.5,
VerticalLineStyle = LineStyle.Dash
};
if (idx != null) SerieStyleApply(serie, PlotStyles[(int)idx]);
return serie;
}
public StairStepSeries? SerieGet(int idx)
{
//return idx >=0 && idx < Series.Count ? (LineSeries)Series[idx] : null;
return idx >=0 && idx < Series.Count ? (StairStepSeries)Series[idx] : null;
}
public void SerieSet(int? idx, StairStepSeries? serie)
{
if (serie == null) return;
if (idx == null)
{
Series.Add(serie);
idx = Series.Count - 1;
}
else
{
Series[(int)idx] = serie;
}
SerieStyleApply((int)idx);
}
public void SerieRedraw(int idx, bool? visible = null, bool force = false)
{
var serie = SerieGet(idx);
if (serie == null) return;
bool changed = visible != null && serie.IsVisible != (bool)visible;
if (changed) serie.IsVisible = (visible == true);
if (changed || force) ((MyPlotModel)serie.PlotModel).Redraw();
}
public void SeriePointsAdd(int idx, List<DataPoint> dataPoints)
{
SerieGet(idx)?.Points.AddRange(dataPoints);
}
public void SeriePointsClear(int idx)
{
SerieGet(idx)?.Points.Clear();
}
private static void SerieStyleApply(StairStepSeries serie, SerieStyle style)
{
serie.Color = style.Color;
serie.LineStyle = style.LineStyle;
serie.MarkerType = style.MarkerType;
//serie.MarkerStroke = style.Color;
//serie.MarkerFill = style.Color;
//serie.MarkerResolution = 10;
}
public void SerieStyleApply(int idx)
{
var serie = SerieGet(idx);
if(serie != null) SerieStyleApply(serie, PlotStyles[idx]); // todo@ check idx range
}
public void PlotViewTypeSet(string plotViewType)
{
PlotViewType = plotViewType;
foreach (var serie in Series) serie.TrackerFormatString = TrackerFormatStringBuild();
}
private string TrackerFormatStringBuild()
{
return "{0}"
+ System.Environment.NewLine + "Time={2:hh\\:mm\\:ss\\.fff}"
+ System.Environment.NewLine + "{3}={4:0} " + AxisYUnitBuild(PlotViewType)
//+ AxisYAdditionalInfo(PlotViewType)
;
}
public static IExporter GetExporter(string? ext = null)
{
// todo@ Rid off the OxyPlot.Wpf.PngExporter by implementing the required ExportToBitmap functionality
return (ext?.ToUpper()) switch
{
"SVG" => new OxyPlot.SkiaSharp.SvgExporter { Width = 2400, Height = 600 },
"PDF" => new OxyPlot.SkiaSharp.PdfExporter { Width = 3200, Height = 800 },
"PNG" => new OxyPlot.SkiaSharp.PngExporter { Width = 3200, Height = 800 },
_ => new OxyPlot.Wpf.PngExporter { Width = 3200, Height = 800 } // OxyPlot.SkiaSharp.PngExporter does not have ExportToBitmap that needed to export to Clipboard
};
}
}
}