| 1 | use plotters::prelude::*; |
| 2 | |
| 3 | const OUT_FILE_NAME: &'static str = "plotters-doc-data/twoscale.png" ; |
| 4 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 5 | let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area(); |
| 6 | root.fill(&WHITE)?; |
| 7 | |
| 8 | let mut chart = ChartBuilder::on(&root) |
| 9 | .x_label_area_size(35) |
| 10 | .y_label_area_size(40) |
| 11 | .right_y_label_area_size(40) |
| 12 | .margin(5) |
| 13 | .caption("Dual Y-Axis Example" , ("sans-serif" , 50.0).into_font()) |
| 14 | .build_cartesian_2d(0f32..10f32, (0.1f32..1e10f32).log_scale())? |
| 15 | .set_secondary_coord(0f32..10f32, -1.0f32..1.0f32); |
| 16 | |
| 17 | chart |
| 18 | .configure_mesh() |
| 19 | .disable_x_mesh() |
| 20 | .disable_y_mesh() |
| 21 | .y_desc("Log Scale" ) |
| 22 | .y_label_formatter(&|x| format!("{:e}" , x)) |
| 23 | .draw()?; |
| 24 | |
| 25 | chart |
| 26 | .configure_secondary_axes() |
| 27 | .y_desc("Linear Scale" ) |
| 28 | .draw()?; |
| 29 | |
| 30 | chart |
| 31 | .draw_series(LineSeries::new( |
| 32 | (0..=100).map(|x| (x as f32 / 10.0, (1.02f32).powf(x as f32 * x as f32 / 10.0))), |
| 33 | &BLUE, |
| 34 | ))? |
| 35 | .label("y = 1.02^x^2" ) |
| 36 | .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &BLUE)); |
| 37 | |
| 38 | chart |
| 39 | .draw_secondary_series(LineSeries::new( |
| 40 | (0..=100).map(|x| (x as f32 / 10.0, (x as f32 / 5.0).sin())), |
| 41 | &RED, |
| 42 | ))? |
| 43 | .label("y = sin(2x)" ) |
| 44 | .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED)); |
| 45 | |
| 46 | chart |
| 47 | .configure_series_labels() |
| 48 | .background_style(&RGBColor(128, 128, 128)) |
| 49 | .draw()?; |
| 50 | |
| 51 | // To avoid the IO failure being ignored silently, we manually call the present function |
| 52 | root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir" ); |
| 53 | println!("Result has been saved to {}" , OUT_FILE_NAME); |
| 54 | |
| 55 | Ok(()) |
| 56 | } |
| 57 | #[test] |
| 58 | fn entry_point() { |
| 59 | main().unwrap() |
| 60 | } |
| 61 | |