mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-29 16:14:59 +00:00
feature: enhanced statistics panel (#493)
* replace the `YEAR` tab with `OVERVIEW` tab, which will analyze most recent 20K commits * use `LiveChartsCore.SkiaSharpView.Avalonia` instead of a custom chart view
This commit is contained in:
parent
983607e708
commit
af57c56cd7
13 changed files with 147 additions and 310 deletions
|
@ -5,6 +5,7 @@
|
|||
xmlns:m="using:SourceGit.Models"
|
||||
xmlns:vm="using:SourceGit.ViewModels"
|
||||
xmlns:v="using:SourceGit.Views"
|
||||
xmlns:lvc="using:LiveChartsCore.SkiaSharpView.Avalonia"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="SourceGit.Views.Statistics"
|
||||
x:DataType="vm:Statistics"
|
||||
|
@ -62,6 +63,7 @@
|
|||
|
||||
<ListBox.Styles>
|
||||
<Style Selector="ListBoxItem">
|
||||
<Setter Property="Width" Value="110"/>
|
||||
<Setter Property="Height" Value="28"/>
|
||||
<Setter Property="Padding" Value="0"/>
|
||||
<Setter Property="Background" Value="Transparent"/>
|
||||
|
@ -90,6 +92,7 @@
|
|||
<Style Selector="TextBlock.view_mode_switcher">
|
||||
<Setter Property="FontWeight" Value="Bold"/>
|
||||
<Setter Property="Foreground" Value="{DynamicResource Brush.FG2}"/>
|
||||
<Setter Property="HorizontalAlignment" Value="Center"/>
|
||||
</Style>
|
||||
|
||||
<Style Selector="ListBoxItem:pointerover TextBlock.view_mode_switcher">
|
||||
|
@ -103,7 +106,7 @@
|
|||
|
||||
<ListBoxItem>
|
||||
<Border Classes="switcher_bg">
|
||||
<TextBlock Classes="view_mode_switcher" Text="{DynamicResource Text.Statistics.MostRecentYear}"/>
|
||||
<TextBlock Classes="view_mode_switcher" Text="{DynamicResource Text.Statistics.Overview}"/>
|
||||
</Border>
|
||||
</ListBoxItem>
|
||||
|
||||
|
@ -128,7 +131,7 @@
|
|||
<Grid Grid.Column="0" RowDefinitions="*,16">
|
||||
<!-- Table By Committer -->
|
||||
<ListBox Grid.Column="0"
|
||||
ItemsSource="{Binding ByAuthor}"
|
||||
ItemsSource="{Binding Authors}"
|
||||
SelectionMode="Single"
|
||||
BorderThickness="1"
|
||||
BorderBrush="{DynamicResource Brush.Border2}"
|
||||
|
@ -150,7 +153,7 @@
|
|||
</ListBox.ItemsPanel>
|
||||
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate DataType="m:StatisticsSample">
|
||||
<DataTemplate DataType="m:StaticsticsAuthor">
|
||||
<Border BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}">
|
||||
<Grid ColumnDefinitions="*,100">
|
||||
<Border Grid.Column="0" Padding="8,0" ClipToBounds="True">
|
||||
|
@ -170,7 +173,7 @@
|
|||
<!-- Total Committers -->
|
||||
<StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Bottom">
|
||||
<TextBlock Classes="primary" Text="{DynamicResource Text.Statistics.TotalAuthors}" FontSize="11" Foreground="{DynamicResource Brush.FG2}"/>
|
||||
<TextBlock Classes="primary" Text="{Binding ByAuthor.Count}" FontSize="11" Margin="4,0,0,0"/>
|
||||
<TextBlock Classes="primary" Text="{Binding Authors.Count}" FontSize="11" Margin="4,0,0,0"/>
|
||||
</StackPanel>
|
||||
|
||||
<!-- Total Commits -->
|
||||
|
@ -182,12 +185,7 @@
|
|||
</Grid>
|
||||
|
||||
<!-- Graph -->
|
||||
<v:Chart Grid.Column="1"
|
||||
Margin="16"
|
||||
LabelBrush="{DynamicResource Brush.FG1}"
|
||||
LineBrush="{DynamicResource Brush.Border2}"
|
||||
ShapeBrush="{DynamicResource Brush.Accent}"
|
||||
Samples="{Binding Samples}"/>
|
||||
<lvc:CartesianChart Grid.Column="1" Margin="16" Series="{Binding Series}" XAxes="{Binding XAxes}" YAxes="{Binding YAxes}" ZoomMode="X"/>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ContentControl.DataTemplates>
|
||||
|
|
|
@ -1,220 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Media;
|
||||
|
||||
namespace SourceGit.Views
|
||||
{
|
||||
public class Chart : Control
|
||||
{
|
||||
public static readonly StyledProperty<IBrush> LabelBrushProperty =
|
||||
AvaloniaProperty.Register<Chart, IBrush>(nameof(LabelBrush), Brushes.Black);
|
||||
|
||||
public IBrush LabelBrush
|
||||
{
|
||||
get => GetValue(LabelBrushProperty);
|
||||
set => SetValue(LabelBrushProperty, 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.StatisticsSample>> SamplesProperty =
|
||||
AvaloniaProperty.Register<Chart, List<Models.StatisticsSample>>(nameof(Samples));
|
||||
|
||||
public List<Models.StatisticsSample> Samples
|
||||
{
|
||||
get => GetValue(SamplesProperty);
|
||||
set => SetValue(SamplesProperty, value);
|
||||
}
|
||||
|
||||
static Chart()
|
||||
{
|
||||
SamplesProperty.Changed.AddClassHandler<Chart>((c, _) =>
|
||||
{
|
||||
c._hitBoxes.Clear();
|
||||
c._lastHitIdx = -1;
|
||||
c.InvalidateMeasure();
|
||||
});
|
||||
}
|
||||
|
||||
public override void Render(DrawingContext context)
|
||||
{
|
||||
if (Samples == null || Bounds.Width == 0)
|
||||
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("fonts:SourceGit#JetBrains Mono");
|
||||
var pen = new Pen(LineBrush);
|
||||
var width = Bounds.Width;
|
||||
var height = Bounds.Height;
|
||||
|
||||
// Draw coordinate
|
||||
var maxLabel = new FormattedText($"{maxV}", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 12.0, LabelBrush);
|
||||
var horizonStart = maxLabel.Width + 8;
|
||||
var labelHeight = maxLabel.Height;
|
||||
var bg = this.FindResource("Brush.Contents") as IBrush;
|
||||
context.DrawText(maxLabel, new Point(0, -maxLabel.Height * 0.5));
|
||||
context.DrawRectangle(bg, pen, new Rect(horizonStart, 0, width - horizonStart, 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() { Dashes = [2, 2, 0, 2], Offset = 1 });
|
||||
for (int i = 1; i < 5; i++)
|
||||
{
|
||||
var vLabel = new FormattedText(
|
||||
$"{maxV - i * labelStepV}",
|
||||
CultureInfo.CurrentCulture,
|
||||
FlowDirection.LeftToRight,
|
||||
typeface,
|
||||
12.0,
|
||||
LabelBrush);
|
||||
|
||||
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
|
||||
if (_hitBoxes.Count == 0)
|
||||
{
|
||||
var shapeWidth = Math.Min(32, stepX - 4);
|
||||
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 - 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Draw shapes
|
||||
for (int i = 0; i < samples.Count; i++)
|
||||
{
|
||||
var hLabel = new FormattedText(
|
||||
samples[i].Name,
|
||||
CultureInfo.CurrentCulture,
|
||||
FlowDirection.LeftToRight,
|
||||
typeface,
|
||||
10.0,
|
||||
LabelBrush);
|
||||
var rect = _hitBoxes[i];
|
||||
var xLabel = rect.X - (hLabel.Width - rect.Width) * 0.5;
|
||||
var yLabel = height - labelHeight + 4;
|
||||
|
||||
context.DrawRectangle(ShapeBrush, null, rect);
|
||||
|
||||
var test = (stepX - 4) / hLabel.Width;
|
||||
if (test < 1.0)
|
||||
{
|
||||
var matrix = Matrix.CreateRotation(Math.Acos(test)) * Matrix.CreateTranslation(xLabel + hLabel.Width * 0.5, yLabel);
|
||||
using (context.PushTransform(matrix))
|
||||
context.DrawText(hLabel, new Point(0, 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
context.DrawText(hLabel, new Point(xLabel, yLabel));
|
||||
}
|
||||
}
|
||||
|
||||
// Draw labels on hover
|
||||
if (_lastHitIdx >= 0 && _lastHitIdx < samples.Count)
|
||||
{
|
||||
var rect = _hitBoxes[_lastHitIdx];
|
||||
var tooltip = new FormattedText(
|
||||
$"{samples[_lastHitIdx].Count}",
|
||||
CultureInfo.CurrentCulture,
|
||||
FlowDirection.LeftToRight,
|
||||
typeface,
|
||||
12.0,
|
||||
LabelBrush);
|
||||
|
||||
var tx = rect.X - (tooltip.Width - rect.Width) * 0.5;
|
||||
var ty = rect.Y - tooltip.Height - 4;
|
||||
context.DrawText(tooltip, new Point(tx, ty));
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnPointerMoved(PointerEventArgs e)
|
||||
{
|
||||
base.OnPointerMoved(e);
|
||||
|
||||
var p = e.GetPosition(this);
|
||||
for (int i = 0; i < _hitBoxes.Count; i++)
|
||||
{
|
||||
if (_hitBoxes[i].Contains(p))
|
||||
{
|
||||
if (_lastHitIdx != i)
|
||||
{
|
||||
_lastHitIdx = i;
|
||||
InvalidateVisual();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (_lastHitIdx != -1)
|
||||
{
|
||||
_lastHitIdx = -1;
|
||||
InvalidateVisual();
|
||||
}
|
||||
}
|
||||
|
||||
private readonly List<Rect> _hitBoxes = new List<Rect>();
|
||||
private int _lastHitIdx = -1;
|
||||
}
|
||||
|
||||
public partial class Statistics : ChromelessWindow
|
||||
{
|
||||
public Statistics()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue