pub struct ScaleLinear { /* private fields */ }
Expand description
The scale to represent categorical data.
Implementations§
Source§impl ScaleLinear
impl ScaleLinear
Sourcepub fn new() -> Self
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
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}
Sourcepub fn set_domain(self, range: Vec<f32>) -> Self
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
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}
Sourcepub fn set_range(self, range: Vec<isize>) -> Self
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
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}
Trait Implementations§
Source§impl Debug for ScaleLinear
impl Debug for ScaleLinear
Source§impl Scale<f32> for ScaleLinear
impl Scale<f32> for ScaleLinear
Auto Trait Implementations§
impl Freeze for ScaleLinear
impl RefUnwindSafe for ScaleLinear
impl Send for ScaleLinear
impl Sync for ScaleLinear
impl Unpin for ScaleLinear
impl UnwindSafe for ScaleLinear
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
Mutably borrows from an owned value. Read more