DeepWorks Studios Blazor Component Library

Demo user Administrator ยท Blazor Server Library 20261206222504-6e8ff32.dirty

DwsSparkline

API Reference

Line and bars

A sparkline is a word-sized chart without axes: Values in, a line or (Type='Bar') a row of bars out. It is plain inline SVG - no JavaScript, no measuring. Width is the largest width: without it the sparkline fills its container, so give it one where it sits next to text. For screen readers it is one image whose name is AriaLabel followed by a localised summary (how many values, lowest, highest, latest).

Line Bars
Show Code
Sparkline.razor

<div class="d-flex flex-wrap align-items-center gap-4">
    <span>Line <DwsSparkline Name="SparklineDemo_Basic_Line" Values="@_requests" Width="120" AriaLabel="Requests per minute" /></span>
    <span>Bars <DwsSparkline Name="SparklineDemo_Basic_Bar" Values="@_requests" Type="DwsSparklineType.Bar" Width="120" AriaLabel="Requests per minute" /></span>
</div>
Sparkline.razor.cs

private readonly IReadOnlyList<double> _requests = [120, 132, 128, 140, 151, 149, 160, 158, 171, 166, 180, 192];

In a sentence

Because it needs no measuring, a small sparkline can sit inside running text, a card header or every row of a grid.

In a sentence: errors went over the last day.
Show Code
Sparkline.razor

<span>In a sentence: errors went <DwsSparkline Name="SparklineDemo_Basic_Inline" Values="@_errors" Color="DwsColor.Error" Width="60" Height="16" ShowLastPoint="true" AriaLabel="Errors per hour" /> over the last day.</span>
Sparkline.razor.cs

private readonly IReadOnlyList<double> _errors = [2, 1, 0, 3, 8, 13, 7, 4, 2, 1, 5, 9];

Colour and size

Color takes a theme colour; it is mixed with the text colour so the chart stays visible on light and dark surfaces. Height is in pixels (24 by default). Width is the largest width in pixels: the sparkline fills its container up to it and gets narrower in a narrower one, never wider - so a row of them wraps or shrinks on a phone instead of overflowing.

Show Code
Sparkline.razor

<div class="d-flex flex-wrap align-items-center gap-4">
    <DwsSparkline Name="SparklineDemo_Look_Primary" Values="@_requests" Width="100" />
    <DwsSparkline Name="SparklineDemo_Look_Success" Values="@_requests" Color="DwsColor.Success" Width="160" Height="32" />
    <DwsSparkline Name="SparklineDemo_Look_Warning" Values="@_errors" Type="DwsSparklineType.Bar" Color="DwsColor.Warning" Width="160" Height="32" />
    <DwsSparkline Name="SparklineDemo_Look_Error" Values="@_errors" Type="DwsSparklineType.Bar" Color="DwsColor.Error" Width="72" Height="18" />
</div>
Sparkline.razor.cs

private readonly IReadOnlyList<double> _requests = [120, 132, 128, 140, 151, 149, 160, 158, 171, 166, 180, 192];
private readonly IReadOnlyList<double> _errors = [2, 1, 0, 3, 8, 13, 7, 4, 2, 1, 5, 9];

A shared scale

Without Min / Max every sparkline fills its box with its own values, which makes 4 % and 90 % CPU look alike; a shared Min and Max makes several comparable.

Own scale: 0 to 100:
Show Code
Sparkline.razor

<div class="d-flex flex-column gap-2">
    <span>Own scale: <DwsSparkline Name="SparklineDemo_Scale_OwnA" Values="@_cpuIdle" Width="100" /> <DwsSparkline Name="SparklineDemo_Scale_OwnB" Values="@_cpuBusy" Width="100" /></span>
    <span>0 to 100: <DwsSparkline Name="SparklineDemo_Scale_SharedA" Values="@_cpuIdle" Width="100" Min="0" Max="100" /> <DwsSparkline Name="SparklineDemo_Scale_SharedB" Values="@_cpuBusy" Width="100" Min="0" Max="100" /></span>
</div>
Sparkline.razor.cs

private readonly IReadOnlyList<double> _cpuIdle = [3, 4, 3, 5, 4, 6, 5, 4];
private readonly IReadOnlyList<double> _cpuBusy = [71, 78, 74, 85, 90, 88, 93, 96];

