feature<Statistics>: finish statistiscs

This commit is contained in:
leo 2024-02-23 19:16:28 +08:00
parent 49f6ad0407
commit e070b79d2c
10 changed files with 632 additions and 4 deletions

View file

@ -0,0 +1,32 @@
using System;
namespace SourceGit.Commands {
public class Statistics : Command {
public Statistics(string repo) {
_statistics = new Models.Statistics();
WorkingDirectory = repo;
Context = repo;
Args = $"log --date-order --branches --remotes --since=\"{_statistics.Since()}\" --date=unix --pretty=format:\"%ad$%an\"";
}
public Models.Statistics Result() {
Exec();
_statistics.Complete();
return _statistics;
}
protected override void OnReadline(string line) {
var dateEndIdx = line.IndexOf('$', StringComparison.Ordinal);
if (dateEndIdx == -1) return;
var dateStr = line.Substring(0, dateEndIdx);
var date = 0.0;
if (!double.TryParse(dateStr, out date)) return;
_statistics.AddCommit(line.Substring(dateEndIdx + 1), date);
}
private Models.Statistics _statistics = null;
}
}

129
src/Models/Statistics.cs Normal file
View file

@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
namespace SourceGit.Models {
public class Sample {
public string Name { get; set; }
public int Count { get; set; }
}
public class Statistics {
public int TotalYear { get; set; } = 0;
public int TotalMonth { get; set; } = 0;
public int TotalWeek { get; set; } = 0;
public List<Sample> Year { get; set; } = new List<Sample>();
public List<Sample> Month { get; set; } = new List<Sample>();
public List<Sample> Week { get; set; } = new List<Sample>();
public List<Sample> YearByAuthor { get; set; } = new List<Sample>();
public List<Sample> MonthByAuthor { get; set; } = new List<Sample>();
public List<Sample> WeekByAuthor { get; set; } = new List<Sample>();
public Statistics() {
_utcStart = DateTime.UnixEpoch;
_today = DateTime.Today;
_thisWeekStart = _today.AddSeconds(-(int)_today.DayOfWeek * 3600 * 24 - _today.Hour * 3600 - _today.Minute * 60 - _today.Second);
_thisWeekEnd = _thisWeekStart.AddDays(7);
string[] monthNames = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
for (int i = 0; i < monthNames.Length; i++) {
Year.Add(new Sample {
Name = monthNames[i],
Count = 0,
});
}
var monthDays = DateTime.DaysInMonth(_today.Year, _today.Month);
for (int i = 0; i < monthDays; i++) {
Month.Add(new Sample {
Name = $"{i + 1}",
Count = 0,
});
}
string[] weekDayNames = [
"SUN",
"MON",
"TUE",
"WED",
"THU",
"FRI",
"SAT",
];
for (int i = 0; i < weekDayNames.Length; i++) {
Week.Add(new Sample {
Name = weekDayNames[i],
Count = 0,
});
}
}
public string Since() {
return _today.ToString("yyyy-01-01 00:00:00");
}
public void AddCommit(string author, double timestamp) {
var authorTime = _utcStart.AddSeconds(timestamp);
if (authorTime.CompareTo(_thisWeekStart) >= 0 && authorTime.CompareTo(_thisWeekEnd) < 0) {
Week[(int)authorTime.DayOfWeek].Count++;
TotalWeek++;
AddByAuthor(_mapWeekByAuthor, WeekByAuthor, author);
}
if (authorTime.Month == _today.Month) {
Month[authorTime.Day - 1].Count++;
TotalMonth++;
AddByAuthor(_mapMonthByAuthor, MonthByAuthor, author);
}
Year[authorTime.Month - 1].Count++;
TotalYear++;
AddByAuthor(_mapYearByAuthor, YearByAuthor, author);
}
public void Complete() {
_mapYearByAuthor.Clear();
_mapMonthByAuthor.Clear();
_mapWeekByAuthor.Clear();
YearByAuthor.Sort((l, r) => r.Count - l.Count);
MonthByAuthor.Sort((l, r) => r.Count - l.Count);
WeekByAuthor.Sort((l, r) => r.Count - l.Count);
}
private void AddByAuthor(Dictionary<string, Sample> map, List<Sample> collection, string author) {
if (map.ContainsKey(author)) {
map[author].Count++;
} else {
var sample = new Sample { Name = author, Count = 1 };
map.Add(author, sample);
collection.Add(sample);
}
}
private DateTime _utcStart;
private DateTime _today;
private DateTime _thisWeekStart;
private DateTime _thisWeekEnd;
private Dictionary<string, Sample> _mapYearByAuthor = new Dictionary<string, Sample>();
private Dictionary<string, Sample> _mapMonthByAuthor = new Dictionary<string, Sample>();
private Dictionary<string, Sample> _mapWeekByAuthor = new Dictionary<string, Sample>();
}
}

