XYChart

Struct XYChart 

Source
pub struct XYChart {
    pub id: Option<String>,
    pub x_range: ScaleRange,
    pub y_range: ScaleRange,
    pub plot_width: u32,
    pub plot_height: u32,
    pub to_plot: CoordinateTransform,
    pub to_world: CoordinateTransform,
    pub view_parameters: ViewParameters,
    pub layers: HashMap<&'static str, Layer>,
    pub clip_paths: Vec<ClipPath>,
    pub margins: [i32; 4],
}
Expand description

An XYChart is a two-dimensional chart that can plot data in a Cartesian coordinate system, with x and y axes. It supports various features such as adding ticks, labels, grid lines, and annotations. It can also render images and shapes on the plot area. It is designed to be flexible and extensible, allowing users to customize the appearance and behavior of the chart.

Fields§

§id: Option<String>§x_range: ScaleRange§y_range: ScaleRange§plot_width: u32§plot_height: u32§to_plot: CoordinateTransform

Transformation from world/data coordinates to plot (SVG) coordinates.

§to_world: CoordinateTransform

Transformation from plot (SVG) coordinates to world/data coordinates.

§view_parameters: ViewParameters

Parameters controlling the SVG view box and viewport.

§layers: HashMap<&'static str, Layer>

Layers for rendering chart elements (axes, grid, data, etc.).

§clip_paths: Vec<ClipPath>

Clip paths used for masking chart elements.

§margins: [i32; 4]

Margins around the plot area: [top, right, bottom, left].

Implementations§

Source§

impl XYChart

Source

pub const ANNOTATE_SEP: i32 = 2i32

Separation between annotation and the annotated element, in pixels.

Source

pub const LABEL_HEIGHT: i32 = 16i32

Height of axis labels, in pixels.

Source

pub const DESCRIPTION_HEIGHT: i32 = 20i32

Height of axis descriptions, in pixels.

Source

pub const DESCRIPTION_SEP: i32 = 20i32

Separation between axis description and axis, in pixels.

Source

pub const DESCRIPTION_OFFSET: i32 = 36i32

Offset for axis descriptions, in pixels.

Source

pub fn new( plot_width_and_height: (u32, u32), ranges: (impl RangeBounds<f64>, impl RangeBounds<f64>), ) -> XYChart

Creates a new XYChart with the specified plot size and axis ranges.

§Arguments
  • plot_width_and_height - Tuple of plot width and height in pixels.
  • ranges - Tuple of x and y axis ranges.
Examples found in repository?
examples/sine_curve.rs (line 17)
5pub fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let pi = std::f64::consts::PI;
7    let pi2 = 2.0 * pi;
8
9    const N: i32 = 100;
10    let sin: Vec<(f64, f64)> = (0..N)
11        .map(|i| {
12            let x = i as f64 * pi2 / (N as f64 - 1.0);
13            (x, x.sin())
14        })
15        .collect();
16
17    let chart = XYChart::new((600, 300), (..pi2, -1.1..1.1))
18        .x_labels(1.0, 10, None)
19        .y_labels(0.5, 10, None)
20        .x_axis_description("x", None)
21        .y_axis_description("sin(x)", None)
22        .plot_grid(0.2, 0.1, class("fine-grid"))
23        .plot_grid(1.0, 0.5, class("grid"))
24        .plot_poly_line(sin, class("curve"))
25        .plot_poly_line(vec![(0.0, 0.0), (pi2, 0.0)], class("base-line"));
26
27    SvgDocument::new()
28        .append_scss(STYLE)
29        .add_plot(Box::new(chart))
30        .save("docs/img/sine_curve.svg")
31}
Source

pub fn set_id(self, id: impl Into<String>) -> Self

Source

pub fn ticks( self, x_step: f64, y_step: f64, length: i32, style_attr: Option<StyleAttr>, ) -> Self

Adds tick marks to both axes of the chart.

Tick marks are drawn at regular intervals along the x and y axes, with the specified length and style. The method automatically updates the chart margins if the tick length exceeds the current margin.

