Transitions
When you replace a chart's data (another period, another chart type, another symbol) the new picture normally appears at once. A transition turns that switch into a short animation from the picture on screen to the new one. This chapter covers what moves during a transition, how to start one, and its limits.
What moves
A transition is started with kind: 'morph' (the default) or kind: 'fade'.
Switching periods (morph). Each old bar is matched with every new bar whose time interval it overlaps:
- Several old bars merge into one new bar (1m to 5m).
- One old bar splits into several new bars (5m to 1m).
- An old bar that crosses a boundary of the new period flows into both new bars. This is why any two periods work, also ones that don't divide each other evenly, such as 45m and 1h.
The bar spacing stays the same and the time at the right edge of the view stays where it is. Going to a longer period therefore shows a longer stretch of time, and the camera zooms out; going to a shorter one zooms in. The price scale moves to the new range at the same time.
Switching chart types (morph). Candles shrink to their closes and become a line, and a line grows into candles. Candles, Heikin-Ashi and OHLC bars morph into each other.
Switching symbols (fade). The prices of two symbols have nothing in common, so nothing is matched. The price scale changes to the new symbol's range at once, and the old picture fades out over the new one.
Whatever the kind, the last frame of a transition is exactly the picture you would get by switching without one. Markers, drawings, price lines and axis labels follow the moving view.
Starting a transition
Call chart.beginTransition(options?), then change the data before the next
frame is drawn: setData, addSeries (and the other add*Series methods),
series.remove() and addIndicator. Everything changed until the next frame
belongs to the transition.
| Option | Type | Default | Meaning |
|---|---|---|---|
duration | number | 350 | Length in milliseconds; 0 switches at once |
kind | 'morph' | 'fade' | 'morph' | 'morph' for periods and chart types, 'fade' for symbols |
An invalid duration (negative, not finite) or an unknown kind throws.
| Method | Does |
|---|---|
beginTransition(options?) | Starts a transition for the changes made before the next frame |
finishTransition() | Ends the transition now and shows the new picture |
transitionProgress() | Progress of the running transition, from 0 to 1, linear in time (before easing); null when none runs |
Example: switching periods
Here the page keeps one-minute bars and aggregates them into the chosen
period. Loading the bars of the new period from your backend works the same
way: call beginTransition right before setData.
import { FerroChart, type OhlcRow } from '@ferrochart/core';
declare const minuteBars: OhlcRow[]; // oldest first, time in UTC ms
function aggregate(bars: OhlcRow[], periodMs: number): OhlcRow[] {
const out: OhlcRow[] = [];
for (const b of bars) {
const t = Math.floor(Number(b.time) / periodMs) * periodMs;
const last = out[out.length - 1];
if (last && last.time === t) {
last.high = Math.max(last.high, b.high);
last.low = Math.min(last.low, b.low);
last.close = b.close;
last.volume = (last.volume ?? 0) + (b.volume ?? 0);
} else {
out.push({ time: t, open: b.open, high: b.high, low: b.low, close: b.close, volume: b.volume ?? 0 });
}
}
return out;
}
const chart = await FerroChart.create(canvas);
const candles = chart.addCandlestickSeries();
chart.addVolumeSeries(candles);
candles.setData(aggregate(minuteBars, 60 * 60_000)); // 1h
function setPeriod(periodMs: number) {
chart.beginTransition(); // 350 ms morph
candles.setData(aggregate(minuteBars, periodMs));
}
setPeriod(45 * 60_000); // 1h -> 45mWith bars from a server, fetch first and start the transition when the data has arrived, so the animation does not wait for the network:
async function loadPeriod(period: string) {
const bars = await fetchBars('BTCUSD', period); // your backend
chart.beginTransition({ duration: 400 });
candles.setData(bars);
}Example: switching chart types
To go from candles to a line of closes, remove the candle series and add a line series with the same times:
let candles: Series | null = chart.addCandlestickSeries();
candles.setData(bars);
let line: Series | null = null;
function showLine() {
if (!candles) return;
chart.beginTransition();
candles.remove();
candles = null;
line = chart.addSeries('line', { color: '#2962ff' });
line.setData({ time: bars.map((b) => b.time), value: bars.map((b) => b.close) });
}
function showCandles() {
if (!line) return;
chart.beginTransition();
line.remove();
line = null;
candles = chart.addCandlestickSeries();
candles.setData(bars);
}Candles, Heikin-Ashi and OHLC bars are switched the same way.
Example: switching symbols
async function loadSymbol(symbol: string) {
const bars = await fetchBars(symbol, '1h');
chart.beginTransition({ kind: 'fade' });
candles.setData(bars);
}Indicator series your page computes
Indicators added with addIndicator follow their source series. If instead
your page computes its own series from the bars (a moving average drawn as a
line series, say), replace or clear them in the same transition, before
setData of the main series:
chart.beginTransition();
sma.setData(computeSma(newBars, 20)); // or sma.remove()
candles.setData(newBars);All series share one time axis. A series still holding values at the old bars' times keeps those times on the axis, and the new bars would not get the spacing you expect.
Interrupting
- Gestures end a running transition at once, at its new picture: a press, the wheel, a pinch, a key, a double-click. Moving the pointer does not; the crosshair is hidden while the transition runs and comes back at its end.
finishTransition()ends it from your code.- Calling
beginTransitionagain during a period switch (the user clicks through several periods quickly) starts the next one from the picture on screen at that moment, so the motion has no jump. A running type switch or fade first jumps to its end.
Reduced motion
While the system asks for reduced motion (prefers-reduced-motion: reduce),
beginTransition switches at once. To animate anyway, pass
respectReducedMotion: false when creating the chart:
const chart = await FerroChart.create(canvas, { respectReducedMotion: false });Limits
- Area and baseline series (and points and step lines) are not morphed from one type into another. They are drawn as usual during the transition, at their new data.
- When very many bars are visible (several per pixel of the chart's width), the bars switch at once and only the camera and the price scale animate.
To make the price scale also ease after the transition, during live updates and while panning, see Smoothing.
Python
chart.begin_transition() # duration=350, kind="morph"
candles.set_data(df_45m)
chart.begin_transition(duration=500, kind="fade")
candles.set_data(other_symbol)
chart.finish_transition()
p = chart.transition_progress() # 0.0 to 1.0, None when none runsA duration of 0 or less switches at once. to_png and render_png finish a
running transition first: an image has no clock, so it shows the new picture.
Rust
use ferrochart_core::chart::{Transition, TransitionKind};
chart.begin_transition(Transition { duration_ms: 350.0, kind: TransitionKind::Morph });
// or: chart.begin_transition(Transition::default());
chart.set_data(series, SeriesData::Ohlc(bars_5m))?;
// In your render loop: each frame advances it.
chart.build_frame(now_ms, &text);
if chart.is_animating() {
// request another frame
}
chart.finish_transition();
let p: Option<f32> = chart.transition_progress();The transition advances with the now_ms you pass to build_frame, and
is_animating() is true while it runs. A duration of 0, negative or NaN
switches at once.
C
ferrochart_begin_transition(chart, 350.0f, FERROCHART_TRANSITION_MORPH);
ferrochart_set_data_ohlc(chart, candles, time, open, high, low, close, vol, n);
/* FERROCHART_TRANSITION_FADE for a symbol switch */
ferrochart_finish_transition(chart);
float p;
ferrochart_transition_progress(chart, &p); /* 0 to 1; -1 when none */The transition advances with the now_ms you pass to ferrochart_render or
ferrochart_render_rgba; ferrochart_needs_redraw returns 1 while it runs.