View file

@ -82,4 +82,5 @@
<StreamGeometry x:Key="Icons.EyeClose">M734 128c-33-19-74-8-93 25l-41 70c-28-6-58-9-90-9-294 0-445 298-445 298s82 149 231 236l-31 54c-19 33-8 74 25 93 33 19 74 8 93-25L759 222C778 189 767 147 734 128zM305 512c0-115 93-208 207-208 14 0 27 1 40 4l-37 64c-1 0-2 0-2 0-77 0-140 63-140 140 0 26 7 51 20 71l-37 64C324 611 305 564 305 512zM771 301 700 423c13 27 20 57 20 89 0 110-84 200-192 208l-51 89c12 1 24 2 36 2 292 0 446-298 446-298S895 388 771 301z</StreamGeometry>
<StreamGeometry x:Key="Icons.Empty">M469 235V107h85v128h-85zm-162-94 85 85-60 60-85-85 60-60zm469 60-85 85-60-60 85-85 60 60zm-549 183A85 85 0 01302 341H722a85 85 0 0174 42l131 225A85 85 0 01939 652V832a85 85 0 01-85 85H171a85 85 0 01-85-85v-180a85 85 0 0112-43l131-225zM722 427H302l-100 171h255l10 29a59 59 0 002 5c2 4 5 9 9 14 8 9 18 17 34 17 16 0 26-7 34-17a72 72 0 0011-18l0-0 10-29h255l-100-171zM853 683H624a155 155 0 01-12 17C593 722 560 747 512 747c-48 0-81-25-99-47a155 155 0 01-12-17H171v149h683v-149z</StreamGeometry>
<StreamGeometry x:Key="Icons.VSCode">M719 85 388 417l-209-165L87 299v427l92 47 210-164L720 939 939 850V171zM186 610V412l104 104zm526 55L514 512l198-153z</StreamGeometry>
<StreamGeometry x:Key="Icons.Statistics">M296 912H120c-4.4 0-8-3.6-8-8V520c0-4.4 3.6-8 8-8h176c4.4 0 8 3.6 8 8v384c0 4.4-3.6 8-8 8zM600 912H424c-4.4 0-8-3.6-8-8V121c0-4.4 3.6-8 8-8h176c4.4 0 8 3.6 8 8v783c0 4.4-3.6 8-8 8zM904 912H728c-4.4 0-8-3.6-8-8V280c0-4.4 3.6-8 8-8h176c4.4 0 8 3.6 8 8v624c0 4.4-3.6 8-8 8z</StreamGeometry>
</ResourceDictionary>

View file

@ -147,8 +147,6 @@
<sys:String x:Key="Text.Repository.Submodules">SUBMODULES</sys:String>
<sys:String x:Key="Text.Repository.Submodules.Add">ADD SUBMODULE</sys:String>
<sys:String x:Key="Text.Repository.Submodules.Update">UPDATE SUBMODULE</sys:String>
<sys:String x:Key="Text.Repository.SubTrees">SUBTREES</sys:String>
<sys:String x:Key="Text.Repository.SubTrees.Add">ADD/LINK SUBTREE</sys:String>
<sys:String x:Key="Text.Repository.Resolve">RESOLVE</sys:String>
<sys:String x:Key="Text.Repository.Continue">CONTINUE</sys:String>
<sys:String x:Key="Text.Repository.Abort">ABORT</sys:String>
@ -459,6 +457,15 @@
<sys:String x:Key="Text.AssumeUnchanged.Remove">REMOVE</sys:String>
<sys:String x:Key="Text.AssumeUnchanged.Empty">NO FILES ASSUMED AS UNCHANGED</sys:String>
<sys:String x:Key="Text.Statistics">Statistics</sys:String>
<sys:String x:Key="Text.Statistics.ThisWeek">WEEK</sys:String>
<sys:String x:Key="Text.Statistics.ThisMonth">MONTH</sys:String>
<sys:String x:Key="Text.Statistics.ThisYear">YEAR</sys:String>
<sys:String x:Key="Text.Statistics.TotalAuthors">Total Authors</sys:String>
<sys:String x:Key="Text.Statistics.TotalCommits">Total Commits</sys:String>
<sys:String x:Key="Text.Statistics.AuthorName">AUTHOR</sys:String>
<sys:String x:Key="Text.Statistics.CommitAmount">COMMITS</sys:String>
<sys:String x:Key="Text.NotConfigured">Git has NOT been configured. Please to go [Preference] and configure it first.</sys:String>
<sys:String x:Key="Text.BinaryNotSupported">BINARY FILE NOT SUPPORTED!!!</sys:String>
<sys:String x:Key="Text.BlameTypeNotSupported">BLAME ON THIS FILE IS NOT SUPPORTED!!!</sys:String>

