@@ -4,6 +4,7 @@ use svg::node;
4
4
use svg:: Node ;
5
5
6
6
use crate :: axis;
7
+ use crate :: grid:: GridType ;
7
8
use crate :: histogram;
8
9
use crate :: style;
9
10
use crate :: utils;
@@ -14,14 +15,34 @@ fn value_to_face_offset(value: f64, axis: &axis::ContinuousAxis, face_size: f64)
14
15
( face_size * ( value - axis. min ( ) ) ) / range
15
16
}
16
17
18
+ fn vertical_line < S > ( xpos : f64 , ymin : f64 , ymax : f64 , color : S ) -> node:: element:: Line
19
+ where
20
+ S : AsRef < str > ,
21
+ {
22
+ node:: element:: Line :: new ( )
23
+ . set ( "x1" , xpos)
24
+ . set ( "x2" , xpos)
25
+ . set ( "y1" , ymin)
26
+ . set ( "y2" , ymax)
27
+ . set ( "stroke" , color. as_ref ( ) )
28
+ . set ( "stroke-width" , 1 )
29
+ }
30
+
31
+ fn horizontal_line < S > ( ypos : f64 , xmin : f64 , xmax : f64 , color : S ) -> node:: element:: Line
32
+ where
33
+ S : AsRef < str > ,
34
+ {
35
+ node:: element:: Line :: new ( )
36
+ . set ( "x1" , xmin)
37
+ . set ( "x2" , xmax)
38
+ . set ( "y1" , ypos)
39
+ . set ( "y2" , ypos)
40
+ . set ( "stroke" , color. as_ref ( ) )
41
+ . set ( "stroke-width" , 1 )
42
+ }
43
+
17
44
pub fn draw_x_axis ( a : & axis:: ContinuousAxis , face_width : f64 ) -> node:: element:: Group {
18
- let axis_line = node:: element:: Line :: new ( )
19
- . set ( "x1" , 0 )
20
- . set ( "y1" , 0 )
21
- . set ( "x2" , face_width)
22
- . set ( "y2" , 0 )
23
- . set ( "stroke" , "black" )
24
- . set ( "stroke-width" , 1 ) ;
45
+ let axis_line = horizontal_line ( 0.0 , 0.0 , face_width, "black" ) ;
25
46
26
47
let mut ticks = node:: element:: Group :: new ( ) ;
27
48
let mut labels = node:: element:: Group :: new ( ) ;
@@ -61,13 +82,7 @@ pub fn draw_x_axis(a: &axis::ContinuousAxis, face_width: f64) -> node::element::
61
82
}
62
83
63
84
pub fn draw_y_axis ( a : & axis:: ContinuousAxis , face_height : f64 ) -> node:: element:: Group {
64
- let axis_line = node:: element:: Line :: new ( )
65
- . set ( "x1" , 0 )
66
- . set ( "y1" , 0 )
67
- . set ( "x2" , 0 )
68
- . set ( "y2" , -face_height)
69
- . set ( "stroke" , "black" )
70
- . set ( "stroke-0" , 1 ) ;
85
+ let axis_line = vertical_line ( 0.0 , 0.0 , -face_height, "black" ) ;
71
86
72
87
let mut ticks = node:: element:: Group :: new ( ) ;
73
88
let mut labels = node:: element:: Group :: new ( ) ;
@@ -101,7 +116,8 @@ pub fn draw_y_axis(a: &axis::ContinuousAxis, face_height: f64) -> node::element:
101
116
. set (
102
117
"transform" ,
103
118
format ! ( "rotate(-90 {} {})" , -30 , -( face_height / 2. ) ) ,
104
- ) . add ( node:: Text :: new ( a. get_label ( ) ) ) ;
119
+ )
120
+ . add ( node:: Text :: new ( a. get_label ( ) ) ) ;
105
121
106
122
node:: element:: Group :: new ( )
107
123
. add ( ticks)
@@ -214,7 +230,8 @@ where
214
230
. set (
215
231
"stroke" ,
216
232
style. get_colour ( ) . clone ( ) . unwrap_or_else ( || "" . into ( ) ) ,
217
- ) . set ( "stroke-width" , 2 )
233
+ )
234
+ . set ( "stroke-width" , 2 )
218
235
. set ( "d" , path) ,
219
236
) ;
220
237
}
@@ -253,7 +270,8 @@ where
253
270
. get_fill ( )
254
271
. clone ( )
255
272
. unwrap_or_else ( || "burlywood" . into ( ) ) ,
256
- ) . set ( "stroke" , "black" ) ;
273
+ )
274
+ . set ( "stroke" , "black" ) ;
257
275
group. append ( rect) ;
258
276
}
259
277
@@ -298,7 +316,8 @@ where
298
316
. set (
299
317
"stroke" ,
300
318
style. get_colour ( ) . clone ( ) . unwrap_or_else ( || "" . into ( ) ) ,
301
- ) . set ( "stroke-width" , style. get_width ( ) . clone ( ) . unwrap_or ( 2. ) )
319
+ )
320
+ . set ( "stroke-width" , style. get_width ( ) . clone ( ) . unwrap_or ( 2. ) )
302
321
. set ( "d" , path) ,
303
322
) ;
304
323
@@ -344,7 +363,8 @@ where
344
363
. get_fill ( )
345
364
. clone ( )
346
365
. unwrap_or_else ( || "burlywood" . into ( ) ) ,
347
- ) . set ( "stroke" , "black" ) ,
366
+ )
367
+ . set ( "stroke" , "black" ) ,
348
368
) ;
349
369
350
370
let mid_line = -value_to_face_offset ( median, y_axis, face_height) ;
@@ -421,12 +441,54 @@ where
421
441
. get_fill ( )
422
442
. clone ( )
423
443
. unwrap_or_else ( || "burlywood" . into ( ) ) ,
424
- ) . set ( "stroke" , "black" ) ,
444
+ )
445
+ . set ( "stroke" , "black" ) ,
425
446
) ;
426
447
427
448
group
428
449
}
429
450
451
+ pub ( crate ) fn draw_grid ( grid : GridType , face_width : f64 , face_height : f64 ) -> node:: element:: Group {
452
+ match grid {
453
+ GridType :: HorizontalOnly ( grid) => {
454
+ let ( ymin, ymax) = ( 0f64 , face_height) ;
455
+ let y_step = ( ymax - ymin) / f64:: from ( grid. ny ) ;
456
+ let mut lines = node:: element:: Group :: new ( ) ;
457
+
458
+ for iy in 0 ..=grid. ny {
459
+ let y = f64:: from ( iy) * y_step + ymin;
460
+ let line = horizontal_line ( -y, 0.0 , face_width, grid. color . as_str ( ) ) ;
461
+ lines = lines. add ( line) ;
462
+ }
463
+
464
+ lines
465
+ }
466
+ GridType :: Both ( grid) => {
467
+ let ( xmin, xmax) = ( 0f64 , face_width) ;
468
+ let ( ymin, ymax) = ( 0f64 , face_height) ;
469
+
470
+ let x_step = ( xmax - xmin) / f64:: from ( grid. nx ) ;
471
+ let y_step = ( ymax - ymin) / f64:: from ( grid. ny ) ;
472
+
473
+ let mut lines = node:: element:: Group :: new ( ) ;
474
+
475
+ for iy in 0 ..=grid. ny {
476
+ let y = f64:: from ( iy) * y_step + ymin;
477
+ let line = horizontal_line ( -y, 0.0 , face_width, grid. color . as_str ( ) ) ;
478
+ lines = lines. add ( line) ;
479
+ }
480
+
481
+ for ix in 0 ..=grid. nx {
482
+ let x = f64:: from ( ix) * x_step + xmin;
483
+ let line = vertical_line ( x, 0.0 , -face_height, grid. color . as_str ( ) ) ;
484
+ lines = lines. add ( line) ;
485
+ }
486
+
487
+ lines
488
+ }
489
+ }
490
+ }
491
+
430
492
#[ cfg( test) ]
431
493
mod tests {
432
494
use super :: * ;
0 commit comments