@@ -13,7 +13,8 @@ The following code demostrate how to draw a line series with Plotters
13
13
use plotters :: prelude :: * ;
14
14
15
15
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 ();
17
18
root_area . fill (& WHITE ). unwrap ();
18
19
19
20
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
46
47
use plotters :: prelude :: * ;
47
48
48
49
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 ();
50
52
root_area . fill (& WHITE ). unwrap ();
51
53
52
54
let mut ctx = ChartBuilder :: on (& root_area )
@@ -82,7 +84,8 @@ The following demo demostrate how we can draw an area chart.
82
84
use plotters :: prelude :: * ;
83
85
84
86
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 ();
86
89
root_area . fill (& WHITE ). unwrap ();
87
90
88
91
let mut ctx = ChartBuilder :: on (& root_area )
@@ -126,7 +129,8 @@ is used to set a pixel based margin for the rectangle element.
126
129
use plotters :: prelude :: * ;
127
130
128
131
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 ();
130
134
root_area . fill (& WHITE ). unwrap ();
131
135
132
136
let mut ctx = ChartBuilder :: on (& root_area )
@@ -159,7 +163,8 @@ Similary, the following code draws a vertical bar chart.
159
163
use plotters :: prelude :: * ;
160
164
161
165
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 ();
163
168
root_area . fill (& WHITE ). unwrap ();
164
169
165
170
let mut ctx = ChartBuilder :: on (& root_area )
@@ -201,7 +206,8 @@ use plotters::prelude::*;
201
206
use chrono :: {Utc , TimeZone };
202
207
203
208
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 ();
205
211
root_area . fill (& WHITE ). unwrap ();
206
212
207
213
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.
259
265
use plotters :: prelude :: * ;
260
266
261
267
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 ();
263
270
root_area . fill (& WHITE ). unwrap ();
264
271
265
272
let mut ctx = ChartBuilder :: on (& root_area )
@@ -295,3 +302,50 @@ Result image:
295
302
![ 2.10.png] ( ../../images/2.10.png )
296
303
297
304
## 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