Table of Contents

Class TwChartBase

Namespace
TwBlazor.Components
Assembly
TwBlazor.dll

The base class for every chart. A chart type derives from it and overrides BuildScene(ChartSceneBuilder) to describe what to draw; this class renders that description and supplies the legend, tooltips, keyboard navigation, empty state and table view.

public abstract class TwChartBase : TwBlazorComponentBase, IComponent, IHandleEvent, IHandleAfterRender, ITwComponent, IAsyncDisposable
Inheritance
TwChartBase
Implements
Derived
Inherited Members

Remarks

To add a new chart type, derive from this class (or from TwCategoryChartBase or TwValueChartBase when the data fits), declare its parameters and build its marks with the ChartSceneBuilder. No markup, styling or accessibility code is needed in the chart itself.

Accessibility: the plot is a group named by the surrounding TwChart title. Each data point is an img whose label states its values. The plot is a single tab stop; the arrow, Home and End keys move between data points, and each shows the same tooltip on focus as on hover. The table view presents the same data without the plot.

Constructors

TwChartBase()

protected TwChartBase()

Properties

Chart

Gets or sets the TwChart container the chart sits in, if any.

[CascadingParameter]
public TwChart? Chart { get; set; }

Property Value

TwChart

ChartViewLabel

Gets or sets the label of the button that switches back to the plot. Defaults to the localized Chart_ViewAsChart text ("View as chart").

[Parameter]
public string? ChartViewLabel { get; set; }

Property Value

string

EmptyText

Gets or sets the message shown when there is no data to plot. Defaults to the localized Chart_Empty text ("No data to display").

[Parameter]
public string? EmptyText { get; set; }

Property Value

string

Height

Gets or sets the height of the plot area.

[Parameter]
public ChartHeight? Height { get; set; }

Property Value

ChartHeight?

Remarks

If not set, the surrounding TwChart decides, falling back to Medium.

InteractiveLegend

Gets or sets whether clicking a legend entry hides or shows its series.

[Parameter]
public bool InteractiveLegend { get; set; }

Property Value

bool

Remarks

Default is true. A series keeps its color while others are hidden.

LegendLabel

Gets or sets the accessible name of the legend. Defaults to the localized Chart_Legend text ("Legend").

[Parameter]
public string? LegendLabel { get; set; }

Property Value

string

ShowLegend

Gets or sets whether the legend is shown when the chart has one.

[Parameter]
public bool ShowLegend { get; set; }

Property Value

bool

Remarks

Default is true. A chart with a single series has no legend: its title names it.

ShowTableToggle

Gets or sets whether the button that switches between the plot and a table of the same data is shown.

[Parameter]
public bool ShowTableToggle { get; set; }

Property Value

bool

Remarks

Default is true. The table is the accessible alternative to the plot, so only turn this off when the same data is available elsewhere on the page.

TableLabel

Gets or sets the accessible name of the table view. Defaults to the localized Chart_TableLabel text ("Chart data").

[Parameter]
public string? TableLabel { get; set; }

Property Value

string

TableViewLabel

Gets or sets the label of the button that switches to the table view. Defaults to the localized Chart_ViewAsTable text ("View as table").

[Parameter]
public string? TableViewLabel { get; set; }

Property Value

string

ValueFormat

Gets or sets the .NET numeric format string used for values on axes, tooltips and the table view, for example "C0" or "0.0%".

[Parameter]
public string? ValueFormat { get; set; }

Property Value

string

Remarks

If not set, values use thousands separators and up to two decimal places. Ignored when ValueFormatter is set.

ValueFormatter

Gets or sets a function that formats values, for full control over units and abbreviations.

[Parameter]
public Func<double, string>? ValueFormatter { get; set; }

Property Value

Func<double, string>

datumClass

Gets extra classes for every data point, such as a pointer cursor when datumsActivatable is set.

protected virtual string? datumClass { get; }

Property Value

string

datumsActivatable

Gets whether clicking a data point, or pressing Enter or Space on it, calls OnDatumActivatedAsync(int).

protected virtual bool datumsActivatable { get; }

Property Value

bool

isPlotVisible

Gets whether the plot is on screen: there is data and the table view is not showing.

protected bool isPlotVisible { get; }

Property Value

bool

plotElement

Gets the plot element, for script that works on it.

protected ElementReference plotElement { get; }

Property Value

ElementReference

plotHeight

Gets the height of the plot in pixels, replacing the height set by Height.

protected virtual double? plotHeight { get; }

Property Value

double?

plotMinWidth

