@@ -8,7 +8,7 @@ use plotly::Configuration;
88use  plotly:: { color:: Rgb ,  Plot ,  Scatter } ; 
99use  plotly_utils:: write_example_to_html; 
1010
11- // Subplots  
11+ // SubplotsBuilder  
1212// ANCHOR: simple_subplot 
1313fn  simple_subplot ( show :  bool ,  file_name :  & str )  { 
1414    let  trace1 = Scatter :: new ( vec ! [ 1 ,  2 ,  3 ] ,  vec ! [ 4 ,  5 ,  6 ] ) . name ( "trace1" ) ; 
@@ -232,7 +232,7 @@ fn multiple_custom_sized_subplots(show: bool, file_name: &str) {
232232    plot. add_trace ( trace4) ; 
233233
234234    let  layout = Layout :: new ( ) 
235-         . title ( "Multiple Custom Sized Subplots " ) 
235+         . title ( "Multiple Custom Sized SubplotsBuilder " ) 
236236        . x_axis ( Axis :: new ( ) . domain ( & [ 0. ,  0.45 ] ) . anchor ( "y1" ) ) 
237237        . y_axis ( Axis :: new ( ) . domain ( & [ 0.5 ,  1. ] ) . anchor ( "x1" ) ) 
238238        . x_axis2 ( Axis :: new ( ) . domain ( & [ 0.55 ,  1. ] ) . anchor ( "y2" ) ) 
@@ -423,12 +423,14 @@ fn subplots_with_multiple_traces(show: bool, file_name: &str) {
423423    plot. add_trace ( trace4) ; 
424424    plot. add_trace ( trace5) ; 
425425
426-     let  layout = Layout :: new ( ) . title ( "Subplots with Multiple Traces" ) . grid ( 
427-         LayoutGrid :: new ( ) 
428-             . rows ( 1 ) 
429-             . columns ( 2 ) 
430-             . pattern ( GridPattern :: Independent ) , 
431-     ) ; 
426+     let  layout = Layout :: new ( ) 
427+         . title ( "SubplotsBuilder with Multiple Traces" ) 
428+         . grid ( 
429+             LayoutGrid :: new ( ) 
430+                 . rows ( 1 ) 
431+                 . columns ( 2 ) 
432+                 . pattern ( GridPattern :: Independent ) , 
433+         ) ; 
432434    plot. set_layout ( layout) ; 
433435
434436    let  path = write_example_to_html ( & plot,  file_name) ; 
@@ -438,9 +440,88 @@ fn subplots_with_multiple_traces(show: bool, file_name: &str) {
438440} 
439441// ANCHOR_END: subplots_with_multiple_traces 
440442
443+ // ANCHOR: make_subplots_api 
444+ fn  make_subplots_api ( show :  bool ,  file_name :  & str )  { 
445+     use  plotly:: subplot:: SubplotsBuilder ; 
446+     let  mut  fig = SubplotsBuilder :: new ( 1 ,  2 ) 
447+         . subplot_titles ( vec ! [ "Plot 1" ,  "Plot 2" ] ) 
448+         . x_axis_titles ( vec ! [ "X Axis 1" ,  "X Axis 2" ] ) 
449+         . y_axis_titles ( vec ! [ "Y Axis 1" ,  "Y Axis 2" ] ) 
450+         . x_title_at ( 1 ,  1 ,  "X Axis 1" ) 
451+         . y_title_at ( 1 ,  2 ,  "Y Axis 2" ) 
452+         . horizontal_spacing ( 0.1 ) ; 
453+     fig. add_trace ( Scatter :: new ( vec ! [ 1 ,  2 ,  3 ] ,  vec ! [ 4 ,  5 ,  6 ] ) ,  1 ,  1 ) ; 
454+     fig. add_trace ( Scatter :: new ( vec ! [ 20 ,  30 ,  40 ] ,  vec ! [ 50 ,  60 ,  70 ] ) ,  1 ,  2 ) ; 
455+ 
456+     let  plot = fig. make_subplots ( ) ; 
457+     let  path = write_example_to_html ( & plot,  file_name) ; 
458+     if  show { 
459+         plot. show_html ( path) ; 
460+     } 
461+ } 
462+ 
463+ // ANCHOR: make_subplots_large_grid 
464+ fn  make_subplots_large_grid ( show :  bool ,  file_name :  & str )  { 
465+     use  plotly:: subplot:: SubplotsBuilder ; 
466+ 
467+     // Create a 4x4 grid (16 subplots) to test beyond the 8-axis hardcoded limit 
468+     let  titles = vec ! [ 
469+         "Subplot 1" , 
470+         "Subplot 2" , 
471+         "Subplot 3" , 
472+         "Subplot 4" , 
473+         "Subplot 5" , 
474+         "Subplot 6" , 
475+         "Subplot 7" , 
476+         "Subplot 8" , 
477+         "Subplot 9" , 
478+         "Subplot 10" , 
479+         "Subplot 11" , 
480+         "Subplot 12" , 
481+         "Subplot 13" , 
482+         "Subplot 14" , 
483+         "Subplot 15" , 
484+         "Subplot 16" , 
485+     ] ; 
486+ 
487+     let  rows = 4 ; 
488+     let  cols = 4 ; 
489+     let  mut  fig = SubplotsBuilder :: new ( rows,  cols) 
490+         . subplot_titles ( titles) 
491+         . x_axis_titles ( vec ! [ "X Axis 1" ,  "X Axis 2" ,  "X Axis 3" ,  "X Axis 4" ] ) 
492+         . y_axis_titles ( vec ! [ "Y Axis 1" ,  "Y Axis 2" ,  "Y Axis 3" ,  "Y Axis 4" ] ) 
493+         . x_title_at ( 1 ,  1 ,  "X Axis 1" ) 
494+         . y_title_at ( 1 ,  2 ,  "Y Axis 2" ) 
495+         . horizontal_spacing ( 0.05 ) 
496+         . vertical_spacing ( 0.5 ) ; 
497+ 
498+     for  row in  1 ..=rows { 
499+         for  col in  1 ..=cols { 
500+             let  subplot_num = ( row - 1 )  *  cols + col; 
501+             let  x_data:  Vec < i32 >  = ( 1 ..=5 ) . map ( |i| ( i as  i32 )  *  subplot_num as  i32 ) . collect ( ) ; 
502+             let  y_data:  Vec < i32 >  = ( 1 ..=5 ) 
503+                 . map ( |i| ( i as  i32 )  *  subplot_num as  i32  + 10 ) 
504+                 . collect ( ) ; 
505+ 
506+             fig. add_trace ( 
507+                 Scatter :: new ( x_data,  y_data) . name ( & format ! ( "Trace {}" ,  subplot_num) ) , 
508+                 row as  usize , 
509+                 col as  usize , 
510+             ) ; 
511+         } 
512+     } 
513+ 
514+     let  plot = fig. make_subplots ( ) ; 
515+     let  path = write_example_to_html ( & plot,  file_name) ; 
516+     if  show { 
517+         plot. show_html ( path) ; 
518+     } 
519+ } 
520+ // ANCHOR_END: make_subplots_large_grid 
521+ 
441522fn  main ( )  { 
442523    // Change false to true on any of these lines to display the example. 
443-     // Subplots  
524+     // SubplotsBuilder  
444525    simple_subplot ( false ,  "simple_subplot" ) ; 
445526
446527    simple_subplot_matches_x_axis ( false ,  "simple_subplot_matches_x_axis" ) ; 
@@ -462,4 +543,7 @@ fn main() {
462543    // Multiple Axes 
463544    two_y_axes ( false ,  "two_y_axes" ) ; 
464545    multiple_axes ( false ,  "multiple_axes" ) ; 
546+ 
547+     make_subplots_api ( false ,  "make_subplots_api" ) ; 
548+     make_subplots_large_grid ( false ,  "make_subplots_large_grid" ) ; 
465549} 
0 commit comments