1
- namespace MyFinance . ViewModels ;
1
+ using LiveChartsCore . SkiaSharpView . Painting . Effects ;
2
+ using LiveChartsCore . SkiaSharpView . Painting ;
3
+ using SkiaSharp ;
4
+ using System . Linq ;
5
+ using LiveChartsCore . Drawing ;
6
+
7
+ namespace MyFinance . ViewModels ;
2
8
3
9
public partial class ChartPageViewModel : BaseViewModel
4
10
{
@@ -10,65 +16,149 @@ public partial class ChartPageViewModel : BaseViewModel
10
16
11
17
[ ObservableProperty ]
12
18
private string totalExpense ;
13
- private readonly Random _random = new ( ) ;
19
+
20
+ [ ObservableProperty ]
21
+ private Axis [ ] xAxes ;
22
+
23
+ [ ObservableProperty ]
24
+ private Axis [ ] yAxes ;
25
+
26
+ [ ObservableProperty ]
27
+ private ISeries [ ] series ;
28
+
29
+ [ ObservableProperty ]
30
+ private ChartType cType ;
31
+
32
+
33
+ private List < OperatonItemsVM > items = new ( ) ;
34
+
14
35
public ChartPageViewModel ( )
15
36
{
37
+ CType = ChartType . Weekly ;
16
38
TotalBalance = "25,291.50 ₺" ;
17
39
TotalExpense = "2,367.82 ₺" ;
18
40
TotalIncome = "167.82 ₺" ;
19
- var trend = 100 ;
20
- var values = new List < int > ( ) ;
21
41
22
- for ( var i = 0 ; i < 100 ; i ++ )
23
- {
24
- trend += _random . Next ( - 30 , 50 ) ;
25
- values . Add ( trend ) ;
26
- }
27
42
28
- Series = new ISeries [ ]
43
+
44
+ Random random = new Random ( ) ;
45
+ for ( int i = 1 ; i <= 100 ; i ++ )
29
46
{
30
- new ColumnSeries < int >
31
- {
32
- Values = values
33
- }
34
- } ;
47
+ var amount = random . Next ( 1 , 10000 ) ;
48
+ items . Add (
49
+ new OperatonItemsVM
50
+ {
51
+ Id = Guid . NewGuid ( ) ,
52
+ Icon = amount % 2 == 0 ? "loss.png" : "profits.png" ,
53
+ Color = amount % 2 == 0 ? Red : Green ,
54
+ Date = DateTime . Now . AddDays ( - ( amount % 7 ) ) . ToString ( "dd.MM.yyyy HH:mm" ) ,
55
+ Title = amount % 2 == 0 ? "Borç ödendi" : "Ödeme Alındı" ,
56
+ Description = amount % 2 == 0 ? "Ödemeler yapıldı" : "Yaka parası alındı." ,
57
+ Amount = $ "{ amount } ₺"
58
+ }
59
+ ) ;
60
+ }
35
61
36
- XAxes = new [ ] { new Axis ( ) } ;
62
+ Calc ( 7 ) ;
37
63
}
38
64
39
- public ISeries [ ] Series { get ; }
40
-
41
- public Axis [ ] XAxes { get ; }
42
65
43
66
[ RelayCommand ]
44
- public void GoToPage1 ( )
67
+ public void ChartTypeChanged ( )
45
68
{
46
- var axis = XAxes [ 0 ] ;
47
- axis . MinLimit = - 0.5 ;
48
- axis . MaxLimit = 10.5 ;
69
+ if ( CType == ChartType . Weekly )
70
+ Calc ( 7 ) ;
71
+ else if ( CType == ChartType . Monthly )
72
+ Calc ( 30 ) ;
73
+ else if ( CType == ChartType . SixMonthly )
74
+ Calc ( 180 ) ;
75
+ else if ( CType == ChartType . Yearly )
76
+ Calc ( 365 ) ;
49
77
}
50
78
51
- [ RelayCommand ]
52
- public void GoToPage2 ( )
79
+ private void Calc ( int days )
53
80
{
54
- var axis = XAxes [ 0 ] ;
55
- axis . MinLimit = - 0.5 ;
56
- axis . MaxLimit = 20.5 ;
57
- }
81
+ var graphVals = items
82
+ . Where ( e => DateTime . Parse ( e . Date ) > DateTime . Now . AddDays ( - days ) )
83
+ . Select ( e => new OperationGraphVM { Date = DateTime . Parse ( e . Date ) . Date , Amount = double . Parse ( e . Amount . Trim ( ) . Trim ( '₺' ) ) , IsIncome = e . Color == Green } )
84
+ . ToList ( ) ;
58
85
59
- [ RelayCommand ]
60
- public void GoToPage3 ( )
61
- {
62
- var axis = XAxes [ 0 ] ;
63
- axis . MinLimit = - 0.5 ;
64
- axis . MaxLimit = 30.5 ;
65
- }
86
+ var strokeThickness = 6 ;
87
+ var strokeDashArray = new float [ ] { 3 * strokeThickness , 2 * strokeThickness } ;
88
+ var effect = new DashEffect ( strokeDashArray ) ;
66
89
67
- [ RelayCommand ]
68
- public void SeeAll ( )
69
- {
70
- var axis = XAxes [ 0 ] ;
71
- axis . MinLimit = null ;
72
- axis . MaxLimit = null ;
90
+ var inVals = graphVals
91
+ . Where ( x => x . IsIncome )
92
+ . GroupBy ( e => e . Date )
93
+ . OrderBy ( g => g . Key . Year )
94
+ . ThenBy ( g => g . Key . Month )
95
+ . ThenBy ( g => g . Key . Day )
96
+ . Select ( e => new OperationGraphVM { Date = e . Key , Amount = e . Sum ( x => x . Amount ) , IsIncome = true } )
97
+ . ToList ( ) ;
98
+
99
+ var outVals = graphVals
100
+ . Where ( x => ! x . IsIncome )
101
+ . GroupBy ( e => e . Date )
102
+ . OrderBy ( g => g . Key . Year )
103
+ . ThenBy ( g => g . Key . Month )
104
+ . ThenBy ( g => g . Key . Day )
105
+ . Select ( e => new OperationGraphVM { Date = e . Key , Amount = e . Sum ( x => x . Amount ) , IsIncome = false } )
106
+ . ToList ( ) ;
107
+
108
+ var vals = inVals
109
+ . Join ( outVals ,
110
+ i => i . Date ,
111
+ e => e . Date ,
112
+ ( i , e ) => i . Amount - e . Amount )
113
+ . ToList ( ) ;
114
+
115
+ Series = new ISeries [ ]
116
+ {
117
+ new LineSeries < double >
118
+ {
119
+ Values = inVals . Select ( e => e . Amount ) ,
120
+ LineSmoothness = 0 ,
121
+ Fill = null
122
+ } ,
123
+
124
+ new LineSeries < double >
125
+ {
126
+ Values = outVals . Select ( e => e . Amount ) ,
127
+ LineSmoothness = 1 ,
128
+ Fill = null
129
+ } ,
130
+
131
+ new LineSeries < double >
132
+ {
133
+ Values = vals ,
134
+ LineSmoothness = 2
135
+ }
136
+ } ;
137
+
138
+ XAxes = new Axis [ ]
139
+ {
140
+ new Axis
141
+ {
142
+ CrosshairLabelsBackground = SKColors . DarkOrange . AsLvcColor ( ) ,
143
+ CrosshairLabelsPaint = new SolidColorPaint ( SKColors . DarkRed , 1 ) ,
144
+ CrosshairPaint = new SolidColorPaint ( SKColors . DarkOrange , 1 ) ,
145
+ Labels = DateTimeHelper . GetDayNames ( graphVals . GroupBy ( e => e . Date ) . Select ( e => e . Key ) . ToList ( ) ) ,
146
+ LabelsRotation = 90 ,
147
+ NamePadding = new Padding ( 0 ) ,
148
+ Padding = new Padding ( 0 ) ,
149
+ NameTextSize = 12
150
+ }
151
+ } ;
152
+
153
+ YAxes = new Axis [ ]
154
+ {
155
+ new Axis
156
+ {
157
+ CrosshairLabelsBackground = SKColors . DarkOrange . AsLvcColor ( ) ,
158
+ CrosshairLabelsPaint = new SolidColorPaint ( SKColors . DarkRed , 1 ) ,
159
+ CrosshairPaint = new SolidColorPaint ( SKColors . DarkOrange , 1 ) ,
160
+ CrosshairSnapEnabled = true // snapping is also supported
161
+ }
162
+ } ;
73
163
}
74
164
}
0 commit comments