Skip to content

Commit 7afd4d2

Browse files
committed
Add new example
1 parent 9f9ebb6 commit 7afd4d2

File tree

7 files changed

+114
-1
lines changed

7 files changed

+114
-1
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ criterion = "0.3.2"
9696
rayon = "1.3.0"
9797
rand_xorshift = "0.2.0"
9898
plotters-bitmap = "^0.3.*"
99+
serde_json = "1.0.57"
100+
serde = "1.0.115"
101+
serde_derive = "1.0.115"
99102

100103
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
101104
wasm-bindgen-test = "0.3.12"

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ To view the source code for each example, please click on the example image.
126126
<img src="https://plotters-rs.github.io/plotters-doc-data/3d-plot2.gif" class="galleryItem" width=200px></img>
127127
</a>
128128

129+
<a href="https://github.com/38/plotters/blob/master/examples/tick_control.rs">
130+
<img src="https://plotters-rs.github.io/plotters-doc-data/tick_control.gif" class="galleryItem" width=200px></img>
131+
</a>
132+
129133

130134
## Table of Contents
131135
* [Gallery](#gallery)

doc-template/readme/gallery

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ To view the source code for each example, please click on the example image.
9696
<a href="https://github.com/38/plotters/blob/master/examples/3d-plot2.rs">
9797
<img src="https://plotters-rs.github.io/plotters-doc-data/3d-plot2.gif" class="galleryItem" width=200px></img>
9898
</a>
99+
100+
<a href="https://github.com/38/plotters/blob/master/examples/tick_control.rs">
101+
<img src="https://plotters-rs.github.io/plotters-doc-data/tick_control.gif" class="galleryItem" width=200px></img>
102+
</a>

doc-template/rustdoc/gallery

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,13 @@
227227
<a href="https://github.com/38/plotters/blob/master/examples/3d-plot2.rs">[code]</a>
228228
</div>
229229
</div>
230+
231+
<div class="galleryItem">
232+
<a href="https://plotters-rs.github.io/plotters-doc-data/tick_control.gif">
233+
<img src="https://plotters-rs.github.io/plotters-doc-data/tick_control.gif" class="galleryItem"></img>
234+
</a>
235+
<div class="galleryText">
236+
COVID-19 Visualization
237+
<a href="https://github.com/38/plotters/blob/master/examples/tick_control.rs">[code]</a>
238+
</div>
239+
</div>

examples/tick_control.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Data is pulled from https://covid.ourworldindata.org/data/owid-covid-data.json
2+
use plotters::prelude::*;
3+
use std::fs::File;
4+
use std::io::BufReader;
5+
6+
#[derive(serde_derive::Deserialize)]
7+
struct DailyData {
8+
#[serde(default)]
9+
new_cases: f64,
10+
#[serde(default)]
11+
total_cases: f64,
12+
}
13+
14+
#[derive(serde_derive::Deserialize)]
15+
struct CountryData {
16+
data: Vec<DailyData>,
17+
}
18+
19+
fn main() -> Result<(), Box<dyn std::error::Error>> {
20+
let root =
21+
SVGBackend::new("plotters-doc-data/tick_control.svg", (1024, 768)).into_drawing_area();
22+
23+
root.fill(&WHITE)?;
24+
25+
let (upper, lower) = root.split_vertically(750);
26+
27+
lower.titled(
28+
"Data Source: https://covid.ourworldindata.org/data/owid-covid-data.json",
29+
("sans-serif", 10).into_font().color(&BLACK.mix(0.5)),
30+
)?;
31+
32+
let mut chart = ChartBuilder::on(&upper)
33+
.caption("World COVID-19 Cases", ("sans-serif", (5).percent_height()))
34+
.set_label_area_size(LabelAreaPosition::Left, (8).percent())
35+
.set_label_area_size(LabelAreaPosition::Bottom, (4).percent())
36+
.margin((1).percent())
37+
.build_cartesian_2d(
38+
(20u32..5000_0000u32)
39+
.log_scale()
40+
.with_key_points(vec![50, 100, 1000, 10000, 100000, 1000000, 10000000]),
41+
(0u32..50_0000u32)
42+
.log_scale()
43+
.with_key_points(vec![10, 50, 100, 1000, 10000, 100000, 200000]),
44+
)?;
45+
46+
chart
47+
.configure_mesh()
48+
.x_desc("Total Cases")
49+
.y_desc("New Cases")
50+
.draw()?;
51+
52+
let data: std::collections::HashMap<String, CountryData> = serde_json::from_reader(
53+
BufReader::new(File::open("plotters-doc-data/covid-data.json")?),
54+
)?;
55+
56+
for (idx, &series) in ["CHN", "USA", "RUS", "JPN", "DEU", "IND", "OWID_WRL"]
57+
.iter()
58+
.enumerate()
59+
{
60+
let color = Palette99::pick(idx).mix(0.9);
61+
chart
62+
.draw_series(LineSeries::new(
63+
data[series].data.iter().map(
64+
|&DailyData {
65+
new_cases,
66+
total_cases,
67+
..
68+
}| (total_cases as u32, new_cases as u32),
69+
),
70+
color.stroke_width(3),
71+
))?
72+
.label(series)
73+
.legend(move |(x, y)| Rectangle::new([(x, y - 5), (x + 10, y + 5)], color.filled()));
74+
}
75+
76+
chart
77+
.configure_series_labels()
78+
.border_style(&BLACK)
79+
.draw()?;
80+
81+
Ok(())
82+
}

plotters-doc-data

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ including bitmap, vector graph, piston window, GTK/Cairo and WebAssembly.
259259
</div>
260260
</div>
261261
262+
<div class="galleryItem">
263+
<a href="https://plotters-rs.github.io/plotters-doc-data/tick_control.gif">
264+
<img src="https://plotters-rs.github.io/plotters-doc-data/tick_control.gif" class="galleryItem"></img>
265+
</a>
266+
<div class="galleryText">
267+
COVID-19 Visualization
268+
<a href="https://github.com/38/plotters/blob/master/examples/tick_control.rs">[code]</a>
269+
</div>
270+
</div>
271+
262272
263273
## Table of Contents
264274
* [Gallery](#gallery)

0 commit comments

Comments
 (0)