Skip to content

Commit 30456d9

Browse files
committed
Merge branch 'master' into pr-185
2 parents 2cf740b + e836f33 commit 30456d9

File tree

13 files changed

+439
-191
lines changed

13 files changed

+439
-191
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ optional = true
2424
default_features = false
2525

2626
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
27-
rusttype = { version = "0.8.2", optional = true }
27+
ttf-parser = { version = "0.8.2", optional = true }
2828
lazy_static = { version = "1.4.0", optional = true }
29+
pathfinder_geometry = { version = "0.5.1", optional = true }
2930
font-kit = { version = "0.7.0", optional = true }
3031

3132
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.image]
@@ -81,7 +82,7 @@ point_series = []
8182
surface_series = []
8283

8384
# Font implemnetation
84-
ttf = ["font-kit", "rusttype", "lazy_static"]
85+
ttf = ["font-kit", "ttf-parser", "lazy_static", "pathfinder_geometry"]
8586

8687
# Misc
8788
datetime = ["chrono"]

examples/3d-plot.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4747
.draw()?;
4848
Ok(())
4949
}
50+
#[test]
51+
fn entry_point() {
52+
main().unwrap()
53+
}

examples/3d-plot2.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4242

4343
Ok(())
4444
}
45+
#[test]
46+
fn entry_point() {
47+
main().unwrap()
48+
}

examples/tick_control.rs

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ use plotters::prelude::*;
33
use std::fs::File;
44
use std::io::BufReader;
55

6-
use chrono::NaiveDate;
7-
use std::str::FromStr;
8-
96
#[derive(serde_derive::Deserialize)]
107
struct DailyData {
11-
date: String,
128
#[serde(default)]
13-
new_cases_smoothed_per_million: f64,
9+
new_cases: f64,
1410
#[serde(default)]
15-
total_cases_per_million: f64,
11+
total_cases: f64,
1612
}
1713

1814
#[derive(serde_derive::Deserialize)]
@@ -22,68 +18,68 @@ struct CountryData {
2218

2319
fn main() -> Result<(), Box<dyn std::error::Error>> {
2420
let root =
25-
BitMapBackend::gif("/tmp/tick_control.gif", (800, 600), 100)?.into_drawing_area();
26-
27-
for a in 0..200 {
28-
29-
root.fill(&WHITE)?;
30-
31-
32-
let mut chart = ChartBuilder::on(&root)
33-
.set_label_area_size(LabelAreaPosition::Left, (8).percent())
34-
.set_label_area_size(LabelAreaPosition::Bottom, (6).percent())
35-
.margin((1).percent())
36-
.build_cartesian_3d(
37-
(20u32..10_0000u32)
38-
.log_scale()
39-
.with_key_points(vec![50, 100, 200, 500, 1000, 10000]),
40-
(0u32..1000u32)
41-
.log_scale()
42-
.with_key_points(vec![2, 5, 10, 20, 50, 100, 200]),
43-
NaiveDate::from_ymd(2020, 1, 1)..NaiveDate::from_ymd(2020, 9, 5),
44-
)?;
21+
SVGBackend::new("plotters-doc-data/tick_control.svg", (1024, 768)).into_drawing_area();
22+
root.fill(&WHITE)?;
4523

46-
chart.with_projection(|mut pb| {
47-
pb.yaw = (1.57 - a as f64 / 100.0 * 1.57).abs();
48-
pb.into_matrix()
49-
});
24+
let (upper, lower) = root.split_vertically(750);
5025

51-
chart
52-
.configure_axes()
53-
.draw()?;
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+
)?;
5430

55-
let data: std::collections::HashMap<String, CountryData> = serde_json::from_reader(
56-
BufReader::new(File::open("plotters-doc-data/covid-data.json")?),
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]),
5743
)?;
5844

59-
for (idx, &series) in ["USA", "CHN"]
60-
.iter()
61-
.enumerate()
62-
{
63-
let color = Palette99::pick(idx).mix(1.0);
64-
chart
65-
.draw_series(LineSeries::new(
66-
data[series].data.iter().map(
67-
|DailyData {
68-
date,
69-
new_cases_smoothed_per_million,
70-
total_cases_per_million,
71-
..
72-
}| (*total_cases_per_million as u32, *new_cases_smoothed_per_million as u32, chrono::NaiveDate::from_str(date).unwrap(),),
73-
),
74-
color.stroke_width(1),
75-
))?
76-
.label(series)
77-
.legend(move |(x, y)| Rectangle::new([(x, y - 5), (x + 10, y + 5)], color.filled()));
78-
}
45+
chart
46+
.configure_mesh()
47+
.x_desc("Total Cases")
48+
.y_desc("New Cases")
49+
.draw()?;
7950

80-
chart
81-
.configure_series_labels()
82-
.border_style(&BLACK)
83-
.draw()?;
51+
let data: std::collections::HashMap<String, CountryData> = serde_json::from_reader(
52+
BufReader::new(File::open("plotters-doc-data/covid-data.json")?),
53+
)?;
8454

85-
root.present()?;
55+
for (idx, &series) in ["CHN", "USA", "RUS", "JPN", "DEU", "IND", "OWID_WRL"]
56+
.iter()
57+
.enumerate()
58+
{
59+
let color = Palette99::pick(idx).mix(0.9);
60+
chart
61+
.draw_series(LineSeries::new(
62+
data[series].data.iter().map(
63+
|&DailyData {
64+
new_cases,
65+
total_cases,
66+
..
67+
}| (total_cases as u32, new_cases as u32),
68+
),
69+
color.stroke_width(3),
70+
))?
71+
.label(series)
72+
.legend(move |(x, y)| Rectangle::new([(x, y - 5), (x + 10, y + 5)], color.filled()));
8673
}
8774

75+
chart
76+
.configure_series_labels()
77+
.border_style(&BLACK)
78+
.draw()?;
79+
8880
Ok(())
8981
}
82+
#[test]
83+
fn entry_point() {
84+
main().unwrap()
85+
}

examples/two-scales.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
.right_y_label_area_size(40)
1212
.margin(5)
1313
.caption("Dual Y-Axis Example", ("sans-serif", 50.0).into_font())
14-
.build_cartesian_2d(0f32..10f32, LogRange(0.1f32..1e10f32))?
14+
.build_cartesian_2d(0f32..10f32, (0.1f32..1e10f32).log_scale())?
1515
.set_secondary_coord(0f32..10f32, -1.0f32..1.0f32);
1616

1717
chart

src/chart/context.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,10 @@ impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged> ChartContext<'a, DB, Cartesia
302302
.iter()
303303
.map(|(_, text)| {
304304
if orientation.0 > 0 && orientation.1 == 0 && tick_size >= 0 {
305-
let ((x0, _), (x1, _)) = label_style
306-
.font
307-
.layout_box(text)
308-
.unwrap_or(((0, 0), (0, 0)));
309-
x1 - x0
305+
self.drawing_area
306+
.estimate_text_size(text, &label_style)
307+
.map(|(w, _)| w)
308+
.unwrap_or(0) as i32
310309
} else {
311310
// Don't ever do the layout estimationfor the drawing area that is either not
312311
// the right one or the tick mark is inward.

0 commit comments

Comments
 (0)