Skip to content
Data Chart

Chart

A line, area, bar or donut chart drawn as SVG by the server, with a tooltip, a legend and a data table.

On this page

Examples

Lines and areas

Hover a column, or focus a chart and use the arrow keys, Home and End. The values are announced as the focus moves.

Line, with points and a smooth curve

  • Desktop
  • Mobile
Line chart
MonthDesktopMobile
Jan18680
Feb305200
Mar237120
Apr73190
May209130
Jun214140

Stacked area

  • Desktop
  • Mobile
Visitors by month, desktop and mobile stacked
CategoryDesktopMobile
Jan18680
Feb305200
Mar237120
Apr73190
May209130
Jun214140
Razor
@* The chart is drawn by the server as SVG. Hover a column, or focus a chart and use the arrow keys, to see the values. *@
<div class="grid w-full gap-8 md:grid-cols-2">
    <div>
        <p class="mb-2 text-sm font-medium text-(--klyroo-text-highlighted)">Line, with points and a smooth curve</p>
        <KlyChart TItem="Visit" TIndex="string" Data="_data" Index="@(v => v.Month)" Categories="_categories" Points Curve="KlyChartCurve.Monotone"
                 XLabel="Month" YLabel="Visitors" Width="420" Height="260" data-testid="chart-line" />
    </div>
    <div>
        <p class="mb-2 text-sm font-medium text-(--klyroo-text-highlighted)">Stacked area</p>
        <KlyChart TItem="Visit" TIndex="string" Data="_data" Index="@(v => v.Month)" Categories="_categories" Type="KlyChartType.Area" Stacked
                 Width="420" Height="260" AriaLabel="Visitors by month, desktop and mobile stacked" data-testid="chart-area" />
    </div>
</div>

@code {
    private sealed record Visit(string Month, double Desktop, double Mobile);

    private static readonly Visit[] _data =
    [
        new("Jan", 186, 80), new("Feb", 305, 200), new("Mar", 237, 120),
        new("Apr", 73, 190), new("May", 209, 130), new("Jun", 214, 140),
    ];

    private static readonly KlyChartCategory<Visit>[] _categories =
    [
        new("desktop", v => v.Desktop) { Label = "Desktop" },
        new("mobile", v => v.Mobile) { Label = "Mobile" },
    ];
}

Bars

Grouped, stacked and horizontal. The data table is behind a disclosure; by default it is visually hidden but still read by screen readers.

Grouped

  • Online
  • Store
Data table
Bar chart
CategoryOnlineStore
Q1$4,200$3,100
Q2$5,100$2,800
Q3$4,700$3,600
Q4$6,900$4,100

Stacked and horizontal

  • Online
  • Store
Horizontal bar chart
CategoryOnlineStore
Q1$4,200$3,100
Q2$5,100$2,800
Q3$4,700$3,600
Q4$6,900$4,100
Razor
@* Series differ by fill pattern as well as by color. The data table of the first chart is shown behind a disclosure. *@
<div class="grid w-full gap-8 md:grid-cols-2">
    <div>
        <p class="mb-2 text-sm font-medium text-(--klyroo-text-highlighted)">Grouped</p>
        <KlyChart TItem="Sale" TIndex="string" Data="_data" Index="@(s => s.Quarter)" Categories="_categories" Type="KlyChartType.Bar"
                 Format="Money" Table="KlyChartTable.Toggle" Width="420" Height="260" data-testid="chart-bar" />
    </div>
    <div>
        <p class="mb-2 text-sm font-medium text-(--klyroo-text-highlighted)">Stacked and horizontal</p>
        <KlyChart TItem="Sale" TIndex="string" Data="_data" Index="@(s => s.Quarter)" Categories="_categories" Type="KlyChartType.Bar"
                 Stacked Horizontal Format="Money" Width="420" Height="260" data-testid="chart-bar-horizontal" />
    </div>
</div>

@code {
    private static string Money(double value) => "$" + value.ToString("N0");

    private sealed record Sale(string Quarter, double Online, double Store);

    private static readonly Sale[] _data =
    [
        new("Q1", 4200, 3100), new("Q2", 5100, 2800), new("Q3", 4700, 3600), new("Q4", 6900, 4100),
    ];

    private static readonly KlyChartCategory<Sale>[] _categories =
    [
        new("online", s => s.Online) { Label = "Online" },
        new("store", s => s.Store) { Label = "Store", Color = KlyColor.Info },
    ];
}

