Skip to content

Export an image

An image export returns content to the host. The library renders the chart; your application chooses a destination and writes a file, copies an image or starts a browser download. It does not open an account or upload the image.

Create a first chart and Export::default() once. Call export.show(ui, &mut chart) each frame; it shows the chart itself. Pressing Prepare SVG captures the current view after the chart has been shown and stores the complete UTF-8 image in export.bytes. The scene reports the byte count or an error; it leaves delivery to the host.

Prepare an image of the current chart viewOpen full size

Pan or zoom, then press Prepare SVG. The status shows how many encoded bytes the host receives.

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

export.rs
/// Return encoded image bytes to the host without choosing a file or download destination.
#[derive(Default)]
pub struct Export {
/// The most recently prepared SVG; the host takes and delivers these bytes once.
pub bytes: Option<Vec<u8>>,
error: Option<String>,
}
impl Export {
/// Capture after showing the chart so the first export has a completed frame.
pub fn show(&mut self, ui: &mut eframe::egui::Ui, chart: &mut egui_charts::Chart) {
use egui_charts::widget::ImageExportOptions;
let mut prepare = false;
ui.horizontal_wrapped(|ui| {
prepare = ui.button("Prepare SVG").clicked();
if let Some(bytes) = &self.bytes {
ui.label(format!("{} SVG bytes ready for the host", bytes.len()));
}
if let Some(error) = &self.error {
ui.label(error);
}
});
chart.show(ui);
if prepare {
let mut options = ImageExportOptions::default();
options.size = [1440, 900];
options.include_timestamp = false;
match chart.export_svg(ui.ctx(), &options) {
Ok(svg) => {
self.bytes = Some(svg.into_bytes());
self.error = None;
}
Err(error) => {
self.bytes = None;
self.error = Some(error.to_string());
}
}
ui.ctx().request_repaint();
}
}
}

export_svg works with widget alone. The scene converts the returned string into bytes without another encoding step. Set ImageExportOptions::size in points; a PNG has pixel_density times as many pixels on each side, and an SVG document has the size itself. The first chart frame must have completed, or export returns ExportError::NoFrame. Unsupported content is reported as an error rather than silently omitted.

Take export.bytes after the scene runs, using export.bytes.take() so it is delivered once. For a native application, write those bytes with std::fs::write after the user chooses a path and report any I/O error. Use a .svg filename. In a browser, create a Blob with MIME type image/svg+xml, create an object URL, trigger a download link from the host UI, then revoke the URL after it is no longer needed. Browser clipboard and download permissions belong to that integration. The preview prepares bytes without saving a file.

Enable the existing optional png-export feature to use export_png instead. It returns Vec<u8> already encoded as PNG, with CPU rendering and no GPU readback. Save those bytes directly as .png, or use MIME type image/png for a browser download. Do not encode them again.

include_crosshair is independent of title, timestamp and watermark options. Export uses an isolated chart snapshot, preserving the live view and feed. Host legends, tooltips and panels are outside the chart capture. Use export_layout_svg or export_layout_png to draw a supplied layout into the export. Supply host texture pixels through ImageExportOptions::textures when required; custom paint callbacks can be unsupported.