Skip to content

Synchronize two charts

Share UTC times between charts, keeping each chart’s price scale independent. Pixel positions, bar indices and spacing depend on the local data and width. The chart under the pointer leads. Forward a range only when that chart’s view changes, so moving onto a chart does not send its view back to the other one.

Keep both charts and the last forwarded cursor

Section titled “Keep both charts and the last forwarded cursor”

Create two first charts once. Pass one to Synchronized::new(other) and retain the other as chart. Each frame call synchronized.show(ui, &mut chart); this shows both charts in stacked regions, which also fits a narrow viewport. The scene makes the second chart a line so the two views are easy to distinguish.

Two charts sharing a UTC cursor and viewOpen full size

Hover either plot, pan or zoom it, then move onto the other. Its price scale stays independent and the view does not jump on entry.

The interactive example needs JavaScript and WebAssembly. You can still use the chart setup alongside it.

synchronized.rs
/// Share UTC positions while keeping each chart's price scale independent.
pub struct Synchronized {
other: egui_charts::Chart,
last_crosshair: Option<(usize, chrono::DateTime<chrono::Utc>)>,
}
impl Synchronized {
/// Retain the second chart and draw its prices as a line.
pub fn new(mut other: egui_charts::Chart) -> Self {
other.set_chart_type(egui_charts::ChartType::Line);
other.config_mut().show_volume = false;
Self {
other,
last_crosshair: None,
}
}
/// Show both charts before forwarding the local cursor or a changed viewport.
pub fn show(&mut self, ui: &mut eframe::egui::Ui, chart: &mut egui_charts::Chart) {
use eframe::egui;
let mut charts = [chart, &mut self.other];
let before = charts.each_ref().map(|chart| chart.visible_time_range());
let height = (ui.available_height() - ui.spacing().item_spacing.y) / 2.0;
let mut source = None;
for (i, chart) in charts.iter_mut().enumerate() {
ui.push_id(i, |ui| {
let response = ui.allocate_ui_with_layout(
egui::vec2(ui.available_width(), height),
egui::Layout::top_down(egui::Align::Min),
|ui| chart.show(ui),
);
if ui.rect_contains_pointer(response.response.rect) {
source = Some(i);
}
});
}
if let Some(i) = source {
let time = charts[i].hover_time();
let crosshair = time.map(|time| (i, time));
if crosshair != self.last_crosshair {
charts[i].set_synced_crosshair_time(None);
charts[1 - i].set_synced_crosshair_time(time);
self.last_crosshair = crosshair;
ui.ctx().request_repaint();
}
let range = charts[i].visible_time_range();
if range != before[i]
&& let Some(range) = range
{
charts[1 - i].apply_synced_time_range(range);
ui.ctx().request_repaint();
}
} else if self.last_crosshair.take().is_some() {
for chart in &mut charts {
chart.set_synced_crosshair_time(None);
}
ui.ctx().request_repaint();
}
for chart in &mut charts {
chart.drain_events().for_each(drop);
}
}
}

Both charts are shown before ranges are applied, because a target needs a known plot width. Comparing each visible range before and after show detects a local pan or zoom. The next frame starts with any forwarded range already applied, so it does not echo that range back. Repaint after changing a chart already drawn in this frame. Retaining the last source and time avoids repainting indefinitely while the pointer stays still.

hover_time and set_synced_crosshair_time forward a UTC instant without sharing a price. A receiver with coarser bars marks the bar whose period contains that time. None clears the synchronized cursor; the scene also clears it when the pointer leaves both plots. Programmatic cursors do not emit local CrosshairMoved events.

visible_time_range returns the UTC plot edges after layout. apply_synced_time_range maps those edges into the receiver’s slots; available history and spacing limits can constrain the result. For a larger workspace, ChartLayout provides grouped synchronization. The native synchronized_charts example also demonstrates different intervals.