()
| 1 | use charts::{Chart, ScaleLinear, ScatterView, MarkerType, PointLabelPosition}; |
| 2 | |
| 3 | fn 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 | } |
nothing calls this directly
no test coverage detected