@@ -4,6 +4,7 @@ use svg::node;
44use svg:: Node ;
55
66use crate :: axis;
7+ use crate :: grid:: GridType ;
78use crate :: histogram;
89use crate :: style;
910use crate :: utils;
@@ -14,14 +15,34 @@ fn value_to_face_offset(value: f64, axis: &axis::ContinuousAxis, face_size: f64)
1415 ( face_size * ( value - axis. min ( ) ) ) / range
1516}
1617
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+
1744pub 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" ) ;
2546
2647 let mut ticks = node:: element:: Group :: new ( ) ;
2748 let mut labels = node:: element:: Group :: new ( ) ;
@@ -61,13 +82,7 @@ pub fn draw_x_axis(a: &axis::ContinuousAxis, face_width: f64) -> node::element::
6182}
6283
6384pub 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" ) ;
7186
7287 let mut ticks = node:: element:: Group :: new ( ) ;
7388 let mut labels = node:: element:: Group :: new ( ) ;
@@ -101,7 +116,8 @@ pub fn draw_y_axis(a: &axis::ContinuousAxis, face_height: f64) -> node::element:
101116 . set (
102117 "transform" ,
103118 format ! ( "rotate(-90 {} {})" , -30 , -( face_height / 2. ) ) ,
104- ) . add ( node:: Text :: new ( a. get_label ( ) ) ) ;
119+ )
120+ . add ( node:: Text :: new ( a. get_label ( ) ) ) ;
105121
106122 node:: element:: Group :: new ( )
107123 . add ( ticks)
@@ -214,7 +230,8 @@ where
214230 . set (
215231 "stroke" ,
216232 style. get_colour ( ) . clone ( ) . unwrap_or_else ( || "" . into ( ) ) ,
217- ) . set ( "stroke-width" , 2 )
233+ )
234+ . set ( "stroke-width" , 2 )
218235 . set ( "d" , path) ,
219236 ) ;
220237 }
@@ -253,7 +270,8 @@ where
253270 . get_fill ( )
254271 . clone ( )
255272 . unwrap_or_else ( || "burlywood" . into ( ) ) ,
256- ) . set ( "stroke" , "black" ) ;
273+ )
274+ . set ( "stroke" , "black" ) ;
257275 group. append ( rect) ;
258276 }
259277
@@ -298,7 +316,8 @@ where
298316 . set (
299317 "stroke" ,
300318 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. ) )
302321 . set ( "d" , path) ,
303322 ) ;
304323
@@ -344,7 +363,8 @@ where
344363 . get_fill ( )
345364 . clone ( )
346365 . unwrap_or_else ( || "burlywood" . into ( ) ) ,
347- ) . set ( "stroke" , "black" ) ,
366+ )
367+ . set ( "stroke" , "black" ) ,
348368 ) ;
349369
350370 let mid_line = -value_to_face_offset ( median, y_axis, face_height) ;
@@ -421,12 +441,54 @@ where
421441 . get_fill ( )
422442 . clone ( )
423443 . unwrap_or_else ( || "burlywood" . into ( ) ) ,
424- ) . set ( "stroke" , "black" ) ,
444+ )
445+ . set ( "stroke" , "black" ) ,
425446 ) ;
426447
427448 group
428449}
429450
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+
430492#[ cfg( test) ]
431493mod tests {
432494 use super :: * ;
0 commit comments