Skip to content

Commit 33a463c

Browse files
committed
feature: allow to view contribution chart based on selected author (#1196)
Signed-off-by: leo <[email protected]>
1 parent 90b3766 commit 33a463c

File tree

3 files changed

+81
-26
lines changed

3 files changed

+81
-26
lines changed

src/Models/Statistics.cs

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,31 @@
1111

1212
namespace SourceGit.Models
1313
{
14-
public enum StaticsticsMode
14+
public enum StatisticsMode
1515
{
1616
All,
1717
ThisMonth,
1818
ThisWeek,
1919
}
2020

21-
public class StaticsticsAuthor(User user, int count)
21+
public class StatisticsAuthor(User user, int count)
2222
{
2323
public User User { get; set; } = user;
2424
public int Count { get; set; } = count;
2525
}
2626

27-
public class StaticsticsSample(DateTime time, int count)
28-
{
29-
public DateTime Time { get; set; } = time;
30-
public int Count { get; set; } = count;
31-
}
32-
3327
public class StatisticsReport
3428
{
3529
public static readonly string[] WEEKDAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
3630

3731
public int Total { get; set; } = 0;
38-
public List<StaticsticsAuthor> Authors { get; set; } = new List<StaticsticsAuthor>();
32+
public List<StatisticsAuthor> Authors { get; set; } = new List<StatisticsAuthor>();
3933
public List<ISeries> Series { get; set; } = new List<ISeries>();
4034
public List<Axis> XAxes { get; set; } = new List<Axis>();
4135
public List<Axis> YAxes { get; set; } = new List<Axis>();
36+
public StatisticsAuthor SelectedAuthor { get => _selectedAuthor; set => ChangeAuthor(value); }
4237

43-
public StatisticsReport(StaticsticsMode mode, DateTime start)
38+
public StatisticsReport(StatisticsMode mode, DateTime start)
4439
{
4540
_mode = mode;
4641

@@ -51,14 +46,14 @@ public StatisticsReport(StaticsticsMode mode, DateTime start)
5146
SeparatorsPaint = new SolidColorPaint(new SKColor(0x40808080)) { StrokeThickness = 1 }
5247
}];
5348

54-
if (mode == StaticsticsMode.ThisWeek)
49+
if (mode == StatisticsMode.ThisWeek)
5550
{
5651
for (int i = 0; i < 7; i++)
5752
_mapSamples.Add(start.AddDays(i), 0);
5853

5954
XAxes.Add(new DateTimeAxis(TimeSpan.FromDays(1), v => WEEKDAYS[(int)v.DayOfWeek]) { TextSize = 10 });
6055
}
61-
else if (mode == StaticsticsMode.ThisMonth)
56+
else if (mode == StatisticsMode.ThisMonth)
6257
{
6358
var now = DateTime.Now;
6459
var maxDays = DateTime.DaysInMonth(now.Year, now.Month);
@@ -78,7 +73,7 @@ public void AddCommit(DateTime time, User author)
7873
Total++;
7974

8075
var normalized = DateTime.MinValue;
81-
if (_mode == StaticsticsMode.ThisWeek || _mode == StaticsticsMode.ThisMonth)
76+
if (_mode == StatisticsMode.ThisWeek || _mode == StatisticsMode.ThisMonth)
8277
normalized = time.Date;
8378
else
8479
normalized = new DateTime(time.Year, time.Month, 1).ToLocalTime();
@@ -92,10 +87,30 @@ public void AddCommit(DateTime time, User author)
9287
_mapUsers[author] = vu + 1;
9388
else
9489
_mapUsers.Add(author, 1);
90+
91+
if (_mapUserSamples.TryGetValue(author, out var vus))
92+
{
93+
if (vus.TryGetValue(normalized, out var n))
94+
vus[normalized] = n + 1;
95+
else
96+
vus.Add(normalized, 1);
97+
}
98+
else
99+
{
100+
_mapUserSamples.Add(author, new Dictionary<DateTime, int>
101+
{
102+
{ normalized, 1 }
103+
});
104+
}
95105
}
96106

97107
public void Complete()
98108
{
109+
foreach (var kv in _mapUsers)
110+
Authors.Add(new StatisticsAuthor(kv.Key, kv.Value));
111+
112+
Authors.Sort((l, r) => r.Count - l.Count);
113+
99114
var samples = new List<DateTimePoint>();
100115
foreach (var kv in _mapSamples)
101116
samples.Add(new DateTimePoint(kv.Key, kv.Value));
@@ -110,24 +125,59 @@ public void Complete()
110125
}
111126
);
112127

113-
foreach (var kv in _mapUsers)
114-
Authors.Add(new StaticsticsAuthor(kv.Key, kv.Value));
115-
116-
Authors.Sort((l, r) => r.Count - l.Count);
117-
118128
_mapUsers.Clear();
119129
_mapSamples.Clear();
120130
}
121131