§Arguments
  • x_step - Interval between tick marks on the x-axis.
  • y_step - Interval between tick marks on the y-axis.
  • length - Length of each tick mark in pixels.
  • style_attr - Style attributes for the tick marks.
§Returns

Returns the updated chart with tick marks added.

Source

pub fn x_labels( self, step: f64, offset: usize, style_attr: Option<StyleAttr>, ) -> Self

Adds x-axis labels to the axes layer of the chart.

The labels are positioned along the x-axis at intervals specified by step. The offset parameter controls the vertical distance from the axis to each label, which is useful for preventing overlap with the axis or other elements.

§Arguments
  • step - The interval between labels along the x-axis.
  • offset - The offset from the axis to the label, in pixels.
Examples found in repository?
examples/sine_curve.rs (line 18)
5pub fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let pi = std::f64::consts::PI;
7    let pi2 = 2.0 * pi;
8
9    const N: i32 = 100;
10    let sin: Vec<(f64, f64)> = (0..N)
11        .map(|i| {
12            let x = i as f64 * pi2 / (N as f64 - 1.0);
13            (x, x.sin())
14        })
15        .collect();
16
17    let chart = XYChart::new((600, 300), (..pi2, -1.1..1.1))
18        .x_labels(1.0, 10, None)
19        .y_labels(0.5, 10, None)
20        .x_axis_description("x", None)
21        .y_axis_description("sin(x)", None)
22        .plot_grid(0.2, 0.1, class("fine-grid"))
23        .plot_grid(1.0, 0.5, class("grid"))
24        .plot_poly_line(sin, class("curve"))
25        .plot_poly_line(vec![(0.0, 0.0), (pi2, 0.0)], class("base-line"));
26
27    SvgDocument::new()
28        .append_scss(STYLE)
29        .add_plot(Box::new(chart))
30        .save("docs/img/sine_curve.svg")
31}
Source

pub fn y_labels( self, step: f64, offset: usize, style_attr: Option<StyleAttr>, ) -> Self

Adds y-axis labels to the axes layer of the chart.

The labels are rotated 90 degrees counter-clockwise and positioned to the left of the axis. The offset parameter controls the distance from the axis to each label, which is useful for long labels that might otherwise overlap the axis.

§Arguments
  • step - The interval between labels.
  • offset - The offset from the axis to the label, in pixels.
Examples found in repository?
examples/sine_curve.rs (line 19)
5pub fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let pi = std::f64::consts::PI;
7    let pi2 = 2.0 * pi;
8
9    const N: i32 = 100;
10    let sin: Vec<(f64, f64)> = (0..N)
11        .map(|i| {
12            let x = i as f64 * pi2 / (N as f64 - 1.0);
13            (x, x.sin())
14        })
15        .collect();
16
17    let chart = XYChart::new((600, 300), (..pi2, -1.1..1.1))
18        .x_labels(1.0, 10, None)
19        .y_labels(0.5, 10, None)
20        .x_axis_description("x", None)
21        .y_axis_description("sin(x)", None)
22        .plot_grid(0.2, 0.1, class("fine-grid"))
23        .plot_grid(1.0, 0.5, class("grid"))
24        .plot_poly_line(sin, class("curve"))
25        .plot_poly_line(vec![(0.0, 0.0), (pi2, 0.0)], class("base-line"));
26
27    SvgDocument::new()
28        .append_scss(STYLE)
29        .add_plot(Box::new(chart))
30        .save("docs/img/sine_curve.svg")
31}
Source

pub fn x_axis_description( self, description: &str, style_attr: Option<StyleAttr>, ) -> Self

Add an x-axis description, positioned below the axis, onto the axes layer.

