aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Statistics/Views/JobRunsView.xaml
blob: 6431d72fb62d87401e1182fc6af8285fe00e34ce (plain)
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
<UserControl x:Class="Tango.MachineStudio.Statistics.Views.JobRunsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:sharedConverters="clr-namespace:Tango.SharedUI.Converters;assembly=Tango.SharedUI"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:localConverters="clr-namespace:Tango.MachineStudio.Statistics.Converters"
             xmlns:mahapps="http://metro.mahapps.com/winfx/xaml/controls"
             xmlns:autoComplete="clr-namespace:Tango.AutoComplete.Editors;assembly=Tango.AutoComplete"
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             xmlns:tooltips="clr-namespace:Tango.MachineStudio.Statistics.Tooltips"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:enumerations="clr-namespace:Tango.BL.Enumerations;assembly=Tango.BL"
             xmlns:local="clr-namespace:Tango.MachineStudio.Statistics.Views"
             xmlns:model="clr-namespace:Tango.MachineStudio.Statistics.Models"
             xmlns:wellknowntypes="clr-namespace:Google.Protobuf.WellKnownTypes;assembly=Google.Protobuf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="1800" Foreground="{StaticResource JobFieldForeground}">
    <UserControl.Resources>
        <sharedConverters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <sharedConverters:BooleanToYesNoConverter x:Key="BooleanToYesNoConverter"/>
        <sharedConverters:EnumToDescriptionConverter x:Key="EnumToDescriptionConverter" />
        <localConverters:DateTimeToStringFormatConverter x:Key="DateTimeToStringFormatConverter" />
        <sharedConverters:DateTimeUTCToShortDateTimeConverter x:Key="DateTimeUTCToShortDateTimeConverter"/>
        <localConverters:StringToBoolYesNoNullConverter x:Key="StringToBoolYesNoNullConverter"/>
        <localConverters:LiquidTypeToColorConverter x:Key="LiquidTypeToColorConverter"/>
        <localConverters:CollectionConverter x:Key="CollectionConverter"/>
        <localConverters:StringToFirstLetterConverter x:Key="StringToFirstLetterConverter"/>
        <localConverters:MidTankLevelToElementHeightConverter x:Key="MidTankLevelToElementHeightConverter"/>
        <localConverters:JobLengthConverter x:Key="JobLengthConverter"/>
        <localConverters:NanoLiterToLiterFormatConverter x:Key="NanoLiterToLiterFormatConverter"/>

        <ResourceDictionary x:Key="SelectAllTextBoxResource">
            <Style TargetType="TextBox">
                <EventSetter Event="GotFocus" Handler="TextBox_GotFocus"></EventSetter>
                <EventSetter Event="MouseDown" Handler="TextBox_PreviewMouseUp"></EventSetter>
            </Style>
        </ResourceDictionary>

        <Style TargetType="{x:Type TextBlock}" x:Key="WrapText">
            <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>

        <Style TargetType="lvc:PieSeries">
            <Setter Property="Stroke" Value="#99F9F9F9"></Setter>
            <Setter Property="StrokeThickness" Value="2"/>
            <Setter Property="Foreground"  Value="#FF1C1C1F" />
        </Style>
        <Style TargetType="lvc:PieChart">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="LegendLocation" Value="None"/>
        </Style>

        <DataTemplate x:Key="PopUpDataTemplate">
            <CheckBox VerticalAlignment="Center" FontSize="11" DockPanel.Dock="Left" IsChecked="{Binding IsSelected}">
                <CheckBox.Content>
                    <TextBlock Margin="5 0 0 0" VerticalAlignment="Center" Text="{Binding Data}"></TextBlock>
                </CheckBox.Content>
            </CheckBox>
        </DataTemplate>

        <DataTemplate x:Key="PopUpSNDataTemplate">
            <CheckBox VerticalAlignment="Center" FontSize="11" DockPanel.Dock="Left" IsChecked="{Binding IsSelected}" >
                <CheckBox.Content>
                    <TextBlock Margin="5 0 0 0" VerticalAlignment="Center" Text="{Binding Data.SerialNumber}" ToolTip="{Binding Data.SerialNumber}" FontFamily="{StaticResource FontName}"></TextBlock>
                </CheckBox.Content>
            </CheckBox>
        </DataTemplate>

        <DataTemplate x:Key="PopupBoolDataTemplate">
            <CheckBox VerticalAlignment="Center" FontSize="11" DockPanel.Dock="Left" IsChecked="{Binding IsSelected}">
                <CheckBox.Content>
                    <TextBlock Margin="5 0 0 0" VerticalAlignment="Center" Text="{Binding Data, Converter={StaticResource BooleanToYesNoConverter}}"/>
                </CheckBox.Content>
            </CheckBox>
        </DataTemplate>

        <DataTemplate x:Key="PopupThreadDataTemplate">
            <CheckBox VerticalAlignment="Center" FontSize="11" DockPanel.Dock="Left" IsChecked="{Binding IsSelected}" >
                <CheckBox.Content>
                    <TextBlock Margin="5 0 5 0" VerticalAlignment="Center" Text="{Binding Data.Name}" ToolTip="{Binding Data.Name}" FontFamily="{StaticResource FontName}" FontSize="11"></TextBlock>
                </CheckBox.Content>
            </CheckBox>
        </DataTemplate>


        <Style x:Key="{x:Type ToolTip}" TargetType="{x:Type ToolTip}" BasedOn="{StaticResource MaterialDesignToolTip}">
            <Setter Property="Background"  Value="{StaticResource Logging.Background}" />
            <Setter Property="Foreground"  Value="{StaticResource MainWindow.Foreground}" />
        </Style>

    </UserControl.Resources>
    
    <Grid IsEnabled="{Binding IsFree}" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Background="{StaticResource WhiteBackgroundBrush}"  Width="Auto" Margin="20 0 0 1" >
            <Border Padding="14 14 12 14" BorderBrush="DimGray" BorderThickness="0" Background="{StaticResource Logging.Background}" HorizontalAlignment="Stretch" CornerRadius="2" >
                <Border.Effect>
                    <DropShadowEffect ShadowDepth="4" BlurRadius="10" Opacity="0.5"/>
                </Border.Effect>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <Grid >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="1*"/>
                            <RowDefinition Height="1*"/>
                        </Grid.RowDefinitions>
                        <StackPanel Orientation="Horizontal"  Grid.Row="0">
                            <StackPanel  Margin="10 5 0 0"  Orientation="Vertical"  HorizontalAlignment="Center">
                                <TextBlock Text="Machine:" VerticalAlignment="Center" FontSize="11"></TextBlock>
                                <ToggleButton  Width="140" Margin="0 10 0 0" x:Name="selectMachineButton" HorizontalAlignment="Left" FontSize="16" VerticalAlignment="Center">
                                    <ToggleButton.Template>
                                        <ControlTemplate>
                                            <Grid>
                                                <Border CornerRadius="3" BorderBrush="{StaticResource borderBrush}" BorderThickness="1" TextElement.Foreground="Black" Height="24">
                                                    <DockPanel>
                                                        <Button x:Name="selectMachineButton" Width="18" Padding="0" Height="16" DockPanel.Dock="Right" BorderBrush="{x:Null}" HorizontalAlignment="Left" Click="Button_Click" Style="{StaticResource MaterialDesignFlatButton}" Foreground="{StaticResource MainWindow.Foreground}">
                                                            <materialDesign:PackIcon Width="16"  Height="16"  Kind="ChevronDown" VerticalAlignment="Center" Foreground="{StaticResource MainWindow.Foreground}" HorizontalAlignment="Right"/>
                                                        </Button>
                                                        <TextBlock VerticalAlignment="Center"  Foreground="{StaticResource MainWindow.Foreground}" FontSize="11" Margin="5">
                                                            <TextBlock.Style>
                                                                <Style TargetType="{x:Type TextBlock}">
                                                                    <Setter Property="Text" Value="{Binding SelectedMachines.SynchedSource.Count, StringFormat={}Selected Machines({0})}">
                                                                    </Setter>
                                                                    <Style.Triggers>
                                                                        <DataTrigger Binding="{Binding SelectedMachines.SynchedSource.Count}" Value="0">
                                                                            <Setter Property="Text" Value="All machines"/>
                                                                        </DataTrigger>
                                                                    </Style.Triggers>
                                                                </Style>
                                                            </TextBlock.Style>
                                                        </TextBlock>
                                                    </DockPanel>
                                                </Border>
                                                <Popup AllowsTransparency="True" StaysOpen="False" IsOpen="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},Path=IsChecked }">
                                                    <Border Padding="3" CornerRadius="0 0 3 3" MinWidth="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},Path=Width}" Margin="5" Background="#F6F6F6" BorderBrush="{StaticResource AutoCompleteTextBox.Popup.BorderBrush}" >
                                                        <Border.Effect>
                                                            <DropShadowEffect Opacity="0.2" />
                                                        </Border.Effect>
                                                        <ScrollViewer MaxHeight="600" Background="#F6F6F6" >
                                                            <ItemsControl ItemsSource="{Binding SelectedMachines}"  Foreground="{StaticResource MainWindow.Foreground}" ItemTemplate="{StaticResource PopUpSNDataTemplate}"/>
                                                        </ScrollViewer>
                                                    </Border>
                                                </Popup>
                                            </Grid>
                                        </ControlTemplate>
                                    </ToggleButton.Template>
                                </ToggleButton>
                            </StackPanel>

                            <StackPanel   Margin="50 10 0 0" HorizontalAlignment="Center">
                                <TextBlock FontSize="11">Start Date:</TextBlock>
                                <DatePicker x:Name="startdatePicker" Margin="0 5 0 0" SelectedDate="{Binding StartSelectedDate}"   materialDesign:HintAssist.Hint="Pick start date"  Width="130" VerticalAlignment="Center" FontSize="11" />
                            </StackPanel>
                            <StackPanel   Margin="50 10 0 0" HorizontalAlignment="Center">
                                <TextBlock FontSize="11">End Date:</TextBlock>
                                <DatePicker x:Name="endDatePicker"  Margin="0 5 0 0" SelectedDate="{Binding EndSelectedDate}" materialDesign:HintAssist.Hint="Pick end date"  Width="130" VerticalAlignment="Center" FontSize="11" />
                            </StackPanel>
                        </StackPanel>

                        <StackPanel Orientation="Horizontal" Grid.Row="1">
                            <StackPanel  Margin="10 10 0 0" Orientation="Vertical" HorizontalAlignment="Center">
                                <TextBlock  Text="Job Name:" VerticalAlignment="Center" FontSize="11"></TextBlock>
                                <autoComplete:AutoCompleteTextBox Margin="0 10 0 0" Provider="{Binding JobsProvider}" Width="140" FontSize="11" LoadingContent="Loading..." SelectedItem="{Binding SelectedJob,Mode=TwoWay}" materialDesign:HintAssist.Hint="All" DisplayMember="Name" materialDesign:HintAssist.IsFloating="False">
                                    <autoComplete:AutoCompleteTextBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel>
                                                <TextBlock Text="{Binding Name}" FontWeight="Bold" FontStyle="Italic"></TextBlock>
                                            </StackPanel>
                                        </DataTemplate>
                                    </autoComplete:AutoCompleteTextBox.ItemTemplate>
                                </autoComplete:AutoCompleteTextBox>
                            </StackPanel>
                            <StackPanel  Margin="50 10 0 0" Orientation="Vertical"  HorizontalAlignment="Center">
                                <TextBlock  Text="Source:" VerticalAlignment="Center" FontSize="11"></TextBlock>
                                <ToggleButton Width="130" Height="24" Margin="0 10 0 0" x:Name="selectJobRunSources" HorizontalAlignment="Left" FontSize="16" VerticalAlignment="Center">
                                    <ToggleButton.Template>
                                        <ControlTemplate>
                                            <Grid>
                                                <Border CornerRadius="3" BorderBrush="{StaticResource borderBrush}" BorderThickness="1" TextElement.Foreground="Black" >
                                                    <DockPanel>
                                                        <Button x:Name="jobRunSourcesButton" Width="18" Padding="0" Height="16" DockPanel.Dock="Right" BorderBrush="{x:Null}" HorizontalAlignment="Left" Click="JobRunSourcesButton_Click" Style="{StaticResource MaterialDesignFlatButton}" Foreground="{StaticResource MainWindow.Foreground}">
                                                            <materialDesign:PackIcon Width="16"  Height="16"  Kind="ChevronDown" VerticalAlignment="Center" Foreground="{StaticResource MainWindow.Foreground}" HorizontalAlignment="Right"/>
                                                        </Button>
                                                        <TextBlock VerticalAlignment="Center"  Foreground="{StaticResource MainWindow.Foreground}" FontSize="11" Margin="5" Text="{Binding JobRunSelectedSources.SynchedSource, Converter={StaticResource CollectionConverter}}"/>
                                                     </DockPanel>
                                                </Border>
                                                <Popup AllowsTransparency="True" StaysOpen="False" IsOpen="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},Path=IsChecked }">
                                                    <Border Padding="3" CornerRadius="0 0 3 3" MinWidth="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},Path=Width}" Margin="5" Background="#F6F6F6" BorderBrush="{StaticResource AutoCompleteTextBox.Popup.BorderBrush}" >
                                                        <Border.Effect>
                                                            <DropShadowEffect Opacity="0.2" />
                                                        </Border.Effect>
                                                        <ItemsControl ItemsSource="{Binding JobRunSelectedSources}"  Foreground="{StaticResource MainWindow.Foreground}" ItemTemplate="{StaticResource PopUpDataTemplate}"/>

                                                    </Border>
                                                </Popup>
                                            </Grid>
                                        </ControlTemplate>
                                    </ToggleButton.Template>
                                </ToggleButton>
                            </StackPanel>
                            <StackPanel  Margin="50 10 0 0" Orientation="Vertical"  HorizontalAlignment="Center">
                                <TextBlock  Text="Gradient:" VerticalAlignment="Center" FontSize="11"></TextBlock>
                                <ToggleButton Height="24" Width="130" Margin="0 10 0 0" x:Name="selectIsGradient" HorizontalAlignment="Left" FontSize="16" VerticalAlignment="Center">
                                    <ToggleButton.Template>
                                        <ControlTemplate>
                                            <Grid>
                                                <Border CornerRadius="3" BorderBrush="{StaticResource borderBrush}" BorderThickness="1" TextElement.Foreground="Black" >
                                                    <DockPanel>
                                                        <Button x:Name="isGradientButton" Width="18" Padding="0" Height="16" DockPanel.Dock="Right" BorderBrush="{x:Null}" HorizontalAlignment="Left" Click="IsGradientButton_Click" Style="{StaticResource MaterialDesignFlatButton}" Foreground="{StaticResource MainWindow.Foreground}">
                                                            <materialDesign:PackIcon Width="16"  Height="16"  Kind="ChevronDown" VerticalAlignment="Center" Foreground="{StaticResource MainWindow.Foreground}" HorizontalAlignment="Right"/>
                                                        </Button>
                                                        <TextBlock VerticalAlignment="Center"  Foreground="{StaticResource MainWindow.Foreground}" FontSize="11" Margin="5 0 2 0">
                                                         <Run Text="{Binding IsGradientSelection.SynchedSource, Converter={StaticResource CollectionConverter}}"></Run>
                                                        </TextBlock>
                                                    </DockPanel>
                                                </Border>
                                                <Popup AllowsTransparency="True" StaysOpen="False" IsOpen="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},Path=IsChecked }">
                                                    <Border Padding="3" CornerRadius="0 0 3 3" MinWidth="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},Path=Width}" Margin="5" Background="#F6F6F6" BorderBrush="{StaticResource AutoCompleteTextBox.Popup.BorderBrush}">
                                                        <Border.Effect>
                                                            <DropShadowEffect Opacity="0.2" />
                                                        </Border.Effect>
                                                        <ItemsControl ItemsSource="{Binding IsGradientSelection}"  Foreground="{StaticResource MainWindow.Foreground}" ItemTemplate="{StaticResource PopupBoolDataTemplate}"/>

                                                    </Border>
                                                </Popup>
                                            </Grid>
                                        </ControlTemplate>
                                    </ToggleButton.Template>
                                </ToggleButton>
                            </StackPanel>
                            <StackPanel  Margin="40 10 0 0" Orientation="Vertical"  HorizontalAlignment="Center">
                                <TextBlock  Text="Length:" VerticalAlignment="Center" FontSize="11"></TextBlock>
                                <Border BorderThickness="1" CornerRadius="3" BorderBrush="{StaticResource borderBrush}" Margin="0 10 0 0" Height="24" Padding="10 0">
                                    <StackPanel Orientation="Horizontal">
                                    <TextBlock  Text="{Binding LengthLowerValue, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" FontSize="11" Width="30"></TextBlock>
                                    <mahapps:RangeSlider Focusable="True" Height="40" Margin="5 5 5 5" Minimum="0" Maximum="5000" Width="140" ExtendedMode="True"
                                             LowerValue="{Binding LengthLowerValue,  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                             UpperValue="{Binding LengthUpperValue,  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                             VerticalAlignment="Center" IsSnapToTickEnabled="True" FontSize="8"/>
                                        <TextBlock  Text="{Binding LengthUpperValue}" VerticalAlignment="Center" FontSize="11" Width="30"></TextBlock>
                                    </StackPanel>
                                </Border>
                            </StackPanel>
                            <StackPanel  Margin="40 10 0 0" Orientation="Vertical"  HorizontalAlignment="Center">
                                <TextBlock  Text="Status:" VerticalAlignment="Center" FontSize="11"></TextBlock>
                                <ToggleButton Height="24" Width="170"  Margin="0 10 0 0" x:Name="selectJobRunStatus" HorizontalAlignment="Left" FontSize="16" VerticalAlignment="Center">
                                    <ToggleButton.Template>
                                        <ControlTemplate>
                                            <Grid>
                                                <Border CornerRadius="3" BorderBrush="{StaticResource borderBrush}" BorderThickness="1" TextElement.Foreground="Black" >
                                                    <DockPanel>
                                                        <Button x:Name="jobRunStatusButton" Width="18" Padding="0" Height="16" DockPanel.Dock="Right" BorderBrush="{x:Null}" HorizontalAlignment="Left" Click="JobRunStatusButton_Click" Style="{StaticResource MaterialDesignFlatButton}" Foreground="{StaticResource MainWindow.Foreground}">
                                                            <materialDesign:PackIcon Width="16"  Height="16"  Kind="ChevronDown" VerticalAlignment="Center" Foreground="{StaticResource MainWindow.Foreground}" HorizontalAlignment="Right"/>
                                                        </Button>
                                                        <TextBlock VerticalAlignment="Center"  Foreground="{StaticResource MainWindow.Foreground}" FontSize="11" Margin="5" Text="{Binding JobRunSelectedStatuses.SynchedSource, Converter={StaticResource CollectionConverter}}"/>
                                                    </DockPanel>
                                                </Border>
                                                <Popup AllowsTransparency="True"  StaysOpen="False" IsOpen="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},Path=IsChecked }">
                                                    <Border Padding="3" CornerRadius="0 0 3 3" MinWidth="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},Path=Width}" Margin="5" Background="#F6F6F6" BorderBrush="{StaticResource AutoCompleteTextBox.Popup.BorderBrush}" >
                                                        <Border.Effect>
                                                            <DropShadowEffect Opacity="0.2" />
                                                        </Border.Effect>
                                                        <ItemsControl ItemsSource="{Binding JobRunSelectedStatuses}"  Foreground="{StaticResource MainWindow.Foreground}" ItemTemplate="{StaticResource PopUpDataTemplate}"/>
                                                    </Border>
                                                </Popup>
                                            </Grid>
                                        </ControlTemplate>
                                    </ToggleButton.Template>
                                </ToggleButton>
                            </StackPanel>
                            <StackPanel  Margin="40 10 0 0"  Orientation="Vertical"  HorizontalAlignment="Center">
                                <TextBlock Text="Threads:" VerticalAlignment="Center" FontSize="11"></TextBlock>
                                <ToggleButton  Width="140" Margin="0 10 0 0" x:Name="selectThreadsButton" HorizontalAlignment="Left" FontSize="16" VerticalAlignment="Center">
                                    <ToggleButton.Template>
                                        <ControlTemplate>
                                            <Grid>
                                                <Border CornerRadius="3" BorderBrush="{StaticResource borderBrush}" BorderThickness="1" TextElement.Foreground="Black" Height="24">
                                                    <DockPanel>
                                                        <Button x:Name="selectThreadButton" Width="18" Padding="0" Height="16" DockPanel.Dock="Right" BorderBrush="{x:Null}" HorizontalAlignment="Left" Click="SelectMachineButton_Click"  Style="{StaticResource MaterialDesignFlatButton}" Foreground="{StaticResource MainWindow.Foreground}">
                                                            <materialDesign:PackIcon Width="16"  Height="16"  Kind="ChevronDown" VerticalAlignment="Center" Foreground="{StaticResource MainWindow.Foreground}" HorizontalAlignment="Right"/>
                                                        </Button>
                                                        <TextBlock VerticalAlignment="Center"  Foreground="{StaticResource MainWindow.Foreground}" FontSize="11" Margin="5" >
                                                            <TextBlock.Style>
                                                                <Style TargetType="{x:Type TextBlock}">
                                                                    <Setter Property="Text" Value="{Binding SelectedThreads.SynchedSource.Count, StringFormat={}Selected Threads({0})}">
                                                                    </Setter>
                                                                    <Style.Triggers>
                                                                        <DataTrigger Binding="{Binding SelectedThreads.SynchedSource.Count}" Value="0">
                                                                            <Setter Property="Text" Value="All threads"/>
                                                                        </DataTrigger>
                                                                    </Style.Triggers>
                                                                </Style>
                                                            </TextBlock.Style>
                                                        </TextBlock>
                                                    </DockPanel>
                                                </Border>
                                                <Popup AllowsTransparency="True"  StaysOpen="False" IsOpen="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},Path=IsChecked }">
                                                    <Border Padding="3" CornerRadius="0 0 3 3" MinWidth="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},Path=Width}" Margin="5" BorderBrush="{StaticResource AutoCompleteTextBox.Popup.BorderBrush}" >
                                                        <Border.Effect>
                                                            <DropShadowEffect Opacity="0.2" />
                                                        </Border.Effect>
                                                        <ScrollViewer MaxHeight="600" Background="#F6F6F6">
                                                            <ItemsControl ItemsSource="{Binding SelectedThreads}"  Foreground="{StaticResource MainWindow.Foreground}" ItemTemplate="{StaticResource PopupThreadDataTemplate}"/>

                                                        </ScrollViewer>
                                                    </Border>
                                                </Popup>
                                            </Grid>
                                        </ControlTemplate>
                                    </ToggleButton.Template>
                                </ToggleButton>
                            </StackPanel>
                        </StackPanel>
                    </Grid>
                    <Button Grid.Column="1" HorizontalAlignment="Right" Command="{Binding LoadJobRunsCommand}"  Margin="0 0 10 0" Padding="70 15" Height="Auto" VerticalAlignment="Center">ANALYZE</Button>
                </Grid>
            </Border>
        </Grid>

        <DataGrid x:Name="ItemsGrid" GridLinesVisibility="None" Grid.Row="1" Margin="20 0 0 10" SelectionMode="Single" SelectionUnit="FullRow"  BorderBrush="{StaticResource borderBrush}" IsReadOnly="True" BorderThickness="1" RowHeight="80" 
                           Background="{StaticResource TransparentBackgroundBrush}"  AlternatingRowBackground="{StaticResource Transparent200}" AutoGenerateColumns="False" CanUserReorderColumns="False"
                           CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding JobRuns}" HorizontalScrollBarVisibility="Disabled"  CanUserResizeColumns="False" CanUserResizeRows="False"
                           SelectedItem="{Binding SelectedJobRun}" CanUserSortColumns="True" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" FontSize="11">

            <DataGrid.Resources>
                <Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
                    <Setter Property="HorizontalAlignment" Value="Left"></Setter>
                    <Setter Property="HorizontalContentAlignment" Value="Left"></Setter>
                    <Setter Property="Padding" Value="0 8 0 8"></Setter>
                    <Setter Property="Margin" Value="0 0 0 0"></Setter>
                    <Setter Property="FontWeight" Value="SemiBold"/>
                </Style>
            </DataGrid.Resources>

            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                    <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                    <Setter Property="HorizontalAlignment" Value="Left"/>
                    <Setter Property="Margin" Value="0 0 10 0"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridCell}">
                                <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Transparent"></Setter>
                            <Setter Property="Foreground" Value="{StaticResource AccentColorBrush}" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}">
                    <Setter Property="BorderThickness" Value="0 0 0 1"/>
                    <Setter Property="BorderBrush" Value="LightGray"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Transparent"></Setter>
                            <Setter Property="Foreground" Value="{StaticResource AccentColorBrush}" />
                            <Setter Property="Cursor" Value="Hand"></Setter>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Transparent"></Setter>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" Value="Transparent"></Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>

            <DataGrid.Columns>
                <DataGridTemplateColumn Header="" Width="50">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <materialDesign:PackIcon Width="24" Height="24" Margin="8 -5 0 0">
                                <materialDesign:PackIcon.Style>
                                    <Style TargetType="materialDesign:PackIcon">
                                        <Setter Property="Kind" Value="Check"></Setter>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding JobRun.JobRunStatus}" Value="Completed">
                                                <Setter Property="Kind" Value="Check"></Setter>
                                                <Setter Property="Foreground" Value="{StaticResource GreenOpenFileBrush}"></Setter>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding JobRun.JobRunStatus}" Value="Aborted">
                                                <Setter Property="Kind" Value="Alert"></Setter>
                                                <Setter Property="Foreground" Value="{StaticResource OrangeBrush}"></Setter>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding JobRun.JobRunStatus}" Value="Failed">
                                                <Setter Property="Kind" Value="AlertOctagon"></Setter>
                                                <Setter Property="Foreground" Value="{StaticResource RedBrush100}"></Setter>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </materialDesign:PackIcon.Style>
                            </materialDesign:PackIcon>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="ID" Binding="{Binding JobRun.ID}" Width="50" ElementStyle="{StaticResource WrapText}"/>
                <DataGridTextColumn Header="Machine" Binding="{Binding Machine.SerialNumber}" Width="100" ></DataGridTextColumn>
                <DataGridTextColumn Header="User" Binding="{Binding User.Contact.FullName}" Width="100" ElementStyle="{StaticResource WrapText}" />
                <DataGridTextColumn Header="Job Name" Binding="{Binding JobRun.JobName}" Width="100" ElementStyle="{StaticResource WrapText}"/>
                <DataGridTextColumn Header="Thread" Binding="{Binding Rml.Name}" Width="80" ElementStyle="{StaticResource WrapText}"/>
                <DataGridTextColumn Header="Length" Binding="{Binding JobRun.JobLength, StringFormat={}{0:0.00}}" Width="60" />
                <DataGridTextColumn Header="Source" Binding="{Binding JobRun.Source, Converter={StaticResource EnumToDescriptionConverter}}" Width="60" />
                <DataGridTextColumn Header="Upload &#10;Duration" Binding="{Binding UploadDuration, Converter={StaticResource DateTimeToStringFormatConverter}, FallbackValue='N/A',TargetNullValue='N/A'}" Width="80"/>
                <DataGridTextColumn Header="Heating &#10;Duration" Binding="{Binding HeatingDuration, Converter={StaticResource DateTimeToStringFormatConverter}, FallbackValue='N/A',TargetNullValue='N/A'}" Width="80" />
                <DataGridTextColumn Header="Start Time" Binding="{Binding JobRun.ActualStartDate, Converter={StaticResource DateTimeUTCToShortDateTimeConverter}}" Width="95" />
                <DataGridTextColumn Header="IsGradient" Binding="{Binding JobRun.IsGradient}" Width="70" />
                <DataGridTextColumn Header="GR" Binding="{Binding JobRun.GradientResolutionCm}" Width="30" />
                <DataGridTextColumn Header="Status" Binding="{Binding JobRun.JobRunStatus, Converter={StaticResource EnumToDescriptionConverter}}" Width="70"/>
                <DataGridTextColumn Header="End Time" Binding="{Binding JobRun.EndDate, Converter={StaticResource DateTimeUTCToShortDateTimeConverter}}" Width="95" />
                <DataGridTextColumn Header="End &#10;Position" Binding="{Binding JobRun.EndPosition, StringFormat={}{0:0.00}}" Width="70" />
                <DataGridTemplateColumn Header="Liquid Quantities" Width="1*">
                    <DataGridTemplateColumn.CellStyle>
                        <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
                            <Setter Property="BorderThickness" Value="0"/>
                            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                            <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                            <Setter Property="HorizontalAlignment" Value="Stretch"/>
                            <Setter Property="Margin" Value="0 0 10 0"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                                        <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Background" Value="Transparent"></Setter>
                                    <Setter Property="Foreground" Value="{StaticResource AccentColorBrush}" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTemplateColumn.CellStyle>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid  HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="1*"/>
                                    <RowDefinition Height="20"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="1*" MaxWidth="200"/>
                                </Grid.ColumnDefinitions>
                                <ItemsControl ItemsSource="{Binding JobRun.LiquidQuantitiesFast,Mode=OneWay}" Margin="0 0 0 0" Height="70">
                                <ItemsControl.ItemContainerStyle>
                                    <Style TargetType="ContentPresenter">
                                        <Setter Property="Visibility" Value="Visible"></Setter>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Quantity}" Value="0">
                                                <Setter Property="Visibility" Value="Collapsed"></Setter>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </ItemsControl.ItemContainerStyle>
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" IsItemsHost="True"></StackPanel>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <DockPanel  ToolTip="{Binding Quantity}" ToolTipService.Placement="Center" ToolTipService.VerticalOffset="10">
                                                <Grid DockPanel.Dock="Top" Height="40" Margin="0 5 8 0" Width="16">
                                                    <Border x:Name="LiquidTypeBorder"  BorderThickness="1" BorderBrush="{StaticResource borderBrush}"  CornerRadius="2">
                                                        <Canvas x:Name="LiquidCanvas" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0" ClipToBounds="True" ToolTip="{Binding Quantity}" ToolTipService.Placement="Left" ToolTipService.VerticalOffset="10">
                                                            <Border  Height="{Binding RelativeSource={RelativeSource AncestorType=Canvas}, Path=ActualHeight }" Width="{Binding RelativeSource={RelativeSource AncestorType=Canvas}, Path=ActualWidth }">
                                                                <Border.Background>
                                                                    <SolidColorBrush Color="{Binding LiquidType,Converter={StaticResource LiquidTypeToColorConverter}}" />
                                                                </Border.Background>
                                                                <Border.Style>
                                                                    <Style>
                                                                        <Setter Property="Canvas.Top" >
                                                                            <Setter.Value>
                                                                                <MultiBinding Converter="{StaticResource MidTankLevelToElementHeightConverter}">
                                                                                    <Binding RelativeSource="{RelativeSource Self}" Path="ActualHeight" />
                                                                                    <Binding Path="Quantity" />
                                                                                </MultiBinding>
                                                                            </Setter.Value>
                                                                        </Setter>
                                                                    </Style>
                                                                </Border.Style>
                                                            </Border>
                                                        </Canvas>
                                                    </Border>
                                                </Grid>
                                                <TextBlock DockPanel.Dock="Bottom" FontSize="9" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0 0 10 0" Text="{Binding LiquidType,Converter={StaticResource StringToFirstLetterConverter}}" Foreground="Gray"/>
                                            </DockPanel>

                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                                <Border Grid.Row="1" Grid.Column="0" Background="Transparent" HorizontalAlignment="Stretch" Height="8" BorderThickness="1" BorderBrush="{StaticResource borderBrush}">
                                    <Rectangle Margin="0 0 -1 0" Fill="{StaticResource AccentColorBrush}" HorizontalAlignment="Left" VerticalAlignment="Stretch" ToolTip="{Binding JobRun.EndPosition}">
                                        <Rectangle.Width>
                                            <MultiBinding Converter="{StaticResource JobLengthConverter}">
                                                <Binding Path="JobRun.JobLength" />
                                                <Binding Path="JobRun.EndPosition"/>
                                                <Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType=Border}"/>
                                            </MultiBinding>
                                        </Rectangle.Width>
                                    </Rectangle>
                                </Border>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Border Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Margin="20, 0, 20, 20" BorderBrush="{StaticResource borderBrush}" Background="{StaticResource TransparentBackgroundBrush}" BorderThickness="1">
            
            <StackPanel>
                <TextBlock Margin="10" Text="{Binding SelectedJobName}"></TextBlock>
            </StackPanel>
        </Border>
        <Border Grid.Row="2" Margin="20, 0, 0, 20" Height="200" BorderBrush="{StaticResource borderBrush}" Background="{StaticResource TransparentBackgroundBrush}" BorderThickness="1">
            <Grid  >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="2"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="2"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Border Grid.Column="0" BorderBrush="{StaticResource borderBrush}" Padding="5" BorderThickness="1" CornerRadius="5" Margin="10">
                    <StackPanel Orientation="Vertical" Margin="5">
                        <ItemsControl ItemsSource="{Binding StatisticsValueCollection.StatisticsCollection}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" Margin="0 3">
                                        <TextBlock Text="{Binding Name}" FontWeight="SemiBold"/>
                                        <TextBlock Text=": "/>
                                        <TextBlock Text="{Binding Value, StringFormat={}{0:#,0.##}}"/>
                                        <TextBlock Text="{Binding Unit}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                        <TextBlock Visibility="Collapsed" Margin="0 10 0 0" Text="Total Thread Consumption per thread:" FontWeight="SemiBold">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Visibility" Value="Visible" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding StatisticsValueCollection.ThreadConsumptionPerThreadCollection.Count}" Value="0">
                                            <Setter Property="Visibility" Value="Collapsed" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                        <ItemsControl Visibility="Collapsed" ItemsSource="{Binding StatisticsValueCollection.ThreadConsumptionPerThreadCollection}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <UniformGrid Rows="3" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.Resources>
                                <DataTemplate DataType="{x:Type model:StatisticsValue}">
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Name}" FontWeight="SemiBold"/>
                                        <TextBlock Text=": "/>
                                        <TextBlock Text="{Binding Value, StringFormat={}{0:0.0}}"/>
                                        <TextBlock Text="{Binding Unit}"/>
                                    </StackPanel>
                                </DataTemplate>
                                <DataTemplate DataType="{x:Type model:MoreValue}">
                                    <Border BorderThickness="0">
                                        <TextBlock Text="{Binding Text}" Tag="{Binding Path=DataContext.StatisticsValueCollection.ThreadConsumptionPerThread, RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType={x:Type ItemsControl}}}"   HorizontalAlignment="Left"   VerticalAlignment="Center" >
                                            <TextBlock.ToolTip >
                                                <ToolTip DataContext="{Binding RelativeSource={RelativeSource Self},Path=PlacementTarget.Tag}" Placement="Right" Background="{StaticResource Logging.Background}">
                                                    <Border CornerRadius="5">
                                                        <ItemsControl  ItemsSource="{Binding }">
                                                            <ItemsControl.ItemTemplate>
                                                                <DataTemplate>
                                                                    <StackPanel Orientation="Horizontal" Margin="2">
                                                                        <TextBlock Margin="5 0 0 0" Text="{Binding Name}" FontWeight="SemiBold"/>
                                                                        <TextBlock Text=": "/>
                                                                        <TextBlock Text="{Binding Value, StringFormat={}{0:0.0}}"/>
                                                                        <TextBlock Text="{Binding Unit}"/>
                                                                    </StackPanel>
                                                                </DataTemplate>
                                                            </ItemsControl.ItemTemplate>
                                                        </ItemsControl>
                                                    </Border>
                                                </ToolTip>
                                            </TextBlock.ToolTip>
                                        </TextBlock>
                                    </Border>
                                </DataTemplate>
                            </ItemsControl.Resources>
                        </ItemsControl>
                    </StackPanel>
                </Border>
                <Border Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Stretch" Width="2" Background="{StaticResource borderBrush}" Margin="0 4" Visibility="Visible"></Border>
                <Border Grid.Column="2" BorderBrush="{StaticResource borderBrush}" Padding="5" BorderThickness="1" CornerRadius="5" Margin="10">
                    <DockPanel>
                        <UniformGrid Columns="3">
                            <DockPanel>
                                <TextBlock DockPanel.Dock="Top" FontSize="12" FontWeight="Bold" HorizontalAlignment="Center" Padding="2" Text="PPC/MS"></TextBlock>
                                <lvc:PieChart  Series="{Binding StatisticsValueCollection.PieJobSource.SeriesCollection}" >
                                    <lvc:PieChart.DataTooltip>
                                        <tooltips:PieChartTooltipControl Visibility="Visible" />
                                    </lvc:PieChart.DataTooltip>
                                </lvc:PieChart>
                            </DockPanel>
                            <DockPanel>
                                <TextBlock DockPanel.Dock="Top" FontSize="12" FontWeight="Bold" HorizontalAlignment="Center" Padding="2" Text="Failed/Aborted/Completed"></TextBlock>
                                <lvc:PieChart  Series="{Binding StatisticsValueCollection.PieJobRunStatus.SeriesCollection}">
                                    <lvc:PieChart.DataTooltip>
                                        <tooltips:PieChartTooltipControl Visibility="Visible" />
                                    </lvc:PieChart.DataTooltip>
                                </lvc:PieChart>
                            </DockPanel>
                            <DockPanel>
                                <TextBlock DockPanel.Dock="Top" FontSize="12" FontWeight="Bold" HorizontalAlignment="Center" Padding="2" Text="Gradient/Solid"></TextBlock>
                                <lvc:PieChart  Series="{Binding StatisticsValueCollection.PieGradientSolid.SeriesCollection}"  >
                                    <lvc:PieChart.DataTooltip>
                                        <tooltips:PieChartTooltipControl Visibility="Visible" />
                                    </lvc:PieChart.DataTooltip>
                                </lvc:PieChart>
                            </DockPanel>
                        </UniformGrid>
                    </DockPanel>
                </Border>

                <Border Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Stretch" Width="2" Background="{StaticResource borderBrush}" Margin="0 6" CornerRadius="1"/>

                <Border Grid.Column="4" BorderBrush="{StaticResource borderBrush}" Padding="5" BorderThickness="1" CornerRadius="5" Margin="10">
                    <DockPanel>
                        <ItemsControl DockPanel.Dock="Top" ItemsSource="{Binding StatisticsValueCollection.LiquidQuantities}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                    <UniformGrid Columns="3"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                    <StackPanel Orientation="Horizontal" Margin="4">
                                        <Ellipse Width="30" Height="30">
                                            <Ellipse.Fill>
                                                <SolidColorBrush Color="{Binding LiquidType,Converter={StaticResource LiquidTypeToColorConverter}}" />
                                            </Ellipse.Fill>
                                        </Ellipse>
                                        <StackPanel Orientation="Vertical" Margin="4">
                                            <TextBlock Text="{Binding LiquidType,Converter={StaticResource EnumToDescriptionConverter}}" FontWeight="SemiBold"></TextBlock>
                                            <TextBlock >
                                             <Run Text="{Binding Quantity, Converter={StaticResource NanoLiterToLiterFormatConverter}}"></Run>
                                            <Run Text=" liters"></Run>
                                            </TextBlock>
                                        </StackPanel>
                                    </StackPanel>
                                </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        </ItemsControl>
                        <TextBlock DockPanel.Dock="Bottom" Margin="4 10 0 0 ">
                            <Run Text="Total liquid quantities for all: " FontWeight="SemiBold"/>
                            <Run Text="{Binding StatisticsValueCollection.TotalLiquidQuantities, Converter={StaticResource NanoLiterToLiterFormatConverter}}"></Run>
                            <Run Text=" liters"></Run>
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Visibility" Value="Visible" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding StatisticsValueCollection.LiquidQuantities.Count}" Value="0">
                                            <Setter Property="Visibility" Value="Collapsed" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </DockPanel>
                </Border>
            </Grid>
        </Border>
    </Grid>
