Struct ScaleLinear

Source
pub struct ScaleLinear { /* private fields */ }
Expand description

The scale to represent categorical data.

Implementations§

Source§

impl ScaleLinear

Source

pub fn new() -> Self

Create a new linear scale with default values.

Examples found in repository?
examples/horizontal_bar_chart.rs (line 11)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
10    // values in [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 100_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a band scale that maps ["A", "B", "C"] categories to values in the [0, availableHeight]
16    // range (the height of the chart without the margins).
17    let y = ScaleBand::new()
18        .set_domain(vec![String::from("A"), String::from("B"), String::from("C")])
19        .set_range(vec![0, height - top - bottom]);
20
21    // You can use your own iterable as data as long as its items implement the `BarDatum` trait.
22    let data = vec![("A", 90), ("B", 10), ("C", 30)];
23
24    // Create HorizontalBar view that is going to represent the data as vertical bars.
25    let view = HorizontalBarView::new()
26        .set_x_scale(&x)
27        .set_y_scale(&y)
28        .load_data(&data).unwrap();
29
30    // Generate and save the chart.
31    Chart::new()
32        .set_width(width)
33        .set_height(height)
34        .set_margins(top, right, bottom, left)
35        .add_title(String::from("Horizontal Bar Chart"))
36        .add_view(&view)
37        .add_axis_bottom(&x)
38        .add_axis_top(&x)
39        .add_axis_left(&y)
40        .add_left_axis_label("Y Axis Custom Label")
41        .add_bottom_axis_label("X Axis Custom Label")
42        .save("horizontal-bar-chart.svg").unwrap();
43}
More examples
Hide additional examples
examples/stacked_horizontal_bar_chart.rs (line 11)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
10    // values in [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 100_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a band scale that maps ["A", "B", "C"] categories to values in the [0, availableHeight]
16    // range (the height of the chart without the margins).
17    let y = ScaleBand::new()
18        .set_domain(vec![String::from("A"), String::from("B"), String::from("C")])
19        .set_range(vec![0, height - top - bottom]);
20
21    // You can use your own iterable as data as long as its items implement the `BarDatum` trait.
22    let data = vec![("A", 70, "foo"), ("B", 10, "foo"), ("C", 30, "foo"), ("A", 20, "bar"), ("A", 5, "baz")];
23
24    // Create VerticalBar view that is going to represent the data as vertical bars.
25    let view = HorizontalBarView::new()
26        .set_x_scale(&x)
27        .set_y_scale(&y)
28        .set_label_position(BarLabelPosition::Center)
29        .load_data(&data).unwrap();
30
31    // Generate and save the chart.
32    Chart::new()
33        .set_width(width)
34        .set_height(height)
35        .set_margins(top, right, bottom, left)
36        .add_title(String::from("Horizontal Stacked Bar Chart"))
37        .add_view(&view)
38        .add_axis_bottom(&x)
39        .add_axis_left(&y)
40        .add_left_axis_label("Y Axis Custom Label")
41        .add_bottom_axis_label("X Axis Custom Label")
42        .save("stacked-horizontal-bar-chart.svg").unwrap();
43}
examples/vertical_bar_chart.rs (line 22)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that maps ["A", "B", "C"] categories to values in the [0, availableWidth]
10    // range (the width of the chart without the margins).
11    let x = ScaleBand::new()
12        .set_domain(vec![String::from("A"), String::from("B"), String::from("C")])
13        .set_range(vec![0, width - left - right])
14        .set_inner_padding(0.1)
15        .set_outer_padding(0.1);
16
17    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
18    // values in [availableHeight, 0] range (the height of the chart without the margins).
19    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
20    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
21    // the range on Y axis for the chart to display as though its origin is at bottom left.
22    let y = ScaleLinear::new()
23        .set_domain(vec![0_f32, 100_f32])
24        .set_range(vec![height - top - bottom, 0]);
25
26    // You can use your own iterable as data as long as its items implement the `BarDatum` trait.
27    let data = vec![("A", 90), ("B", 10), ("C", 30)];
28
29    // Create VerticalBar view that is going to represent the data as vertical bars.
30    let view = VerticalBarView::new()
31        .set_x_scale(&x)
32        .set_y_scale(&y)
33        .load_data(&data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Bar Chart"))
41        .add_view(&view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Units of Measurement")
45        .add_bottom_axis_label("Categories")
46        .save("vertical-bar-chart.svg").unwrap();
47}
examples/line_series_chart.rs (line 11)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that will interpolate values in [0, 200] to values in the
10    // [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 200_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
16    // values in [availableHeight, 0] range (the height of the chart without the margins).
17    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
18    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
19    // the range on Y axis for the chart to display as though its origin is at bottom left.
20    let y = ScaleLinear::new()
21        .set_domain(vec![0_f32, 100_f32])
22        .set_range(vec![height - top - bottom, 0]);
23
24    // You can use your own iterable as data as long as its items implement the `PointDatum` trait.
25    let line_data = vec![(12, 54), (100, 40), (120, 50), (180, 70)];
26
27    // Create Line series view that is going to represent the data.
28    let line_view = LineSeriesView::new()
29        .set_x_scale(&x)
30        .set_y_scale(&y)
31        .set_marker_type(MarkerType::Circle)
32        .set_label_position(PointLabelPosition::N)
33        .load_data(&line_data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Line Chart"))
41        .add_view(&line_view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Custom Y Axis Label")
45        .add_bottom_axis_label("Custom X Axis Label")
46        .save("line-chart.svg").unwrap();
47}
examples/area_series_chart.rs (line 11)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that will interpolate values in [0, 200] to values in the
10    // [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![12_f32, 180_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
16    // values in [availableHeight, 0] range (the height of the chart without the margins).
17    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
18    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
19    // the range on Y axis for the chart to display as though its origin is at bottom left.
20    let y = ScaleLinear::new()
21        .set_domain(vec![0_f32, 100_f32])
22        .set_range(vec![height - top - bottom, 0]);
23
24    // You can use your own iterable as data as long as its items implement the `PointDatum` trait.
25    let area_data = vec![(12, 54), (100, 40), (120, 50), (180, 70)];
26
27    // Create Area series view that is going to represent the data.
28    let area_view = AreaSeriesView::new()
29        .set_x_scale(&x)
30        .set_y_scale(&y)
31        .set_marker_type(MarkerType::Circle)
32        .set_label_position(PointLabelPosition::N)
33        .load_data(&area_data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Area Chart"))
41        .add_view(&area_view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Custom Y Axis Label")
45        .add_bottom_axis_label("Custom X Axis Label")
46        .save("area-chart.svg").unwrap();
47}
examples/scatter_chart.rs (line 11)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that will interpolate values in [0, 200] to values in the
10    // [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 200_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
16    // values in [availableHeight, 0] range (the height of the chart without the margins).
17    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
18    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
19    // the range on Y axis for the chart to display as though its origin is at bottom left.
20    let y = ScaleLinear::new()
21        .set_domain(vec![0_f32, 100_f32])
22        .set_range(vec![height - top - bottom, 0]);
23
24    // You can use your own iterable as data as long as its items implement the `PointDatum` trait.
25    let scatter_data = vec![(120, 90), (12, 54), (100, 40), (180, 10)];
26
27    // Create Scatter view that is going to represent the data as points.
28    let scatter_view = ScatterView::new()
29        .set_x_scale(&x)
30        .set_y_scale(&y)
31        .set_label_position(PointLabelPosition::E)
32        .set_marker_type(MarkerType::Square)
33        .load_data(&scatter_data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Scatter Chart"))
41        .add_view(&scatter_view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Custom X Axis Label")
45        .add_bottom_axis_label("Custom Y Axis Label")
46        .save("scatter-chart.svg").unwrap();
47}
Source

pub fn set_domain(self, range: Vec<f32>) -> Self

Set the domain limits for the scale band.

Examples found in repository?
examples/horizontal_bar_chart.rs (line 12)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
10    // values in [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 100_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a band scale that maps ["A", "B", "C"] categories to values in the [0, availableHeight]
16    // range (the height of the chart without the margins).
17    let y = ScaleBand::new()
18        .set_domain(vec![String::from("A"), String::from("B"), String::from("C")])
19        .set_range(vec![0, height - top - bottom]);
20
21    // You can use your own iterable as data as long as its items implement the `BarDatum` trait.
22    let data = vec![("A", 90), ("B", 10), ("C", 30)];
23
24    // Create HorizontalBar view that is going to represent the data as vertical bars.
25    let view = HorizontalBarView::new()
26        .set_x_scale(&x)
27        .set_y_scale(&y)
28        .load_data(&data).unwrap();
29
30    // Generate and save the chart.
31    Chart::new()
32        .set_width(width)
33        .set_height(height)
34        .set_margins(top, right, bottom, left)
35        .add_title(String::from("Horizontal Bar Chart"))
36        .add_view(&view)
37        .add_axis_bottom(&x)
38        .add_axis_top(&x)
39        .add_axis_left(&y)
40        .add_left_axis_label("Y Axis Custom Label")
41        .add_bottom_axis_label("X Axis Custom Label")
42        .save("horizontal-bar-chart.svg").unwrap();
43}
More examples
Hide additional examples
examples/stacked_horizontal_bar_chart.rs (line 12)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
10    // values in [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 100_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a band scale that maps ["A", "B", "C"] categories to values in the [0, availableHeight]
16    // range (the height of the chart without the margins).
17    let y = ScaleBand::new()
18        .set_domain(vec![String::from("A"), String::from("B"), String::from("C")])
19        .set_range(vec![0, height - top - bottom]);
20
21    // You can use your own iterable as data as long as its items implement the `BarDatum` trait.
22    let data = vec![("A", 70, "foo"), ("B", 10, "foo"), ("C", 30, "foo"), ("A", 20, "bar"), ("A", 5, "baz")];
23
24    // Create VerticalBar view that is going to represent the data as vertical bars.
25    let view = HorizontalBarView::new()
26        .set_x_scale(&x)
27        .set_y_scale(&y)
28        .set_label_position(BarLabelPosition::Center)
29        .load_data(&data).unwrap();
30
31    // Generate and save the chart.
32    Chart::new()
33        .set_width(width)
34        .set_height(height)
35        .set_margins(top, right, bottom, left)
36        .add_title(String::from("Horizontal Stacked Bar Chart"))
37        .add_view(&view)
38        .add_axis_bottom(&x)
39        .add_axis_left(&y)
40        .add_left_axis_label("Y Axis Custom Label")
41        .add_bottom_axis_label("X Axis Custom Label")
42        .save("stacked-horizontal-bar-chart.svg").unwrap();
43}
examples/vertical_bar_chart.rs (line 23)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that maps ["A", "B", "C"] categories to values in the [0, availableWidth]
10    // range (the width of the chart without the margins).
11    let x = ScaleBand::new()
12        .set_domain(vec![String::from("A"), String::from("B"), String::from("C")])
13        .set_range(vec![0, width - left - right])
14        .set_inner_padding(0.1)
15        .set_outer_padding(0.1);
16
17    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
18    // values in [availableHeight, 0] range (the height of the chart without the margins).
19    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
20    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
21    // the range on Y axis for the chart to display as though its origin is at bottom left.
22    let y = ScaleLinear::new()
23        .set_domain(vec![0_f32, 100_f32])
24        .set_range(vec![height - top - bottom, 0]);
25
26    // You can use your own iterable as data as long as its items implement the `BarDatum` trait.
27    let data = vec![("A", 90), ("B", 10), ("C", 30)];
28
29    // Create VerticalBar view that is going to represent the data as vertical bars.
30    let view = VerticalBarView::new()
31        .set_x_scale(&x)
32        .set_y_scale(&y)
33        .load_data(&data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Bar Chart"))
41        .add_view(&view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Units of Measurement")
45        .add_bottom_axis_label("Categories")
46        .save("vertical-bar-chart.svg").unwrap();
47}
examples/line_series_chart.rs (line 12)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that will interpolate values in [0, 200] to values in the
10    // [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 200_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
16    // values in [availableHeight, 0] range (the height of the chart without the margins).
17    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
18    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
19    // the range on Y axis for the chart to display as though its origin is at bottom left.
20    let y = ScaleLinear::new()
21        .set_domain(vec![0_f32, 100_f32])
22        .set_range(vec![height - top - bottom, 0]);
23
24    // You can use your own iterable as data as long as its items implement the `PointDatum` trait.
25    let line_data = vec![(12, 54), (100, 40), (120, 50), (180, 70)];
26
27    // Create Line series view that is going to represent the data.
28    let line_view = LineSeriesView::new()
29        .set_x_scale(&x)
30        .set_y_scale(&y)
31        .set_marker_type(MarkerType::Circle)
32        .set_label_position(PointLabelPosition::N)
33        .load_data(&line_data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Line Chart"))
41        .add_view(&line_view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Custom Y Axis Label")
45        .add_bottom_axis_label("Custom X Axis Label")
46        .save("line-chart.svg").unwrap();
47}
examples/area_series_chart.rs (line 12)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that will interpolate values in [0, 200] to values in the
10    // [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![12_f32, 180_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
16    // values in [availableHeight, 0] range (the height of the chart without the margins).
17    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
18    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
19    // the range on Y axis for the chart to display as though its origin is at bottom left.
20    let y = ScaleLinear::new()
21        .set_domain(vec![0_f32, 100_f32])
22        .set_range(vec![height - top - bottom, 0]);
23
24    // You can use your own iterable as data as long as its items implement the `PointDatum` trait.
25    let area_data = vec![(12, 54), (100, 40), (120, 50), (180, 70)];
26
27    // Create Area series view that is going to represent the data.
28    let area_view = AreaSeriesView::new()
29        .set_x_scale(&x)
30        .set_y_scale(&y)
31        .set_marker_type(MarkerType::Circle)
32        .set_label_position(PointLabelPosition::N)
33        .load_data(&area_data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Area Chart"))
41        .add_view(&area_view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Custom Y Axis Label")
45        .add_bottom_axis_label("Custom X Axis Label")
46        .save("area-chart.svg").unwrap();
47}
examples/scatter_chart.rs (line 12)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that will interpolate values in [0, 200] to values in the
10    // [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 200_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
16    // values in [availableHeight, 0] range (the height of the chart without the margins).
17    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
18    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
19    // the range on Y axis for the chart to display as though its origin is at bottom left.
20    let y = ScaleLinear::new()
21        .set_domain(vec![0_f32, 100_f32])
22        .set_range(vec![height - top - bottom, 0]);
23
24    // You can use your own iterable as data as long as its items implement the `PointDatum` trait.
25    let scatter_data = vec![(120, 90), (12, 54), (100, 40), (180, 10)];
26
27    // Create Scatter view that is going to represent the data as points.
28    let scatter_view = ScatterView::new()
29        .set_x_scale(&x)
30        .set_y_scale(&y)
31        .set_label_position(PointLabelPosition::E)
32        .set_marker_type(MarkerType::Square)
33        .load_data(&scatter_data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Scatter Chart"))
41        .add_view(&scatter_view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Custom X Axis Label")
45        .add_bottom_axis_label("Custom Y Axis Label")
46        .save("scatter-chart.svg").unwrap();
47}
Source

pub fn domain(&self) -> &Vec<f32>

Get the domain limits of the scale.

Source

pub fn set_range(self, range: Vec<isize>) -> Self

Set the range limits for the scale band.

Examples found in repository?
examples/horizontal_bar_chart.rs (line 13)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
10    // values in [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 100_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a band scale that maps ["A", "B", "C"] categories to values in the [0, availableHeight]
16    // range (the height of the chart without the margins).
17    let y = ScaleBand::new()
18        .set_domain(vec![String::from("A"), String::from("B"), String::from("C")])
19        .set_range(vec![0, height - top - bottom]);
20
21    // You can use your own iterable as data as long as its items implement the `BarDatum` trait.
22    let data = vec![("A", 90), ("B", 10), ("C", 30)];
23
24    // Create HorizontalBar view that is going to represent the data as vertical bars.
25    let view = HorizontalBarView::new()
26        .set_x_scale(&x)
27        .set_y_scale(&y)
28        .load_data(&data).unwrap();
29
30    // Generate and save the chart.
31    Chart::new()
32        .set_width(width)
33        .set_height(height)
34        .set_margins(top, right, bottom, left)
35        .add_title(String::from("Horizontal Bar Chart"))
36        .add_view(&view)
37        .add_axis_bottom(&x)
38        .add_axis_top(&x)
39        .add_axis_left(&y)
40        .add_left_axis_label("Y Axis Custom Label")
41        .add_bottom_axis_label("X Axis Custom Label")
42        .save("horizontal-bar-chart.svg").unwrap();
43}
More examples
Hide additional examples
examples/stacked_horizontal_bar_chart.rs (line 13)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
10    // values in [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 100_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a band scale that maps ["A", "B", "C"] categories to values in the [0, availableHeight]
16    // range (the height of the chart without the margins).
17    let y = ScaleBand::new()
18        .set_domain(vec![String::from("A"), String::from("B"), String::from("C")])
19        .set_range(vec![0, height - top - bottom]);
20
21    // You can use your own iterable as data as long as its items implement the `BarDatum` trait.
22    let data = vec![("A", 70, "foo"), ("B", 10, "foo"), ("C", 30, "foo"), ("A", 20, "bar"), ("A", 5, "baz")];
23
24    // Create VerticalBar view that is going to represent the data as vertical bars.
25    let view = HorizontalBarView::new()
26        .set_x_scale(&x)
27        .set_y_scale(&y)
28        .set_label_position(BarLabelPosition::Center)
29        .load_data(&data).unwrap();
30
31    // Generate and save the chart.
32    Chart::new()
33        .set_width(width)
34        .set_height(height)
35        .set_margins(top, right, bottom, left)
36        .add_title(String::from("Horizontal Stacked Bar Chart"))
37        .add_view(&view)
38        .add_axis_bottom(&x)
39        .add_axis_left(&y)
40        .add_left_axis_label("Y Axis Custom Label")
41        .add_bottom_axis_label("X Axis Custom Label")
42        .save("stacked-horizontal-bar-chart.svg").unwrap();
43}
examples/vertical_bar_chart.rs (line 24)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that maps ["A", "B", "C"] categories to values in the [0, availableWidth]
10    // range (the width of the chart without the margins).
11    let x = ScaleBand::new()
12        .set_domain(vec![String::from("A"), String::from("B"), String::from("C")])
13        .set_range(vec![0, width - left - right])
14        .set_inner_padding(0.1)
15        .set_outer_padding(0.1);
16
17    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
18    // values in [availableHeight, 0] range (the height of the chart without the margins).
19    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
20    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
21    // the range on Y axis for the chart to display as though its origin is at bottom left.
22    let y = ScaleLinear::new()
23        .set_domain(vec![0_f32, 100_f32])
24        .set_range(vec![height - top - bottom, 0]);
25
26    // You can use your own iterable as data as long as its items implement the `BarDatum` trait.
27    let data = vec![("A", 90), ("B", 10), ("C", 30)];
28
29    // Create VerticalBar view that is going to represent the data as vertical bars.
30    let view = VerticalBarView::new()
31        .set_x_scale(&x)
32        .set_y_scale(&y)
33        .load_data(&data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Bar Chart"))
41        .add_view(&view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Units of Measurement")
45        .add_bottom_axis_label("Categories")
46        .save("vertical-bar-chart.svg").unwrap();
47}
examples/line_series_chart.rs (line 13)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that will interpolate values in [0, 200] to values in the
10    // [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 200_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
16    // values in [availableHeight, 0] range (the height of the chart without the margins).
17    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
18    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
19    // the range on Y axis for the chart to display as though its origin is at bottom left.
20    let y = ScaleLinear::new()
21        .set_domain(vec![0_f32, 100_f32])
22        .set_range(vec![height - top - bottom, 0]);
23
24    // You can use your own iterable as data as long as its items implement the `PointDatum` trait.
25    let line_data = vec![(12, 54), (100, 40), (120, 50), (180, 70)];
26
27    // Create Line series view that is going to represent the data.
28    let line_view = LineSeriesView::new()
29        .set_x_scale(&x)
30        .set_y_scale(&y)
31        .set_marker_type(MarkerType::Circle)
32        .set_label_position(PointLabelPosition::N)
33        .load_data(&line_data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Line Chart"))
41        .add_view(&line_view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Custom Y Axis Label")
45        .add_bottom_axis_label("Custom X Axis Label")
46        .save("line-chart.svg").unwrap();
47}
examples/area_series_chart.rs (line 13)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that will interpolate values in [0, 200] to values in the
10    // [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![12_f32, 180_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
16    // values in [availableHeight, 0] range (the height of the chart without the margins).
17    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
18    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
19    // the range on Y axis for the chart to display as though its origin is at bottom left.
20    let y = ScaleLinear::new()
21        .set_domain(vec![0_f32, 100_f32])
22        .set_range(vec![height - top - bottom, 0]);
23
24    // You can use your own iterable as data as long as its items implement the `PointDatum` trait.
25    let area_data = vec![(12, 54), (100, 40), (120, 50), (180, 70)];
26
27    // Create Area series view that is going to represent the data.
28    let area_view = AreaSeriesView::new()
29        .set_x_scale(&x)
30        .set_y_scale(&y)
31        .set_marker_type(MarkerType::Circle)
32        .set_label_position(PointLabelPosition::N)
33        .load_data(&area_data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Area Chart"))
41        .add_view(&area_view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Custom Y Axis Label")
45        .add_bottom_axis_label("Custom X Axis Label")
46        .save("area-chart.svg").unwrap();
47}
examples/scatter_chart.rs (line 13)
3fn main() {
4    // Define chart related sizes.
5    let width = 800;
6    let height = 600;
7    let (top, right, bottom, left) = (90, 40, 50, 60);
8
9    // Create a band scale that will interpolate values in [0, 200] to values in the
10    // [0, availableWidth] range (the width of the chart without the margins).
11    let x = ScaleLinear::new()
12        .set_domain(vec![0_f32, 200_f32])
13        .set_range(vec![0, width - left - right]);
14
15    // Create a linear scale that will interpolate values in [0, 100] range to corresponding
16    // values in [availableHeight, 0] range (the height of the chart without the margins).
17    // The [availableHeight, 0] range is inverted because SVGs coordinate system's origin is
18    // in top left corner, while chart's origin is in bottom left corner, hence we need to invert
19    // the range on Y axis for the chart to display as though its origin is at bottom left.
20    let y = ScaleLinear::new()
21        .set_domain(vec![0_f32, 100_f32])
22        .set_range(vec![height - top - bottom, 0]);
23
24    // You can use your own iterable as data as long as its items implement the `PointDatum` trait.
25    let scatter_data = vec![(120, 90), (12, 54), (100, 40), (180, 10)];
26
27    // Create Scatter view that is going to represent the data as points.
28    let scatter_view = ScatterView::new()
29        .set_x_scale(&x)
30        .set_y_scale(&y)
31        .set_label_position(PointLabelPosition::E)
32        .set_marker_type(MarkerType::Square)
33        .load_data(&scatter_data).unwrap();
34
35    // Generate and save the chart.
36    Chart::new()
37        .set_width(width)
38        .set_height(height)
39        .set_margins(top, right, bottom, left)
40        .add_title(String::from("Scatter Chart"))
41        .add_view(&scatter_view)
42        .add_axis_bottom(&x)
43        .add_axis_left(&y)
44        .add_left_axis_label("Custom X Axis Label")
45        .add_bottom_axis_label("Custom Y Axis Label")
46        .save("scatter-chart.svg").unwrap();
47}
Source

pub fn range(&self) -> &Vec<isize>

Get the range limits of the scale.

Trait Implementations§

Source§

impl Debug for ScaleLinear

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Scale<f32> for ScaleLinear

Source§

fn get_type(&self) -> ScaleType

Get the type of the scale.

Source§

fn scale(&self, domain: &f32) -> f32

Get the range value for the given domain entry.

Source§

fn bandwidth(&self) -> Option<f32>

Get the bandwidth (if present).

Source§

fn range_start(&self) -> f32

Get the start range value.

Source§

fn range_end(&self) -> f32

Get the end range value.

Source§

fn get_ticks(&self) -> Vec<f32>

Get the list of ticks that represent the scale on a chart axis.

Source§

fn is_range_reversed(&self) -> bool

Check whether the range is in reversed order, meaning the start is greater than the end.

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, 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.