Examples found in repository?
examples/sine_curve.rs (line 20)
5pub fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let pi = std::f64::consts::PI;
7    let pi2 = 2.0 * pi;
8
9    const N: i32 = 100;
10    let sin: Vec<(f64, f64)> = (0..N)
11        .map(|i| {
12            let x = i as f64 * pi2 / (N as f64 - 1.0);
13            (x, x.sin())
14        })
15        .collect();
16
17    let chart = XYChart::new((600, 300), (..pi2, -1.1..1.1))
18        .x_labels(1.0, 10, None)
19        .y_labels(0.5, 10, None)
20        .x_axis_description("x", None)
21        .y_axis_description("sin(x)", None)
22        .plot_grid(0.2, 0.1, class("fine-grid"))
23        .plot_grid(1.0, 0.5, class("grid"))
24        .plot_poly_line(sin, class("curve"))
25        .plot_poly_line(vec![(0.0, 0.0), (pi2, 0.0)], class("base-line"));
26
27    SvgDocument::new()
28        .append_scss(STYLE)
29        .add_plot(Box::new(chart))
30        .save("docs/img/sine_curve.svg")
31}
Source

pub fn y_axis_description( self, description: &str, style_attr: Option<StyleAttr>, ) -> Self

Add a y-axis description, positioned to the left of the axis, onto the axes layer. The description is centered vertically along the y-axis.

Examples found in repository?
examples/sine_curve.rs (line 21)
5pub fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let pi = std::f64::consts::PI;
7    let pi2 = 2.0 * pi;
8
9    const N: i32 = 100;
10    let sin: Vec<(f64, f64)> = (0..N)
11        .map(|i| {
12            let x = i as f64 * pi2 / (N as f64 - 1.0);
13            (x, x.sin())
14        })
15        .collect();
16
17    let chart = XYChart::new((600, 300), (..pi2, -1.1..1.1))
18        .x_labels(1.0, 10, None)
19        .y_labels(0.5, 10, None)
20        .x_axis_description("x", None)
21        .y_axis_description("sin(x)", None)
22        .plot_grid(0.2, 0.1, class("fine-grid"))
23        .plot_grid(1.0, 0.5, class("grid"))
24        .plot_poly_line(sin, class("curve"))
25        .plot_poly_line(vec![(0.0, 0.0), (pi2, 0.0)], class("base-line"));
26
27    SvgDocument::new()
28        .append_scss(STYLE)
29        .add_plot(Box::new(chart))
30        .save("docs/img/sine_curve.svg")
31}
Source

pub fn plot_grid( self, x_step: f64, y_step: f64, style_attr: Option<StyleAttr>, ) -> Self

Draw a grid on the plot area, using the specified step sizes for x and y axes. The grid lines are drawn as paths on the plot layer. The grid can be placed before or after other object on the plot layer, by the order of the method calls.

Examples found in repository?
examples/sine_curve.rs (line 22)
5pub fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let pi = std::f64::consts::PI;
7    let pi2 = 2.0 * pi;
8
9    const N: i32 = 100;
10    let sin: Vec<(f64, f64)> = (0..N)
11        .map(|i| {
12            let x = i as f64 * pi2 / (N as f64 - 1.0);
13            (x, x.sin())
14        })
15        .collect();
16
17    let chart = XYChart::new((600, 300), (..pi2, -1.1..1.1))
18        .x_labels(1.0, 10, None)
19        .y_labels(0.5, 10, None)
20        .x_axis_description("x", None)
21        .y_axis_description("sin(x)", None)
22        .plot_grid(0.2, 0.1, class("fine-grid"))
23        .plot_grid(1.0, 0.5, class("grid"))
24        .plot_poly_line(sin, class("curve"))
25        .plot_poly_line(vec![(0.0, 0.0), (pi2, 0.0)], class("base-line"));
26
27    SvgDocument::new()
28        .append_scss(STYLE)
29        .add_plot(Box::new(chart))
30        .save("docs/img/sine_curve.svg")
31}
Source

pub fn plot_image( self, image: impl Into<Image>, style_attr: Option<StyleAttr>, ) -> Self

Plot an image onto the plot layer. The image will be scaled to fit the plot area.

The image is clipped to the plot area, so it will not extend beyond the plot boundaries.

§Arguments
  • image - The image to plot, which can be any type that implements Into<Image>.
  • style_attr - Style attributes to apply to the image, such as width, height, and class.
Source

pub fn label_pin( self, cxy: (f64, f64), r: f64, angle_and_length: (i32, i32), text: impl AsRef<str>, style_attr: Option<StyleAttr>, ) -> Self

