Data and interaction
A report has two related contracts: which records become marks on the chart,
and what selecting one of those marks means. Keep both explicit. Drawing the
right numbers does not prove a click opens the right product after a refresh.
Fission represents data, encodings, and interaction as Rust values. The app
still owns loading, validation, durable selection, and navigation.
Choose direct series data first
Use one series value vector when a chart only needs one small sequence.
The series guide
shows a complete Compose request-history component with actual parent-supplied
daily records, empty state, translated labels, and an explicit zero baseline. A category line spaces those buckets equally. It is not a timestamp plot merely
because its labels look like dates.
Move to datasets when data is shared or tabular
A product sales report can compare two years using the same rows. Use grouped
bars for category comparisons rather than connecting unrelated products with a
line that implies a continuous trend:
use fission::prelude::*;
use fission::charts::{
Axis, BarSeries, Chart, ChartInteraction, ChartTooltipTrigger,
Dataset, Encode, Series,
};
#[derive(Clone)]
pub struct ProductSales {
pub id: String,
pub label: String,
pub units_2025: u32,
pub units_2026: u32,
}
fission_component! {
pub YearlySalesChart<()> {
props {
products: Vec<ProductSales>,
select_product: ActionEnvelope,
}
}
view(report, _state, cx) {
let dataset = Dataset::new()
.dimensions(vec!["product".into(), "2025".into(), "2026".into()])
.source(report.products.iter().map(|product| vec![
product.label.clone().into(),
(product.units_2025 as f32).into(),
(product.units_2026 as f32).into(),
]).collect());
} =>
if report.products.is_empty() {
EmptyState(title: cx.tr("sales.no_products"))
} else {
Chart(
title: cx.tr("sales.units_by_product"),
dataset: dataset,
x_axis: Axis::category(report.products.iter()
.map(|product| product.label.as_str()).collect()),
y_axis: Axis::value().min(0.0),
series: vec![
Series::Bar(BarSeries::new("2025")
.encode(Encode::new().x("product").y("2025"))),
Series::Bar(BarSeries::new("2026")
.encode(Encode::new().x("product").y("2026"))),
],
interaction: ChartInteraction::tooltips(ChartTooltipTrigger::Axis)
.emit_events(true),
on_interaction: report.select_product.clone(),
)
}
}
Enable the charts feature and supply the sales.* messages in the app's
translation bundles. The two series measure the same unit. Their year labels
describe this fixed report period, not the machine's current date.
The parent derives these display records from authoritative sales data. Keep
the original integer totals for accounting or exact labels; converting to
f32 for paint does not replace those records.
Use direct series data when | |
|---|
One layer owns a small vector. | Several series read the same rows. |
The mapping is clear from the series type. | Stable dimension names make the mapping clearer. |
A value-vector test explains the requirement. | A table/encoding test explains the requirement. |
Think about large or live data as an app architecture problem
Keep a bounded visible window in app state and load its data through a job,
service, or resource. Rendering receives the currently available snapshot; it
must not wait for the next database page.
A live tail and a historical page can overlap. Deduplicate them by sample
identity, release unused pages, and reject delayed results for an older query
generation. Otherwise a correct renderer can still show duplicate data or
replace the user's new range with an old response.
For large histories, derive an aggregation level from the visible range and
resolution. Do not send decades of raw readings simply because Dataset
accepts a vector.
Axes turn values into position
| | |
|---|
| Names or equally spaced buckets. | Missing buckets and long labels need an explicit policy. |
| Numeric counts or measurements. | Preserve units; comparisons need an honest baseline. |
| Time values with meaningful position/range. | Keep timestamp data and check the series' actual x-value support. |
| Measurements spanning orders of magnitude. | Explain the scale and validate nonpositive values before rendering. |
A y-only LineSeries::data(Vec<f32>) does not restore timestamps discarded by
the app. Likewise, putting orders and revenue on one value axis does not make
those units comparable. Test the mapping and scale, not only whether the
constructor accepts the values.
Visual maps turn values into color
A visual map maps a bounded numeric range to colour. Its bounds should come
from the report's meaning, not from whichever sample happened to arrive last.
For a percentage score with a known 0–100 range, view setup can use theme
colours:
use fission::charts::VisualMap;
let tokens = &cx.view.env().theme.tokens;
let visual_map = VisualMap::new()
.min(0.0)
.max(100.0)
.in_range_colors(vec![tokens.colors.surface, tokens.colors.primary]);
This is configuration data passed to the chart. Keep numeric readouts, labels,
or a legend available so colour is not the only way to understand a result.
A categorical palette is not automatically a meaningful low-to-high scale.
Tooltips and axis pointers provide temporary readouts. They do not own a
durable selected product. The component above receives a bound application
action for on_interaction, and its current event is decoded separately from
that action's stable payload.
For example, the parent can bind its report generation to this reducer.
SalesState owns report_generation, the corresponding visible_products,
and selected_product_id:
use fission::charts::{ChartInteractionEvent, ChartInteractionKind};
#[fission_reducer(SelectSalesProduct)]
fn select_sales_product(
state: &mut SalesState,
generation: u64,
cx: &mut ReducerContext<SalesState>,
) {
if generation != state.report_generation { return; }
let Some((_source, event)) = ChartInteractionEvent::from_action_input(cx.input) else {
return;
};
if event.kind != ChartInteractionKind::Press { return; }
let Some(index) = event.hit.and_then(|hit| hit.data_index) else { return; };
let Some(product) = state.visible_products.get(index) else { return; };
state.selected_product_id = Some(product.id.clone());
}
The parent builds the chart from exactly that generation's ordered records.
Do not treat the numeric data index as a durable product ID.
Brush configuration is separate from committing a brushed result. Configure
ChartInteraction::brush(ChartBrush::rect()) and bind Chart::on_brush when
the app needs the completed selection; decode it with
ChartBrushSelection::from_action_input. Do not expect emit_events(true) to
implement an app's filtering, paging, or navigation on its own.
Animation should explain change
Use motion when it clarifies how values changed. A finished report generally
should settle rather than loop indefinitely. Keep loading state separate from
chart entrance motion: a bar finishing its animation is not a network
completion signal.
Chart animation describes rendering behaviour, not business progress. Check
reduced-motion preferences and avoid hiding values behind continuous effects.
Testing chart behavior
| |
|---|
| Correct totals, shared units, distinct sample identity, and bounded windows. |
| Matching labels/rows, valid encodings, and appropriate axes. |
| Correct source generation and stable record selection. |
| Readable labels, intended scale, theme and narrow-layout behaviour. |
| Actual rendering and input on the target you ship. |
Lifecycle fit and verification
A useful failure case is refreshing the report while an old selection or
query result is pending. Verify that its generation guard preserves the new
records. Then inspect the actual selected product and visible detail, not only
a chart highlight or an emitted event count.