Skip to content

Host legend and tooltip

Keep the built-in legend and tooltip, and render your own readout from ChartEvent::CrosshairMoved. The event carries a ChartPointer with the bar, optional stored time, pane value, added-series bars and study values. You do not need to repeat coordinate lookup or read the chart’s internals.

Create a first chart and one HostReadout::default() before the frame loop. Call readout.show(ui, &mut chart) each frame; it shows the chart itself. Keep this state alive so the legend can use the last event on the next frame. It calls drain_events after show. The source below is the complete readout, including its imports.

Built-in values with a host readoutOpen full size

Move over a candle to compare the chart tooltip with the host's UTC close readout, then click and leave the plot.

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

host_readout.rs
/// Keep the last local cursor event for a host legend and an additional tooltip.
#[derive(Default)]
pub struct HostReadout {
pointer: Option<egui_charts::widget::ChartPointer>,
clicked: Option<chrono::DateTime<chrono::Utc>>,
}
impl HostReadout {
/// Show the chart, dispatch its events once, then paint the host tooltip.
pub fn show(&mut self, ui: &mut eframe::egui::Ui, chart: &mut egui_charts::Chart) {
use eframe::egui;
use egui_charts::widget::ChartEvent;
let close = self
.pointer
.as_ref()
.and_then(|point| point.bar.as_ref())
.filter(|bar| !bar.is_whitespace())
.map_or_else(
|| "Move over a candle".into(),
|bar| format!("Host close: {:.2}", bar.close),
);
ui.horizontal_wrapped(|ui| {
ui.label(close);
if let Some(time) = self.clicked {
ui.label(format!("Clicked: {} UTC", time.format("%H:%M")));
}
});
chart.show(ui);
for event in chart.drain_events() {
match event {
ChartEvent::CrosshairMoved(pointer) => {
self.pointer = pointer;
ui.ctx().request_repaint();
}
ChartEvent::Click { position, .. } => {
self.clicked = position.time;
ui.ctx().request_repaint();
}
ChartEvent::EventsDropped(_) => {
self.pointer = None;
self.clicked = None;
ui.ctx().request_repaint();
}
_ => {}
}
}
if let Some(point) = &self.pointer
&& let Some(bar) = point.bar.as_ref().filter(|bar| !bar.is_whitespace())
{
egui::Area::new(ui.scope_id().with("host-readout"))
.order(egui::Order::Tooltip)
.fixed_pos(point.pos + egui::vec2(16.0, 90.0))
.interactable(false)
.show(ui.ctx(), |ui| {
egui::Frame::popup(ui.style()).show(ui, |ui| {
ui.label("Host tooltip");
ui.label(format!("{} UTC", bar.time.format("%H:%M")));
ui.label(format!("Close {:.2}", bar.close));
});
});
}
}
}

The host legend appears above the chart; the additional tooltip follows the pointer. A click saves its timestamp in the host state. The built-in tooltip stays on by default; set_tooltip_enabled turns it off when the host readout replaces it. The saved main bar’s close is a data value; ChartPointer::price is the pointer’s value on the hovered pane and can be in volume or study units.

CrosshairMoved(None) clears the host readout when the pointer leaves. A pointer over empty history or outside stored slots can still have screen coordinates and a pane value while its time and bar are absent. A whitespace bar has a timestamp but no price; the scene filters it before formatting. Keep missing values absent rather than displaying a zero or a stale close.

series_bars contains only added series with a matching timestamp, including hidden series. studies contains visible study plots by default. Use set_cursor_data_policy with IncludeHidden if the host readout needs hidden studies too. Match by stable series or study id, not list position. For production formatting, see the formatter contracts.

Drain the event queue in one place and dispatch events to all interested host components there. A second drain cannot see the first drain’s events. The interaction guide explains queue overflow and the wildcard required for this non-exhaustive enum.