Skip to content

Commit b9cf97a

Browse files
committed
Add legend doc
1 parent 4ebe22b commit b9cf97a

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ path = "code/2_10_mixed.rs"
5353
[[bin]]
5454
name = "2_11_timeseries"
5555
path = "code/2_11_timeseries.rs"
56+
57+
[[bin]]
58+
name = "2_12_legend"
59+
path = "code/2_12_legend.rs"

code/2_12_legend.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use plotters::prelude::*;
2+
3+
fn main() {
4+
let root_area = BitMapBackend::new("images/2.12.png", (600, 400)).into_drawing_area();
5+
root_area.fill(&WHITE).unwrap();
6+
7+
let mut ctx = ChartBuilder::on(&root_area)
8+
.set_label_area_size(LabelAreaPosition::Left, 40)
9+
.set_label_area_size(LabelAreaPosition::Bottom, 40)
10+
.caption("Legend", ("Arial", 40))
11+
.build_ranged(-4.0..4.0, -1.2..1.2)
12+
.unwrap();
13+
14+
ctx.configure_mesh().draw().unwrap();
15+
16+
let x_kps: Vec<_> = (-80..80).map(|x| x as f64 / 20.0).collect();
17+
ctx.draw_series(LineSeries::new(x_kps.iter().map(|x| (*x, x.sin())), &RED))
18+
.unwrap()
19+
.label("Sine")
20+
.legend(|(x, y)| Path::new(vec![(x, y), (x + 20, y)], &RED));
21+
22+
ctx.draw_series(LineSeries::new(x_kps.iter().map(|x| (*x, x.cos())), &BLUE))
23+
.unwrap()
24+
.label("Cosine")
25+
.legend(|(x, y)| Path::new(vec![(x, y), (x + 20, y)], &BLUE));
26+
27+
ctx.configure_series_labels()
28+
.border_style(&BLACK)
29+
.background_style(&WHITE.mix(0.8))
30+
.draw()
31+
.unwrap();
32+
}

src/basic/basic_data_plotting.md

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ The following code demostrate how to draw a line series with Plotters
1313
use plotters::prelude::*;
1414

