MCPcopy Create free account
hub / github.com/bedroombuilds/python2rust / main

Function main

26_plotting/rust/plotters_ex/src/main.rs:20–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

18
19const OUT_FILE_NAME: &'static str = "plot.svg";
20fn main() -> Result<(), Box<dyn std::error::Error>> {
21 let root = SVGBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area();
22 root.fill(&WHITE)?;
23
24 let (upper, lower) = root.split_vertically(750);
25
26 lower.titled(
27 "Data Source: https://covid.ourworldindata.org/data/owid-covid-data.json",
28 ("sans-serif", 10).into_font().color(&BLACK.mix(0.5)),
29 )?;
30
31 let mut chart = ChartBuilder::on(&upper)
32 .caption("World COVID-19 Cases", ("sans-serif", (5).percent_height()))
33 .set_label_area_size(LabelAreaPosition::Left, (8).percent())
34 .set_label_area_size(LabelAreaPosition::Bottom, (4).percent())
35 .margin((1).percent())
36 .build_cartesian_2d(
37 (20u32..5000_0000u32)
38 .log_scale()
39 .with_key_points(vec![50, 100, 1000, 10000, 100000, 1000000, 10000000]),
40 (0u32..50_0000u32)
41 .log_scale()
42 .with_key_points(vec![10, 50, 100, 1000, 10000, 100000, 200000]),
43 )?;
44
45 chart
46 .configure_mesh()
47 .x_desc("Total Cases")
48 .y_desc("New Cases")
49 .draw()?;
50
51 let data: std::collections::HashMap<String, CountryData> =
52 serde_json::from_reader(BufReader::new(File::open("owid-covid-data.json")?))?;
53
54 for (idx, &series) in ["CHN", "USA", "RUS", "JPN", "DEU", "IND", "OWID_WRL"]
55 .iter()
56 .enumerate()
57 {
58 let color = Palette99::pick(idx).mix(0.9);
59 chart
60 .draw_series(LineSeries::new(
61 data[series].data.iter().map(
62 |&DailyData {
63 new_cases,
64 total_cases,
65 ..
66 }| (total_cases as u32, new_cases as u32),
67 ),
68 color.stroke_width(3),
69 ))?
70 .label(series)
71 .legend(move |(x, y)| Rectangle::new([(x, y - 5), (x + 10, y + 5)], color.filled()));
72 }
73
74 chart
75 .configure_series_labels()
76 .border_style(&BLACK)
77 .draw()?;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected