()
| 1 | use charts::{Chart, HorizontalBarView, ScaleBand, ScaleLinear}; |
| 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 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, 100]) |
| 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 | } |
nothing calls this directly
no test coverage detected