Gaps and negative values

A value that is not a finite number (NaN, infinity) is a gap: the line is interrupted there, a bar chart leaves the slot empty. Bars grow from zero, so negative values hang below the zero line.

Show Code
Sparkline.razor

<div class="d-flex flex-wrap align-items-center gap-4">
    <DwsSparkline Name="SparklineDemo_Scale_Gaps" Values="@_withGaps" Width="160" Height="32" ShowLastPoint="true" AriaLabel="Sensor readings with outages" />
    <DwsSparkline Name="SparklineDemo_Scale_Negative" Values="@_profitAndLoss" Type="DwsSparklineType.Bar" Color="DwsColor.Info" Width="160" Height="32" AriaLabel="Profit and loss per month" />
</div>
Sparkline.razor.cs

// NaN (or infinity) is a gap: the line is interrupted there.
private readonly IReadOnlyList<double> _withGaps = [4, 6, 5, double.NaN, double.NaN, 8, 9, 7, double.NaN, 6];

// Bars grow from zero: negative values hang below it.
private readonly IReadOnlyList<double> _profitAndLoss = [4, 7, -3, -6, 2, 9, -1, 5];

Newest value and tooltip

ShowLastPoint marks the newest value - a dot at the end of a line, a stronger last bar. ShowTooltip repeats the accessible name and summary as the browser's native tooltip: hover the charts (a touch screen shows no native tooltip, so do not put anything there that is not also on the page).

Show Code
Sparkline.razor

<div class="d-flex flex-wrap align-items-center gap-4">
    <DwsSparkline Name="SparklineDemo_LastPoint_Line" Values="@_requests" Width="160" Height="32" ShowLastPoint="true" ShowTooltip="true" AriaLabel="Requests per minute" />
    <DwsSparkline Name="SparklineDemo_LastPoint_Bar" Values="@_errors" Type="DwsSparklineType.Bar" Color="DwsColor.Warning" Width="160" Height="32" ShowLastPoint="true" ShowTooltip="true" AriaLabel="Errors per hour" />
</div>
Sparkline.razor.cs

private readonly IReadOnlyList<double> _requests = [120, 132, 128, 140, 151, 149, 160, 158, 171, 166, 180, 192];
private readonly IReadOnlyList<double> _errors = [2, 1, 0, 3, 8, 13, 7, 4, 2, 1, 5, 9];

No values

Without values the box stays empty but keeps its place in the row, and the accessible name says that there are no values. EmptyContent shows something instead - a dash or a short text.

No values: (the box keeps its place) EmptyContent: no data yet
Show Code
Sparkline.razor

<div class="d-flex flex-column gap-2">
    <span>No values: <DwsSparkline Name="SparklineDemo_Scale_Empty" Values="@_noValues" Width="100" /> (the box keeps its place)</span>
    <span>EmptyContent: <DwsSparkline Name="SparklineDemo_Empty_Content" Values="@_noValues" Width="100"><EmptyContent>no data yet</EmptyContent></DwsSparkline></span>
</div>
Sparkline.razor.cs

private readonly IReadOnlyList<double> _noValues = [];

KPI cards

A row of key figures, each with its trend under the number. Without a Width the sparkline fills its card: three cards side by side on a wide screen, one below the other on a phone, where each sparkline grows to the full card width. The drawing stretches, the line keeps its thickness and the end dot stays round.

Requests per minute
192
Errors per hour
9
CPU load (%)
96
Show Code
Sparkline.razor

