2024-03-17 18:37:06 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
|
2024-02-23 03:16:28 -08:00
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Input;
|
|
|
|
using Avalonia.Interactivity;
|
|
|
|
using Avalonia.Media;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
namespace SourceGit.Views
|
|
|
|
{
|
|
|
|
public class Chart : Control
|
|
|
|
{
|
2024-06-19 02:53:02 -07:00
|
|
|
public static readonly StyledProperty<IBrush> LabelBrushProperty =
|
|
|
|
AvaloniaProperty.Register<Chart, IBrush>(nameof(LabelBrush), Brushes.Black);
|
|
|
|
|
|
|
|
public IBrush LabelBrush
|
|
|
|
{
|
|
|
|
get => GetValue(LabelBrushProperty);
|
|
|
|
set => SetValue(LabelBrushProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-02-23 03:16:28 -08:00
|
|
|
public static readonly StyledProperty<IBrush> LineBrushProperty =
|
|
|
|
AvaloniaProperty.Register<Chart, IBrush>(nameof(LineBrush), Brushes.Gray);
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public IBrush LineBrush
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
get => GetValue(LineBrushProperty);
|
|
|
|
set => SetValue(LineBrushProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<IBrush> ShapeBrushProperty =
|
|
|
|
AvaloniaProperty.Register<Chart, IBrush>(nameof(ShapeBrush), Brushes.Gray);
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public IBrush ShapeBrush
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
get => GetValue(ShapeBrushProperty);
|
|
|
|
set => SetValue(ShapeBrushProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-02-24 19:32:15 -08:00
|
|
|
public static readonly StyledProperty<List<Models.StatisticsSample>> SamplesProperty =
|
|
|
|
AvaloniaProperty.Register<Chart, List<Models.StatisticsSample>>(nameof(Samples), null);
|
2024-02-23 03:16:28 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public List<Models.StatisticsSample> Samples
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
get => GetValue(SamplesProperty);
|
|
|
|
set => SetValue(SamplesProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
static Chart()
|
|
|
|
{
|
|
|
|
SamplesProperty.Changed.AddClassHandler<Chart>((c, e) =>
|
|
|
|
{
|
2024-03-04 04:50:10 -08:00
|
|
|
c._hitBoxes.Clear();
|
|
|
|
c._lastHitIdx = -1;
|
2024-05-13 01:39:48 -07:00
|
|
|
c.InvalidateMeasure();
|
2024-03-04 04:50:10 -08:00
|
|
|
});
|
2024-02-23 03:16:28 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
2024-05-13 01:39:48 -07:00
|
|
|
if (Samples == null || Bounds.Width == 0)
|
2024-03-31 01:54:29 -07:00
|
|
|
return;
|
2024-02-23 03:16:28 -08:00
|
|
|
|
|
|
|
var samples = Samples;
|
|
|
|
int maxV = 0;
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var s in samples)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (maxV < s.Count)
|
|
|
|
maxV = s.Count;
|
2024-02-23 03:16:28 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (maxV < 5)
|
2024-02-23 03:16:28 -08:00
|
|
|
maxV = 5;
|
2024-03-17 18:37:06 -07:00
|
|
|
else if (maxV < 10)
|
2024-02-23 03:16:28 -08:00
|
|
|
maxV = 10;
|
2024-03-17 18:37:06 -07:00
|
|
|
else if (maxV < 50)
|
2024-02-23 03:16:28 -08:00
|
|
|
maxV = 50;
|
2024-03-17 18:37:06 -07:00
|
|
|
else if (maxV < 100)
|
2024-02-23 03:16:28 -08:00
|
|
|
maxV = 100;
|
2024-03-17 18:37:06 -07:00
|
|
|
else if (maxV < 200)
|
2024-02-23 03:16:28 -08:00
|
|
|
maxV = 200;
|
2024-03-17 18:37:06 -07:00
|
|
|
else if (maxV < 500)
|
2024-02-23 03:16:28 -08:00
|
|
|
maxV = 500;
|
2024-03-17 18:37:06 -07:00
|
|
|
else
|
2024-02-23 03:16:28 -08:00
|
|
|
maxV = (int)Math.Ceiling(maxV / 500.0) * 500;
|
|
|
|
|
2024-03-04 23:53:38 -08:00
|
|
|
var typeface = new Typeface("fonts:SourceGit#JetBrains Mono");
|
2024-02-23 03:16:28 -08:00
|
|
|
var pen = new Pen(LineBrush, 1);
|
|
|
|
var width = Bounds.Width;
|
|
|
|
var height = Bounds.Height;
|
|
|
|
|
2024-03-04 04:50:10 -08:00
|
|
|
// Transparent background to block mouse move events.
|
2024-05-13 01:39:48 -07:00
|
|
|
context.DrawRectangle(Brushes.Transparent, null, new Rect(0, 0, width, height));
|
2024-03-04 04:50:10 -08:00
|
|
|
|
2024-02-23 03:16:28 -08:00
|
|
|
// Draw coordinate
|
2024-06-19 02:53:02 -07:00
|
|
|
var maxLabel = new FormattedText($"{maxV}", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 12.0, LabelBrush);
|
2024-02-23 03:16:28 -08:00
|
|
|
var horizonStart = maxLabel.Width + 8;
|
2024-03-04 01:34:46 -08:00
|
|
|
var labelHeight = maxLabel.Height;
|
2024-02-23 03:16:28 -08:00
|
|
|
context.DrawText(maxLabel, new Point(0, -maxLabel.Height * 0.5));
|
2024-04-03 02:34:06 -07:00
|
|
|
context.DrawRectangle(pen, new Rect(horizonStart, 0, width - horizonStart, height - labelHeight));
|
2024-02-23 03:16:28 -08:00
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
if (samples.Count == 0)
|
|
|
|
return;
|
2024-02-23 03:16:28 -08:00
|
|
|
|
|
|
|
// Draw horizontal lines
|
|
|
|
var stepX = (width - horizonStart) / samples.Count;
|
|
|
|
var stepV = (height - labelHeight) / 5;
|
|
|
|
var labelStepV = maxV / 5;
|
2024-04-03 02:34:06 -07:00
|
|
|
var gridPen = new Pen(LineBrush, 1, new DashStyle() { Dashes = [2, 2, 0, 2], Offset = 1 });
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = 1; i < 5; i++)
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
var vLabel = new FormattedText(
|
|
|
|
$"{maxV - i * labelStepV}",
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
12.0,
|
2024-06-19 02:53:02 -07:00
|
|
|
LabelBrush);
|
2024-02-23 03:16:28 -08:00
|
|
|
|
|
|
|
var dashHeight = i * stepV;
|
|
|
|
var vy = Math.Max(0, dashHeight - vLabel.Height * 0.5);
|
2024-03-17 18:37:06 -07:00
|
|
|
using (context.PushOpacity(.1))
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
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
|
2024-03-17 18:37:06 -07:00
|
|
|
if (_hitBoxes.Count == 0)
|
|
|
|
{
|
2024-03-04 04:50:10 -08:00
|
|
|
var shapeWidth = Math.Min(32, stepX - 4);
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = 0; i < samples.Count; i++)
|
|
|
|
{
|
2024-03-04 04:50:10 -08:00
|
|
|
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));
|
|
|
|
}
|
2024-02-23 03:16:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw shapes
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = 0; i < samples.Count; i++)
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
var hLabel = new FormattedText(
|
|
|
|
samples[i].Name,
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
10.0,
|
2024-06-19 02:53:02 -07:00
|
|
|
LabelBrush);
|
2024-03-04 04:50:10 -08:00
|
|
|
var rect = _hitBoxes[i];
|
2024-02-23 03:16:28 -08:00
|
|
|
var xLabel = rect.X - (hLabel.Width - rect.Width) * 0.5;
|
|
|
|
var yLabel = height - labelHeight + 4;
|
|
|
|
|
|
|
|
context.DrawRectangle(ShapeBrush, null, rect);
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (stepX < 32)
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
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
|
2024-03-17 18:37:06 -07:00
|
|
|
using (context.PushTransform(matrix))
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
context.DrawText(hLabel, new Point(0, 0));
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
context.DrawText(hLabel, new Point(xLabel, yLabel));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw labels on hover
|
2024-03-17 18:37:06 -07:00
|
|
|
if (_lastHitIdx >= 0 && _lastHitIdx < samples.Count)
|
|
|
|
{
|
2024-03-04 04:50:10 -08:00
|
|
|
var rect = _hitBoxes[_lastHitIdx];
|
|
|
|
var tooltip = new FormattedText(
|
|
|
|
$"{samples[_lastHitIdx].Count}",
|
2024-02-23 03:16:28 -08:00
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
12.0,
|
2024-06-19 02:53:02 -07:00
|
|
|
LabelBrush);
|
2024-02-23 03:16:28 -08:00
|
|
|
|
2024-03-04 04:50:10 -08:00
|
|
|
var tx = rect.X - (tooltip.Width - rect.Width) * 0.5;
|
|
|
|
var ty = rect.Y - tooltip.Height - 4;
|
|
|
|
context.DrawText(tooltip, new Point(tx, ty));
|
2024-02-23 03:16:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnPointerMoved(PointerEventArgs e)
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
base.OnPointerMoved(e);
|
2024-03-04 04:50:10 -08:00
|
|
|
|
|
|
|
var p = e.GetPosition(this);
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = 0; i < _hitBoxes.Count; i++)
|
|
|
|
{
|
|
|
|
if (_hitBoxes[i].Contains(p))
|
|
|
|
{
|
|
|
|
if (_lastHitIdx != i)
|
|
|
|
{
|
2024-03-04 04:50:10 -08:00
|
|
|
_lastHitIdx = i;
|
|
|
|
InvalidateVisual();
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
2024-03-04 04:50:10 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (_lastHitIdx != -1)
|
|
|
|
{
|
2024-03-04 04:50:10 -08:00
|
|
|
_lastHitIdx = -1;
|
|
|
|
InvalidateVisual();
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-02-23 03:16:28 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly List<Rect> _hitBoxes = new List<Rect>();
|
2024-03-04 04:50:10 -08:00
|
|
|
private int _lastHitIdx = -1;
|
2024-02-23 03:16:28 -08:00
|
|
|
}
|
|
|
|
|
2024-06-12 20:54:10 -07:00
|
|
|
public partial class Statistics : ChromelessWindow
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
|
|
|
public Statistics()
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void BeginMoveWindow(object sender, PointerPressedEventArgs e)
|
|
|
|
{
|
2024-03-14 03:23:36 -07:00
|
|
|
BeginMoveDrag(e);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void CloseWindow(object sender, RoutedEventArgs e)
|
|
|
|
{
|
2024-02-23 03:16:28 -08:00
|
|
|
Close();
|
|
|
|
}
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
}
|