Sparkline
Since 0.1.0-alpha.22Use a sparkline to show the trend beside a number or in a table row. It has no scale, and each one fits its own values. To compare levels between rows, use a line chart.
A trend beside each row
Give Sparkline the Values and a Label. In a list or table, a sparkline in each row shows that row's trend beside its numbers. Each fills its cell's width, with a dot on the last value, the current one.
Static
Preview
Checkout API
Search
Image resizer
Code
@* Each service's requests per minute over the last 24 hours, one value an hour. *@
<Stack Gap="3" Grow>
foreach (var service in _services)
{
<Grid Template='"8rem minmax(0, 1fr)"' Gap="4" Align="Align.Center">
<TextBlock Size="TextSize.Sm">service.Name</TextBlock>
<Sparkline Values="service.PerHour" Label="($"{service.Name}, requests per minute, last 24 hours")" ValueFormat="N0" />
</Grid>
}
</Stack>
{
private record Service(string Name, double?[] PerHour);
private readonly Service[] _services =
[
new("Checkout API", [210, 190, 160, 140, 130, 135, 170, 260, 410, 520, 580, 600, 640, 610, 590, 570, 560, 540, 500, 470, 420, 360, 300, 240]),
new("Search", [820, 760, 690, 640, 620, 650, 780, 1_040, 1_380, 1_520, 1_610, 1_590, 1_660, 1_640, 1_580, 1_550, 1_500, 1_470, 1_410, 1_330, 1_240, 1_120, 990, 880]),
new("Image resizer", [95, 90, 88, 84, 80, 82, 96, 130, 180, 210, null, null, 205, 230, 220, 214, 208, 196, 180, 170, 150, 130, 118, 104]),
];
}Add this example to a page with the izzy CLI:
izzy add component Sparkline --at <file>:<line>A missing value, null, breaks the line, as the image resizer's outage does here.
A sparkline has no axes or tooltip. ValueFormat formats the values screen readers hear with the label.
Line, area, bars and win-loss
Type sets how a sparkline draws its values: Line (the default), Area, Bar or WinLoss.
Static
Preview
Open tickets
First reply, hours
New tickets a day
Queue shrank or grew
Code
@* The last 30 days of a support queue, drawn four ways. The win-loss bars are how the queue moved
each day: up when it shrank, down when it grew. *@
<Grid Columns="new() { Base = 1, Sm = 2, Lg = 4 }" Gap="6" Grow>
<Stack Gap="2">
<TextBlock Size="TextSize.Sm" Color="Color.Muted">Open tickets</TextBlock>
<Sparkline Values="_open" Label="Open tickets, last 30 days" Height="3.Rem" />
</Stack>
<Stack Gap="2">
<TextBlock Size="TextSize.Sm" Color="Color.Muted">First reply, hours</TextBlock>
<Sparkline Values="_reply" Label="First reply in hours, last 30 days" Type="SparklineType.Area" Height="3.Rem" ValueFormat="N1" />
</Stack>
<Stack Gap="2">
<TextBlock Size="TextSize.Sm" Color="Color.Muted">New tickets a day</TextBlock>
<Sparkline Values="_new" Label="New tickets a day, last 30 days" Type="SparklineType.Bar" Height="3.Rem" />
</Stack>
<Stack Gap="2">
<TextBlock Size="TextSize.Sm" Color="Color.Muted">Queue shrank or grew</TextBlock>
<Sparkline Values="_shrank" Label="Days the queue shrank or grew, last 30 days" Type="SparklineType.WinLoss" Height="3.Rem" />
</Stack>
</Grid>
{
private static readonly double?[] _open = [64, 61, 66, 70, 68, 59, 55, 58, 62, 67, 72, 75, 71, 63, 57, 54, 56, 60, 58, 52, 47, 45, 49, 51, 48, 44, 40, 38, 41, 39];
private readonly double?[] _reply = [5.2, 4.8, 5.6, 6.1, 5.9, 4.4, 3.9, 4.2, 4.7, 5.3, 6.4, 6.8, 6.0, 4.9, 4.1, 3.8, 4.0, 4.5, 4.3, 3.7, 3.2, 3.0, 3.5, 3.6, 3.3, 2.9, 2.6, 2.5, 2.8, 2.7];
private readonly double?[] _new = [31, 28, 35, 39, 33, 14, 11, 29, 34, 38, 42, 40, 16, 12, 27, 30, 33, 29, 26, 10, 9, 25, 28, 31, 27, 24, 8, 7, 22, 26];
// Each day's drop in open tickets: more than 0 when the queue shrank.
private readonly double?[] _shrank = _open.Skip(1).Select((open, i) => _open[i] - open).ToArray();
}Win-loss bars are all one height: up for a value over Threshold (0 by default), down for one under, and a tick for one at it. They show only which way each value went.
Up bars take --ib-chart-increase and down bars --ib-chart-decrease, as a waterfall's do. Height sets how tall a sparkline is, 2rem by default, and Color sets its color in place of the theme's primary color.
To put a sparkline in a Stat, set the stat's Sparkline.
Marking values
Points marks values with a dot: Last by default, or any of First, Last, Low and High combined with |, or None. On bars, the marked ones are solid and the rest lighter. Curve runs the line smooth, or in steps for a value that holds until it changes, as a price does.
Static
Preview
Visitors a day
Team plan, per seat
Code
@* A store's daily visitors over the last 30 days, its lowest, highest and latest marked, and the
price of its plan over a year, which holds until it changes. *@
<Grid Columns="new() { Base = 1, Sm = 2 }" Gap="6" Grow>
<Stack Gap="2">
<TextBlock Size="TextSize.Sm" Color="Color.Muted">Visitors a day</TextBlock>
<Sparkline Values="_visitors" Label="Visitors a day, last 30 days" Points="SparklinePoints.Low | SparklinePoints.High | SparklinePoints.Last" Curve="ChartCurve.Smooth" Height="3.Rem" ValueFormat="N0" />
</Stack>
<Stack Gap="2">
<TextBlock Size="TextSize.Sm" Color="Color.Muted">Team plan, per seat</TextBlock>
<Sparkline Values="_price" Label="Team plan price per seat, last 12 months" Points="SparklinePoints.First | SparklinePoints.Last" Curve="ChartCurve.Step" Height="3.Rem" ValueFormat="C0" />
</Stack>
</Grid>
{
private readonly double?[] _visitors = [1_840, 1_910, 1_760, 2_050, 2_230, 1_420, 1_310, 1_980, 2_120, 2_260, 2_190, 2_410, 1_560, 1_480, 2_200, 2_380, 2_450, 2_310, 2_520, 1_690, 1_610, 2_480, 2_590, 2_640, 2_550, 2_720, 1_820, 1_760, 2_690, 2_810];
private readonly double?[] _price = [12, 12, 12, 12, 14, 14, 14, 14, 14, 15, 15, 15];
}One scale for a column
Each sparkline spans its own lowest to highest value, which suits one alone but makes a column of them misleading: a product that sells little looks as busy as one that sells a lot. Give them all the same Min and Max so their heights compare.
Details
It has no axes, legend or tooltip. As wide as its container; Height sets how tall.
Mode support
| Feature | Static | Interactive |
|---|---|---|
| A line, an area, bars or win-loss bars, as wide as its container | Yes | Yes |
| The latest value marked, and the first, lowest or highest when they matter | Yes | Yes |
| One scale for a column of sparklines, so their heights compare | Yes | Yes |
| A name that says the first, last, lowest and highest values | Yes | Yes |
Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.
Accessibility
- One image, named by
Label, which is required, and a summary of what it shows: "Signups, last 12 weeks: from 410 to 482, low 390, high 520" (IzzyText.SparklineSummary); for win-loss bars, "Games this season: 9 up, 4 down, 1 even" (SparklineWinLossSummary). - The marked points (
Points) are what the summary already reads, so they add nothing to hear. - It's a glance, not the data: when the values matter, show them too, as a Chart or a Table.
Parameters
| Prop | Type | Default |
|---|---|---|
RefreshHow often the chart reads its data again, at least a second, such as Refresh="TimeSpan.FromSeconds(30)". | TimeSpan? | |
RefreshFromOn a static page with Refresh: a lighter page to fetch than this one, which renders the same chart with the same id. | string? | |
ValuesThe values, in order; null for one that's missing, which breaks the line. | IEnumerable<double?>? | Required |
LabelWhat it shows, such as "Signups, last 12 weeks": its accessible name, with a summary of the values. | string | Required |
TypeA line (the default), an area, bars, or win-loss bars (up or down from Threshold). | SparklineType | SparklineType.Line |
PointsThe values marked: a dot on a line or area; on bars, solid while the rest are lighter. | SparklinePoints | SparklinePoints.Last |
CurveHow a line or area runs from value to value: straight (the default), a smooth curve, or steps. | ChartCurve | ChartCurve.Straight |
MinThe bottom of the value range, in place of the lowest value (or zero, for bars). | double? | |
MaxThe top of the value range, in place of the highest value. | double? | |
ThresholdFor win-loss bars: values over it go up, under it down. | double | 0 |
HeightHow tall it is. | Length? | computed |
ColorA CSS color, such as var(--ib-color-success). | string? | computed |
ValueFormatA .NET format string for the values in the summary, such as C0 or N1. | string? | |
OnRefreshOn an interactive page with Refresh: raised at each interval, to load new data. | EventCallback |
Plus 17 item props and 4 common props.
Full APISee also
- Chart: draws a chart on the server from a list of items, with axes, value labels, reference lines, trends and a table of the values, for every chart type.
- Stat: shows a figure with its trend, read as words.