View file

@ -146,8 +146,6 @@
<sys:String x:Key="Text.Repository.Submodules">子模块列表</sys:String>
<sys:String x:Key="Text.Repository.Submodules.Add">添加子模块</sys:String>
<sys:String x:Key="Text.Repository.Submodules.Update">更新子模块</sys:String>
<sys:String x:Key="Text.Repository.SubTrees">子树列表</sys:String>
<sys:String x:Key="Text.Repository.SubTrees.Add">添加子树</sys:String>
<sys:String x:Key="Text.Repository.Resolve">解决冲突</sys:String>
<sys:String x:Key="Text.Repository.Continue">下一步</sys:String>
<sys:String x:Key="Text.Repository.Abort">终止冲突解决</sys:String>
@ -458,6 +456,15 @@
<sys:String x:Key="Text.AssumeUnchanged.Remove">移除</sys:String>
<sys:String x:Key="Text.AssumeUnchanged.Empty">没有不跟踪更改的文件</sys:String>
<sys:String x:Key="Text.Statistics">提交统计</sys:String>
<sys:String x:Key="Text.Statistics.ThisWeek">本周</sys:String>
<sys:String x:Key="Text.Statistics.ThisMonth">本月</sys:String>
<sys:String x:Key="Text.Statistics.ThisYear">本年</sys:String>
<sys:String x:Key="Text.Statistics.TotalAuthors">作者人数</sys:String>
<sys:String x:Key="Text.Statistics.TotalCommits">总计提交次数</sys:String>
<sys:String x:Key="Text.Statistics.AuthorName">作者</sys:String>
<sys:String x:Key="Text.Statistics.CommitAmount">提交次数</sys:String>
<sys:String x:Key="Text.NotConfigured">GIT尚未配置。请打开【偏好设置】配置GIT路径。</sys:String>
<sys:String x:Key="Text.BinaryNotSupported">二进制文件不支持该操作!!!</sys:String>
<sys:String x:Key="Text.BlameTypeNotSupported">选中文件不支持该操作!!!</sys:String>

View file

@ -0,0 +1,33 @@
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Threading.Tasks;
namespace SourceGit.ViewModels {
public class Statistics : ObservableObject {
public bool IsLoading {
get => _isLoading;
private set => SetProperty(ref _isLoading, value);
}
public Models.Statistics Data {
get => _data;
private set => SetProperty(ref _data, value);
}
public Statistics(string repo) {
_repo = repo;
Task.Run(() => {
var result = new Commands.Statistics(_repo).Result();
Dispatcher.UIThread.Invoke(() => {
IsLoading = false;
Data = result;
});
});
}
private string _repo = string.Empty;
private bool _isLoading = true;
private Models.Statistics _data = null;
}
}

View file

@ -81,6 +81,10 @@
<Path Width="14" Height="14" Data="{StaticResource Icons.Clean}"/>
</Button>
<Button Classes="icon_button" Width="32" Click="OpenStatistics" ToolTip.Tip="{DynamicResource Text.Repository.Statistics}">
<Path Width="14" Height="14" Data="{StaticResource Icons.Statistics}"/>
</Button>
<Button Classes="icon_button" Width="32" Command="{Binding OpenConfigure}" ToolTip.Tip="{DynamicResource Text.Repository.Configure}">
<Path Width="15" Height="15" Data="{StaticResource Icons.Settings1}"/>
</Button>

View file

@ -188,6 +188,14 @@ namespace SourceGit.Views {
}
}
}
private async void OpenStatistics(object sender, RoutedEventArgs e) {
if (DataContext is ViewModels.Repository repo) {
var dialog = new Statistics() { DataContext = new ViewModels.Statistics(repo.FullPath) };
await dialog.ShowDialog(TopLevel.GetTopLevel(this) as Window);
e.Handled = true;
}
}
}
}