122132
public void ChangeColor(uint color)
123133
{
124-
if (Series is [ColumnSeries<DateTimePoint> series])
125-
series.Fill = new SolidColorPaint(new SKColor(color));
134+
_fillColor = color;
135+
136+
var fill = new SKColor(color);
137+
138+
if (Series.Count > 0 && Series[0] is ColumnSeries<DateTimePoint> total)
139+
total.Fill = new SolidColorPaint(_selectedAuthor == null ? fill : fill.WithAlpha(51));
140+
141+
if (Series.Count > 1 && Series[1] is ColumnSeries<DateTimePoint> user)
142+
user.Fill = new SolidColorPaint(fill);
143+
}
144+
145+
public void ChangeAuthor(StatisticsAuthor author)
146+
{
147+
if (author == _selectedAuthor)
148+
return;
149+
150+
_selectedAuthor = author;
151+
Series.RemoveRange(1, Series.Count - 1);
152+
if (author == null || !_mapUserSamples.TryGetValue(author.User, out var userSamples))
153+
{
154+
ChangeColor(_fillColor);
155+
return;
156+
}
157+
158+
var samples = new List<DateTimePoint>();
159+
foreach (var kv in userSamples)
160+
samples.Add(new DateTimePoint(kv.Key, kv.Value));
161+
162+
Series.Add(
163+
new ColumnSeries<DateTimePoint>()
164+
{
165+
Values = samples,
166+
Stroke = null,
167+
Fill = null,
168+
Padding = 1,
169+
}
170+
);
171+
172+
ChangeColor(_fillColor);
126173
}
127174

128-
private StaticsticsMode _mode = StaticsticsMode.All;
175+
private StatisticsMode _mode = StatisticsMode.All;
129176
private Dictionary<User, int> _mapUsers = new Dictionary<User, int>();
130177
private Dictionary<DateTime, int> _mapSamples = new Dictionary<DateTime, int>();
178+
private Dictionary<User, Dictionary<DateTime, int>> _mapUserSamples = new Dictionary<User, Dictionary<DateTime, int>>();
179+
private StatisticsAuthor _selectedAuthor = null;
180+
private uint _fillColor = 255;
131181
}
132182

133183
public class Statistics
@@ -143,9 +193,9 @@ public Statistics()
143193
_thisWeekStart = _today.AddDays(-weekOffset);
144194
_thisMonthStart = _today.AddDays(1 - _today.Day);
145195

146-
All = new StatisticsReport(StaticsticsMode.All, DateTime.MinValue);
147-
Month = new StatisticsReport(StaticsticsMode.ThisMonth, _thisMonthStart);
148-
Week = new StatisticsReport(StaticsticsMode.ThisWeek, _thisWeekStart);
196+
All = new StatisticsReport(StatisticsMode.All, DateTime.MinValue);
197+
Month = new StatisticsReport(StatisticsMode.ThisMonth, _thisMonthStart);
198+
Week = new StatisticsReport(StatisticsMode.ThisWeek, _thisWeekStart);
149199
}
150200

151201
public void AddCommit(string author, double timestamp)

src/ViewModels/Statistics.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public int SelectedIndex
2828
public Models.StatisticsReport SelectedReport
2929
{
3030
get => _selectedReport;
31-
private set => SetProperty(ref _selectedReport, value);
31+
private set
32+
{
33+
value?.ChangeAuthor(null);
34+
SetProperty(ref _selectedReport, value);
35+
}
3236
}
3337

3438
public uint SampleColor

src/Views/Statistics.axaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
<!-- Table By Autor -->
140140
<ListBox Grid.Column="0"
141141
ItemsSource="{Binding Authors}"
142+
SelectedItem="{Binding SelectedAuthor, Mode=TwoWay}"
142143
SelectionMode="Single"
143144
BorderThickness="1"
144145
BorderBrush="{DynamicResource Brush.Border2}"
@@ -160,7 +161,7 @@
160161
</ListBox.ItemsPanel>
161162

162163
<ListBox.ItemTemplate>
163-
<DataTemplate DataType="m:StaticsticsAuthor">
164+
<DataTemplate DataType="m:StatisticsAuthor">
164165
<Border BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}">
165166
<Grid ColumnDefinitions="26,*,100">
166167
<v:Avatar Grid.Column="0"

0 commit comments

Comments
 (0)