Skip to content

Commit d8488b1

Browse files
committed
updated view model
1 parent 19b015c commit d8488b1

File tree

2 files changed

+68
-53
lines changed

2 files changed

+68
-53
lines changed

PortfolioAnalysis/Portfolio/MainPage.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@
9393
<chart:ChartLegend Placement="Right"/>
9494
</chart:SfCircularChart.Legend>
9595

96-
<chart:PieSeries ItemsSource="{Binding Source={x:Reference viewModel}, Path=PortfolioData}"
97-
PaletteBrushes="{Binding Source={x:Reference viewModel}, Path=Palette}" ShowDataLabels="True"
96+
<chart:PieSeries ItemsSource="{Binding PortfolioData}"
97+
PaletteBrushes="{Binding Palette}" ShowDataLabels="True"
9898
x:Name="UpdateCircleData" XBindingPath="Sector" YBindingPath="Value">
9999
<chart:PieSeries.DataLabelSettings>
100100
<chart:CircularDataLabelSettings>

PortfolioAnalysis/Portfolio/ViewModel/PortfolioViewModel.cs

+66-51
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
namespace Portfolio.ViewModel
66
{
7-
public record PortfolioViewModel(ObservableCollection<PortfolioModel> PortfolioHistory, ObservableCollection<Brush> Palette, ObservableCollection<string> Years, string SelectedYear) : INotifyPropertyChanged
7+
public class PortfolioViewModel : INotifyPropertyChanged
88
{
9+
public ObservableCollection<PortfolioModel> PortfolioHistory { get; set; }
10+
public ObservableCollection<Brush> Palette { get; set; }
11+
public ObservableCollection<string> Years { get; set; }
12+
public string SelectedYear { get; set; }
13+
914
private ObservableCollection<PortfolioModel>? _portfolioData;
1015
public ObservableCollection<PortfolioModel> PortfolioData
1116
{
@@ -19,16 +24,24 @@ public ObservableCollection<PortfolioModel> PortfolioData
1924
}
2025
}
2126
}
27+
2228
public event PropertyChangedEventHandler? PropertyChanged;
23-
public PortfolioViewModel() : this([], [
24-
new SolidColorBrush(Color.FromArgb("#56EBF5")),
25-
new SolidColorBrush(Color.FromArgb("#FC922C")),
26-
new SolidColorBrush(Color.FromArgb("#FC95B2")),
27-
new SolidColorBrush(Color.FromArgb("#FEE7A0")),
28-
new SolidColorBrush(Color.FromArgb("#DDB1E1")),
29-
new SolidColorBrush(Color.FromArgb("#EBD9EB")),
30-
], ["2005", "2010", "2015", "2020", "2025"], "2005")
29+
30+
public PortfolioViewModel()
31+
{
32+
PortfolioHistory = new ObservableCollection<PortfolioModel>();
33+
Palette = new ObservableCollection<Brush>
3134
{
35+
new SolidColorBrush(Color.FromArgb("#56EBF5")),
36+
new SolidColorBrush(Color.FromArgb("#FC922C")),
37+
new SolidColorBrush(Color.FromArgb("#FC95B2")),
38+
new SolidColorBrush(Color.FromArgb("#FEE7A0")),
39+
new SolidColorBrush(Color.FromArgb("#DDB1E1")),
40+
new SolidColorBrush(Color.FromArgb("#EBD9EB"))
41+
};
42+
Years = new ObservableCollection<string> { "2005", "2010", "2015", "2020", "2025" };
43+
SelectedYear = "2005";
44+
3245
UpdatePortfolioData(SelectedYear);
3346
GeneratePortfolioData();
3447
}
@@ -37,59 +50,60 @@ protected void OnPropertyChanged(string propertyName)
3750
{
3851
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
3952
}
53+
4054
public void UpdatePortfolioData(string year)
4155
{
4256
PortfolioData = year switch
4357
{
44-
"2005" =>
45-
[
46-
new("Stocks", 60),
47-
new("Bonds", 20),
48-
new("Real Estate", 10),
49-
new("Gold", 5),
50-
new("Cash", 5)
51-
],
52-
"2010" =>
53-
[
54-
new("Stocks", 55),
55-
new("Bonds", 25),
56-
new("Real Estate", 12),
57-
new("Gold", 5),
58-
new("Cash", 3)
59-
],
60-
"2015" =>
61-
[
62-
new("Stocks", 50),
63-
new("Bonds", 30),
64-
new("Real Estate", 15),
65-
new("Gold", 4),
66-
new("Cash", 1)
67-
],
68-
"2020" =>
69-
[
70-
new("Stocks", 45),
71-
new("Bonds", 35),
72-
new("Real Estate", 17),
73-
new("Gold", 2),
74-
new("Cash", 1)
75-
],
76-
"2025" =>
77-
[
78-
new("Stocks", 40),
79-
new("Bonds", 40),
80-
new("Real Estate", 18),
81-
new("Gold", 1),
82-
new("Cash", 1)
83-
],
58+
"2005" => new ObservableCollection<PortfolioModel>
59+
{
60+
new PortfolioModel("Stocks", 60),
61+
new PortfolioModel("Bonds", 20),
62+
new PortfolioModel("Real Estate", 10),
63+
new PortfolioModel("Gold", 5),
64+
new PortfolioModel("Cash", 5)
65+
},
66+
"2010" => new ObservableCollection<PortfolioModel>
67+
{
68+
new PortfolioModel("Stocks", 55),
69+
new PortfolioModel("Bonds", 25),
70+
new PortfolioModel("Real Estate", 12),
71+
new PortfolioModel("Gold", 5),
72+
new PortfolioModel("Cash", 3)
73+
},
74+
"2015" => new ObservableCollection<PortfolioModel>
75+
{
76+
new PortfolioModel("Stocks", 50),
77+
new PortfolioModel("Bonds", 30),
78+
new PortfolioModel("Real Estate", 15),
79+
new PortfolioModel("Gold", 4),
80+
new PortfolioModel("Cash", 1)
81+
},
82+
"2020" => new ObservableCollection<PortfolioModel>
83+
{
84+
new PortfolioModel("Stocks", 45),
85+
new PortfolioModel("Bonds", 35),
86+
new PortfolioModel("Real Estate", 17),
87+
new PortfolioModel("Gold", 2),
88+
new PortfolioModel("Cash", 1)
89+
},
90+
"2025" => new ObservableCollection<PortfolioModel>
91+
{
92+
new PortfolioModel("Stocks", 40),
93+
new PortfolioModel("Bonds", 40),
94+
new PortfolioModel("Real Estate", 18),
95+
new PortfolioModel("Gold", 1),
96+
new PortfolioModel("Cash", 1)
97+
},
8498
_ => PortfolioData
8599
};
86100
}
101+
87102
private void GeneratePortfolioData()
88103
{
89104
double initialInvestment = 10000;
90105
double currentValue = initialInvestment;
91-
Random random = new();
92-
Random rand = random;
106+
Random rand = new();
93107

94108
for (int year = 2005; year <= 2025; year++)
95109
{
@@ -105,4 +119,5 @@ private void GeneratePortfolioData()
105119
}
106120
}
107121
}
122+
108123
}

0 commit comments

Comments
 (0)