Donut

One slice per row, sized by the first category. Series differ by fill pattern as well as by color.

100% of visits
  • Chrome54
  • Safari24
  • Firefox9
  • Edge8
  • Other5
Donut chart
CategoryShare of visits
Chrome54
Safari24
Firefox9
Edge8
Other5
Razor
@* A donut has one slice per row, sized by the first category. The Center fragment sits over the hole. *@
<div class="w-full">
    <KlyChart TItem="Browser" TIndex="string" Data="_data" Index="@(b => b.Name)" Categories="_categories" Type="KlyChartType.Donut"
             Height="260" Thickness="36" data-testid="chart-donut">
        <Center>
            <span class="text-2xl font-semibold text-(--klyroo-text-highlighted)">@_data.Sum(b => b.Share)%</span>
            <span class="text-xs text-(--klyroo-text-muted)">of visits</span>
        </Center>
    </KlyChart>
</div>

@code {
    private sealed record Browser(string Name, double Share);

    private static readonly Browser[] _data =
    [
        new("Chrome", 54), new("Safari", 24), new("Firefox", 9), new("Edge", 8), new("Other", 5),
    ];

    private static readonly KlyChartCategory<Browser>[] _categories = [new("share", b => b.Share) { Label = "Share of visits" }];
}

Funnel

Type=Funnel draws one bar per row, sized by the first category relative to the largest step, with the share of the previous step, as a list of steps.

  1. Visits 1,000
  2. Signups 240 (24%)
  3. Trials 90 (38%)
  4. Paid 36 (40%)
Razor
<div class="w-full max-w-lg">
    <KlyChart TItem="Step" TIndex="string" Type="KlyChartType.Funnel" Data="_steps" Index="@(s => s.Label)"
              Categories="@(new[] { new KlyChartCategory<Step>("count", s => s.Count) { Label = "Visitors" } })" Width="480" Height="260" />
</div>

@code {
    private sealed record Step(string Label, double Count);

    private static readonly Step[] _steps = [new("Visits", 1000), new("Signups", 240), new("Trials", 90), new("Paid", 36)];
}

Selection

OnSelect reports the row, its index, the series, the value and the row item when a bar, point or slice is clicked, or chosen with Enter or Space on the row the arrow keys are on. Only a chart with a handler is clickable: it gets a pointer cursor and a keyboard hint that says Enter selects.

Bars

  • Online
  • Store
Bar chart
CategoryOnlineStore
Q14,2003,100
Q25,1002,800
Q34,7003,600
Q46,9004,100

Donut

  • Q14,200
  • Q25,100
  • Q34,700
  • Q46,900
Donut chart
CategoryOnline sales
Q14,200
Q25,100
Q34,700
Q46,900

Nothing selected.

Razor
@* OnSelect makes the bars, points and slices selectable: a click, or Enter and Space on the row the arrow keys are on. *@
<div class="grid w-full gap-8 md:grid-cols-2">
    <div>
        <p class="mb-2 text-sm font-medium text-(--klyroo-text-highlighted)">Bars</p>
        <KlyChart TItem="Sale" TIndex="string" Data="_data" Index="@(s => s.Quarter)" Categories="_categories" Type="KlyChartType.Bar"
                 OnSelect="@(e => _picked = e)" Width="420" Height="260" data-testid="chart-select-bar" />
    </div>
    <div>
        <p class="mb-2 text-sm font-medium text-(--klyroo-text-highlighted)">Donut</p>
        <KlyChart TItem="Sale" TIndex="string" Data="_data" Index="@(s => s.Quarter)" Categories="_slices" Type="KlyChartType.Donut"
                 OnSelect="@(e => _picked = e)" Height="260" Thickness="36" data-testid="chart-select-donut" />
    </div>
</div>
<p class="mt-4 text-sm text-(--klyroo-text-muted)" data-testid="chart-select-state">
    @(_picked is null ? "Nothing selected." : $"Selected {_picked.Index} ({_picked.SeriesKey}): {_picked.Value:N0}, row {_picked.Row} of {_data.Length}.")
</p>

