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: CoordinateTransformTransformation from world/data coordinates to plot (SVG) coordinates.
to_world: CoordinateTransformTransformation from plot (SVG) coordinates to world/data coordinates.
view_parameters: ViewParametersParameters 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
impl XYChart
Sourcepub const ANNOTATE_SEP: i32 = 2i32
pub const ANNOTATE_SEP: i32 = 2i32
Separation between annotation and the annotated element, in pixels.
Sourcepub const LABEL_HEIGHT: i32 = 16i32
pub const LABEL_HEIGHT: i32 = 16i32
Height of axis labels, in pixels.
Sourcepub const DESCRIPTION_HEIGHT: i32 = 20i32
pub const DESCRIPTION_HEIGHT: i32 = 20i32
Height of axis descriptions, in pixels.
Sourcepub const DESCRIPTION_SEP: i32 = 20i32
pub const DESCRIPTION_SEP: i32 = 20i32
Separation between axis description and axis, in pixels.
Sourcepub const DESCRIPTION_OFFSET: i32 = 36i32
pub const DESCRIPTION_OFFSET: i32 = 36i32
Offset for axis descriptions, in pixels.
Sourcepub fn new(
plot_width_and_height: (u32, u32),
ranges: (impl RangeBounds<f64>, impl RangeBounds<f64>),
) -> XYChart
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?
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}pub fn set_id(self, id: impl Into<String>) -> Self
Sourcepub fn ticks(
self,
x_step: f64,
y_step: f64,
length: i32,
style_attr: Option<StyleAttr>,
) -> Self
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.
Sourcepub fn x_labels(
self,
step: f64,
offset: usize,
style_attr: Option<StyleAttr>,
) -> Self
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?
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}Sourcepub fn y_labels(
self,
step: f64,
offset: usize,
style_attr: Option<StyleAttr>,
) -> Self
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?
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}Sourcepub fn x_axis_description(
self,
description: &str,
style_attr: Option<StyleAttr>,
) -> Self
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?
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}Sourcepub fn y_axis_description(
self,
description: &str,
style_attr: Option<StyleAttr>,
) -> Self
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?
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}Sourcepub fn plot_grid(
self,
x_step: f64,
y_step: f64,
style_attr: Option<StyleAttr>,
) -> Self
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?
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}Sourcepub fn plot_image(
self,
image: impl Into<Image>,
style_attr: Option<StyleAttr>,
) -> Self
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 implementsInto<Image>.style_attr- Style attributes to apply to the image, such as width, height, and class.
Sourcepub fn label_pin(
self,
cxy: (f64, f64),
r: f64,
angle_and_length: (i32, i32),
text: impl AsRef<str>,
style_attr: Option<StyleAttr>,
) -> Self
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.
Sourcepub fn draw_path(
self,
layer: &str,
path: Path,
style_attr: Option<StyleAttr>,
) -> Self
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.
Sourcepub fn draw_data(
self,
layer: &str,
data: Data,
style_attr: Option<StyleAttr>,
) -> Self
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.
Sourcepub fn plot_poly_line(
self,
data: impl IntoIterator<Item = (f64, f64)>,
style_attr: Option<StyleAttr>,
) -> Self
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?
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}Sourcepub fn plot_shape(
self,
data: impl IntoIterator<Item = (f64, f64)>,
style_attr: Option<StyleAttr>,
) -> Self
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.
Sourcepub fn update_view(&mut self)
pub fn update_view(&mut self)
Updates the chart’s view parameters and recalculates the view box.
Trait Implementations§
Source§impl Rendable for XYChart
impl Rendable for XYChart
Source§fn view_parameters(&self) -> ViewParameters
fn view_parameters(&self) -> ViewParameters
Source§fn set_view_parameters(&mut self, view_box: ViewParameters)
fn set_view_parameters(&mut self, view_box: ViewParameters)
Source§fn set_height(&mut self, height: u32)
fn set_height(&mut self, height: u32)
fn view_box(&mut self) -> (i32, i32, u32, u32)
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> 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.