()
| 1 | use charts::{Chart, VerticalBarView, ScaleBand, ScaleLinear, ScatterView, MarkerType, Color, 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 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 | |
| 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 `BarDatum` trait. |
| 25 | let bar_data = vec![("A", 70), ("B", 10), ("C", 30)]; |
| 26 | |
| 27 | // You can use your own iterable as data as long as its items implement the `PointDatum` trait. |
| 28 | let scatter_data = vec![(String::from("A"), 90.3), (String::from("B"), 20.1), (String::from("C"), 10.8)]; |
| 29 | |
| 30 | // Create VerticalBar view that is going to represent the data as vertical bars. |
| 31 | let bar_view = VerticalBarView::new() |
| 32 | .set_x_scale(&x) |
| 33 | .set_y_scale(&y) |
| 34 | .load_data(&bar_data).unwrap(); |
| 35 | |
| 36 | // Create Scatter view that is going to represent the data as points. |
| 37 | let scatter_view = ScatterView::new() |
| 38 | .set_x_scale(&x) |
| 39 | .set_y_scale(&y) |
| 40 | .set_label_position(PointLabelPosition::NE) |
| 41 | .set_marker_type(MarkerType::Circle) |
| 42 | .set_colors(Color::from_vec_of_hex_strings(vec!["#FF4700"])) |
| 43 | .load_data(&scatter_data).unwrap(); |
| 44 | |
| 45 | // Generate and save the chart. |
| 46 | Chart::new() |
| 47 | .set_width(width) |
| 48 | .set_height(height) |
| 49 | .set_margins(top, right, bottom, left) |
| 50 | .add_title(String::from("Composite Bar + Scatter Chart")) |
| 51 | .add_view(&bar_view) // <-- add bar view |
| 52 | .add_view(&scatter_view) // <-- add scatter view |
| 53 | .add_axis_bottom(&x) |
| 54 | .add_axis_left(&y) |
| 55 | .add_left_axis_label("Units of Measurement") |
| 56 | .add_bottom_axis_label("Categories") |
| 57 | .save("composite-bar-and-scatter-chart.svg").unwrap(); |
| 58 | } |
nothing calls this directly
no test coverage detected