dataviz::figure::canvas::svgcanvas

Struct SvgCanvas

source
pub struct SvgCanvas {
    pub width: u32,
    pub height: u32,
    pub elements: Vec<String>,
    pub margin: u32,
    pub background_color: String,
}
Expand description

A structure for creating and managing an SVG-based drawing canvas.

Fields§

§width: u32

Width of the SVG canvas.

§height: u32

Height of the SVG canvas.

§elements: Vec<String>

A vector storing SVG elements as strings.

§margin: u32

Margin size for the SVG canvas.

§background_color: String

Background color of the SVG canvas.

Implementations§

source§

impl SvgCanvas

source

pub fn new(width: u32, height: u32, background_color: &str, margin: u32) -> Self

Creates a new SvgCanvas with the specified dimensions, background color, and margin.

§Parameters
  • width: The width of the canvas in pixels.
  • height: The height of the canvas in pixels.
  • background_color: The background color as a string.
  • margin: Margin size in pixels.
§Returns

A new SvgCanvas instance.

source

pub fn clear(&mut self)

Clears the SVG canvas by removing all elements and reinitializing.

source

pub fn draw_line( &mut self, x1: f64, y1: f64, x2: f64, y2: f64, color: &str, stroke_width: f64, )

Adds a line to the SVG canvas.

§Parameters
  • x1, y1: Coordinates of the start point.
  • x2, y2: Coordinates of the end point.
  • color: The stroke color of the line.
  • stroke_width: The width of the line stroke.
source

pub fn draw_line_rgb( &mut self, x1: f64, y1: f64, x2: f64, y2: f64, color: [u8; 3], stroke_width: f64, )

Adds a line with RGB color to the SVG canvas.

§Parameters
  • x1, y1: Coordinates of the start point.
  • x2, y2: Coordinates of the end point.
  • color: The RGB color of the line.
  • stroke_width: The width of the line stroke.
source

pub fn draw_rect( &mut self, x: f64, y: f64, width: f64, height: f64, fill_color: &str, stroke_color: &str, stroke_width: f64, opacity: f64, )

Adds a rectangle to the SVG canvas.

§Parameters
  • x, y: Coordinates of the top-left corner.
  • width, height: Dimensions of the rectangle.
  • fill_color: Fill color of the rectangle.
  • stroke_color: Stroke color of the rectangle.
  • stroke_width: Width of the rectangle’s border.
  • opacity: Opacity of the rectangle fill (0.0 to 1.0).
source

pub fn add_font_style( &mut self, font_url: &str, class_name: &str, font_family: &str, )

Adds a font style definition to the SVG canvas.

§Parameters
  • font_url: URL of the font to be imported.
  • class_name: CSS class name for applying the font.
  • font_family: Name of the font family.
source

pub fn draw_circle(&mut self, cx: f64, cy: f64, r: f64, color: &str)

Draws a circle on the SVG canvas.

§Parameters
  • cx, cy: Coordinates of the circle’s center.
  • r: Radius of the circle.
  • color: Fill color of the circle.
source

pub fn draw_text( &mut self, x: f64, y: f64, text: &str, font_size: f64, color: &str, )

Adds a text element to the SVG canvas.

§Parameters
  • x, y: Coordinates of the text’s position.
  • text: The text content.
  • font_size: Font size of the text.
  • color: Text color.
source

pub fn draw_title( &mut self, x: f64, y: f64, text: &str, font_size: f64, color: &str, )

Adds a text element to the SVG canvas.

§Parameters
  • x, y: Coordinates of the text’s position.
  • text: The text content.
  • font_size: Font size of the text.
  • color: Text color.
source

pub fn draw_grid( &mut self, x_min: f64, x_max: f64, y_min: f64, y_max: f64, x_ticks: usize, y_ticks: usize, color: &str, )

Draws a grid on the SVG canvas.

§Parameters
  • x_min, x_max: Horizontal range for the grid.
  • y_min, y_max: Vertical range for the grid.
  • x_ticks: Number of grid lines along the x-axis.
  • y_ticks: Number of grid lines along the y-axis.
  • color: Color of the grid lines.
source

pub fn save(&self, file_path: &str) -> Result<()>

Saves the SVG content to a file.

§Parameters
  • file_path: The path to save the SVG file.
§Errors

Returns an io::Result if saving fails.

source

pub fn get_svg_as_text(&self) -> String

Retrieves the SVG content as a single string.

§Returns

The complete SVG as a string.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V