pub struct SvgBackend { /* private fields */ }Expand description
Accumulates SVG markup for a plot rendered via DrawBackend.
Every number written to an attribute is finite: marks whose geometry is
NaN/±inf are dropped (a polyline keeps its finite vertices), and
non-finite style values fall back to safe defaults. All data-derived
attribute values and text go through [escape].
Implementations§
Source§impl SvgBackend
impl SvgBackend
pub fn new(width: u32, height: u32, plot_area: Rect) -> Self
Sourcepub fn set_physical_size(&mut self, width: String, height: String)
pub fn set_physical_size(&mut self, width: String, height: String)
Add extra attributes to the root <svg> element (e.g. the
data-domain family from root_data_attrs). Names must be plain
attribute names; values are escaped on output.
#527: write the root’s width and height as these lengths (e.g.
16cm, 7.5cm), so a document embeds the chart at that size; the
viewBox stays the canvas the chart was laid out on.
pub fn set_root_attrs(&mut self, attrs: Vec<(String, String)>)
Sourcepub fn extend_root_attrs(&mut self, attrs: Vec<(String, String)>)
pub fn extend_root_attrs(&mut self, attrs: Vec<(String, String)>)
Append extra attributes to the root <svg> element, after any
already set by set_root_attrs (e.g. the
data-domain family from root_data_attrs). Unlike
set_root_attrs, this does not replace what is already there.
Callers (GGPlot::svg_attr/PlotGrid::svg_attr, via
[validate_root_attrs]) are responsible for name validation and
collision checks; this just appends. Values are escaped on output.
Sourcepub fn take_warnings(&mut self) -> Vec<String>
pub fn take_warnings(&mut self) -> Vec<String>
Take the warnings reported while drawing (see DrawBackend::warn).
Sourcepub fn finish(self) -> String
pub fn finish(self) -> String
Wrap the accumulated elements in a complete <svg> document. The
data-plot attribute records the panel (data) area in viewBox units so a
host can map screen coordinates back to data (e.g. for zoom/crosshair).
Sourcepub fn finish_fragment(self, x: f64, y: f64) -> String
pub fn finish_fragment(self, x: f64, y: f64) -> String
Wrap the accumulated elements in a nested <svg> positioned at
(x, y) in a parent SVG’s user space — no xmlns (it inherits the
parent’s namespace), so the result can be pasted into a larger SVG as-is.
data-plot stays in this fragment’s own viewBox units.
Trait Implementations§
Source§impl DrawBackend for SvgBackend
impl DrawBackend for SvgBackend
Source§fn draw_shape(
&mut self,
(cx, cy): (f64, f64),
radius: f64,
style: &PointStyle,
) -> Result<(), RenderError>
fn draw_shape( &mut self, (cx, cy): (f64, f64), radius: f64, style: &PointStyle, ) -> Result<(), RenderError>
Native shapes: filled polygons for square/triangle/diamond, a single
stroked <path> for the + / × glyphs (so each point stays one
hoverable mark carrying its data-* attributes).
Source§fn draw_quadratic(
&mut self,
p0: (f64, f64),
ctrl: (f64, f64),
p1: (f64, f64),
style: &LineStyle,
) -> Result<(), RenderError>
fn draw_quadratic( &mut self, p0: (f64, f64), ctrl: (f64, f64), p1: (f64, f64), style: &LineStyle, ) -> Result<(), RenderError>
Emit a native <path d="M.. Q.."> quadratic Bézier (one real curve
instead of a flattened polyline). Non-finite input draws nothing.
Source§fn draw_rect_pattern(
&mut self,
top_left: (f64, f64),
bottom_right: (f64, f64),
pattern: &FillPattern,
style: &RectStyle,
) -> Result<(), RenderError>
fn draw_rect_pattern( &mut self, top_left: (f64, f64), bottom_right: (f64, f64), pattern: &FillPattern, style: &RectStyle, ) -> Result<(), RenderError>
#206: same markup draw_rect builds, filled
with pattern’s registered url(#…) instead of style.fill.
Source§fn draw_image(
&mut self,
bytes: &[u8],
rect: &Rect,
clip: &Rect,
alpha: f64,
) -> Result<(), RenderError>
fn draw_image( &mut self, bytes: &[u8], rect: &Rect, clip: &Rect, alpha: f64, ) -> Result<(), RenderError>
#325: an <image> with the bytes as a data: URI, in a nested
<svg> viewport that clips it to clip.
Source§fn draw_polygon_pattern(
&mut self,
pts: &[(f64, f64)],
pattern: &FillPattern,
style: &RectStyle,
) -> Result<(), RenderError>
fn draw_polygon_pattern( &mut self, pts: &[(f64, f64)], pattern: &FillPattern, style: &RectStyle, ) -> Result<(), RenderError>
#206: same markup draw_polygon builds,
filled with pattern’s registered url(#…) instead of style.fill.
Source§fn draw_polygon_rings(
&mut self,
rings: &[Vec<(f64, f64)>],
style: &RectStyle,
) -> Result<(), RenderError>
fn draw_polygon_rings( &mut self, rings: &[Vec<(f64, f64)>], style: &RectStyle, ) -> Result<(), RenderError>
One <path>, every ring a subpath (M…Z), fill-rule="evenodd" so
holes (rings after the first) are cut out of the fill — SVG’s native
equivalent of the even-odd rasteriser the pixel/plotters backends run
(#178). A ring with fewer than 3 finite points is dropped; the whole
shape draws nothing when every ring is.
Source§fn draw_polygon_rings_pattern(
&mut self,
rings: &[Vec<(f64, f64)>],
pattern: &FillPattern,
style: &RectStyle,
) -> Result<(), RenderError>
fn draw_polygon_rings_pattern( &mut self, rings: &[Vec<(f64, f64)>], pattern: &FillPattern, style: &RectStyle, ) -> Result<(), RenderError>
#206: same markup draw_polygon_rings
builds (holes cut out under the same even-odd rule), filled with
pattern’s registered url(#…) instead of style.fill.
Source§fn draw_polygon_rings_precise(
&mut self,
rings: &[Vec<(f64, f64)>],
pattern: Option<&FillPattern>,
precision: Option<u8>,
style: &RectStyle,
) -> Result<(), RenderError>
fn draw_polygon_rings_precise( &mut self, rings: &[Vec<(f64, f64)>], pattern: Option<&FillPattern>, precision: Option<u8>, style: &RectStyle, ) -> Result<(), RenderError>
#214: with precision set, writes a compact d (absolute M per
ring, then one l with implicit-repeat relative coordinate pairs,
rounded to precision decimals) instead of the one-L-per-point
absolute encoding Self::polygon_rings_markup builds. None
falls back to the trait default (today’s encoding, unchanged).
Source§fn draw_text_with_title(
&mut self,
text: &str,
(x, y): (f64, f64),
style: &TextStyle,
title: Option<&str>,
) -> Result<(), RenderError>
fn draw_text_with_title( &mut self, text: &str, (x, y): (f64, f64), style: &TextStyle, title: Option<&str>, ) -> Result<(), RenderError>
#159: with title, wraps the <text> in a <title> child
carrying the full (escaped) label — e.g. a legend key truncated
with an ellipsis to fit a vertical multi-column grid.
Source§fn draw_rich_text(
&mut self,
runs: &[RichRun],
(x, y): (f64, f64),
style: &TextStyle,
) -> Result<(), RenderError>
fn draw_rich_text( &mut self, runs: &[RichRun], (x, y): (f64, f64), style: &TextStyle, ) -> Result<(), RenderError>
#196: each linked run becomes <a href="…" target="_blank" rel="noopener"><tspan text-decoration="underline">…</tspan></a>
(rel="noopener" so the opened tab can’t reach back via
window.opener); a plain run is a bare <tspan>. All runs sit
inside one <text>, so style.anchor aligns their combined width
exactly as it would a single run’s.
Rich text (#413) as one <text> of styled <tspan>s — the anchor
aligns the whole label, super / subscripts use baseline-shift.
Source§fn begin_panel(&mut self, index: usize, label: &str, clip: &Rect, data: &Rect)
fn begin_panel(&mut self, index: usize, label: &str, clip: &Rect, data: &Rect)
facet_wrap (e.g. coord_sf small
multiples): marks until end_panel belong to
panel index (strip label label), clipped to clip; data is the
rect the coord maps the data window onto. The SVG backend wraps them
in a clipped <g data-facet-panel> and lists every panel on the root
as data-panels. Default: no-op.Source§fn end_panel(&mut self)
fn end_panel(&mut self)
begin_panel.
Default: no-op.Source§fn enable_element_classes(&mut self, on: bool)
fn enable_element_classes(&mut self, on: bool)
set_element_class tags the non-data
elements drawn (axes, grid, legend, strips, titles). Default: no-op.Source§fn set_element_class(&mut self, class: Option<&'static str>)
fn set_element_class(&mut self, class: Option<&'static str>)
"gg-axis gg-axis-x gg-axis-text", …) of the
non-data elements drawn until the next call; None clears it. Only
written once enable_element_classes
is on. Default: no-op.Source§fn begin_clip(&mut self, rect: &Rect)
fn begin_clip(&mut self, rect: &Rect)
end_clip to rect
(#411: a single panel’s layers, when they reach past it). Default:
no-op.Source§fn end_clip(&mut self)
fn end_clip(&mut self)
begin_clip.Source§fn set_viewport(&mut self, rect: &Rect)
fn set_viewport(&mut self, rect: &Rect)
coord_sf). The SVG backend writes it as the root data-viewport.
Default: no-op.fn plot_area(&self) -> Rect
fn total_area(&self) -> Rect
Source§fn corner_claims(&self) -> Vec<Rect>
fn corner_claims(&self) -> Vec<Rect>
Self::claim_corner_box so far this render pass — the scale
bar, attribution text, bivariate legend, or an inside ordinary
legend already placed (render::corner_layout, #273). Default:
always empty — a backend that never overrides this (anything that
pre-dates #273, including a third-party impl DrawBackend) keeps
every corner item positioned exactly as before, byte-identical.Source§fn claim_corner_box(&mut self, rect: Rect)
fn claim_corner_box(&mut self, rect: Rect)
rect as claimed in a panel corner, for a later corner
geom in the same render pass to see via Self::corner_claims
(#273). Default: no-op, paired with that method’s empty default.Source§fn clear_corner_claims(&mut self)
fn clear_corner_claims(&mut self)
Self::claim_corner_box — called
once per panel (renderer.rs), so a facet’s second panel doesn’t
see the first panel’s claims (#273). Default: no-op, paired with
the above.Source§fn set_tooltip(&mut self, tooltip: Option<String>)
fn set_tooltip(&mut self, tooltip: Option<String>)
None.
Backends that produce interactive output (SVG) emit it as a <title>
for a native hover tooltip; others ignore it. Default: no-op.Source§fn set_mark_axis(&mut self, key: Option<String>)
fn set_mark_axis(&mut self, key: Option<String>)
data-x attribute so a host can group marks by x (axis-pointer tooltip).
Default: no-op.Source§fn set_mark_series(&mut self, series: Option<String>)
fn set_mark_series(&mut self, series: Option<String>)
data-series, so a host can highlight or
filter one series without parsing the tooltip. Default: no-op.Source§fn set_mark_value(&mut self, value: Option<String>)
fn set_mark_value(&mut self, value: Option<String>)
data-value. Default: no-op.Source§fn set_mark_geom(&mut self, geom: Option<&str>)
fn set_mark_geom(&mut self, geom: Option<&str>)
Geom::name) until cleared with None. SVG emits
class="gg-mark gg-<kind>" (kind from the primitive: point, line,
band, bar, text) plus data-geom, so a host can select data
marks without matching axes, grid lines or legends. Default: no-op.Source§fn warn(&mut self, message: String)
fn warn(&mut self, message: String)
Source§fn set_mark_part(&mut self, part: Option<&str>)
fn set_mark_part(&mut self, part: Option<&str>)
"arrow" for an arrowhead drawn alongside a segment/curve/
path/line). While set, the SVG backend writes data-part="<part>" aria-hidden="true" and omits the <title> tooltip (the part is
decorative; the parent mark already carries the semantic info).
data-x/data-series/data-value (and any user data-* attrs) are
still written. Default: no-op.Source§fn set_mark_attrs(&mut self, attrs: Vec<(String, String)>)
fn set_mark_attrs(&mut self, attrs: Vec<(String, String)>)
data-* attributes to subsequently-drawn marks
(B1-7’s Aes::data_attr). The SVG backend writes each, escaped; an
attr with the same name as a built-in (data-x, data-series,
data-value) replaces it rather than duplicating it. An empty
Vec clears any previously set attrs. Default: no-op.Source§fn wants_formatted_values(&self) -> bool
fn wants_formatted_values(&self) -> bool
crate::plot::GGPlot::svg_formatted_values, B2-4) — a query a
geom checks before doing the (otherwise pointless) work of resolving
a scale’s formatter for set_mark_fvalue/set_mark_fx. Default
false, so the feature is byte-identical when never turned on.Source§fn set_mark_fvalue(&mut self, value: Option<String>)
fn set_mark_fvalue(&mut self, value: Option<String>)
data-fvalue on subsequently-drawn marks: the “value” aesthetic’s
raw data value (y for most geoms, fill for hex/tile), formatted
through that aesthetic’s scale (Scale::format_value) — e.g. a
de-DE currency string. Only meaningful while
wants_formatted_values is true;
None clears any previous mark’s value, per row. Default: no-op.Source§fn set_mark_fx(&mut self, value: Option<String>)
fn set_mark_fx(&mut self, value: Option<String>)
data-fx on subsequently-drawn marks: the x value formatted through
the x scale (e.g. a datetime axis’s ISO/locale date). The SVG
backend omits it when it matches the already-written data-x, so a
plain numeric x doesn’t get a redundant duplicate attribute. Default:
no-op.Source§fn set_mark_class(&mut self, class: Option<String>)
fn set_mark_class(&mut self, class: Option<String>)
Aes::class), space-separated and already validated by the caller;
None clears them, per row. The SVG backend appends them to the
mark’s own class="gg-mark gg-<kind>" (re-validating every token as
defence in depth). Default: no-op.Source§fn set_mark_group(&mut self, group: Option<(String, String)>)
fn set_mark_group(&mut self, group: Option<(String, String)>)
(attribute name, value) — e.g. ("data-of", "node-7") — until
cleared with None (#221’s Aes::group_wrap). The SVG backend
collects every mark with the same pair into one <g name="value">,
placed where the group’s first mark was drawn. Default: no-op (a
raster backend has no grouping).Source§fn set_layer_attrs(&mut self, attrs: Vec<(String, String)>)
fn set_layer_attrs(&mut self, attrs: Vec<(String, String)>)
GGPlot::layer_svg_attr):
class tokens, aria-hidden, pointer-events, data-*, already
validated by the caller. An empty Vec clears them (set before a
layer draws, cleared after). Default: no-op.fn draw_circle( &mut self, (cx, cy): (f64, f64), radius: f64, style: &PointStyle, ) -> Result<(), RenderError>
fn draw_line( &mut self, pts: &[(f64, f64)], style: &LineStyle, ) -> Result<(), RenderError>
fn draw_rect( &mut self, top_left: (f64, f64), bottom_right: (f64, f64), style: &RectStyle, ) -> Result<(), RenderError>
fn draw_polygon( &mut self, pts: &[(f64, f64)], style: &RectStyle, ) -> Result<(), RenderError>
fn draw_text( &mut self, text: &str, pos: (f64, f64), style: &TextStyle, ) -> Result<(), RenderError>
Source§fn draw_text_runs(
&mut self,
runs: &[(&str, Option<&str>)],
(x, y): (f64, f64),
style: &TextStyle,
) -> Result<(), RenderError>
fn draw_text_runs( &mut self, runs: &[(&str, Option<&str>)], (x, y): (f64, f64), style: &TextStyle, ) -> Result<(), RenderError>
http(s)
hyperlink (ggspatial-style attribution: “© BKG” linked, a licence
code linked, plain connective text between them —
annotation_attribution’s .link()/.runs(), #196). pos/style
place and style the line as a whole, same as draw_text
— style.anchor aligns the total run sequence, not each run. Read moreSource§fn set_clipping(&mut self, _on: bool)
fn set_clipping(&mut self, _on: bool)
Auto Trait Implementations§
impl Forget for SvgBackend
impl Freeze for SvgBackendwhere
*const u8 is !null: Freeze,
PhantomData<u8>: Freeze,
PhantomData<(String, String)>: Freeze,
PhantomData<String>: Freeze,
PhantomData<(FillPattern, String)>: Freeze,
PhantomData<(String, String, String)>: Freeze,
PhantomData<(usize, usize)>: Freeze,
PhantomData<Rect>: Freeze,
PhantomData<(usize, String, Rect, Rect)>: Freeze,
&'static str: Freeze,
impl Move for SvgBackend
impl RefUnwindSafe for SvgBackend
impl Send for SvgBackend
impl Sync for SvgBackendwhere
RawVecInner: Sync,
impl Unpin for SvgBackend
impl UnsafeUnpin for SvgBackendwhere
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
PhantomData<(String, String)>: UnsafeUnpin,
PhantomData<String>: UnsafeUnpin,
PhantomData<(FillPattern, String)>: UnsafeUnpin,
PhantomData<(String, String, String)>: UnsafeUnpin,
PhantomData<(usize, usize)>: UnsafeUnpin,
PhantomData<Rect>: UnsafeUnpin,
PhantomData<(usize, String, Rect, Rect)>: UnsafeUnpin,
&'static str: UnsafeUnpin,
impl UnwindSafe for SvgBackend
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more