Gets the minimum width of the plot in pixels. When set, a plot wider than its container scrolls horizontally while the labels on its left stay in view.

protected virtual double? plotMinWidth { get; }

Property Value

double?

plotOverlay

Gets content layered over the plot, above the data points. Positions inside it are percentages of the plot.

protected virtual RenderFragment? plotOverlay { get; }

Property Value

RenderFragment

plotScrollerClass

Gets the classes for the element that scrolls the plot when plotMinWidth is set. It needs to scroll horizontally, such as with overflow-x-auto.

protected virtual string? plotScrollerClass { get; }

Property Value

string

scrollElement

Gets the element that scrolls the plot when plotMinWidth is set.

protected ElementReference scrollElement { get; }

Property Value

ElementReference

toolbarContent

Gets content shown at the start of the toolbar, before the legend, such as controls that change the view.

protected virtual RenderFragment? toolbarContent { get; }

Property Value

RenderFragment

yAxisClass

Gets extra classes for the labels along the left of the plot, such as a different maximum width. A scrolling plot (plotMinWidth) sits in a taller cell than the plot itself, so these classes should then keep the labels to the top of it.

protected virtual string? yAxisClass { get; }

Property Value

string

Methods

BuildRenderTree(RenderTreeBuilder)

Renders the component to the supplied RenderTreeBuilder.

protected override void BuildRenderTree(RenderTreeBuilder __builder)

Parameters

__builder RenderTreeBuilder

BuildScene(ChartSceneBuilder)

Describes what the chart draws by adding marks, data points, axes, legend entries and a table to builder. Add nothing to show the empty state.

protected abstract void BuildScene(ChartSceneBuilder builder)

Parameters

builder ChartSceneBuilder

The builder that collects the scene.

DisposeAsync()

Releases the keydown guard registered for the plot.

public ValueTask DisposeAsync()

Returns

ValueTask

DisposeCoreAsync()

Releases what a chart type holds, such as references handed to script. Called by DisposeAsync().

protected virtual ValueTask DisposeCoreAsync()

Returns

ValueTask

FormatValue(double)

Formats a value for display using ValueFormatter or ValueFormat.

protected string FormatValue(double value)

Parameters

value double

The value to format.

Returns

string

IsSeriesHidden(int)

Gets whether the reader has hidden a series by clicking its legend entry.

protected bool IsSeriesHidden(int seriesIndex)

Parameters

seriesIndex int

The zero-based index of the series.

Returns

bool

OnAfterRenderAsync(bool)

Method invoked after each time the component has been rendered interactively and the UI has finished updating (for example, after elements have been added to the browser DOM). Any ElementReference fields will be populated by the time this runs.

This method is not invoked during prerendering or server-side rendering, because those processes are not attached to any live browser DOM and are already complete before the DOM is updated.

Note that the component does not automatically re-render after the completion of any returned Task, because that would cause an infinite render loop.

protected override Task OnAfterRenderAsync(bool firstRender)

Parameters

firstRender bool

Set to true if this is the first time OnAfterRender(bool) has been invoked on this component instance; otherwise false.

Returns

Task

A Task representing any asynchronous operation.

Remarks

The OnAfterRender(bool) and OnAfterRenderAsync(bool) lifecycle methods are useful for performing interop, or interacting with values received from @ref. Use the firstRender parameter to ensure that initialization work is only performed once.

OnDatumActivatedAsync(int)

Called when a data point is clicked, or Enter or Space is pressed on it, while datumsActivatable is true.

protected virtual Task OnDatumActivatedAsync(int index)

Parameters

index int

The zero-based position of the data point.

Returns

Task

OnParametersSet()

Invoked when the component's parameters have been set. Ensures that the component has a unique identifier by generating one if none is provided.

protected override void OnParametersSet()

Remarks

If the component's identifier is not set, a unique ID is generated based on the component's type name (with a 'Tw' prefix removed if present) and a GUID. This helps ensure that each component instance can be uniquely identified, which is useful for scenarios such as associating labels with form elements or targeting elements in client-side scripts.

Refresh()

Rebuilds the scene and renders the chart again, after state the chart keeps itself has changed.

protected void Refresh()

RenderYTick(ChartTick, int)

Gets content to show for a label along the left of the plot instead of its text. Return null to show the text. Content that holds controls makes the labels readable by assistive technology, so it should repeat the label's text.

protected virtual RenderFragment? RenderYTick(ChartTick tick, int index)

Parameters

tick ChartTick

The label.

index int

The zero-based position of the label.

Returns

RenderFragment