Clicks and selection
Handle clicks on points, bars and legend items, and let people select points or a range, with events in .NET. Clicks and selections call back into .NET once, when they happen; hover feedback stays in the browser.
Point clicks
Click a point to get its item in OnPointClick, with the series it belongs to.
What counts as a click on a point: on lines, areas and scatter charts, a click within 16 px of a point, so a click on a line next to a point picks that point; with several series the closest point wins, and a click on a line between two points far apart picks nothing. On bars, anywhere on the bar (box plots and candlesticks: anywhere in their slot); on pies, heatmaps, treemaps and calendars, the slice or cell. A point wins over a reference line or band underneath, but not over a point annotation or callout (see Annotations).
Clicked: nothing yet
Code
<RioChart TItem="Reading" OnPointClick="e => clicked = e">
<LineSeries Items="web" Name="Web" XValue="r => r.Time" YValue="r => r.Value" Format="#,0 'sessions'" />
<LineSeries Items="app" Name="App" XValue="r => r.Time" YValue="r => r.Value" Format="#,0 'sessions'" />
</RioChart>
<p>@clicked?.SeriesName @clicked?.Item?.Value sessions at @clicked?.Item?.Time</p>Legend toggles
Hover or focus a legend item for a summary of its series (range, last value, trend; for slices the value and share). Click it to
hide the series; @bind-HiddenSeries keeps the hidden series in your page.
Hidden: none.
Code
<RioChart TItem="Reading" @bind-HiddenSeries="hidden">
<LineSeries Items="web" Name="Web" XValue="r => r.Time" YValue="r => r.Value" Format="#,0 'sessions'" />
<LineSeries Items="app" Name="App" XValue="r => r.Time" YValue="r => r.Value" Format="#,0 'sessions'" />
</RioChart>
<p>Hidden: @string.Join(", ", hidden)</p>Highlight and show series from code
Hover or focus a legend item, or one of the buttons below the chart, to fade the other series: HighlightSeriesAsync(id).
Here OnLegendClicking cancels the usual toggle and shows only the clicked series instead, with
ShowSeriesAsync/HideSeriesAsync inside ApplyAsync so the chart draws once.
Code
<RioChart @ref="chart" TItem="Reading" OnLegendClicking="ShowOnly">
<LineSeries Id="search" Items="search" Name="Search" ... />
<LineSeries Id="social" Items="social" Name="Social" ... />
<LineSeries Id="email" Items="email" Name="Email" ... />
</RioChart>
<button @onmouseenter="() => chart.HighlightSeriesAsync("social")"
@onmouseleave="() => chart.HighlightSeriesAsync(null)">Social</button>
@code {
async Task ShowOnly(LegendClickingEventArgs e)
{
e.Cancel = true; // instead of toggling: show only the clicked series
await chart.ApplyAsync(async () =>
{
foreach (var id in ids)
await (id == e.SeriesId ? chart.ShowSeriesAsync(id) : chart.HideSeriesAsync(id));
});
}
}Selecting a range
Selection="ChartSelectionMode.Range": drag across the plot to select a range, click to clear it. Bind
@bind-SelectedRange and @bind-SelectedItems, or handle OnSelectionChanged.
A range selects the items of the visible series inside it.
Drag across the chart to select a range.
Code
<RioChart TItem="Reading" Selection="ChartSelectionMode.Range" @bind-SelectedRange="range" @bind-SelectedItems="readings">
<LineSeries Items="web" Name="Web" XValue="r => r.Time" YValue="r => r.Value" />
</RioChart>
<p>@readings.Count readings, @readings.Sum(r => r.Value) sessions</p>Selecting points
Selection="ChartSelectionMode.Points": click a point or bar to select it, Ctrl/⌘ or Shift+click to add or remove one,
click beside the points to clear. @bind-SelectedItems gets the items behind the selected points.
Click a bar to see its tickets.
Code
<RioChart TItem="Ticket" Items="tickets" Selection="ChartSelectionMode.Points" @bind-SelectedItems="selected">
<BarSeries Name="Handling time" XValue="t => t.Topic" YValue="t => t.Minutes" Aggregate="Aggregate.Sum" />
</RioChart>
<p>@string.Join(", ", selected.Select(t => t.Subject))</p>Selection on any chart
Horizontal bars, box plots and waterfalls, heatmap cells, pie and donut slices and radial bars show their selection too, as an
outline. Here two charts bind the same SelectedItems: select a topic in either and both outline it. The grouped
"Other" slice holds several items and is not outlined.
Click a bar or a slice.
Code
<RioChart TItem="Ticket" Items="tickets" Horizontal="true" Selection="ChartSelectionMode.Points" @bind-SelectedItems="selected">
<BarSeries Name="Handling time" XValue="t => t.Topic" YValue="t => t.Minutes" Aggregate="Aggregate.Sum" />
</RioChart>
<RioChart TItem="Ticket" Items="tickets" Selection="ChartSelectionMode.Points" @bind-SelectedItems="selected">
<DonutSeries Name="Handling time" XValue="t => t.Topic" YValue="t => t.Minutes" Aggregate="Aggregate.Sum" />
</RioChart>From code
Everything the user can do has an API: take a reference with @ref and call SelectItemsAsync,
SelectRangeAsync, ClearSelectionAsync, GetItemsInRange, ShowSeriesAsync,
HideSeriesAsync, ToggleSeriesAsync, HighlightSeriesAsync or Refresh.
ApplyAsync(async () => …) groups several of them into one redraw. OnLegendClicking can change or cancel
what the user did. Zooming has its own API, on Zoom and pan.
To place your own elements over a chart, ValueToPixel(x, y) gives the position of a data value in CSS pixels from the chart's
top left corner, and PixelToValue(x, y) the value at a position. Layout exposes the scales and the hit-test index.