1515
fn main() {
16-
let root_area = BitMapBackend::new("images/2.5.png", (600, 400)).into_drawing_area();
16+
let root_area = BitMapBackend::new("images/2.5.png", (600, 400))
17+
.into_drawing_area();
1718
root_area.fill(&WHITE).unwrap();
1819

1920
let mut ctx = ChartBuilder::on(&root_area)
@@ -46,7 +47,8 @@ In this example, we assume there are `DATA1` and `DATA2` defined. See the source
4647
use plotters::prelude::*;
4748

4849
fn main() {
49-
let root_area = BitMapBackend::new("images/2.6.png", (600, 400)).into_drawing_area();
50+
let root_area = BitMapBackend::new("images/2.6.png", (600, 400))
51+
.into_drawing_area();
5052
root_area.fill(&WHITE).unwrap();
5153

5254
let mut ctx = ChartBuilder::on(&root_area)
@@ -82,7 +84,8 @@ The following demo demostrate how we can draw an area chart.
8284
use plotters::prelude::*;
8385

8486
fn main() {
85-
let root_area = BitMapBackend::new("images/2.7.png", (600, 400)).into_drawing_area();
87+
let root_area = BitMapBackend::new("images/2.7.png", (600, 400))
88+
.into_drawing_area();
8689
root_area.fill(&WHITE).unwrap();
8790

8891
let mut ctx = ChartBuilder::on(&root_area)
@@ -126,7 +129,8 @@ is used to set a pixel based margin for the rectangle element.
126129
use plotters::prelude::*;
127130

128131
fn main() {
129-
let root_area = BitMapBackend::new("images/2.8.png", (600, 400)).into_drawing_area();
132+
let root_area = BitMapBackend::new("images/2.8.png", (600, 400))
133+
.into_drawing_area();
130134
root_area.fill(&WHITE).unwrap();
131135

132136
let mut ctx = ChartBuilder::on(&root_area)
@@ -159,7 +163,8 @@ Similary, the following code draws a vertical bar chart.
159163
use plotters::prelude::*;
160164

161165
fn main() {
162-
let root_area = BitMapBackend::new("images/2.9.png", (600, 400)).into_drawing_area();
166+
let root_area = BitMapBackend::new("images/2.9.png", (600, 400))
167+
.into_drawing_area();
163168
root_area.fill(&WHITE).unwrap();
164169

165170
let mut ctx = ChartBuilder::on(&root_area)
@@ -201,7 +206,8 @@ use plotters::prelude::*;
201206
use chrono::{Utc, TimeZone};
202207

203208
fn main() {
204-
let root_area = BitMapBackend::new("images/2.11.png", (600, 400)).into_drawing_area();
209+
let root_area = BitMapBackend::new("images/2.11.png", (600, 400))
210+
.into_drawing_area();
205211
root_area.fill(&WHITE).unwrap();
206212

207213
let start_date = Utc.ymd(2019, 10, 1);
@@ -259,7 +265,8 @@ image. The following example shows plotting a histogram along with a line plot.
259265
use plotters::prelude::*;
260266

261267
fn main() {
262-
let root_area = BitMapBackend::new("images/2.10.png", (600, 400)).into_drawing_area();
268+
let root_area = BitMapBackend::new("images/2.10.png", (600, 400))
269+
.into_drawing_area();
263270
root_area.fill(&WHITE).unwrap();
264271

265272
let mut ctx = ChartBuilder::on(&root_area)
@@ -295,3 +302,50 @@ Result image:
295302
![2.10.png](../../images/2.10.png)
296303

297304
## Legend
305+
306+
Plotters allows user add legend on the figure. Specifically, Plotters called the it "series label".
307+
When you call `ChartContext::draw_series`, a result type that carries a handle to a series annotation
308+
is returned and you can use it to add a series label to that.
309+
310+
After you complete the data plotting, `ChartContext::configure_series_label` can be called to configure and draw
311+
the collections of series label. The following example demostrate how.
312+
313+
```rust
314+
use plotters::prelude::*;
315+
316+
fn main() {
317+
let root_area = BitMapBackend::new("images/2.12.png", (600, 400))
318+
.into_drawing_area();
319+
root_area.fill(&WHITE).unwrap();
320+
321+
let mut ctx = ChartBuilder::on(&root_area)
322+
.set_label_area_size(LabelAreaPosition::Left, 40)
323+
.set_label_area_size(LabelAreaPosition::Bottom, 40)
324+
.caption("Legend", ("Arial", 40))
325+
.build_ranged(-4.0..4.0, -1.2..1.2)
326+
.unwrap();
327+
328+
ctx.configure_mesh().draw().unwrap();
329+
330+
let x_kps: Vec<_> = (-80..80).map(|x| x as f64 / 20.0).collect();
331+
ctx.draw_series(LineSeries::new(x_kps.iter().map(|x| (*x, x.sin())), &RED))
332+
.unwrap()
333+
.label("Sine")
334+
.legend(|(x, y)| Path::new(vec![(x, y), (x + 20, y)], &RED));
335+
336+
ctx.draw_series(LineSeries::new(x_kps.iter().map(|x| (*x, x.cos())), &BLUE))
337+
.unwrap()
338+
.label("Cosine")
339+
.legend(|(x, y)| Path::new(vec![(x, y), (x + 20, y)], &BLUE));
340+
341+
ctx.configure_series_labels()
342+
.border_style(&BLACK)
343+
.background_style(&WHITE.mix(0.8))
344+
.draw()
345+
.unwrap();
346+
}
347+
```
348+
349+
Result image:
350+
351+
![2.12.png](../../images/2.12.png)

0 commit comments

Comments
 (0)