@code {
    private sealed record Sale(string Quarter, double Online, double Store);

    private KlyChartSelectEventArgs<Sale, string>? _picked;

    private static readonly Sale[] _data =
    [
        new("Q1", 4200, 3100), new("Q2", 5100, 2800), new("Q3", 4700, 3600), new("Q4", 6900, 4100),
    ];

    // With several series, a click on a bar reports the bar under the pointer; from the keyboard, with several series, no series is named (SeriesKey is null) and Item is the row to work from.
    private static readonly KlyChartCategory<Sale>[] _categories =
    [
        new("online", s => s.Online) { Label = "Online" },
        new("store", s => s.Store) { Label = "Store", Color = KlyColor.Info },
    ];

    private static readonly KlyChartCategory<Sale>[] _slices = [new("online", s => s.Online) { Label = "Online sales" }];
}

API

KlyChart<TItem, TIndex>

Line, area, bar or donut chart drawn as SVG on the server, with a generated text description and a data table for accessibility. No charting library or JavaScript is involved.

Parameters

Name Type Default Description
AriaDescription string A longer description for assistive technology, in addition to the name.
AriaLabel string The accessible name of the chart. Without it the chart is described by a generated summary of its values.
Categories (required) IReadOnlyList<KlyChartCategory<TItem>> The series, in drawing order. With a donut the first one gives the size of each slice.
Class string Classes added to the root element, merged last so they win over the theme.
Colors IReadOnlyList<KlyColor> The colors of the series in order, in place of the default palette. A color set on a category wins.
Culture CultureInfo The culture of every number, date and percentage the chart writes (axis ticks, tooltip, table, summary). Defaults to the culture of the locale in effect (the same on the server and in WebAssembly for one culture).
Curve KlyChartCurve KlyChartCurve.Linear The shape of the line between values. Only applies to line and area. Values: Linear, Monotone, Step.
Data (required) IReadOnlyList<TItem> The rows to plot: one per x value, or per slice with Donut .
Format Func<Double, string> Formats the values on the value axis, in the tooltip and in the table. Defaults to the format culture of the locale in effect (the regional current culture when it has the same language).
Grid bool true Draws the grid lines at the ticks of the value axis.
Height int 300 The height of the drawing in pixels, at Width . The chart scales down with its container.
Horizontal bool false Draws bars along the x axis, with the index on the y axis. Only applies to Bar .
Index (required) Func<TItem, TIndex> The value of a row on the x axis (the label of a slice with a donut). Dates and numbers are formatted with the format culture of the locale in effect (the regional current culture when it has the same language).
IndexFormat string The format string of an index that is formattable (a date, a number).
Legend bool? Shows the legend. By default it shows when there is more than one series, or more than one slice with a donut.
Points bool false Always shows a marker at each value of a line chart. Without it the marker shows on the value under the pointer or the keyboard.
Responsive bool true Draws the chart at the width it is given when that is less than Width ( true by default), instead of shrinking a drawing made for Width : the labels and the lines keep their size, and the height follows the width down to 180 pixels, or Height when it is smaller. The browser reports the width, so without scripts (a page that is not interactive) the chart is drawn at Width and scales with its container. A donut is not concerned.
Stacked bool false Stacks the series instead of drawing them side by side or over one another. Only applies to area and bar.
Table KlyChartTable KlyChartTable.Hidden Where the table with the chart's data goes. It is always rendered; by default it is visually hidden. Values: Hidden, Visible, Toggle.
Thickness int 24 The width of the ring in pixels. Only applies to Donut .
Tooltip bool true Shows the value under the pointer, and under the focus when the chart is focused: the arrow keys move between points. It runs in the browser, so it works in static rendering too.
Type KlyChartType KlyChartType.Line Kind of chart to draw: line, area, bar or donut ( KlyChartType ). Values: Line, Area, Bar, Donut, Funnel.
Ui ChartUi Per-instance class overrides of the slots of the chart, see ChartUi . They are merged after the theme and the Class parameter wins on the root slot.
Width int 640 The width of the drawing in pixels, and the widest the chart gets.
XAxis bool true Shows the labels of the index axis (the x axis, or the y axis of horizontal bars).
XLabel string A title along the index axis.
YAxis bool true Shows the ticks and labels of the value axis.
YLabel string A title along the value axis.

Events