</UserControl>
class="w"> int lenX, lenY; if (dy >= 0) { lenY = dy; } else { lenY = -dy; } if (dx >= 0) { lenX = dx; } else { lenX = -dx; } if (lenX > lenY) { // x increases by +/- 1 if (dx < 0) { int t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; } // Init steps and start int incy = (dy << PRECISION_SHIFT) / dx; int y1s = y1 << PRECISION_SHIFT; int y2s = y2 << PRECISION_SHIFT; int hs = pixelHeight << PRECISION_SHIFT; if (y1 < y2) { if (y1 >= pixelHeight || y2 < 0) { return; } if (y1s < 0) { if (incy == 0) { return; } int oldy1s = y1s; // Find lowest y1s that is greater or equal than 0. y1s = incy - 1 + ((y1s + 1) % incy); x1 += (y1s - oldy1s) / incy; } if (y2s >= hs) { if (incy != 0) { // Find highest y2s that is less or equal than ws - 1. // y2s = y1s + n * incy. Find n. y2s = hs - 1 - (hs - 1 - y1s) % incy; x2 = x1 + (y2s - y1s) / incy; } } } else { if (y2 >= pixelHeight || y1 < 0) { return; } if (y1s >= hs) { if (incy == 0) { return; } int oldy1s = y1s; // Find highest y1s that is less or equal than ws - 1. // y1s = oldy1s + n * incy. Find n. y1s = hs - 1 + (incy - (hs - 1 - oldy1s) % incy); x1 += (y1s - oldy1s) / incy; } if (y2s < 0) { if (incy != 0) { // Find lowest y2s that is greater or equal than 0. // y2s = y1s + n * incy. Find n. y2s = y1s % incy; x2 = x1 + (y2s - y1s) / incy; } } } if (x1 < 0) { y1s -= incy * x1; x1 = 0; } if (x2 >= pixelWidth) { x2 = pixelWidth - 1; } int ys = y1s; // Walk the line! int y = ys >> PRECISION_SHIFT; int previousY = y; int index = x1 + y * pixelWidth; int k = incy < 0 ? 1 - pixelWidth : 1 + pixelWidth; for (int x = x1; x <= x2; ++x) { pixels[index] = color; ys += incy; y = ys >> PRECISION_SHIFT; if (y != previousY) { previousY = y; index += k; } else { ++index; } } } else { // Prevent division by zero if (lenY == 0) { return; } if (dy < 0) { int t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; } // Init steps and start int x1s = x1 << PRECISION_SHIFT; int x2s = x2 << PRECISION_SHIFT; int ws = pixelWidth << PRECISION_SHIFT; int incx = (dx << PRECISION_SHIFT) / dy; if (x1 < x2) { if (x1 >= pixelWidth || x2 < 0) { return; } if (x1s < 0) { if (incx == 0) { return; } int oldx1s = x1s; // Find lowest x1s that is greater or equal than 0. x1s = incx - 1 + ((x1s + 1) % incx); y1 += (x1s - oldx1s) / incx; } if (x2s >= ws) { if (incx != 0) { // Find highest x2s that is less or equal than ws - 1. // x2s = x1s + n * incx. Find n. x2s = ws - 1 - (ws - 1 - x1s) % incx; y2 = y1 + (x2s - x1s) / incx; } } } else { if (x2 >= pixelWidth || x1 < 0) { return; } if (x1s >= ws) { if (incx == 0) { return; } int oldx1s = x1s; // Find highest x1s that is less or equal than ws - 1. // x1s = oldx1s + n * incx. Find n. x1s = ws - 1 + (incx - (ws - 1 - oldx1s) % incx); y1 += (x1s - oldx1s) / incx; } if (x2s < 0) { if (incx != 0) { // Find lowest x2s that is greater or equal than 0. // x2s = x1s + n * incx. Find n. x2s = x1s % incx; y2 = y1 + (x2s - x1s) / incx; } } } if (y1 < 0) { x1s -= incx * y1; y1 = 0; } if (y2 >= pixelHeight) { y2 = pixelHeight - 1; } int index = x1s; int indexBaseValue = y1 * pixelWidth; // Walk the line! var inc = (pixelWidth << PRECISION_SHIFT) + incx; for (int y = y1; y <= y2; ++y) { pixels[indexBaseValue + (index >> PRECISION_SHIFT)] = color; index += inc; } } } #endregion #region Penned line /// <summary> /// Bitfields used to partition the space into 9 regions /// </summary> private const byte INSIDE = 0; // 0000 private const byte LEFT = 1; // 0001 private const byte RIGHT = 2; // 0010 private const byte BOTTOM = 4; // 0100 private const byte TOP = 8; // 1000 /// <summary> /// Draws a line using a pen / stamp for the line /// </summary> /// <param name="bmp">The WriteableBitmap containing the pixels as int RGBA value.</param> /// <param name="w">The width of one scanline in the pixels array.</param> /// <param name="h">The height of the bitmap.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="penBmp">The pen bitmap.</param> internal static void DrawLinePenned(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, WriteableBitmap penBmp) { using (var context = bmp.GetBitmapContext()) { using (var penContext = penBmp.GetBitmapContext()) { DrawLinePenned(context, bmp.PixelWidth, bmp.PixelHeight, x1, y1, x2, y2, penContext); } } } /// <summary> /// Draws a line using a pen / stamp for the line /// </summary> /// <param name="context">The context containing the pixels as int RGBA value.</param> /// <param name="w">The width of one scanline in the pixels array.</param> /// <param name="h">The height of the bitmap.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="pen">The pen context.</param> internal static void DrawLinePenned(BitmapContext context, int w, int h, int x1, int y1, int x2, int y2, BitmapContext pen) { // Edge case where lines that went out of vertical bounds clipped instead of disappearing if((y1 < 0 && y2 < 0) || (y1 > h && y2 > h)) return; if (x1 == x2 && y1 == y2) return; // Perform cohen-sutherland clipping if either point is out of the viewport if (!CohenSutherlandLineClip(new Rect(0, 0, w, h), ref x1, ref y1, ref x2, ref y2)) return; int size = pen.WriteableBitmap.PixelWidth; int pw = size; var srcRect = new Rect(0, 0, size, size); // Distance start and end point int dx = x2 - x1; int dy = y2 - y1; // Determine sign for direction x int incx = 0; if (dx < 0) { dx = -dx; incx = -1; } else if (dx > 0) { incx = 1; } // Determine sign for direction y int incy = 0; if (dy < 0) { dy = -dy; incy = -1; } else if (dy > 0) { incy = 1; } // Which gradient is larger int pdx, pdy, odx, ody, es, el; if (dx > dy) { pdx = incx; pdy = 0; odx = incx; ody = incy; es = dy; el = dx; } else { pdx = 0; pdy = incy; odx = incx; ody = incy; es = dx; el = dy; } // Init start int x = x1; int y = y1; int error = el >> 1; var destRect = new Rect(x, y, size, size); if (y < h && y >= 0 && x < w && x >= 0) { //Blit(context.WriteableBitmap, new Rect(x,y,3,3), pen.WriteableBitmap, new Rect(0,0,3,3)); Blit(context, w, h, destRect, pen, srcRect, pw); //pixels[y * w + x] = color; } // Walk the line! for (int i = 0; i < el; i++) { // Update error term error -= es; // Decide which coord to use if (error < 0) { error += el; x += odx; y += ody; } else { x += pdx; y += pdy; } // Set pixel if (y < h && y >= 0 && x < w && x >= 0) { //Blit(context, w, h, destRect, pen, srcRect, pw); Blit(context, w, h, new Rect(x, y, size, size), pen, srcRect, pw); //Blit(context.WriteableBitmap, destRect, pen.WriteableBitmap, srcRect); //pixels[y * w + x] = color; } } } /// <summary> /// Compute the bit code for a point (x, y) using the clip rectangle /// bounded diagonally by (xmin, ymin), and (xmax, ymax) /// ASSUME THAT xmax , xmin , ymax and ymin are global constants. /// </summary> /// <param name="extents">The extents.</param> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <returns></returns> private static byte ComputeOutCode(Rect extents, double x, double y) { // initialized as being inside of clip window byte code = INSIDE; if (x < extents.Left) // to the left of clip window code |= LEFT; else if (x > extents.Right) // to the right of clip window code |= RIGHT; if (y > extents.Bottom) // below the clip window code |= BOTTOM; else if (y < extents.Top) // above the clip window code |= TOP; return code; } #endregion #region Anti-alias line /// <summary> /// Draws an anti-aliased, alpha blended, colored line by connecting two points using Wu's antialiasing algorithm /// Uses the pixels array and the width directly for best performance. /// </summary> /// <param name="bmp">The WriteableBitmap.</param> /// <param name="x1">The x0.</param> /// <param name="y1">The y0.</param> /// <param name="x2">The x1.</param> /// <param name="y2">The y1.</param> /// <param name="sa">Alpha color component</param> /// <param name="sr">Premultiplied red color component</param> /// <param name="sg">Premultiplied green color component</param> /// <param name="sb">Premultiplied blue color component</param> internal static void DrawLineWu(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int sa, int sr, int sg, int sb) { using (var context = bmp.GetBitmapContext()) { DrawLineWu(context, bmp.PixelWidth, bmp.PixelHeight, x1, y1, x2, y2, sa, sr, sg, sb); } } /// <summary> /// Draws an anti-aliased, alpha-blended, colored line by connecting two points using Wu's antialiasing algorithm /// Uses the pixels array and the width directly for best performance. /// </summary> /// <param name="context">An array containing the pixels as int RGBA value.</param> /// <param name="pixelWidth">The width of one scanline in the pixels array.</param> /// <param name="pixelHeight">The height of the bitmap.</param> /// <param name="x1">The x0.</param> /// <param name="y1">The y0.</param> /// <param name="x2">The x1.</param> /// <param name="y2">The y1.</param> /// <param name="sa">Alpha color component</param> /// <param name="sr">Premultiplied red color component</param> /// <param name="sg">Premultiplied green color component</param> /// <param name="sb">Premultiplied blue color component</param> internal static void DrawLineWu(BitmapContext context, int pixelWidth, int pixelHeight, int x1, int y1, int x2, int y2, int sa, int sr, int sg, int sb) { // Perform cohen-sutherland clipping if either point is out of the viewport if (!CohenSutherlandLineClip(new Rect(0, 0, pixelWidth, pixelHeight), ref x1, ref y1, ref x2, ref y2)) return; var pixels = context.Pixels; const ushort INTENSITY_BITS = 8; const short NUM_LEVELS = 1 << INTENSITY_BITS; // 256 // mask used to compute 1-value by doing (value XOR mask) const ushort WEIGHT_COMPLEMENT_MASK = NUM_LEVELS - 1; // 255 // # of bits by which to shift ErrorAcc to get intensity level const ushort INTENSITY_SHIFT = (ushort)(16 - INTENSITY_BITS); // 8 ushort ErrorAdj, ErrorAcc; ushort ErrorAccTemp, Weighting; short DeltaX, DeltaY, XDir; int tmp; // ensure line runs from top to bottom if (y1 > y2) { tmp = y1; y1 = y2; y2 = tmp; tmp = x1; x1 = x2; x2 = tmp; } // draw initial pixel, which is always intersected by line to it's at 100% intensity pixels[y1 * pixelWidth + x1] = AlphaBlend(sa, sr, sg, sb, pixels[y1 * pixelWidth + x1]); //bitmap.SetPixel(X0, Y0, BaseColor); DeltaX = (short)(x2 - x1); if (DeltaX >= 0) { XDir = 1; } else { XDir = -1; DeltaX = (short)-DeltaX; /* make DeltaX positive */ } // Special-case horizontal, vertical, and diagonal lines, which // require no weighting because they go right through the center of // every pixel; also avoids division by zero later DeltaY = (short)(y2 - y1); if (DeltaY == 0) // if horizontal line { while (DeltaX-- != 0) { x1 += XDir; pixels[y1 * pixelWidth + x1] = AlphaBlend(sa, sr, sg, sb, pixels[y1 * pixelWidth + x1]); } return; } if (DeltaX == 0) // if vertical line { do { y1++; pixels[y1 * pixelWidth + x1] = AlphaBlend(sa, sr, sg, sb, pixels[y1 * pixelWidth + x1]); } while (--DeltaY != 0); return; } if (DeltaX == DeltaY) // diagonal line { do { x1 += XDir; y1++; pixels[y1 * pixelWidth + x1] = AlphaBlend(sa, sr, sg, sb, pixels[y1 * pixelWidth + x1]); } while (--DeltaY != 0); return; } // Line is not horizontal, diagonal, or vertical ErrorAcc = 0; // initialize the line error accumulator to 0 // Is this an X-major or Y-major line? if (DeltaY > DeltaX) { // Y-major line; calculate 16-bit fixed-point fractional part of a // pixel that X advances each time Y advances 1 pixel, truncating the // result so that we won't overrun the endpoint along the X axis ErrorAdj = (ushort)(((ulong)DeltaX << 16) / (ulong)DeltaY); // Draw all pixels other than the first and last while (--DeltaY != 0) { ErrorAccTemp = ErrorAcc; // remember current accumulated error ErrorAcc += ErrorAdj; // calculate error for next pixel if (ErrorAcc <= ErrorAccTemp) { // The error accumulator turned over, so advance the X coord */ x1 += XDir; } y1++; /* Y-major, so always advance Y */ // The IntensityBits most significant bits of ErrorAcc give us the // intensity weighting for this pixel, and the complement of the // weighting for the paired pixel Weighting = (ushort)(ErrorAcc >> INTENSITY_SHIFT); int weight = Weighting ^ WEIGHT_COMPLEMENT_MASK; pixels[y1 * pixelWidth + x1] = AlphaBlend(sa, (sr * weight) >> 8, (sg * weight) >> 8, (sb * weight) >> 8, pixels[y1 * pixelWidth + x1]); weight = Weighting; pixels[y1 * pixelWidth + x1 + XDir] = AlphaBlend(sa, (sr * weight) >> 8, (sg * weight) >> 8, (sb * weight) >> 8, pixels[y1 * pixelWidth + x1 + XDir]); //bitmap.SetPixel(X0, Y0, 255 - (BaseColor + Weighting)); //bitmap.SetPixel(X0 + XDir, Y0, 255 - (BaseColor + (Weighting ^ WeightingComplementMask))); } // Draw the final pixel, which is always exactly intersected by the line and so needs no weighting pixels[y2 * pixelWidth + x2] = AlphaBlend(sa, sr, sg, sb, pixels[y2 * pixelWidth + x2]); //bitmap.SetPixel(X1, Y1, BaseColor); return; } // It's an X-major line; calculate 16-bit fixed-point fractional part of a // pixel that Y advances each time X advances 1 pixel, truncating the // result to avoid overrunning the endpoint along the X axis */ ErrorAdj = (ushort)(((ulong)DeltaY << 16) / (ulong)DeltaX); // Draw all pixels other than the first and last while (--DeltaX != 0) { ErrorAccTemp = ErrorAcc; // remember current accumulated error ErrorAcc += ErrorAdj; // calculate error for next pixel if (ErrorAcc <= ErrorAccTemp) // if error accumulator turned over { // advance the Y coord y1++; } x1 += XDir; // X-major, so always advance X // The IntensityBits most significant bits of ErrorAcc give us the // intensity weighting for this pixel, and the complement of the // weighting for the paired pixel Weighting = (ushort)(ErrorAcc >> INTENSITY_SHIFT); int weight = Weighting ^ WEIGHT_COMPLEMENT_MASK; pixels[y1 * pixelWidth + x1] = AlphaBlend(sa, (sr * weight) >> 8, (sg * weight) >> 8, (sb * weight) >> 8, pixels[y1 * pixelWidth + x1]); weight = Weighting; pixels[(y1 + 1) * pixelWidth + x1] = AlphaBlend(sa, (sr * weight) >> 8, (sg * weight) >> 8, (sb * weight) >> 8, pixels[(y1 + 1) * pixelWidth + x1]); //bitmap.SetPixel(X0, Y0, 255 - (BaseColor + Weighting)); //bitmap.SetPixel(X0, Y0 + 1, // 255 - (BaseColor + (Weighting ^ WeightingComplementMask))); } // Draw the final pixel, which is always exactly intersected by the line and thus needs no weighting pixels[y2 * pixelWidth + x2] = AlphaBlend(sa, sr, sg, sb, pixels[y2 * pixelWidth + x2]); //bitmap.SetPixel(X1, Y1, BaseColor); } /// <summary> /// Draws an anti-aliased line with a desired stroke thickness /// <param name="context">The context containing the pixels as int RGBA value.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="color">The color for the line.</param> /// <param name="strokeThickness">The stroke thickness of the line.</param> /// </summary> internal static void DrawLineAa(BitmapContext context, int pixelWidth, int pixelHeight, int x1, int y1, int x2, int y2, int color, int strokeThickness) { AAWidthLine(pixelWidth, pixelHeight, context, x1, y1, x2, y2, strokeThickness, color); } /// <summary> /// Draws an anti-aliased line with a desired stroke thickness /// <param name="bmp">The WriteableBitmap.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="color">The color for the line.</param> /// <param name="strokeThickness">The stroke thickness of the line.</param> /// </summary> internal static void DrawLineAa(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color, int strokeThickness) { using (var context = bmp.GetBitmapContext()) { AAWidthLine(bmp.PixelWidth, bmp.PixelHeight, context, x1, y1, x2, y2, strokeThickness, color); } } /// <summary> /// Draws an anti-aliased line with a desired stroke thickness /// <param name="context">The context containing the pixels as int RGBA value.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="color">The color for the line.</param> /// <param name="strokeThickness">The stroke thickness of the line.</param> /// </summary> internal static void DrawLineAa(BitmapContext context, int pixelWidth, int pixelHeight, int x1, int y1, int x2, int y2, Color color, int strokeThickness) { var col = ConvertColor(color); AAWidthLine(pixelWidth, pixelHeight, context, x1, y1, x2, y2, strokeThickness, col); } /// <summary> /// Draws an anti-aliased line with a desired stroke thickness /// <param name="bmp">The WriteableBitmap.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="color">The color for the line.</param> /// <param name="strokeThickness">The stroke thickness of the line.</param> /// </summary> internal static void DrawLineAa(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color, int strokeThickness) { var col = ConvertColor(color); using (var context = bmp.GetBitmapContext()) { AAWidthLine(bmp.PixelWidth, bmp.PixelHeight, context, x1, y1, x2, y2, strokeThickness, col); } } /// <summary> /// Draws an anti-aliased line, using an optimized version of Gupta-Sproull algorithm /// From http://nokola.com/blog/post/2010/10/14/Anti-aliased-Lines-And-Optimizing-Code-for-Windows-Phone-7e28093First-Look.aspx /// <param name="bmp">The WriteableBitmap.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="color">The color for the line.</param> /// </summary> internal static void DrawLineAa(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color) { var col = ConvertColor(color); bmp.DrawLineAa(x1, y1, x2, y2, col); } /// <summary> /// Draws an anti-aliased line, using an optimized version of Gupta-Sproull algorithm /// From http://nokola.com/blog/post/2010/10/14/Anti-aliased-Lines-And-Optimizing-Code-for-Windows-Phone-7e28093First-Look.aspx /// <param name="bmp">The WriteableBitmap.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="color">The color for the line.</param> /// </summary> internal static void DrawLineAa(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color) { using (var context = bmp.GetBitmapContext()) { DrawLineAa(context, context.Width, context.Height, x1, y1, x2, y2, color); } } /// <summary> /// Draws an anti-aliased line, using an optimized version of Gupta-Sproull algorithm /// From http://nokola.com/blog/post/2010/10/14/Anti-aliased-Lines-And-Optimizing-Code-for-Windows-Phone-7e28093First-Look.aspx /// <param name="context">The context containing the pixels as int RGBA value.</param> /// <param name="pixelWidth">The width of one scanline in the pixels array.</param> /// <param name="pixelHeight">The height of the bitmap.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="color">The color for the line.</param> /// </summary> internal static void DrawLineAa(BitmapContext context, int pixelWidth, int pixelHeight, int x1, int y1, int x2, int y2, int color) { if ((x1 == x2) && (y1 == y2)) return; // edge case causing invDFloat to overflow, found by Shai Rubinshtein // Perform cohen-sutherland clipping if either point is out of the viewport if (!CohenSutherlandLineClip(new Rect(0, 0, pixelWidth, pixelHeight), ref x1, ref y1, ref x2, ref y2)) return; if (x1 < 1) x1 = 1; if (x1 > pixelWidth - 2) x1 = pixelWidth - 2; if (y1 < 1) y1 = 1; if (y1 > pixelHeight - 2) y1 = pixelHeight - 2; if (x2 < 1) x2 = 1; if (x2 > pixelWidth - 2) x2 = pixelWidth - 2; if (y2 < 1) y2 = 1; if (y2 > pixelHeight - 2) y2 = pixelHeight - 2; var addr = y1 * pixelWidth + x1; var dx = x2 - x1; var dy = y2 - y1; int du; int dv; int u; int v; int uincr; int vincr; // Extract color var a = (color >> 24) & 0xFF; var srb = (uint)(color & 0x00FF00FF); var sg = (uint)((color >> 8) & 0xFF); // By switching to (u,v), we combine all eight octants int adx = dx, ady = dy; if (dx < 0) adx = -dx; if (dy < 0) ady = -dy; if (adx > ady) { du = adx; dv = ady; u = x2; v = y2; uincr = 1; vincr = pixelWidth; if (dx < 0) uincr = -uincr; if (dy < 0) vincr = -vincr; } else { du = ady; dv = adx; u = y2; v = x2; uincr = pixelWidth; vincr = 1; if (dy < 0) uincr = -uincr; if (dx < 0) vincr = -vincr; } var uend = u + du; var d = (dv << 1) - du; // Initial value as in Bresenham's var incrS = dv << 1; // &#916;d for straight increments var incrD = (dv - du) << 1; // &#916;d for diagonal increments var invDFloat = 1.0 / (4.0 * Math.Sqrt(du * du + dv * dv)); // Precomputed inverse denominator var invD2DuFloat = 0.75 - 2.0 * (du * invDFloat); // Precomputed constant const int PRECISION_SHIFT = 10; // result distance should be from 0 to 1 << PRECISION_SHIFT, mapping to a range of 0..1 const int PRECISION_MULTIPLIER = 1 << PRECISION_SHIFT; var invD = (int)(invDFloat * PRECISION_MULTIPLIER); var invD2Du = (int)(invD2DuFloat * PRECISION_MULTIPLIER * a); var zeroDot75 = (int)(0.75 * PRECISION_MULTIPLIER * a); var invDMulAlpha = invD * a; var duMulInvD = du * invDMulAlpha; // used to help optimize twovdu * invD var dMulInvD = d * invDMulAlpha; // used to help optimize twovdu * invD //int twovdu = 0; // Numerator of distance; starts at 0 var twovduMulInvD = 0; // since twovdu == 0 var incrSMulInvD = incrS * invDMulAlpha; var incrDMulInvD = incrD * invDMulAlpha; do { AlphaBlendNormalOnPremultiplied(context, addr, (zeroDot75 - twovduMulInvD) >> PRECISION_SHIFT, srb, sg); AlphaBlendNormalOnPremultiplied(context, addr + vincr, (invD2Du + twovduMulInvD) >> PRECISION_SHIFT, srb, sg); AlphaBlendNormalOnPremultiplied(context, addr - vincr, (invD2Du - twovduMulInvD) >> PRECISION_SHIFT, srb, sg); if (d < 0) { // choose straight (u direction) twovduMulInvD = dMulInvD + duMulInvD; d += incrS; dMulInvD += incrSMulInvD; } else { // choose diagonal (u+v direction) twovduMulInvD = dMulInvD - duMulInvD; d += incrD; dMulInvD += incrDMulInvD; v++; addr += vincr; } u++; addr += uincr; } while (u < uend); } /// <summary> /// Blends a specific source color on top of a destination premultiplied color /// </summary> /// <param name="context">Array containing destination color</param> /// <param name="index">Index of destination pixel</param> /// <param name="sa">Source alpha (0..255)</param> /// <param name="srb">Source non-premultiplied red and blue component in the format 0x00rr00bb</param> /// <param name="sg">Source green component (0..255)</param> private static void AlphaBlendNormalOnPremultiplied(BitmapContext context, int index, int sa, uint srb, uint sg) { var pixels = context.Pixels; var destPixel = (uint)pixels[index]; var da = (destPixel >> 24); var dg = ((destPixel >> 8) & 0xff); var drb = destPixel & 0x00FF00FF; // blend with high-quality alpha and lower quality but faster 1-off RGBs pixels[index] = (int)( ((sa + ((da * (255 - sa) * 0x8081) >> 23)) << 24) | // alpha (((sg - dg) * sa + (dg << 8)) & 0xFFFFFF00) | // green (((((srb - drb) * sa) >> 8) + drb) & 0x00FF00FF) // red and blue ); } #endregion #region Helper internal static bool CohenSutherlandLineClipWithViewPortOffset(Rect viewPort, ref float xi0, ref float yi0, ref float xi1, ref float yi1, int offset) { var viewPortWithOffset = new Rect(viewPort.X - offset, viewPort.Y - offset, viewPort.Width + 2 * offset, viewPort.Height + 2 * offset); return CohenSutherlandLineClip(viewPortWithOffset, ref xi0, ref yi0, ref xi1, ref yi1); } internal static bool CohenSutherlandLineClip(Rect extents, ref float xi0, ref float yi0, ref float xi1, ref float yi1) { // Fix #SC-1555: Log(0) issue // CohenSuzerland line clipping algorithm returns NaN when point has infinity value double x0 = ClipToInt(xi0); double y0 = ClipToInt(yi0); double x1 = ClipToInt(xi1); double y1 = ClipToInt(yi1); var isValid = CohenSutherlandLineClip(extents, ref x0, ref y0, ref x1, ref y1); // Update the clipped line xi0 = (float)x0; yi0 = (float)y0; xi1 = (float)x1; yi1 = (float)y1; return isValid; } private static float ClipToInt(float d) { if (d > int.MaxValue) return int.MaxValue; if (d < int.MinValue) return int.MinValue; return d; } internal static bool CohenSutherlandLineClip(Rect extents, ref int xi0, ref int yi0, ref int xi1, ref int yi1) { double x0 = xi0; double y0 = yi0; double x1 = xi1; double y1 = yi1; var isValid = CohenSutherlandLineClip(extents, ref x0, ref y0, ref x1, ref y1); // Update the clipped line xi0 = (int)x0; yi0 = (int)y0; xi1 = (int)x1; yi1 = (int)y1; return isValid; } /// <summary> /// Cohen–Sutherland clipping algorithm clips a line from /// P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with /// diagonal from (xmin, ymin) to (xmax, ymax). /// </summary> /// <remarks>See http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm for details</remarks> /// <returns>a list of two points in the resulting clipped line, or zero</returns> internal static bool CohenSutherlandLineClip(Rect extents, ref double x0, ref double y0, ref double x1, ref double y1) { // compute outcodes for P0, P1, and whatever point lies outside the clip rectangle byte outcode0 = ComputeOutCode(extents, x0, y0); byte outcode1 = ComputeOutCode(extents, x1, y1); // No clipping if both points lie inside viewport if (outcode0 == INSIDE && outcode1 == INSIDE) return true; bool isValid = false; while (true) { // Bitwise OR is 0. Trivially accept and get out of loop if ((outcode0 | outcode1) == 0) { isValid = true; break; } // Bitwise AND is not 0. Trivially reject and get out of loop else if ((outcode0 & outcode1) != 0) { break; } else { // failed both tests, so calculate the line segment to clip // from an outside point to an intersection with clip edge double x, y; // At least one endpoint is outside the clip rectangle; pick it. byte outcodeOut = (outcode0 != 0) ? outcode0 : outcode1; // Now find the intersection point; // use formulas y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0) if ((outcodeOut & TOP) != 0) { // point is above the clip rectangle x = x0 + (x1 - x0) * (extents.Top - y0) / (y1 - y0); y = extents.Top; } else if ((outcodeOut & BOTTOM) != 0) { // point is below the clip rectangle x = x0 + (x1 - x0) * (extents.Bottom - y0) / (y1 - y0); y = extents.Bottom; } else if ((outcodeOut & RIGHT) != 0) { // point is to the right of clip rectangle y = y0 + (y1 - y0) * (extents.Right - x0) / (x1 - x0); x = extents.Right; } else if ((outcodeOut & LEFT) != 0) { // point is to the left of clip rectangle y = y0 + (y1 - y0) * (extents.Left - x0) / (x1 - x0); x = extents.Left; } else { x = double.NaN; y = double.NaN; } // Now we move outside point to intersection point to clip // and get ready for next pass. if (outcodeOut == outcode0) { x0 = x; y0 = y; outcode0 = ComputeOutCode(extents, x0, y0); } else { x1 = x; y1 = y; outcode1 = ComputeOutCode(extents, x1, y1); } } } return isValid; } /// <summary> /// Alpha blends 2 premultiplied colors with each other /// </summary> /// <param name="sa">Source alpha color component</param> /// <param name="sr">Premultiplied source red color component</param> /// <param name="sg">Premultiplied source green color component</param> /// <param name="sb">Premultiplied source blue color component</param> /// <param name="destPixel">Premultiplied destination color</param> /// <returns>Premultiplied blended color value</returns> internal static int AlphaBlend(int sa, int sr, int sg, int sb, int destPixel) { int dr, dg, db; int da; da = ((destPixel >> 24) & 0xff); dr = ((destPixel >> 16) & 0xff); dg = ((destPixel >> 8) & 0xff); db = ((destPixel) & 0xff); destPixel = ((sa + (((da * (255 - sa)) * 0x8081) >> 23)) << 24) | ((sr + (((dr * (255 - sa)) * 0x8081) >> 23)) << 16) | ((sg + (((dg * (255 - sa)) * 0x8081) >> 23)) << 8) | ((sb + (((db * (255 - sa)) * 0x8081) >> 23))); return destPixel; } #endregion } }