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:
5 files changed
+1351
No files matched your search
@@ -0,0 +1,202 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using Titano.Core.Localization;
|
||||
|
||||
namespace Titano.Ui.Visuals;
|
||||
|
||||
/// <summary>
|
||||
/// Library timeline mode (BRIEF §12.2): one band per year with its label on the left and a density bar per slot
|
||||
/// (month) on a scale shared by all bands, slot labels underneath and a divider between bands. The current slot
|
||||
/// (<see cref="CurrentBand"/>, <see cref="CurrentSlot"/>) is outlined in Accent; clicking a slot makes it current
|
||||
/// and raises <see cref="SlotActivatedEvent"/>. Desired height = bands × <see cref="BandHeight"/>, so the control
|
||||
/// scrolls inside a ScrollViewer.
|
||||
/// </summary>
|
||||
public sealed class TimelineBands : DrawingElement
|
||||
{
|
||||
public static readonly DependencyProperty BandsProperty = DependencyProperty.Register(
|
||||
nameof(Bands), typeof(IReadOnlyList<ChartRow>), typeof(TimelineBands), new PropertyMetadata(null, OnDataPropertyChanged));
|
||||
|
||||
public static readonly DependencyProperty SlotLabelsProperty = DependencyProperty.Register(
|
||||
nameof(SlotLabels), typeof(IReadOnlyList<string>), typeof(TimelineBands), new PropertyMetadata(null, OnDataPropertyChanged));
|
||||
|
||||
public static readonly DependencyProperty BrushKeyProperty = DependencyProperty.Register(
|
||||
nameof(BrushKey), typeof(string), typeof(TimelineBands), new FrameworkPropertyMetadata("Brush.Accent", FrameworkPropertyMetadataOptions.AffectsRender));
|
||||
|
||||
public static readonly DependencyProperty BandHeightProperty = DependencyProperty.Register(
|
||||
nameof(BandHeight), typeof(double), typeof(TimelineBands),
|
||||
new FrameworkPropertyMetadata(64.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure), v => v is double d && d >= 24);
|
||||
|
||||
public static readonly DependencyProperty CurrentBandProperty = DependencyProperty.Register(
|
||||
nameof(CurrentBand), typeof(int), typeof(TimelineBands),
|
||||
new FrameworkPropertyMetadata(-1, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.AffectsRender));
|
||||
|
||||
public static readonly DependencyProperty CurrentSlotProperty = DependencyProperty.Register(
|
||||
nameof(CurrentSlot), typeof(int), typeof(TimelineBands),
|
||||
new FrameworkPropertyMetadata(-1, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.AffectsRender));
|
||||
|
||||
public static readonly DependencyProperty ValueFormatterProperty = DependencyProperty.Register(
|
||||
nameof(ValueFormatter), typeof(Func<double, string>), typeof(TimelineBands), new PropertyMetadata(null));
|
||||
|
||||
public static readonly RoutedEvent SlotActivatedEvent = EventManager.RegisterRoutedEvent(
|
||||
nameof(SlotActivated), RoutingStrategy.Bubble, typeof(ChartItemEventHandler), typeof(TimelineBands));
|
||||
|
||||
private const double LabelColumn = 64;
|
||||
private const double LabelSize = 11;
|
||||
|
||||
private (int Band, int Slot) _hover = (-1, -1);
|
||||
|
||||
/// <summary>One band per year: label + one value per slot.</summary>
|
||||
public IReadOnlyList<ChartRow>? Bands { get => (IReadOnlyList<ChartRow>?)GetValue(BandsProperty); set => SetValue(BandsProperty, value); }
|
||||
|
||||
/// <summary>Slot captions (culture-aware month initials); also fixes the slot count.</summary>
|
||||
public IReadOnlyList<string>? SlotLabels { get => (IReadOnlyList<string>?)GetValue(SlotLabelsProperty); set => SetValue(SlotLabelsProperty, value); }
|
||||
|
||||
public string BrushKey { get => (string)GetValue(BrushKeyProperty); set => SetValue(BrushKeyProperty, value); }
|
||||
|
||||
public double BandHeight { get => (double)GetValue(BandHeightProperty); set => SetValue(BandHeightProperty, value); }
|
||||
|
||||
/// <summary>Band of the current slot (two-way); −1 = none.</summary>
|
||||
public int CurrentBand { get => (int)GetValue(CurrentBandProperty); set => SetValue(CurrentBandProperty, value); }
|
||||
|
||||
/// <summary>Current slot inside <see cref="CurrentBand"/> (two-way); −1 = none.</summary>
|
||||
public int CurrentSlot { get => (int)GetValue(CurrentSlotProperty); set => SetValue(CurrentSlotProperty, value); }
|
||||
|
||||
/// <summary>Culture-aware value formatting for readouts.</summary>
|
||||
public Func<double, string>? ValueFormatter { get => (Func<double, string>?)GetValue(ValueFormatterProperty); set => SetValue(ValueFormatterProperty, value); }
|
||||
|
||||
public event ChartItemEventHandler SlotActivated
|
||||
{
|
||||
add => AddHandler(SlotActivatedEvent, value);
|
||||
remove => RemoveHandler(SlotActivatedEvent, value);
|
||||
}
|
||||
|
||||
private int SlotCount
|
||||
{
|
||||
get
|
||||
{
|
||||
if (SlotLabels is { Count: > 0 } labels) return labels.Count;
|
||||
var max = 0;
|
||||
if (Bands is { } bands)
|
||||
for (var b = 0; b < bands.Count; b++) max = Math.Max(max, bands[b].Values.Count);
|
||||
return max;
|
||||
}
|
||||
}
|
||||
|
||||
private double SlotPitch => SlotCount == 0 ? 0 : Math.Max(0, ActualWidth - LabelColumn) / SlotCount;
|
||||
|
||||
protected override Size MeasureOverride(Size availableSize) => new(0, (Bands?.Count ?? 0) * BandHeight);
|
||||
|
||||
protected override void OnDataChanged()
|
||||
{
|
||||
_hover = (-1, -1);
|
||||
base.OnDataChanged();
|
||||
}
|
||||
|
||||
protected override void OnRender(DrawingContext dc)
|
||||
{
|
||||
DrawHitArea(dc);
|
||||
if (Bands is not { Count: > 0 } bands || SlotCount == 0) return;
|
||||
var slots = SlotCount;
|
||||
var pitch = SlotPitch;
|
||||
var scale = PixelScale;
|
||||
var max = 0.0;
|
||||
for (var b = 0; b < bands.Count; b++)
|
||||
for (var s = 0; s < bands[b].Values.Count; s++)
|
||||
if (double.IsFinite(bands[b].Values[s])) max = Math.Max(max, bands[b].Values[s]);
|
||||
if (max <= 0) max = 1;
|
||||
|
||||
var text = GetBrush("Brush.Text");
|
||||
var faint = GetBrush("Brush.TextFaint");
|
||||
var fill = GetBrush(BrushKey);
|
||||
var divider = GetHairline("Brush.Stroke");
|
||||
var captionHeight = Math.Ceiling(CreateText("0", LabelSize, faint).Height);
|
||||
var barWidth = Math.Max(1, Math.Min(pitch * 0.6, 28));
|
||||
|
||||
for (var b = 0; b < bands.Count; b++)
|
||||
{
|
||||
var band = bands[b];
|
||||
var top = b * BandHeight;
|
||||
var baseline = top + BandHeight - captionHeight - 8;
|
||||
var barSpace = baseline - top - 8;
|
||||
var title = CreateText(band.Label, 13, text, semiBold: true);
|
||||
dc.DrawText(title, new Point(0, top + (BandHeight - title.Height) / 2));
|
||||
for (var s = 0; s < slots; s++)
|
||||
{
|
||||
var cx = LabelColumn + pitch * (s + 0.5);
|
||||
var value = s < band.Values.Count ? band.Values[s] : double.NaN;
|
||||
if (double.IsFinite(value) && value > 0)
|
||||
{
|
||||
var h = Math.Max(1, barSpace * Math.Clamp(value / max, 0, 1));
|
||||
dc.DrawRoundedRectangle(fill, null, new Rect(cx - barWidth / 2, baseline - h, barWidth, h), 2, 2);
|
||||
}
|
||||
if (SlotLabels is { } labels && s < labels.Count)
|
||||
{
|
||||
var caption = CreateText(labels[s], LabelSize, faint);
|
||||
dc.DrawText(caption, new Point(cx - caption.Width / 2, baseline + 4));
|
||||
}
|
||||
if (b == CurrentBand && s == CurrentSlot)
|
||||
dc.DrawRoundedRectangle(null, GetPen("Brush.Accent", 1.5), new Rect(cx - pitch / 2 + 2, top + 4, pitch - 4, BandHeight - 8), 4, 4);
|
||||
}
|
||||
if (b < bands.Count - 1)
|
||||
{
|
||||
var y = PixelGrid.SnapLine(top + BandHeight, divider.Thickness, scale);
|
||||
dc.DrawLine(divider, new Point(0, y), new Point(ActualWidth, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseMove(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseMove(e);
|
||||
var p = e.GetPosition(this);
|
||||
_hover = HitTest(p);
|
||||
Readout.Show(_hover.Band >= 0 ? ReadoutText(_hover.Band, _hover.Slot) : null, p);
|
||||
}
|
||||
|
||||
protected override void OnMouseLeave(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseLeave(e);
|
||||
_hover = (-1, -1);
|
||||
Readout.Hide();
|
||||
}
|
||||
|
||||
protected override void OnQueryCursor(QueryCursorEventArgs e)
|
||||
{
|
||||
base.OnQueryCursor(e);
|
||||
if (e.Handled || _hover.Band < 0) return;
|
||||
e.Cursor = Cursors.Hand;
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
|
||||
{
|
||||
base.OnMouseLeftButtonUp(e);
|
||||
var (band, slot) = HitTest(e.GetPosition(this));
|
||||
if (band < 0) return;
|
||||
SetCurrentValue(CurrentBandProperty, band);
|
||||
SetCurrentValue(CurrentSlotProperty, slot);
|
||||
RaiseEvent(new ChartItemEventArgs(SlotActivatedEvent, band, slot));
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private (int Band, int Slot) HitTest(Point p)
|
||||
{
|
||||
var count = Bands?.Count ?? 0;
|
||||
var pitch = SlotPitch;
|
||||
if (count == 0 || pitch <= 0 || p.X < LabelColumn) return (-1, -1);
|
||||
var band = (int)Math.Floor(p.Y / BandHeight);
|
||||
var slot = (int)Math.Floor((p.X - LabelColumn) / pitch);
|
||||
return band >= 0 && band < count && slot >= 0 && slot < SlotCount ? (band, slot) : (-1, -1);
|
||||
}
|
||||
|
||||
private string ReadoutText(int band, int slot)
|
||||
{
|
||||
var row = Bands![band];
|
||||
if (row.ToolTips is { } tips && slot < tips.Count && !string.IsNullOrEmpty(tips[slot])) return tips[slot]!;
|
||||
var value = slot < row.Values.Count ? row.Values[slot] : double.NaN;
|
||||
var caption = SlotLabels is { } labels && slot < labels.Count ? " " + labels[slot] : string.Empty;
|
||||
var format = ValueFormatter ?? (v => v.ToString("N0", Loc.Culture));
|
||||
return row.Label + caption + (double.IsFinite(value) ? " · " + format(value) : string.Empty);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user