Annotate a point on the chart with a circle, a line, and text, using the annotations layer, which is on top of all the other layers, and is unconstrained by and clip paths.

Source

pub fn draw_path( self, layer: &str, path: Path, style_attr: Option<StyleAttr>, ) -> Self

Draw a Path onto the selected layer, without using any scaling.

Source

pub fn draw_data( self, layer: &str, data: Data, style_attr: Option<StyleAttr>, ) -> Self

Add Data, composed of e.g. move_to and line_to operations, to a specified layer. This is low level convenience method for drawing paths with data.

Source

pub fn plot_poly_line( self, data: impl IntoIterator<Item = (f64, f64)>, style_attr: Option<StyleAttr>, ) -> Self

Add a line to the chart, for a set of world coordinates, as specified by x and y ranges, from an iterator, onto the plot layer.

Examples found in repository?
examples/sine_curve.rs (line 24)
5pub fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let pi = std::f64::consts::PI;
7    let pi2 = 2.0 * pi;
8
9    const N: i32 = 100;
10    let sin: Vec<(f64, f64)> = (0..N)
11        .map(|i| {
12            let x = i as f64 * pi2 / (N as f64 - 1.0);
13            (x, x.sin())
14        })
15        .collect();
16
17    let chart = XYChart::new((600, 300), (..pi2, -1.1..1.1))
18        .x_labels(1.0, 10, None)
19        .y_labels(0.5, 10, None)
20        .x_axis_description("x", None)
21        .y_axis_description("sin(x)", None)
22        .plot_grid(0.2, 0.1, class("fine-grid"))
23        .plot_grid(1.0, 0.5, class("grid"))
24        .plot_poly_line(sin, class("curve"))
25        .plot_poly_line(vec![(0.0, 0.0), (pi2, 0.0)], class("base-line"));
26
27    SvgDocument::new()
28        .append_scss(STYLE)
29        .add_plot(Box::new(chart))
30        .save("docs/img/sine_curve.svg")
31}
Source

pub fn plot_shape( self, data: impl IntoIterator<Item = (f64, f64)>, style_attr: Option<StyleAttr>, ) -> Self

Plot a shape from a set of coordinates from an iterator. It will close the path.

Source

pub fn update_view(&mut self)

Updates the chart’s view parameters and recalculates the view box.

Trait Implementations§

Source§

impl Clone for XYChart

Source§

fn clone(&self) -> XYChart

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl From<XYChart> for SVG

Source§

fn from(chart: XYChart) -> Self

Converts to this type from the input type.
Source§

impl Rendable for XYChart

Source§

fn view_parameters(&self) -> ViewParameters

Returns the current view parameters for the object.
Source§

fn set_view_parameters(&mut self, view_box: ViewParameters)

Sets the view parameters for the object.
Source§

fn render(&self) -> SVG

Renders the object as an SVG element.
Source§

fn set_x(&mut self, x: i32)

Sets the x position of the view box (optional).
Source§

fn set_y(&mut self, y: i32)

Sets the y position of the view box (optional).
Source§

fn width(&self) -> u32

Returns the width of the view box (optional).
Source§

fn set_width(&mut self, width: u32)

Sets the width of the view box (optional).
Source§

fn height(&self) -> u32

Returns the height of the view box (optional).
Source§

fn set_height(&mut self, height: u32)

Sets the height of the view box (optional).
Source§

fn view_box(&mut self) -> (i32, i32, u32, u32)

Source§

fn set_view_box(&mut self, vx: i32, vy: i32, vw: u32, vh: u32)

Auto Trait Implementations§

§

impl Freeze for XYChart

§

impl !RefUnwindSafe for XYChart

§

impl !Send for XYChart

§

impl !Sync for XYChart

§

impl Unpin for XYChart

§

impl !UnwindSafe for XYChart

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

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

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

Source§

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

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

fn is_in_subset(&self) -> bool

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

fn to_subset_unchecked(&self) -> SS

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

fn from_subset(element: &SS) -> SP

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

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

Source§

fn is_within(&self, b: &G2) -> bool