@@ -145,7 +145,7 @@ describe('config argument', function() {
145
145
fillFrame : false ,
146
146
frameMargins : 0.1
147
147
} ;
148
- var layoutHeight = 360 ;
148
+ var layoutHeight = Math . round ( 0.8 * containerHeightBeforePlot ) ;
149
149
var relayoutHeight = layoutHeight ;
150
150
151
151
testAutosize ( autosize , config , layoutHeight , relayoutHeight , done ) ;
@@ -157,8 +157,8 @@ describe('config argument', function() {
157
157
fillFrame : false ,
158
158
frameMargins : 0.1
159
159
} ;
160
- var layoutHeight = 360 ;
161
- var relayoutHeight = 288 ;
160
+ var layoutHeight = Math . round ( 0.8 * containerHeightBeforePlot ) ;
161
+ var relayoutHeight = Math . round ( 0.8 * containerHeightBeforeRelayout ) ;
162
162
163
163
testAutosize ( autosize , config , layoutHeight , relayoutHeight , done ) ;
164
164
} ) ;
@@ -543,23 +543,26 @@ describe('config argument', function() {
543
543
} ) ;
544
544
545
545
describe ( 'responsive figure' , function ( ) {
546
- var gd ;
547
- var startWidth = 960 , startHeight = 400 ;
548
- var newWidth = 400 , newHeight = 700 ;
549
- var data = [ { x : [ 1 , 2 , 3 , 4 ] , y : [ 5 , 10 , 2 , 8 ] } ] ;
546
+ var gd , data = [ { x : [ 1 , 2 , 3 , 4 ] , y : [ 5 , 10 , 2 , 8 ] } ] ;
547
+ var width = 960 , height = 800 ;
548
+
549
+ var parent , elWidth , elHeight ;
550
550
551
551
beforeEach ( function ( ) {
552
- viewport . set ( startWidth , startHeight ) ;
553
- gd = createGraphDiv ( ) ;
552
+ viewport . set ( width , height ) ;
554
553
555
- // Make the graph fill the parent
556
- gd . style . width = '100%' ;
557
- gd . style . height = '100%' ;
554
+ // Prepare a parent container that fills the viewport
555
+ parent = document . createElement ( 'div' ) ;
556
+ parent . style . width = '100vw' ;
557
+ parent . style . height = '100vh' ;
558
+ parent . style . position = 'fixed' ;
559
+ parent . style . top = '0' ;
560
+ parent . style . left = '0' ;
558
561
} ) ;
559
562
560
563
afterEach ( function ( ) {
561
564
Plotly . purge ( gd ) ; // Needed to remove all event listeners
562
- destroyGraphDiv ( ) ;
565
+ document . body . removeChild ( parent ) ;
563
566
viewport . reset ( ) ;
564
567
} ) ;
565
568
@@ -573,45 +576,64 @@ describe('config argument', function() {
573
576
}
574
577
575
578
function testResponsive ( ) {
576
- checkLayoutSize ( startWidth , startHeight ) ;
577
- viewport . set ( newWidth , newHeight ) ;
579
+ checkLayoutSize ( elWidth , elHeight ) ;
580
+ viewport . set ( width / 2 , height / 2 ) ;
578
581
579
582
return Promise . resolve ( )
580
583
. then ( delay ( 200 ) )
581
584
. then ( function ( ) {
582
- checkLayoutSize ( newWidth , newHeight ) ;
585
+ checkLayoutSize ( elWidth / 2 , elHeight / 2 ) ;
583
586
} )
584
587
. catch ( failTest ) ;
585
588
}
586
589
590
+ function fillParent ( numRows , numCols , cb ) {
591
+ elWidth = width / numCols , elHeight = height / numRows ;
592
+
593
+ // Fill parent
594
+ for ( var i = 0 ; i < ( numCols * numRows ) ; i ++ ) {
595
+ var col = document . createElement ( 'div' ) ;
596
+ col . style . height = '100%' ;
597
+ col . style . width = '100%' ;
598
+ if ( typeof ( cb ) === typeof ( Function ) ) cb . call ( col , i ) ;
599
+ parent . appendChild ( col ) ;
600
+ }
601
+ document . body . appendChild ( parent ) ;
602
+ gd = parent . childNodes [ 0 ] ;
603
+ }
604
+
587
605
it ( 'should resize when the viewport width/height changes' , function ( done ) {
606
+ fillParent ( 1 , 1 ) ;
588
607
Plotly . plot ( gd , data , { } , { responsive : true } )
589
608
. then ( testResponsive )
590
609
. then ( done ) ;
591
610
} ) ;
592
611
593
612
it ( 'should still be responsive if the plot is edited' , function ( done ) {
613
+ fillParent ( 1 , 1 ) ;
594
614
Plotly . plot ( gd , data , { } , { responsive : true } )
595
615
. then ( function ( ) { return Plotly . restyle ( gd , 'y[0]' , data [ 0 ] . y [ 0 ] + 2 ) ; } )
596
616
. then ( testResponsive )
597
617
. then ( done ) ;
598
618
} ) ;
599
619
600
620
it ( 'should still be responsive if the plot is purged and replotted' , function ( done ) {
621
+ fillParent ( 1 , 1 ) ;
601
622
Plotly . plot ( gd , data , { } , { responsive : true } )
602
623
. then ( function ( ) { return Plotly . newPlot ( gd , data , { } , { responsive : true } ) ; } )
603
624
. then ( testResponsive )
604
625
. then ( done ) ;
605
626
} ) ;
606
627
607
628
it ( 'should only have one resize handler when plotted more than once' , function ( done ) {
629
+ fillParent ( 1 , 1 ) ;
608
630
var cntWindowResize = 0 ;
609
631
window . addEventListener ( 'resize' , function ( ) { cntWindowResize ++ ; } ) ;
610
632
spyOn ( Plotly . Plots , 'resize' ) . and . callThrough ( ) ;
611
633
612
634
Plotly . plot ( gd , data , { } , { responsive : true } )
613
635
. then ( function ( ) { return Plotly . restyle ( gd , 'y[0]' , data [ 0 ] . y [ 0 ] + 2 ) ; } )
614
- . then ( function ( ) { viewport . set ( newWidth , newHeight ) ; } )
636
+ . then ( function ( ) { viewport . set ( width / 2 , width / 2 ) ; } )
615
637
. then ( delay ( 200 ) )
616
638
// .then(function() {viewport.set(newWidth, 2 * newHeight);}).then(delay(200))
617
639
. then ( function ( ) {
@@ -623,26 +645,65 @@ describe('config argument', function() {
623
645
} ) ;
624
646
625
647
it ( 'should become responsive if configured as such via Plotly.react' , function ( done ) {
648
+ fillParent ( 1 , 1 ) ;
626
649
Plotly . plot ( gd , data , { } , { responsive : false } )
627
650
. then ( function ( ) { return Plotly . react ( gd , data , { } , { responsive : true } ) ; } )
628
651
. then ( testResponsive )
629
652
. then ( done ) ;
630
653
} ) ;
631
654
632
655
it ( 'should stop being responsive if configured as such via Plotly.react' , function ( done ) {
656
+ fillParent ( 1 , 1 ) ;
633
657
Plotly . plot ( gd , data , { } , { responsive : true } )
634
658
// Check initial size
635
- . then ( function ( ) { checkLayoutSize ( startWidth , startHeight ) ; } )
659
+ . then ( function ( ) { checkLayoutSize ( width , height ) ; } )
636
660
// Turn off responsiveness
637
661
. then ( function ( ) { return Plotly . react ( gd , data , { } , { responsive : false } ) ; } )
638
662
// Resize viewport
639
- . then ( function ( ) { viewport . set ( newWidth , newHeight ) ; } )
663
+ . then ( function ( ) { viewport . set ( width / 2 , height / 2 ) ; } )
640
664
// Wait for resize to happen (Plotly.resize has an internal timeout)
641
665
. then ( delay ( 200 ) )
642
666
// Check that final figure's size hasn't changed
643
- . then ( function ( ) { checkLayoutSize ( startWidth , startHeight ) ; } )
667
+ . then ( function ( ) { checkLayoutSize ( width , height ) ; } )
644
668
. catch ( failTest )
645
669
. then ( done ) ;
646
670
} ) ;
671
+
672
+ // Testing fancier CSS layouts
673
+ it ( 'should resize horizontally in a flexbox when responsive: true' , function ( done ) {
674
+ parent . style . display = 'flex' ;
675
+ parent . style . flexDirection = 'row' ;
676
+ fillParent ( 1 , 2 , function ( ) {
677
+ this . style . flexGrow = '1' ;
678
+ } ) ;
679
+
680
+ Plotly . plot ( gd , data , { } , { responsive : true } )
681
+ . then ( testResponsive )
682
+ . then ( done ) ;
683
+ } ) ;
684
+
685
+ it ( 'should resize vertically in a flexbox when responsive: true' , function ( done ) {
686
+ parent . style . display = 'flex' ;
687
+ parent . style . flexDirection = 'column' ;
688
+ fillParent ( 2 , 1 , function ( ) {
689
+ this . style . flexGrow = '1' ;
690
+ } ) ;
691
+
692
+ Plotly . plot ( gd , data , { } , { responsive : true } )
693
+ . then ( testResponsive )
694
+ . then ( done ) ;
695
+ } ) ;
696
+
697
+ it ( 'should resize in both direction in a grid when responsive: true' , function ( done ) {
698
+ var numCols = 2 , numRows = 2 ;
699
+ parent . style . display = 'grid' ;
700
+ parent . style . gridTemplateColumns = 'repeat(' + numCols + ', 1fr)' ;
701
+ parent . style . gridTemplateRows = 'repeat(' + numRows + ', 1fr)' ;
702
+ fillParent ( numRows , numCols ) ;
703
+
704
+ Plotly . plot ( gd , data , { } , { responsive : true } )
705
+ . then ( testResponsive )
706
+ . then ( done ) ;
707
+ } ) ;
647
708
} ) ;
648
709
} ) ;
0 commit comments