222
src/Views/Statistics.axaml Normal file
View file

@ -0,0 +1,222 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="using:SourceGit.Models"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:v="using:SourceGit.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.Statistics"
x:DataType="vm:Statistics"
Title="{DynamicResource Text.Statistics}"
Background="{DynamicResource Brush.Window}"
Width="800" Height="450"
WindowStartupLocation="CenterOwner"
CanResize="False"
ExtendClientAreaToDecorationsHint="True"
ExtendClientAreaChromeHints="NoChrome">
<Grid RowDefinitions="30,*">
<!-- Title bar -->
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto">
<Border Grid.Column="0" Grid.ColumnSpan="3"
Background="{DynamicResource Brush.TitleBar}"
BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}"
IsHitTestVisible="False"/>
<Path Grid.Column="0"
Width="14" Height="14"
Margin="10,0,0,0"
Data="{StaticResource Icons.Statistics}"
IsVisible="{Binding Source={x:Static vm:Preference.Instance}, Path=UseMacOSStyle, Converter={x:Static BoolConverters.Not}}"/>
<Grid Grid.Column="0" Classes="caption_button_box" Margin="2,4,0,0" IsVisible="{Binding Source={x:Static vm:Preference.Instance}, Path=UseMacOSStyle}">
<Button Classes="caption_button_macos" Click="CloseWindow">
<Grid>
<Ellipse Fill="{DynamicResource Brush.MacOS.Close}"/>
<Path Height="6" Width="6" Stretch="Fill" Fill="#404040" Stroke="#404040" StrokeThickness="1" Data="{StaticResource Icons.Window.Close}"/>
</Grid>
</Button>
</Grid>
<TextBlock Grid.Column="0" Grid.ColumnSpan="3"
Classes="bold"
Text="{DynamicResource Text.Statistics}"
HorizontalAlignment="Center" VerticalAlignment="Center"
IsHitTestVisible="False"/>
<Button Grid.Column="2"
Classes="caption_button"
Click="CloseWindow"
IsVisible="{Binding Source={x:Static vm:Preference.Instance}, Path=UseMacOSStyle, Converter={x:Static BoolConverters.Not}}">
<Path Data="{StaticResource Icons.Window.Close}"/>
</Button>
</Grid>
<!-- Body -->
<TabControl Grid.Row="1" Margin="0,8,0,0">
<TabControl.Styles>
<Style Selector="TabControl /template/ ItemsPresenter#PART_ItemsPresenter">
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</TabControl.Styles>
<TabItem>
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Statistics.ThisYear}"/>
</TabItem.Header>
<Grid RowDefinitions="*,32" ColumnDefinitions="Auto,*" Margin="8">
<!-- Table By Author -->
<DataGrid Grid.Row="0" Grid.Column="0"
ItemsSource="{Binding Data.YearByAuthor}"
SelectionMode="Single"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
HeadersVisibility="Column"
GridLinesVisibility="All"
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border1}"
Background="{DynamicResource Brush.Contents}"
IsReadOnly="True"
RowHeight="26"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Width="150" Header="{DynamicResource Text.Statistics.AuthorName}" Binding="{Binding Name}"/>
<DataGridTextColumn Width="100" Header="{DynamicResource Text.Statistics.CommitAmount}" Binding="{Binding Count}"/>
</DataGrid.Columns>
</DataGrid>
<!-- Total Authors -->
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="{DynamicResource Text.Statistics.TotalAuthors}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.YearByAuthor.Count}" Margin="4,0,0,0"/>
</StackPanel>
<!-- Graph -->
<v:Chart Grid.Row="0" Grid.Column="1"
Margin="16,8,0,0"
FontFamily="{StaticResource JetBrainsMono}"
LineBrush="{DynamicResource Brush.FG1}"
ShapeBrush="{DynamicResource Brush.Accent1}"
Samples="{Binding Data.Year}"/>
<!-- Total Commits -->
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="{DynamicResource Text.Statistics.TotalCommits}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.TotalYear}" Margin="4,0,0,0"/>
</StackPanel>
</Grid>
</TabItem>
<TabItem>
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Statistics.ThisMonth}"/>
</TabItem.Header>
<Grid RowDefinitions="*,32" ColumnDefinitions="Auto,*" Margin="8">
<!-- Table By Author -->
<DataGrid Grid.Row="0" Grid.Column="0"
ItemsSource="{Binding Data.MonthByAuthor}"
SelectionMode="Single"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
HeadersVisibility="Column"
GridLinesVisibility="All"
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border1}"
Background="{DynamicResource Brush.Contents}"
IsReadOnly="True"
RowHeight="26"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Width="150" Header="{DynamicResource Text.Statistics.AuthorName}" Binding="{Binding Name}"/>
<DataGridTextColumn Width="100" Header="{DynamicResource Text.Statistics.CommitAmount}" Binding="{Binding Count}"/>
</DataGrid.Columns>
</DataGrid>
<!-- Total Authors -->
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="{DynamicResource Text.Statistics.TotalAuthors}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.MonthByAuthor.Count}" Margin="4,0,0,0"/>
</StackPanel>
<!-- Graph -->
<v:Chart Grid.Row="0" Grid.Column="1"
Margin="16,8,0,0"
FontFamily="{StaticResource JetBrainsMono}"
LineBrush="{DynamicResource Brush.FG1}"
ShapeBrush="{DynamicResource Brush.Accent1}"
Samples="{Binding Data.Month}"/>
<!-- Total Commits -->
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="{DynamicResource Text.Statistics.TotalCommits}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.TotalMonth}" Margin="4,0,0,0"/>
</StackPanel>
</Grid>
</TabItem>
<TabItem>
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Statistics.ThisWeek}"/>
</TabItem.Header>
<Grid RowDefinitions="*,32" ColumnDefinitions="Auto,*" Margin="8">
<!-- Table By Author -->
<DataGrid Grid.Row="0" Grid.Column="0"
ItemsSource="{Binding Data.WeekByAuthor}"
SelectionMode="Single"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
HeadersVisibility="Column"
GridLinesVisibility="All"
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border1}"
Background="{DynamicResource Brush.Contents}"
IsReadOnly="True"
RowHeight="26"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Width="150" Header="{DynamicResource Text.Statistics.AuthorName}" Binding="{Binding Name}"/>
<DataGridTextColumn Width="100" Header="{DynamicResource Text.Statistics.CommitAmount}" Binding="{Binding Count}"/>
</DataGrid.Columns>
</DataGrid>
<!-- Total Authors -->
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="{DynamicResource Text.Statistics.TotalAuthors}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.WeekByAuthor.Count}" Margin="4,0,0,0"/>
</StackPanel>
<!-- Graph -->
<v:Chart Grid.Row="0" Grid.Column="1"
Margin="16,8,0,0"
FontFamily="{StaticResource JetBrainsMono}"
LineBrush="{DynamicResource Brush.FG1}"
ShapeBrush="{DynamicResource Brush.Accent1}"
Samples="{Binding Data.Week}"/>
<!-- Total Commits -->
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="{DynamicResource Text.Statistics.TotalCommits}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Text="{Binding Data.TotalWeek}" Margin="4,0,0,0"/>
</StackPanel>
</Grid>
</TabItem>
</TabControl>
<!-- Loading Mask -->
<Path Grid.Row="1"
Classes="rotating"
Width="48" Height="48"
Data="{DynamicResource Icons.Loading}"
HorizontalAlignment="Center" VerticalAlignment="Center"
IsVisible="{Binding IsLoading}"/>
</Grid>
</Window>

