MCPcopy Index your code
hub / github.com/askanium/rustplotlib / main

Function main

examples/scatter_chart.rs:3–47  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1use charts::{Chart, ScaleLinear, ScatterView, MarkerType, PointLabelPosition};
2
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, 200])
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, 100])
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}

Callers

nothing calls this directly

Calls 15

set_marker_typeMethod · 0.80
saveMethod · 0.80
add_bottom_axis_labelMethod · 0.80
add_left_axis_labelMethod · 0.80
add_axis_leftMethod · 0.80
add_axis_bottomMethod · 0.80
add_viewMethod · 0.80
add_titleMethod · 0.80
set_marginsMethod · 0.80
set_heightMethod · 0.80
set_widthMethod · 0.80
set_rangeMethod · 0.45

Tested by

no test coverage detected