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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace FLY.ControlLibrary
{
/// <summary>
/// GraphScan.xaml 的交互逻辑
/// </summary>
public partial class GraphScan4 : UserControl, INotifyPropertyChanged
{
//设定数据统计范围
#region IGraphBase
#region 附加属性
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(GraphScan4), new PropertyMetadata("Title"));
public string Title
{
get
{
return GetValue(TitleProperty) as string;
}
set
{
if (value != Title)
{
SetValue(TitleProperty, value);
NotifyPropertyChanged("Title");
}
}
}
public static readonly DependencyProperty Title2Property =
DependencyProperty.Register("Title2", typeof(string), typeof(GraphScan4), new PropertyMetadata(""));
public string Title2
{
get
{
return GetValue(Title2Property) as string;
}
set
{
if (value != Title2)
{
SetValue(Title2Property, value);
NotifyPropertyChanged("Title2");
}
}
}
public static readonly DependencyProperty IsValidProperty =
DependencyProperty.Register("IsValid", typeof(bool), typeof(GraphScan4), new PropertyMetadata(true));
public bool IsValid
{
get
{
return (bool)GetValue(IsValidProperty);
}
set
{
if (value != IsValid)
{
SetValue(IsValidProperty, value);
NotifyPropertyChanged("IsValid");
}
}
}
/// <summary>
/// 计算结果
/// </summary>
public static readonly DependencyProperty CalStateProperty =
DependencyProperty.Register("CalState", typeof(string), typeof(GraphScan4), new PropertyMetadata(
"Max:null Min:null Avg:null 2σ:null",
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
})));
public string CalState
{
get
{
return (string)GetValue(CalStateProperty);
}
set
{
SetValue(CalStateProperty, value);
}
}
/// <summary>
/// 空的数据,定义某个值为空,默认为 99999998
/// </summary>
public static readonly DependencyProperty EmptyValueProperty =
DependencyProperty.Register("EmptyValue", typeof(int), typeof(GraphScan4), new PropertyMetadata(
99999998,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
})));
public int EmptyValue
{
get
{
return (int)GetValue(EmptyValueProperty);
}
set
{
SetValue(EmptyValueProperty, value);
}
}
/// <summary>
/// 放大倍数, 目标值= Target/放大倍数
/// </summary>
public static readonly DependencyProperty MultiProperty =
DependencyProperty.Register("Multi", typeof(int), typeof(GraphScan4), new PropertyMetadata(
100,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
})));
public int Multi
{
get
{
return (int)GetValue(MultiProperty);
}
set
{
SetValue(MultiProperty, value);
}
}
/// <summary>
/// 目标值
/// </summary>
public static readonly DependencyProperty TargetProperty =
DependencyProperty.Register("Target", typeof(int), typeof(GraphScan4), new PropertyMetadata(
3000,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("Target");
})));
public int Target
{
get
{
return (int)GetValue(TargetProperty);
}
set
{
SetValue(TargetProperty, value);
}
}
private int autotarget;
public int AutoTarget
{
get { return autotarget; }
set
{
if (autotarget != value)
{
autotarget = value;
NotifyPropertyChanged("AutoTarget");
}
}
}
/// <summary>
/// 自动目标值
/// </summary>
public static readonly DependencyProperty IsAutoTargetProperty =
DependencyProperty.Register("IsAutoTarget", typeof(bool), typeof(GraphScan4), new PropertyMetadata(
false,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("IsAutoTarget");
})));
public bool IsAutoTarget
{
get
{
return (bool)GetValue(IsAutoTargetProperty);
}
set
{
SetValue(IsAutoTargetProperty, value);
}
}
/// <summary>
/// 获取或设置指示是否将轴反转的标志
/// </summary>
public static readonly DependencyProperty IsReversedProperty =
DependencyProperty.Register("IsReversed", typeof(bool), typeof(GraphScan4), new PropertyMetadata(
false,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("IsReversed");
})));
public bool IsReversed
{
get
{
return (bool)GetValue(IsReversedProperty);
}
set
{
SetValue(IsReversedProperty, value);
}
}
/// <summary>
/// 获取或设置 左右标示是否反转 的标志
/// </summary>
public static readonly DependencyProperty LeftRightIsReversedProperty =
DependencyProperty.Register("LeftRightIsReversed", typeof(bool), typeof(GraphScan4), new PropertyMetadata(
false,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("LeftRightIsReversed");
})));
/// <summary>
/// 左右标示 反转
/// </summary>
public bool LeftRightIsReversed
{
get
{
return (bool)GetValue(LeftRightIsReversedProperty);
}
set
{
SetValue(LeftRightIsReversedProperty, value);
}
}
/// <summary>
/// 左右标示 可见
/// </summary>
public static readonly DependencyProperty LeftRightIsVisableProperty =
DependencyProperty.Register("LeftRightIsVisable", typeof(bool), typeof(GraphScan4), new PropertyMetadata(
false,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("LeftRightIsVisable");
})));
/// <summary>
/// 左右标示 反转
/// </summary>
public bool LeftRightIsVisable
{
get
{
return (bool)GetValue(LeftRightIsVisableProperty);
}
set
{
SetValue(LeftRightIsVisableProperty, value);
}
}
/// <summary>
/// %显示
/// </summary>
public static readonly DependencyProperty IsPercentProperty =
DependencyProperty.Register("IsPercent", typeof(bool), typeof(GraphScan4), new PropertyMetadata(
false,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("IsPercent");
})));
public bool IsPercent
{
get
{
return (bool)GetValue(IsPercentProperty);
}
set
{
SetValue(IsPercentProperty, value);
}
}
/// <summary>
/// 报警值
/// </summary>
public static readonly DependencyProperty AlarmProperty =
DependencyProperty.Register("Alarm", typeof(int), typeof(GraphScan4), new PropertyMetadata(
1000,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("Alarm");
})));
public int Alarm
{
get
{
return (int)GetValue(AlarmProperty);
}
set
{
SetValue(AlarmProperty, value);
}
}
/// <summary>
/// Y轴是 Alarm 的N倍 默认3倍
/// </summary>
public static readonly DependencyProperty YRangePercentProperty =
DependencyProperty.Register("YRangePercent", typeof(double), typeof(GraphScan4), new PropertyMetadata(
3.0,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("YRangePercent");
})));
public double YRangePercent
{
get
{
return (double)GetValue(YRangePercentProperty);
}
set
{
SetValue(YRangePercentProperty, value);
}
}
/// <summary>
/// 控制线
/// </summary>
public static readonly DependencyProperty HasCtrlLineProperty =
DependencyProperty.Register("HasCtrlLine", typeof(bool), typeof(GraphScan4), new PropertyMetadata(
false,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("HasCtrlLine");
})));
public bool HasCtrlLine
{
get
{
return (bool)GetValue(HasCtrlLineProperty);
}
set
{
SetValue(HasCtrlLineProperty, value);
}
}
/// <summary>
/// 控制线
/// </summary>
public static readonly DependencyProperty CtrlLineProperty =
DependencyProperty.Register("CtrlLine", typeof(int), typeof(GraphScan4), new PropertyMetadata(
1000,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("CtrlLine");
})));
public int CtrlLine
{
get
{
return (int)GetValue(CtrlLineProperty);
}
set
{
SetValue(CtrlLineProperty, value);
}
}
/// <summary>
/// 第一个分区号
/// </summary>
public static readonly DependencyProperty FirstBoltNoProperty =
DependencyProperty.Register("FirstBoltNo", typeof(int), typeof(GraphScan4), new PropertyMetadata(
0,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("FirstBoltNo");
})));
public int FirstBoltNo
{
get
{
return (int)GetValue(FirstBoltNoProperty);
}
set
{
SetValue(FirstBoltNoProperty, value);
}
}
/// <summary>
///
/// </summary>
public static readonly DependencyProperty FirstIndexProperty =
DependencyProperty.Register("FirstIndex", typeof(int), typeof(GraphScan4), new PropertyMetadata(
0,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("FirstIndex");
})));
public int FirstIndex
{
get
{
return (int)GetValue(FirstIndexProperty);
}
set
{
SetValue(FirstIndexProperty, value);
}
}
/// <summary>
///
/// </summary>
public static readonly DependencyProperty LastIndexProperty =
DependencyProperty.Register("LastIndex", typeof(int), typeof(GraphScan4), new PropertyMetadata(
49,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("LastIndex");
})));
public int LastIndex
{
get
{
return (int)GetValue(LastIndexProperty);
}
set
{
SetValue(LastIndexProperty, value);
}
}
/// <summary>
/// 数据统计的开始
/// </summary>
public static readonly DependencyProperty DataFirstProperty =
DependencyProperty.Register("DataFirst", typeof(int), typeof(GraphScan4), new PropertyMetadata(
0,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("DataFirst");
})));
public int DataFirst
{
get
{
return (int)GetValue(DataFirstProperty);
}
set
{
SetValue(DataFirstProperty, value);
}
}
/// <summary>
/// 数据统计的结束
/// </summary>
public static readonly DependencyProperty DataLastProperty =
DependencyProperty.Register("DataLast", typeof(int), typeof(GraphScan4), new PropertyMetadata(
49,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("DataLast");
})));
public int DataLast
{
get
{
return (int)GetValue(DataLastProperty);
}
set
{
SetValue(DataLastProperty, value);
}
}
#endregion
#region IGraphBase3
/// <summary>
/// mm/分区
/// </summary>
public static readonly DependencyProperty MmOfBoltProperty =
DependencyProperty.Register("MmOfBolt", typeof(double), typeof(GraphScan4), new PropertyMetadata(
10.0,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("MmOfBolt");
})));
/// <summary>
/// mm/分区
/// </summary>
public double MmOfBolt
{
get
{
return (double)GetValue(MmOfBoltProperty);
}
set
{
if (value != MmOfBolt)
{
SetValue(MmOfBoltProperty, value);
NotifyPropertyChanged("MmOfBolt");
}
}
}
/// <summary>
/// X轴间隔 单位分区
/// </summary>
public static readonly DependencyProperty XIntervalProperty =
DependencyProperty.Register("XInterval", typeof(int), typeof(GraphScan4), new PropertyMetadata(
5,
new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as GraphScan4).NotifyPropertyChanged("XInterval");
})));
/// <summary>
/// X轴间隔 单位分区
/// </summary>
public int XInterval
{
get
{
return (int)GetValue(XIntervalProperty);
}
set
{
if (value != XInterval)
{
SetValue(XIntervalProperty, value);
NotifyPropertyChanged("XInterval");
}
}
}
#endregion
/// <summary>
/// 数据源
/// </summary>
private ObservableCollection<GraphScan4Data> _datasource;
/// <summary>
/// 数据源
/// </summary>
public virtual ObservableCollection<GraphScan4Data> DataSource
{
get
{
return _datasource;
}
set
{
if (_datasource != null)
_datasource.CollectionChanged -= dataSource_CollectionChanged;
_datasource = value;
_datasource.CollectionChanged += dataSource_CollectionChanged;
mRefresh.UpdateData = true;
}
}
public event EventHandler ShowSettingDialogEvent;
public event EventHandler TitleClickEvent;
#endregion
#region 刷新率控制
protected class Refresh
{
/// <summary>
/// 控制刷新率
/// </summary>
System.Windows.Threading.DispatcherTimer timer;
bool updatedata_flag = false;
bool updatey_flag = false;
bool updatex_flag = false;
bool updatecalx_flag = false;
/// <summary>
/// 最快0.1s 刷新一次
/// </summary>
TimeSpan REFRESH_RATE = TimeSpan.FromSeconds(0.1);
public Refresh(GraphScan4 graph)
{
timer = new System.Windows.Threading.DispatcherTimer();
timer.Tick += new EventHandler(delegate (object sender, EventArgs e)
{
if (UpdateData)
{
UpdateData = false;
graph.DataBindAll_all();
}
if (UpdateY)
{
UpdateY = false;
graph.UpdateAxisY();
}
if (UpdateX)
{
UpdateX = false;
graph.UpdateXRange();
}
if (UpdateCalX)
{
UpdateCalX = false;
graph.UpdateDataCalRange();
graph.UpdateCalState_all();
}
timer.Stop();
});
timer.Interval = REFRESH_RATE;
timer.Start();
}
public bool UpdateData
{
get { return updatedata_flag; }
set
{
updatedata_flag = value;
if (value)
{
if (!timer.IsEnabled)
timer.Start();
}
}
}
public bool UpdateY
{
get { return updatey_flag; }
set
{
updatey_flag = value;
if (value)
{
if (!timer.IsEnabled)
timer.Start();
}
}
}
public bool UpdateX
{
get { return updatex_flag; }
set
{
updatex_flag = value;
if (value)
{
if (!timer.IsEnabled)
timer.Start();
}
}
}
public bool UpdateCalX
{
get { return updatecalx_flag; }
set
{
updatecalx_flag = value;
if (value)
{
if (!timer.IsEnabled)
timer.Start();
}
}
}
}
protected Refresh mRefresh;
#endregion
public GraphScan4()
{
InitializeComponent();
InitializeComponent2();
grid_title.DataContext = this;
PropertyChanged += new PropertyChangedEventHandler(GraphScan_PropertyChanged);
mRefresh = new Refresh(this);
SetDefault();
//Test();
}
void GraphScan_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
//if (e.PropertyName == "AutoTarget")
//{
// if (IsAutoTarget)
// {
// mRefresh.UpdateY = true;
// mRefresh.UpdateData = true;
// }
//}
if ((e.PropertyName == "Target") ||
(e.PropertyName == "Alarm") ||
(e.PropertyName == "YRangePercent") ||
(e.PropertyName == "IsAutoTarget") ||
(e.PropertyName == "IsPercent") ||
(e.PropertyName == "AutoTarget"))
{
mRefresh.UpdateY = true;
mRefresh.UpdateData = true;
}
else if (e.PropertyName == "FirstBoltNo")
{
mRefresh.UpdateCalX = true;
mRefresh.UpdateX = true;
mRefresh.UpdateData = true;
}
else if (e.PropertyName == "FirstIndex" || e.PropertyName == "LastIndex")
{
mRefresh.UpdateX = true;
}
else if (e.PropertyName == "DataFirst" || e.PropertyName == "DataLast")
{
mRefresh.UpdateCalX = true;
}
else if (e.PropertyName == "IsReversed")
{
chart1.ChartAreas[0].AxisX.IsReversed = IsReversed;
}
else if ((e.PropertyName == "HasCtrlLine") || (e.PropertyName == "CtrlLine"))
{
UpdateCtrlLine();
}
if (e.PropertyName == "MmOfBolt")
{
mRefresh.UpdateData = true;
}
else if (e.PropertyName == "XInterval")
{
mRefresh.UpdateX = true;
}
}
public void SetDefault()
{
NotifyPropertyChanged("Target");
NotifyPropertyChanged("TAlarm");
NotifyPropertyChanged("YRangePercent");
NotifyPropertyChanged("ChartType");
NotifyPropertyChanged("FirstBoltNo");
NotifyPropertyChanged("FirstIndex");
NotifyPropertyChanged("LastIndex");
NotifyPropertyChanged("DataFirst");
NotifyPropertyChanged("DataLast");
AutoTarget = Target;
}
void InitializeComponent2()
{
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Series series_sigma = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Series series_avg = new System.Windows.Forms.DataVisualization.Charting.Series();
chart1.Name = "chart1";
chart1.BackColor = System.Drawing.Color.Transparent;
//
// chart1
//---------------------------------------------------------------------------------------
chartArea1.Name = "chartArea1";
chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisX.IsMarksNextToAxis = false;
chartArea1.AxisX.IsLabelAutoFit = true;
chartArea1.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;
chartArea1.AxisX.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.VariableCount;
chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
//chartArea1.Position.Height = 100;// 42F;
//chartArea1.Position.Width = 100;// 88F;
//chartArea1.Position.X = 0;// 3F;
//chartArea1.Position.Y = 0;// 10F;
chart1.ChartAreas.Add(chartArea1);
//---------------------------------------------------------------------------------------
series_sigma.Name = "series_sigma";
series_sigma.ChartArea = chartArea1.Name;
series_sigma.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Range;
series_sigma.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));
series_sigma.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
series_sigma.BorderWidth = 2;
//series_sigma.ShadowColor = System.Drawing.Color.Black;
//series_sigma.ShadowOffset = 2;
series_sigma.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
series_sigma.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
chart1.Series.Add(series_sigma);
//---------------------------------------------------------------------------------------
series_avg.Name = "series_avg";
series_avg.ChartArea = chartArea1.Name;
series_avg.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
series_avg.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));
series_avg.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
series_avg.BorderWidth = 3;
series_avg.ShadowColor = System.Drawing.Color.Black;
series_avg.ShadowOffset = 2;
series_avg.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
series_avg.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
chart1.Series.Add(series_avg);
//chart1.PrePaint += new EventHandler<ChartPaintEventArgs>(Chart1_PrePaint);
this.chart1.PrePaint += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs>(Chart1_PrePaint);
}
protected virtual void SetCustomLabel(Axis axisY, double p, GridTickTypes gt, System.Drawing.Color c, bool ispercent, string message)
{
double r = axisY.Maximum - axisY.Minimum;
double f, t;
f = p + r / 2;
t = p - r / 2;
double target = (axisY.Maximum + axisY.Minimum) / 2;
CustomLabel customLabel;
string str;
if (!ispercent)
{
str = p.ToString("N1");
}
else
{
//百分比模式!!!!!!!
if (target == p)
{
str = p.ToString("N1");
}
else
{
double percent;
percent = ((p - target) / target) * 100;
str = percent.ToString("N1") + "%";
}
}
if (!string.IsNullOrEmpty(message))
str = message + " " + str;
customLabel = axisY.CustomLabels.Add(f, t, str);
customLabel.GridTicks = gt;
customLabel.ForeColor = c;
}
/// <summary>
/// 获取实际应用的目标值,公差
/// </summary>
/// <param name="target"></param>
/// <param name="alarm"></param>
/// <param name="yrange"></param>
/// <param name="ispercent"></param>
protected void GetActualTargetAlarm(out double target, out double alarm, out double yrange, out bool ispercent)
{
double multi = Multi;
if (IsAutoTarget)
target = AutoTarget / multi;//AutoTarget 计算得到的平均值
else
target = Target / multi;
ispercent = IsPercent;
if ((ispercent) && (Target <= 0) || (Alarm < 0) || (target <= 0))
ispercent = false;
if (ispercent)
{
alarm = target * Alarm / Target;
}
else
{
alarm = Alarm / multi;
}
yrange = alarm * YRangePercent;
if ((alarm < 0.1) || (yrange < 0.3))
{
alarm = 0.1;
yrange = 0.3;
}
}
/// <summary>
/// 与 Multi,Target,YRange,Alarm 相关
/// </summary>
public virtual void UpdateAxisY()
{
double alarm;
double yrange;
double target;
bool ispercent;
GetActualTargetAlarm(out target, out alarm, out yrange, out ispercent);
Axis axisY = chart1.ChartAreas[0].AxisY;
axisY.Crossing = target;
// Set manual minimum and maximum values.
axisY.Minimum = target - yrange;
axisY.Maximum = target + yrange;
axisY.CustomLabels.Clear();
//中间目标值,肯定不以%显示
SetCustomLabel(axisY, target, GridTickTypes.All, System.Drawing.Color.Green, false, null);
//上下限
SetCustomLabel(axisY, target + alarm, GridTickTypes.All, System.Drawing.Color.DarkGoldenrod, ispercent, null);
SetCustomLabel(axisY, target - alarm, GridTickTypes.All, System.Drawing.Color.DarkGoldenrod, ispercent, null);
//2倍上下限
SetCustomLabel(axisY, target + alarm * 2, GridTickTypes.All, System.Drawing.Color.Red, ispercent, null);
SetCustomLabel(axisY, target - alarm * 2, GridTickTypes.All, System.Drawing.Color.Red, ispercent, null);
//显示范围数值
if ((yrange != alarm) && (yrange != 2 * alarm))
{
SetCustomLabel(axisY, axisY.Minimum, GridTickTypes.TickMark, System.Drawing.Color.Black, ispercent, null);
SetCustomLabel(axisY, axisY.Maximum, GridTickTypes.TickMark, System.Drawing.Color.Black, ispercent, null);
}
//显示范围太小,多加纵坐标
if (yrange <= alarm)
{
SetCustomLabel(axisY, target - yrange / 2, GridTickTypes.All, System.Drawing.Color.Black, ispercent, null);
SetCustomLabel(axisY, target + yrange / 2, GridTickTypes.All, System.Drawing.Color.Black, ispercent, null);
}
}
/// <summary>
/// 与 FirstBoltNo,DataFirst,DataLast 相关
/// </summary>
public virtual void UpdateDataCalRange()
{
int firstBoltNo = FirstBoltNo;
int datafirst = DataFirst;
int datalast = DataLast;
if (datafirst > datalast)
{
int swap = datalast;
datalast = datafirst;
datafirst = swap;
}
System.Windows.Forms.DataVisualization.Charting.Axis axisX = chart1.ChartAreas[0].AxisX;
axisX.CustomLabels.Clear();
System.Windows.Forms.DataVisualization.Charting.CustomLabel customlabel
= new System.Windows.Forms.DataVisualization.Charting.CustomLabel(
datafirst + firstBoltNo, datalast + firstBoltNo, CalState, 1,
System.Windows.Forms.DataVisualization.Charting.LabelMarkStyle.LineSideMark);
axisX.CustomLabels.Add(customlabel);
}
/// <summary>
/// 与 FirstBoltNo,FirstIndex,LastIndex 相关
/// </summary>
public virtual void UpdateXRange()
{
int firstBoltNo = FirstBoltNo;
int first = FirstIndex;
int last = LastIndex;
if (first > last)
{
//交换
int swap = last;
last = first;
first = swap;
}
else if (first == last)
{
last = first + 10;
}
// Set manual minimum and maximum values.
Axis axis = chart1.ChartAreas[0].AxisX;
axis.Minimum = firstBoltNo + first;
axis.Maximum = firstBoltNo + last;
axis.Interval = XInterval;
//0必须在 写着0mm
axis.IntervalOffset = (0 - axis.Minimum) % XInterval;
}
public virtual void UpdateCtrlLine()
{
chart1.Invalidate();
}
protected virtual void Chart1_PrePaint(object sender, System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs e)
{
if (e.ChartElement is ChartArea)
{
ChartArea area = (ChartArea)e.ChartElement;
//if (area.Name == "Default")
{
if (!HasCtrlLine)
return;
double c1 = ((double)(Target + CtrlLine)) / Multi;
double c2 = ((double)(Target - CtrlLine)) / Multi;
if (c1 > area.AxisY.Maximum || c2 < area.AxisY.Minimum)
return;
// Take Graphics object from chart
Graphics graph = e.ChartGraphics.Graphics;
// Convert X and Y values to screen position
c1 = e.ChartGraphics.GetPositionFromAxis(area.Name, AxisName.Y, c1);
c2 = e.ChartGraphics.GetPositionFromAxis(area.Name, AxisName.Y, c2);
double XMin = e.ChartGraphics.GetPositionFromAxis(area.Name, AxisName.X, area.AxisX.Minimum);
double XMax = e.ChartGraphics.GetPositionFromAxis(area.Name, AxisName.X, area.AxisX.Maximum);
// Draw Maximum trangle
System.Drawing.Pen darkPen = new System.Drawing.Pen(System.Drawing.Color.LightPink, 1);
System.Drawing.PointF[] points = new System.Drawing.PointF[2];
points[0].X = (float)XMin;
points[1].X = (float)XMax;
points[0].Y = points[1].Y = (float)c1;
// Convert relative coordinates to absolute coordinates.
points[0] = e.ChartGraphics.GetAbsolutePoint(points[0]);
points[1] = e.ChartGraphics.GetAbsolutePoint(points[1]);
graph.DrawLine(darkPen, points[0], points[1]);
points[0].X = (float)XMin;
points[1].X = (float)XMax;
points[0].Y = points[1].Y = (float)c2;
// Convert relative coordinates to absolute coordinates.
points[0] = e.ChartGraphics.GetAbsolutePoint(points[0]);
points[1] = e.ChartGraphics.GetAbsolutePoint(points[1]);
graph.DrawLine(darkPen, points[0], points[1]);
}
}
}
protected virtual void UpdateOnePoint_all(int index)
{
double multi = Multi;
int emptyvalue = EmptyValue;
DataPoint point_sigma;
DataPoint point_avg;
double avg;
double upper;
double lower;
if ((DataSource[index].Avg == emptyvalue) || (DataSource[index].Sigma == emptyvalue))
{
avg = double.NaN;
upper = double.NaN;
lower = double.NaN;
}
else
{
double sigma2 = 2 * DataSource[index].Sigma / multi;
avg = DataSource[index].Avg / multi;
upper = avg + sigma2;
lower = avg - sigma2;
}
if (index >= chart1.Series["series_sigma"].Points.Count)
{
chart1.Series["series_sigma"].Points.AddXY(
FirstBoltNo + index,
upper, lower);
point_sigma = chart1.Series["series_sigma"].Points[index];
chart1.Series["series_avg"].Points.AddXY(
FirstBoltNo + index,
avg);
point_avg = chart1.Series["series_avg"].Points[index];
}
else
{
point_sigma = chart1.Series["series_sigma"].Points[index];
point_sigma.YValues[0] = upper;
point_sigma.YValues[1] = lower;
point_sigma.XValue = FirstBoltNo + index;
point_avg = chart1.Series["series_avg"].Points[index];
point_avg.YValues[0] = avg;
point_avg.XValue = FirstBoltNo + index;
}
point_avg.AxisLabel = (point_avg.XValue * MmOfBolt).ToString("F0") + "mm";
//设定空数据
if (double.IsNaN(avg))
{
point_sigma.IsEmpty = true;
point_avg.IsEmpty = true;
System.Drawing.Color c = System.Drawing.Color.Transparent;
point_sigma.MarkerColor = c;
point_sigma.BorderColor = c;
point_sigma.Color = c;
point_avg.MarkerColor = c;
point_avg.BorderColor = c;
point_avg.Color = c;
}
else
{
point_sigma.IsEmpty = false;
point_avg.IsEmpty = false;
double alarm;
double target;
double yrange;
bool ispercent;
GetActualTargetAlarm(out target, out alarm, out yrange, out ispercent);
double threshold1 = alarm;
double threshold2 = alarm * 2;
System.Drawing.Color c;
if (((upper) > (target + threshold2)) || ((upper) < (target - threshold2)))
{
c = System.Drawing.Color.Red;
}
else if (((lower) > (target + threshold2)) || ((lower) < (target - threshold2)))
{
c = System.Drawing.Color.Red;
}
else if (((upper) > (target + threshold1)) || ((upper) < (target - threshold1)))
{
c = System.Drawing.Color.Orange;
}
else if (((lower) > (target + threshold1)) || ((lower) < (target - threshold1)))
{
c = System.Drawing.Color.Orange;
}
else
{
c = System.Drawing.Color.Green;
}
point_sigma.BorderColor = c;
point_sigma.Color = c;
point_avg.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));
point_avg.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
}
}
void DataBindAll_all()
{
chart1.Series["series_avg"].Points.Clear();
chart1.Series["series_sigma"].Points.Clear();
if (DataSource != null)
{
for (int i = 0; i < DataSource.Count(); i++)
{
UpdateOnePoint_all(i);
}
}
CheckDataSourceEmpty();
UpdateCalState_all();
}
/// <summary>
/// 与 Multi,EmptyValue,DataFirst,DataLast 相关
/// </summary>
public void UpdateCalState_all()
{
int emptyvalue = EmptyValue;
double multi = Multi;
if (DataSource == null)
{
goto _err;
}
string title;
var dats_max = from data in DataSource where data.Avg != emptyvalue select data.Avg + data.Sigma * 2;
var dats_min = from data in DataSource where data.Avg != emptyvalue select data.Avg - data.Sigma * 2;
List<int> dats = new List<int>();
dats.AddRange(dats_max);
dats.AddRange(dats_min);
if (dats.Count() == 0)
{
goto _err;
}
else
{
//AutoTarget = (int)dats.Average();
double avg = dats.Average() / multi;
double max = dats.Max() / multi;
double min = dats.Min() / multi;
double sum = 0;
foreach (int dat in dats)
{
sum += Math.Pow(avg - (double)dat / multi, 2);
}
sum /= (dats.Count() - 1);
double sigma = Math.Sqrt(sum);
if (IsPercent)
{
title = "Max:+" + ((max / avg * 100) - 100).ToString("N1") + "% " +
"Min:-" + (100 - (min / avg * 100)).ToString("N1") + "% " +
"Avg:" + avg.ToString("N1") + " " +
"2σ:" + (sigma / avg * 2 * 100).ToString("N1") + "%";
}
else
{
title = "Max:" + max.ToString("N1") + " " +
"Min:" + min.ToString("N1") + " " +
"Avg:" + avg.ToString("N1") + " " +
"2σ:" + (sigma * 2).ToString("N1");
}
}
goto _end;
_err:
//title = Properties.Resources.StrMax + ":" + "NULL " +
// Properties.Resources.StrMin+":"+"NULL "+
// Properties.Resources.StrAvg+":"+"NULL "+
// "3Sigma:"+"NULL ";
title = "Max:null Min:null Avg:null 2σ:null";
_end:
CalState = title;
UpdateDataCalRange();
}
/// <summary>
/// DataSource 里面的数据都是 EmptyValue 组成, 当修改数据时,应该clear ,再添加
/// </summary>
private bool DataSourceIsEmpty = false;
/// <summary>
/// 当一个数据都没有时,需要添加空数据
/// </summary>
public void CheckDataSourceEmpty()
{
if ((DataSource == null) || (DataSource.Count() == 0))
{
chart1.Series["series_avg"].Points.Clear();
chart1.Series["series_avg"].Points.AddXY(FirstBoltNo, EmptyValue);
chart1.Series["series_avg"].Points[0].IsEmpty = true;
DataSourceIsEmpty = true;
}
else
{
DataSourceIsEmpty = false;
}
}
void dataSource_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
mRefresh.UpdateData = true;
}
public void Button_Click(object sender, RoutedEventArgs e)
{
ShowSettingDialogEvent?.Invoke(this, new EventArgs());
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
TitleClickEvent?.Invoke(this, new EventArgs());
}
#region INotifyPropertyChanged 成员
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
public struct GraphScan4Data
{
public int Avg;
public int Sigma;
}
}