Name Type Description
OnSelect EventCallback<KlyChartSelectEventArgs<TItem, TIndex>> Raised when a bar, point or donut slice is clicked, or chosen with Enter or Space on the row the arrow keys are on. The arguments carry the row, its index, the series that was picked, its value and the row item. Only a chart with a handler reacts: it gets a pointer cursor on its bars, points and slices, and its keyboard hint says Enter selects. A bar is the one under the pointer, a point on a line or area the series nearest the pointer; from the keyboard, with several series, no series is named ( SeriesIndex -1, SeriesKey and Value null) and the handler chooses from the row item. It needs the tooltip (the keyboard row is the highlighted one) and an interactive render, since the pick is reported by the browser.

Slots

Name Type Description
Center RenderFragment Content over the hole of a donut: a total, a caption.

Ui slots

Ui takes a ChartUi with one class string per slot, merged after the theme.

Slot Styles
Root Classes merged into the root slot of KlyChart`2 , after the theme and the app configuration.
Plot Classes merged into the plot slot of KlyChart`2 , after the theme and the app configuration.
Svg Classes merged into the svg slot of KlyChart`2 , after the theme and the app configuration.
Grid Classes merged into the grid slot of KlyChart`2 , after the theme and the app configuration.
AxisLine Classes merged into the axisLine slot of KlyChart`2 , after the theme and the app configuration.
Tick Classes merged into the tick slot of KlyChart`2 , after the theme and the app configuration.
AxisTitle Classes merged into the axisTitle slot of KlyChart`2 , after the theme and the app configuration.
Line Classes merged into the line slot of KlyChart`2 , after the theme and the app configuration.
Bar Classes merged into the bar slot of KlyChart`2 , after the theme and the app configuration.
Marker Classes merged into the marker slot of KlyChart`2 , after the theme and the app configuration.
Hit Classes merged into the hit slot of KlyChart`2 , after the theme and the app configuration.
HitArea Classes merged into the hitArea slot of KlyChart`2 , after the theme and the app configuration.
Slice Classes merged into the slice slot of KlyChart`2 , after the theme and the app configuration.
Tooltip Classes merged into the tooltip slot of KlyChart`2 , after the theme and the app configuration.
TooltipTitle Classes merged into the tooltipTitle slot of KlyChart`2 , after the theme and the app configuration.
TooltipRow Classes merged into the tooltipRow slot of KlyChart`2 , after the theme and the app configuration.
TooltipIndicator Classes merged into the tooltipIndicator slot of KlyChart`2 , after the theme and the app configuration.
TooltipLabel Classes merged into the tooltipLabel slot of KlyChart`2 , after the theme and the app configuration.
TooltipValue Classes merged into the tooltipValue slot of KlyChart`2 , after the theme and the app configuration.
Center Classes merged into the center slot of KlyChart`2 , after the theme and the app configuration.
Legend Classes merged into the legend slot of KlyChart`2 , after the theme and the app configuration.
LegendItem Classes merged into the legendItem slot of KlyChart`2 , after the theme and the app configuration.
LegendSwatch Classes merged into the legendSwatch slot of KlyChart`2 , after the theme and the app configuration.
LegendLabel Classes merged into the legendLabel slot of KlyChart`2 , after the theme and the app configuration.
LegendValue Classes merged into the legendValue slot of KlyChart`2 , after the theme and the app configuration.
Details Classes merged into the details slot of KlyChart`2 , after the theme and the app configuration.
Summary Classes merged into the summary slot of KlyChart`2 , after the theme and the app configuration.
TableWrapper Classes merged into the tableWrapper slot of KlyChart`2 , after the theme and the app configuration.
Table Classes merged into the table slot of KlyChart`2 , after the theme and the app configuration.
Th Classes merged into the th slot of KlyChart`2 , after the theme and the app configuration.
Td Classes merged into the td slot of KlyChart`2 , after the theme and the app configuration.
Funnel Classes merged into the funnel slot of KlyChart`2 , after the theme and the app configuration.
FunnelStep Classes merged into the funnelStep slot of KlyChart`2 , after the theme and the app configuration.
FunnelHeader Classes merged into the funnelHeader slot of KlyChart`2 , after the theme and the app configuration.
FunnelLabel Classes merged into the funnelLabel slot of KlyChart`2 , after the theme and the app configuration.
FunnelValue Classes merged into the funnelValue slot of KlyChart`2 , after the theme and the app configuration.
FunnelBar Classes merged into the funnelBar slot of KlyChart`2 , after the theme and the app configuration.
FunnelEmpty Classes merged into the funnelEmpty slot of KlyChart`2 , after the theme and the app configuration.