View file

@ -0,0 +1,185 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace SourceGit.Views {
public class Chart : Control {
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
AvaloniaProperty.Register<Chart, FontFamily>(nameof(FontFamily));
public FontFamily FontFamily {
get => GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public static readonly StyledProperty<IBrush> LineBrushProperty =
AvaloniaProperty.Register<Chart, IBrush>(nameof(LineBrush), Brushes.Gray);
public IBrush LineBrush {
get => GetValue(LineBrushProperty);
set => SetValue(LineBrushProperty, value);
}
public static readonly StyledProperty<IBrush> ShapeBrushProperty =
AvaloniaProperty.Register<Chart, IBrush>(nameof(ShapeBrush), Brushes.Gray);
public IBrush ShapeBrush {
get => GetValue(ShapeBrushProperty);
set => SetValue(ShapeBrushProperty, value);
}
public static readonly StyledProperty<List<Models.Sample>> SamplesProperty =
AvaloniaProperty.Register<Chart, List<Models.Sample>>(nameof(Samples), null);
public List<Models.Sample> Samples {
get => GetValue(SamplesProperty);
set => SetValue(SamplesProperty, value);
}
static Chart() {
AffectsRender<Chart>(SamplesProperty);
}
public override void Render(DrawingContext context) {
if (Samples == null) return;
var samples = Samples;
int maxV = 0;
foreach (var s in samples) {
if (maxV < s.Count) maxV = s.Count;
}
if (maxV < 5) {
maxV = 5;
} else if (maxV < 10) {
maxV = 10;
} else if (maxV < 50) {
maxV = 50;
} else if (maxV < 100) {
maxV = 100;
} else if (maxV < 200) {
maxV = 200;
} else if (maxV < 500) {
maxV = 500;
} else {
maxV = (int)Math.Ceiling(maxV / 500.0) * 500;
}
var typeface = new Typeface(FontFamily);
var pen = new Pen(LineBrush, 1);
var width = Bounds.Width;
var height = Bounds.Height;
// Draw coordinate
var maxLabel = new FormattedText($"{maxV}", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 12.0, LineBrush);
var horizonStart = maxLabel.Width + 8;
var labelHeight = 32;
context.DrawText(maxLabel, new Point(0, -maxLabel.Height * 0.5));
context.DrawLine(pen, new Point(horizonStart, 0), new Point(horizonStart, height - labelHeight));
context.DrawLine(pen, new Point(horizonStart, height - labelHeight), new Point(width, height - labelHeight));
if (samples.Count == 0) return;
// Draw horizontal lines
var stepX = (width - horizonStart) / samples.Count;
var stepV = (height - labelHeight) / 5;
var labelStepV = maxV / 5;
var gridPen = new Pen(LineBrush, 1, new DashStyle());
for (int i = 1; i < 5; i++) {
var vLabel = new FormattedText(
$"{maxV - i * labelStepV}",
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
12.0,
LineBrush);
var dashHeight = i * stepV;
var vy = Math.Max(0, dashHeight - vLabel.Height * 0.5);
using (context.PushOpacity(.1)) {
context.DrawLine(gridPen, new Point(horizonStart + 1, dashHeight), new Point(width, dashHeight));
}
context.DrawText(vLabel, new Point(horizonStart - vLabel.Width - 8, vy));
}
// Calculate hit boxes
var shapeWidth = Math.Min(32, stepX - 4);
var hitboxes = new List<Rect>();
for (int i = 0; i < samples.Count; i++) {
var h = samples[i].Count * (height - labelHeight) / maxV;
var x = horizonStart + 1 + stepX * i + (stepX - shapeWidth) * 0.5;
var y = height - labelHeight - h;
hitboxes.Add(new Rect(x, y, shapeWidth, h));
}
// Draw shapes
for (int i = 0; i < samples.Count; i++) {
var hLabel = new FormattedText(
samples[i].Name,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
10.0,
LineBrush);
var rect = hitboxes[i];
var xLabel = rect.X - (hLabel.Width - rect.Width) * 0.5;
var yLabel = height - labelHeight + 4;
context.DrawRectangle(ShapeBrush, null, rect);
if (stepX < 32) {
var matrix = Matrix.CreateTranslation(hLabel.Width * 0.5, -hLabel.Height * 0.5) // Center of label
* Matrix.CreateRotation(Math.PI * 0.25) // Rotate
* Matrix.CreateTranslation(xLabel, yLabel); // Move
using (context.PushTransform(matrix)) {
context.DrawText(hLabel, new Point(0, 0));
}
} else {
context.DrawText(hLabel, new Point(xLabel, yLabel));
}
}
// Draw labels on hover
for (int i = 0; i < samples.Count; i++) {
var rect = hitboxes[i];
if (rect.Contains(_mousePos)) {
var tooltip = new FormattedText(
$"{samples[i].Count}",
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
12.0,
LineBrush);
var tx = rect.X - (tooltip.Width - rect.Width) * 0.5;
var ty = rect.Y - tooltip.Height - 4;
context.DrawText(tooltip, new Point(tx, ty));
break;
}
}
}
protected override void OnPointerMoved(PointerEventArgs e) {
base.OnPointerMoved(e);
_mousePos = e.GetPosition(this);
InvalidateVisual();
}
private Point _mousePos = new Point(0, 0);
}
public partial class Statistics : Window {
public Statistics() {
InitializeComponent();
}
private void CloseWindow(object sender, RoutedEventArgs e) {
Close();
}
}
}