Charts
TwChart
TwChart is the Blazor chart container for over fifty chart types. It adds a title, loading state and shared options. Accessible and script-free.
Last updated
Basic usage
TwChart is the container: it frames a chart with a title, a description and a loading
state. Place one chart component inside it and pass the data. Every chart supplies its own legend,
tooltips on hover and keyboard focus, and a table view of the same data.
Choosing a chart
Start from what the reader needs to see, then pick the chart. Each chart has its own page with
examples of every option.
| To show | Use |
|---|---|
| A comparison between categories | |
| A change over time | |
| Parts of a whole | |
| A hierarchy | |
| How values are distributed | |
| How two values relate | |
| Several variables at once | |
| Flows and connections | |
| How sets overlap |
Title and description
Title names the chart, on screen and for assistive technology. Use
Description for the unit or the period, so the axis does not have to carry it. Both are
optional; without a title, give the chart an AriaLabel.
RAZOR
<TwChart Title="Revenue by region" Description="Thousands of pounds, last quarter">
<TwBarChart Categories="@_regions" Series="@_revenue" />
</TwChart>Height
Height sets how tall the plot is: ChartHeight.Small,
Medium (the default) or Large. Set it on the container, or on a chart to
override the container. The width always follows the space available. Radial charts are square, and
Height sets how wide the square may grow.
RAZOR
<TwChart Title="Revenue by region" Height="ChartHeight.Small">
<TwColumnChart Categories="@_regions" Series="@_revenue" />
</TwChart>Formatting values
Every chart takes
ValueFormat, any .NET numeric format string, or
ValueFormatter, a function, for full control. The format applies to the axis, tooltips,
value labels and table view alike, in the current culture.
RAZOR
<TwChart Title="Revenue by region">
<TwColumnChart Categories="@_regions" Series="@_revenue" ValueFormat="C0" ShowValues="true" />
</TwChart>Legend
A chart with two or more series shows a legend; a single series needs none, because the title names
it. Each entry is a button that hides or shows its series, and a series keeps its color while others
are hidden. Set
InteractiveLegend to false for a plain legend, or
ShowLegend to false to remove it.
- Organic
- Paid
- Referral
RAZOR
<TwChart Title="Monthly visitors" Description="Thousands">
<TwLineChart Categories="@_months" Series="@_visitors" InteractiveLegend="false" />
</TwChart>Table view
"View as table" swaps the plot for a table of the same data, and "View as chart" swaps it back. The
table is the accessible alternative to the plot, so only set
ShowTableToggle to
false when the same data is available elsewhere on the page.
TableViewLabel, ChartViewLabel and TableLabel change the wording.
RAZOR
<TwChart Title="Revenue by region and year">
<TwColumnChart Categories="@_regions" Series="@_yearly" CategoryHeader="Region" TableViewLabel="Show the numbers" ChartViewLabel="Show the chart" />
</TwChart>Loading and empty states
Set
Loading on the container to show a spinner with a "Loading chart" message while data is
fetched; screen readers announce it. A chart with no data shows an empty state with its
EmptyText instead of an empty set of axes. Both keep the chart's height.
Loading chart
Nothing to show for this period
RAZOR
<div class="grid gap-8 md:grid-cols-2">
<TwChart Title="Loading" Height="ChartHeight.Small" Loading="true" />
<TwChart Title="No data" Height="ChartHeight.Small">
<TwColumnChart EmptyText="Nothing to show for this period" />
</TwChart>
</div>Accessibility
The chart is a group named by the container's
Title. Each data point is announced with
its values. The plot is a single tab stop: tab into it, then use the arrow, Home and
End keys to move between data points. On a touch screen, tap a data point to show its tooltip.
A slice, wedge, ring, ribbon, tile or filled curve responds anywhere on its shape. Focus shows the same tooltip as hover, and
Esc dismisses it. The table view presents the same data without the plot. Text never
takes a series color, direction and status are always named as well as colored, and colors are
assigned in a fixed order chosen so neighboring series stay distinguishable for color-blind readers.
Keep to eight series or fewer: there are eight colors, and a ninth series would reuse the last.
Adding your own chart type
Every chart is a small class that describes what to draw. Derive from
TwCategoryChartBase (categories and series), TwValueChartBase (labelled
values) or TwChartBase, and add marks with the ChartSceneBuilder. Positions
are percentages of the plot area. The legend, tooltips, keyboard support, table view and theming
come from the base class, so a new chart needs no markup or styles of its own.
CSHARP
/// <summary>
/// A custom chart: each value is drawn as a filled track out of 100.
/// </summary>
public class CompletionChart : TwValueChartBase
{
protected override void BuildMarks(ChartSceneBuilder builder)
{
builder.Horizontal = true;
var band = new ChartBand(Data.Count);
var color = ChartColor.Series(0);
builder.BandAxis([.. Data.Select(item => item.Label)], band);
for (var i = 0; i < Data.Count; i++)
{
var filled = Math.Clamp(Data[i].Value, 0, 100);
builder.Bar(band.Center(i), 6, 0, 100, ChartColor.Track, ChartCorner.All, gap: false);
builder.Bar(band.Center(i), 6, 0, filled, color, ChartCorner.All, gap: false);
builder.BandDatum(band.Center(i), band.Step, Data[i].Label,
[new ChartDatumRow(effectiveValueHeader, FormatValue(Data[i].Value), color)]);
}
}
}Theme configuration
Styling is controlled by
TwBlazorTheme.Components (a TwChartTheme), configured via the
AddTwBlazor() options callback described on the Get Started page.
Every property is a set of Tailwind classes, so changes are picked up by your own Tailwind build.
| Property | Type | Controls |
|---|---|---|
Container |
string | |
Header |
string | |
Title |
string | |
Description |
string | |
Body |
string | |
Chart |
string | |
Toolbar |
string | |
Legend |
string | |
LegendItem |
string | |
LegendButton |
string | |
LegendItemHidden |
string | |
LegendSwatch |
string | |
TableToggle |
string | |
AxisTitle |
string | |
XAxisTitle |
string | |
Frame |
string | |
FrameWithYAxis |
string | |
YAxis |
string | |
AxisSizer |
string | |
Tick |
string | |
YTick |
string | |
XAxis |
string | |
XTick |
string | |
Plot |
string | |
Small |
string | |
Medium |
string | |
Large |
string | |
Square |
string | |
SquareSmall |
string | |
SquareMedium |
string | |
SquareLarge |
string | |
Canvas |
string | |
Rect |
string | |
RoundedTop |
string | |
RoundedBottom |
string | |
RoundedLeft |
string | |
RoundedRight |
string | |
RoundedAll |
string | |
SurfaceRing |
string | |
Dot |
string | |
Glyph |
string | |
Text |
string | |
TextMuted |
string | |
TextPrimary |
string | |
TextInverse |
string | |
TextStrong |
string | |
TextLarge |
string | |
TextTruncate |
string | |
FillNone |
string | |
StrokeHairline |
string | |
StrokeNormal |
string | |
StrokeThick |
string | |
SurfaceStroke |
string | |
HitWrapper |
string | |
HitWrapperShaped |
string | |
HitShape |
string | |
Hit |
string | |
HitHighlight |
string | |
Crosshair |
string | |
TooltipTitle |
string | |
TooltipRow |
string | |
TooltipKey |
string | |
TooltipLabel |
string | |
TooltipValue |
string | |
TableRowHeader |
string | |
Series |
IReadOnlyList`1 | |
Positive |
TwChartColor | |
Negative |
TwChartColor | |
Total |
TwChartColor | |
Sequential |
TwChartColor | |
Track |
TwChartColor | |
Grid |
TwChartColor | |
Axis |
TwChartColor | |
Ink |
TwChartColor |
Full reference: TwChartTheme on the API docs.