feat(ui-visuals): add series, bar, heatmap, stacked and timeline charts

SeriesChart decimates each line to two points per pixel column and adds a
legend row, nice-tick gridlines, dashed markers, a playhead, click-to-seek
and a hover readout. BarChart, HeatmapStrip, StackedBar and TimelineBands
cover the Archive statistics and the Library timeline bands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:27:09 +00:00
1 parent 3ba68fef1d
commit df4af1013a
5 files changed
+1351

No files matched your search

+277
View File
@@ -0,0 +1,277 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Titano.Core.Localization;
namespace Titano.Ui.Visuals;
/// <summary>
/// Bars for labelled values (Archive: storage by year, by camera, by format; uploads per day). Horizontal bars put
/// the category labels on the left and the value labels after each bar, as in the Archive mockup; vertical bars
/// put the categories underneath (skipping labels that would collide). Optional value gridlines use nice ticks.
/// A bar's opacity can follow its value (<see cref="MinimumOpacity"/>). Hovering reads out the bar; clicking raises
/// <see cref="ItemActivatedEvent"/>.
/// </summary>
public sealed class BarChart : DrawingElement
{
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
nameof(Items), typeof(IReadOnlyList<ChartItem>), typeof(BarChart), new PropertyMetadata(null, OnDataPropertyChanged));
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
nameof(Orientation), typeof(Orientation), typeof(BarChart),
new FrameworkPropertyMetadata(Orientation.Horizontal, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public static readonly DependencyProperty BrushKeyProperty = DependencyProperty.Register(
nameof(BrushKey), typeof(string), typeof(BarChart), new FrameworkPropertyMetadata("Brush.Accent", FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty MinimumOpacityProperty = DependencyProperty.Register(
nameof(MinimumOpacity), typeof(double), typeof(BarChart), new FrameworkPropertyMetadata(1.0, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty ShowValuesProperty = DependencyProperty.Register(
nameof(ShowValues), typeof(bool), typeof(BarChart), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty ShowGridProperty = DependencyProperty.Register(
nameof(ShowGrid), typeof(bool), typeof(BarChart), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(
nameof(Maximum), typeof(double), typeof(BarChart), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty BarThicknessProperty = DependencyProperty.Register(
nameof(BarThickness), typeof(double), typeof(BarChart),
new FrameworkPropertyMetadata(14.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public static readonly DependencyProperty ValueFormatterProperty = DependencyProperty.Register(
nameof(ValueFormatter), typeof(Func<double, string>), typeof(BarChart), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly RoutedEvent ItemActivatedEvent = EventManager.RegisterRoutedEvent(
nameof(ItemActivated), RoutingStrategy.Bubble, typeof(ChartItemEventHandler), typeof(BarChart));
private const double LabelSize = 11;
private const double RowGap = 8;
private Rect _area;
private double _pitch;
private int _hover = -1;
/// <summary>Bars in display order.</summary>
public IReadOnlyList<ChartItem>? Items { get => (IReadOnlyList<ChartItem>?)GetValue(ItemsProperty); set => SetValue(ItemsProperty, value); }
public Orientation Orientation { get => (Orientation)GetValue(OrientationProperty); set => SetValue(OrientationProperty, value); }
/// <summary>Theme brush key of the bars (items may override).</summary>
public string BrushKey { get => (string)GetValue(BrushKeyProperty); set => SetValue(BrushKeyProperty, value); }
/// <summary>Opacity of a zero bar; bars ramp linearly to 1 at the maximum (1 = flat colour).</summary>
public double MinimumOpacity { get => (double)GetValue(MinimumOpacityProperty); set => SetValue(MinimumOpacityProperty, value); }
public bool ShowValues { get => (bool)GetValue(ShowValuesProperty); set => SetValue(ShowValuesProperty, value); }
/// <summary>Value-axis gridlines and tick labels.</summary>
public bool ShowGrid { get => (bool)GetValue(ShowGridProperty); set => SetValue(ShowGridProperty, value); }
/// <summary>Value at full length; NaN = the largest value (or the nice tick above it with the grid on).</summary>
public double Maximum { get => (double)GetValue(MaximumProperty); set => SetValue(MaximumProperty, value); }
/// <summary>Maximum bar thickness in DIPs.</summary>
public double BarThickness { get => (double)GetValue(BarThicknessProperty); set => SetValue(BarThicknessProperty, value); }
/// <summary>Culture-aware value formatting for value labels, ticks and readouts.</summary>
public Func<double, string>? ValueFormatter { get => (Func<double, string>?)GetValue(ValueFormatterProperty); set => SetValue(ValueFormatterProperty, value); }
public event ChartItemEventHandler ItemActivated
{
add => AddHandler(ItemActivatedEvent, value);
remove => RemoveHandler(ItemActivatedEvent, value);
}
protected override Size MeasureOverride(Size availableSize)
{
var count = Items?.Count ?? 0;
return Orientation == Orientation.Horizontal ? new Size(0, count * (BarThickness + RowGap)) : new Size(0, 120);
}
protected override void OnDataChanged()
{
_hover = -1;
base.OnDataChanged();
}
protected override void OnRender(DrawingContext dc)
{
DrawHitArea(dc);
_area = Rect.Empty;
if (Items is not { Count: > 0 } items) return;
var max = ScaleMaximum(items);
if (Orientation == Orientation.Horizontal) RenderHorizontal(dc, items, max);
else RenderVertical(dc, items, max);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
var p = e.GetPosition(this);
_hover = HitTest(p);
Readout.Show(_hover >= 0 && Items is { } items ? ReadoutText(items[_hover]) : null, p);
}
protected override void OnQueryCursor(QueryCursorEventArgs e)
{
base.OnQueryCursor(e);
if (e.Handled || _hover < 0) return;
e.Cursor = Cursors.Hand;
e.Handled = true;
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
_hover = -1;
Readout.Hide();
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
var index = HitTest(e.GetPosition(this));
if (index < 0) return;
RaiseEvent(new ChartItemEventArgs(ItemActivatedEvent, index, -1));
e.Handled = true;
}
private string Format(double value) => (ValueFormatter ?? DefaultFormat)(value);
private static string DefaultFormat(double value) => value.ToString("N0", Loc.Culture);
private string ReadoutText(ChartItem item) => item.ToolTip ?? item.Label + " · " + (item.ValueText ?? Format(item.Value));
private double ScaleMaximum(IReadOnlyList<ChartItem> items)
{
if (double.IsFinite(Maximum) && Maximum > 0) return Maximum;
var max = 0.0;
for (var i = 0; i < items.Count; i++) max = Math.Max(max, items[i].Value);
if (max <= 0) return 1;
return ShowGrid ? NiceScale.Outer(0, max, 5).Last : max;
}
private double OpacityOf(double value, double max) =>
MinimumOpacity >= 1 ? 1 : MinimumOpacity + (1 - MinimumOpacity) * Math.Clamp(value / max, 0, 1);
private void RenderHorizontal(DrawingContext dc, IReadOnlyList<ChartItem> items, double max)
{
var faint = GetBrush("Brush.TextFaint");
var text = GetBrush("Brush.Text");
var labels = new FormattedText[items.Count];
var values = new FormattedText?[items.Count];
double labelWidth = 0, valueWidth = 0;
for (var i = 0; i < items.Count; i++)
{
labels[i] = CreateText(items[i].Label, LabelSize, faint);
labelWidth = Math.Max(labelWidth, labels[i].Width);
if (!ShowValues) continue;
values[i] = CreateText(items[i].ValueText ?? Format(items[i].Value), LabelSize, text);
valueWidth = Math.Max(valueWidth, values[i]!.Width);
}
var gridHeight = ShowGrid ? 16 : 0;
var left = Math.Ceiling(labelWidth) + 8;
var right = ActualWidth - (ShowValues ? valueWidth + 6 : 0);
_area = new Rect(left, 0, Math.Max(0, right - left), Math.Max(0, ActualHeight - gridHeight));
_pitch = Math.Min(_area.Height / items.Count, BarThickness + RowGap);
var thickness = Math.Max(1, Math.Min(BarThickness, _pitch - 2));
if (ShowGrid) DrawValueGrid(dc, max, horizontal: true);
for (var i = 0; i < items.Count; i++)
{
var item = items[i];
var cy = _area.Top + _pitch * (i + 0.5);
dc.DrawText(labels[i], new Point(left - 8 - labels[i].Width, cy - labels[i].Height / 2));
var length = _area.Width * Math.Clamp(item.Value / max, 0, 1);
if (length > 0)
{
dc.PushOpacity(OpacityOf(item.Value, max));
dc.DrawRoundedRectangle(GetBrush(item.BrushKey ?? BrushKey), null, new Rect(left, cy - thickness / 2, length, thickness), 2, 2);
dc.Pop();
}
if (values[i] is { } value) dc.DrawText(value, new Point(left + length + 6, cy - value.Height / 2));
}
}
private void RenderVertical(DrawingContext dc, IReadOnlyList<ChartItem> items, double max)
{
var faint = GetBrush("Brush.TextFaint");
var sample = CreateText("0", LabelSize, faint);
var labelHeight = Math.Ceiling(sample.Height) + 4;
var valueHeight = ShowValues ? labelHeight : 0;
var axisWidth = 0.0;
if (ShowGrid)
{
var top = CreateText(Format(max), LabelSize, faint);
axisWidth = Math.Ceiling(top.Width) + 8;
}
_area = new Rect(axisWidth, valueHeight, Math.Max(0, ActualWidth - axisWidth), Math.Max(0, ActualHeight - labelHeight - valueHeight));
_pitch = _area.Width / items.Count;
var width = Math.Max(1, Math.Min(BarThickness * 2, _pitch * 0.7));
if (ShowGrid) DrawValueGrid(dc, max, horizontal: false);
var text = GetBrush("Brush.Text");
var lastLabelRight = double.NegativeInfinity;
for (var i = 0; i < items.Count; i++)
{
var item = items[i];
var cx = _area.Left + _pitch * (i + 0.5);
var height = _area.Height * Math.Clamp(item.Value / max, 0, 1);
if (height > 0)
{
dc.PushOpacity(OpacityOf(item.Value, max));
dc.DrawRoundedRectangle(GetBrush(item.BrushKey ?? BrushKey), null, new Rect(cx - width / 2, _area.Bottom - height, width, height), 2, 2);
dc.Pop();
}
if (ShowValues && _pitch >= 24)
{
var value = CreateText(item.ValueText ?? Format(item.Value), LabelSize, text);
if (value.Width <= _pitch) dc.DrawText(value, new Point(cx - value.Width / 2, _area.Bottom - height - value.Height - 2));
}
var label = CreateText(item.Label, LabelSize, faint);
var x = cx - label.Width / 2;
if (x < lastLabelRight + 6) continue;
dc.DrawText(label, new Point(x, _area.Bottom + 4));
lastLabelRight = x + label.Width;
}
}
/// <summary>Gridlines at nice ticks along the value axis, with labels below (horizontal bars) or left (vertical bars).</summary>
private void DrawValueGrid(DrawingContext dc, double max, bool horizontal)
{
var ticks = NiceScale.Inner(0, max, horizontal ? Math.Max(2, (int)(_area.Width / 80)) : Math.Max(2, (int)(_area.Height / 36)));
var pen = GetHairline("Brush.Stroke");
var faint = GetBrush("Brush.TextFaint");
var scale = PixelScale;
for (var i = 0; i < ticks.Count; i++)
{
var label = CreateText(Format(ticks[i]), LabelSize, faint);
if (horizontal)
{
var x = PixelGrid.SnapLine(_area.Left + _area.Width * ticks[i] / max, pen.Thickness, scale);
dc.DrawLine(pen, new Point(x, _area.Top), new Point(x, _area.Bottom));
dc.DrawText(label, new Point(Math.Min(x - label.Width / 2, ActualWidth - label.Width), _area.Bottom + 2));
}
else
{
var y = PixelGrid.SnapLine(_area.Bottom - _area.Height * ticks[i] / max, pen.Thickness, scale);
dc.DrawLine(pen, new Point(_area.Left, y), new Point(_area.Right, y));
dc.DrawText(label, new Point(_area.Left - 8 - label.Width, y - label.Height / 2));
}
}
}
private int HitTest(Point p)
{
var count = Items?.Count ?? 0;
if (count == 0 || _area.IsEmpty || !(_pitch > 0)) return -1;
var index = Orientation == Orientation.Horizontal
? (int)Math.Floor((p.Y - _area.Top) / _pitch)
: (int)Math.Floor((p.X - _area.Left) / _pitch);
var inside = Orientation == Orientation.Horizontal ? p.X >= 0 && p.X <= ActualWidth : p.Y >= 0 && p.Y <= ActualHeight;
return inside && index >= 0 && index < count ? index : -1;
}
}