summaryrefslogtreecommitdiffstats
path: root/vendor/plotters/examples/customized_coord.rs
blob: cb3a18f0384fa7ee41ece4ee2e90c3e53eda04d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use plotters::{
    coord::ranged1d::{KeyPointHint, NoDefaultFormatting, ValueFormatter},
    prelude::*,
};
const OUT_FILE_NAME: &'static str = "plotters-doc-data/customized_coord.svg";

struct CustomizedX(u32);

impl Ranged for CustomizedX {
    type ValueType = u32;
    type FormatOption = NoDefaultFormatting;
    fn map(&self, value: &Self::ValueType, limit: (i32, i32)) -> i32 {
        let size = limit.1 - limit.0;
        ((*value as f64 / self.0 as f64) * size as f64) as i32 + limit.0
    }

    fn range(&self) -> std::ops::Range<Self::ValueType> {
        0..self.0
    }

    fn key_points<Hint: KeyPointHint>(&self, hint: Hint) -> Vec<Self::ValueType> {
        if hint.max_num_points() < (self.0 as usize) {
            return vec![];
        }

        (0..self.0).collect()
    }
}

impl ValueFormatter<u32> for CustomizedX {
    fn format_ext(&self, value: &u32) -> String {
        format!("{} of {}", value, self.0)
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let area = SVGBackend::new(OUT_FILE_NAME, (1024, 760)).into_drawing_area();
    area.fill(&WHITE)?;

    let mut chart = ChartBuilder::on(&area)
        .set_all_label_area_size(50)
        .build_cartesian_2d(CustomizedX(7), 0.0..10.0)?;

    chart.configure_mesh().draw()?;

    area.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir");
    println!("Result has been saved to {}", OUT_FILE_NAME);
    Ok(())
}

#[test]
fn entry_point() {
    main().unwrap()
}