<div class="row g-3">
    <div class="col-12 col-sm-4">
        <div class="border rounded p-3 h-100">
            <div class="small">Requests per minute</div>
            <div class="fs-4 fw-semibold mb-1">@_requests[^1]</div>
            @* No Width: the sparkline fills its card. *@
            <DwsSparkline Name="SparklineDemo_Kpi_Requests" Values="@_requests" Height="32" ShowLastPoint="true" AriaLabel="Requests per minute" />
        </div>
    </div>
    <div class="col-12 col-sm-4">
        <div class="border rounded p-3 h-100">
            <div class="small">Errors per hour</div>
            <div class="fs-4 fw-semibold mb-1">@_errors[^1]</div>
            <DwsSparkline Name="SparklineDemo_Kpi_Errors" Values="@_errors" Type="DwsSparklineType.Bar" Color="DwsColor.Error" Height="32" ShowLastPoint="true" AriaLabel="Errors per hour" />
        </div>
    </div>
    <div class="col-12 col-sm-4">
        <div class="border rounded p-3 h-100">
            <div class="small">CPU load (%)</div>
            <div class="fs-4 fw-semibold mb-1">@_cpuBusy[^1]</div>
            <DwsSparkline Name="SparklineDemo_Kpi_Cpu" Values="@_cpuBusy" Color="DwsColor.Warning" Min="0" Max="100" Height="32" ShowLastPoint="true" AriaLabel="CPU load in percent" />
        </div>
    </div>
</div>
Sparkline.razor.cs

private readonly IReadOnlyList<double> _requests = [120, 132, 128, 140, 151, 149, 160, 158, 171, 166, 180, 192];
private readonly IReadOnlyList<double> _errors = [2, 1, 0, 3, 8, 13, 7, 4, 2, 1, 5, 9];
private readonly IReadOnlyList<double> _cpuBusy = [71, 78, 74, 85, 90, 88, 93, 96];

In a DwsDataGrid cell

Put it in a column's CellTemplate. Every row shares one Name, so pass IgnorePermissionChecks and IgnorePermissionSelection: the grid owns the permissions of its cells. The server-side section of the DataGrid page uses the same column.

Services
ServiceErrors (24 h)Per hour
api-gateway383
auth380
billing335
mailer270
search342
Show Code
Sparkline.razor

<DwsDataGrid Name="SparklineDemo_Grid" T="ServiceRow" Items="@_services" Title="Services" ShowFooter="false">
    <DwsDataGridColumns>
        <DwsDataGridColumn Name="SparklineDemoGrid_Column_Name" T="ServiceRow" Field="Name" Title="Service" Sortable="true" />
        <DwsDataGridColumn Name="SparklineDemoGrid_Column_Errors" T="ServiceRow" Field="Errors" Title="Errors (24 h)" Sortable="true" Alignment="DwsHorizontalAlignment.Right" />
        <DwsDataGridColumn Name="SparklineDemoGrid_Column_Trend" T="ServiceRow" Title="Per hour">
            <CellTemplate>
                @* One Name for every row: the grid owns its cells' permissions, so the sparkline registers none. *@
                <DwsSparkline Name="SparklineDemoGrid_Sparkline" IgnorePermissionChecks="true" IgnorePermissionSelection="true"
                              Values="@context.Item.PerHour" Type="DwsSparklineType.Bar" Color="DwsColor.Error" Width="96" Height="20" ShowLastPoint="true"
                              AriaLabel="@($"Errors per hour of {context.Item.Name}")" />
            </CellTemplate>
        </DwsDataGridColumn>
    </DwsDataGridColumns>
</DwsDataGrid>
Sparkline.razor.cs

private readonly List<ServiceRow> _services = BuildServices();

public sealed class ServiceRow
{
    public string Name { get; set; } = string.Empty;
    public int Errors { get; set; }
    public IReadOnlyList<double> PerHour { get; set; } = [];
}

private static List<ServiceRow> BuildServices()
{
    string[] names = ["api-gateway", "auth", "billing", "mailer", "search"];

    // A fixed seed: the same rows on every visit.
    Random random = new(1234);
    List<ServiceRow> rows = [];

    foreach (string name in names)
    {
        List<double> perHour = [.. Enumerable.Range(0, 24).Select(_ => (double)random.Next(0, 30))];

        rows.Add(new ServiceRow { Name = name, Errors = (int)perHour.Sum(), PerHour = perHour });
    }

    return rows;
}

Interface-Description

Name Typ Default Description
Values IReadOnlyList<double>? null The values, oldest first. A value that is not a finite number (NaN, infinity) is a gap: a line is interrupted there, a bar chart leaves the slot empty. Empty or null draws nothing.
Type DwsSparklineType DwsSparklineType.Line Line (one line through all values) or Bar (one bar per value).
Color DwsColor DwsColor.Primary Theme color of the line or bars, passed as a CSS variable and mixed with the text color so it reaches 3:1 against light and dark surfaces.
Width double? null Width in pixels.
Height double? null (24) Height in pixels.
Min double? null Lower end of the value scale. Not set, a line starts at its smallest value and bars start at zero (or at the smallest value, if negative). Set it to compare several sparklines on one scale.
Max double? null Upper end of the value scale; not set, the largest value. Values outside Min / Max are drawn at the edge.
ShowLastPoint bool false Marks the newest value: a dot at the end of a line, a stronger last bar.
AriaLabel string? null What the chart shows, for assistive technology. The localised summary of the values (count, lowest, highest, latest) is appended; without it only that summary is announced.
ShowTooltip bool false Also shows the accessible name as a native tooltip on hover.
EmptyContent RenderFragment? null Shown instead of the drawing when there is no value (a dash, "no data"); the accessible name still says so. Null draws nothing.
Name Typ Default Description
Element ElementReference The associated web component. May be null if accessed before the component is rendered.
ComponentGuid Guid Unique identifier for the component.
TypeString string The name of the component type, obtained from reflection.
Authorized_RegisterEnabled bool false Indicates whether this component is enabled through a permission registry.
Authorized_RegisterSelected bool false Indicates whether this component is selected in permission selection mode.
GetInitializedState bool false Returns whether the component is initialized.
GetRenderModeState DwsRenderMode Returns the current render mode.
GetComponentState DwsComponentStateType Returns the current component state.
GetIsInteractiveState bool false Returns whether the component is interactive.
GetIsPrerenderingState bool false Returns whether the component is prerendering.
GetComponentLoadingState bool false Returns whether the component is loading.
IsFormBusy bool false Whether the DwsForm (or DwsFormBusyRegion) around the component is locked because its submit handler is running. The state itself is the cascaded FormBusyState (IDwsFormBusyState).
Name Typ Parameters Description
DebugInfo void Logs debug information about the component.
UpdateState void Updates the component's state and triggers StateHasChanged.
UpdateStateAsync Task Asynchronously updates the component's state and triggers StateHasChanged.
Dispose void Disposes the component and releases resources.
FocusAsync ValueTask bool containedInScrollableList Sets focus to the component's element.
Name Typ Default Description
Name string Required unique identifier for the component within its container.
Visible bool true Determines if the component is visible in the UI.
Disabled bool false Disables the form control, ensuring it doesn't participate in form submission.
ComponentNameSpace string The namespace for permission-based access control.
PermissionKey string? null A stable key that takes the place of Name in the generated permission namespace (Page.PermissionKey): the stored permission survives a rename, and components that share a key share one permission entry. Null: keyed by Name. Read once, when the component registers; an explicit ComponentNameSpace wins.
Class string Optional CSS class names.
Style string Optional in-line styles.
Tag object An optional user-defined object to store any additional data with the component.
BackReference DwsReference A reference to the enclosing component.
ID string The HTML ID attribute for the component.
AdditionalAttributes Dictionary<string, object> Additional HTML attributes for the component's root element.
IgnorePermissionChecks bool false Bypasses all permission checks for this component.
IgnorePermissionSelection bool false Ignore permission selection mode for this component.
IgnoreFormBusy bool false Takes the component out of the submit lock of the DwsForm (or DwsFormBusyRegion) around it: IsFormBusy stays false for it.
Name Typ Description
OnInitializedCallbackStarted EventCallback Callback when OnInitialized starts.
OnInitializedCallback EventCallback Callback during OnInitialized.
OnInitializedCallbackCompleted EventCallback Callback when OnInitialized completes.
OnInitializedAsyncCallbackStarted EventCallback Callback when OnInitializedAsync starts.
OnInitializedAsyncCallback EventCallback Callback during OnInitializedAsync.
OnInitializedAsyncCallbackCompleted EventCallback Callback when OnInitializedAsync completes.
OnIsComponentLoadingChanged EventCallback<bool> Callback when IsComponentLoading changes.
OnCurrentRenderModeChanged EventCallback<DwsRenderMode> Callback when CurrentRenderMode changes.
OnComponentStateChanged EventCallback<DwsComponentStateType> Callback when ComponentState changes.
An error has occurred. This app may no longer respond until reloaded.