@@ -11,6 +11,10 @@ var createGraphDiv = require('../assets/create_graph_div');
1111var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
1212var fail = require ( '../assets/fail_test' ) ;
1313var customMatchers = require ( '../assets/custom_matchers' ) ;
14+ var checkTicks = require ( '../assets/check_ticks' ) ;
15+ var negateIf = require ( '../assets/negate_if' ) ;
16+
17+ var d3 = require ( 'd3' ) ;
1418
1519describe ( 'Bar.supplyDefaults' , function ( ) {
1620 'use strict' ;
@@ -762,10 +766,16 @@ describe('Bar.setPositions', function() {
762766describe ( 'A bar plot' , function ( ) {
763767 'use strict' ;
764768
769+ var gd ;
770+
765771 beforeAll ( function ( ) {
766772 jasmine . addMatchers ( customMatchers ) ;
767773 } ) ;
768774
775+ beforeEach ( function ( ) {
776+ gd = createGraphDiv ( ) ;
777+ } ) ;
778+
769779 afterEach ( destroyGraphDiv ) ;
770780
771781 function getAllTraceNodes ( node ) {
@@ -830,15 +840,13 @@ describe('A bar plot', function() {
830840 }
831841
832842 it ( 'should show bar texts (inside case)' , function ( done ) {
833- var gd = createGraphDiv ( ) ,
834- data = [ {
835- y : [ 10 , 20 , 30 ] ,
836- type : 'bar' ,
837- text : [ '1' , 'Very very very very very long bar text' ] ,
838- textposition : 'inside' ,
839- } ] ,
840- layout = {
841- } ;
843+ var data = [ {
844+ y : [ 10 , 20 , 30 ] ,
845+ type : 'bar' ,
846+ text : [ '1' , 'Very very very very very long bar text' ] ,
847+ textposition : 'inside' ,
848+ } ] ;
849+ var layout = { } ;
842850
843851 Plotly . plot ( gd , data , layout ) . then ( function ( ) {
844852 var traceNodes = getAllTraceNodes ( gd ) ,
@@ -862,16 +870,15 @@ describe('A bar plot', function() {
862870 } ) ;
863871
864872 it ( 'should show bar texts (outside case)' , function ( done ) {
865- var gd = createGraphDiv ( ) ,
866- data = [ {
867- y : [ 10 , - 20 , 30 ] ,
868- type : 'bar' ,
869- text : [ '1' , 'Very very very very very long bar text' ] ,
870- textposition : 'outside' ,
871- } ] ,
872- layout = {
873- barmode : 'relative'
874- } ;
873+ var data = [ {
874+ y : [ 10 , - 20 , 30 ] ,
875+ type : 'bar' ,
876+ text : [ '1' , 'Very very very very very long bar text' ] ,
877+ textposition : 'outside' ,
878+ } ] ;
879+ var layout = {
880+ barmode : 'relative'
881+ } ;
875882
876883 Plotly . plot ( gd , data , layout ) . then ( function ( ) {
877884 var traceNodes = getAllTraceNodes ( gd ) ,
@@ -896,15 +903,13 @@ describe('A bar plot', function() {
896903 } ) ;
897904
898905 it ( 'should show bar texts (horizontal case)' , function ( done ) {
899- var gd = createGraphDiv ( ) ,
900- data = [ {
901- x : [ 10 , - 20 , 30 ] ,
902- type : 'bar' ,
903- text : [ 'Very very very very very long bar text' , - 20 ] ,
904- textposition : 'outside' ,
905- } ] ,
906- layout = {
907- } ;
906+ var data = [ {
907+ x : [ 10 , - 20 , 30 ] ,
908+ type : 'bar' ,
909+ text : [ 'Very very very very very long bar text' , - 20 ] ,
910+ textposition : 'outside' ,
911+ } ] ;
912+ var layout = { } ;
908913
909914 Plotly . plot ( gd , data , layout ) . then ( function ( ) {
910915 var traceNodes = getAllTraceNodes ( gd ) ,
@@ -929,17 +934,16 @@ describe('A bar plot', function() {
929934 } ) ;
930935
931936 it ( 'should show bar texts (barnorm case)' , function ( done ) {
932- var gd = createGraphDiv ( ) ,
933- data = [ {
934- x : [ 100 , - 100 , 100 ] ,
935- type : 'bar' ,
936- text : [ 100 , - 100 , 100 ] ,
937- textposition : 'outside' ,
938- } ] ,
939- layout = {
940- barmode : 'relative' ,
941- barnorm : 'percent'
942- } ;
937+ var data = [ {
938+ x : [ 100 , - 100 , 100 ] ,
939+ type : 'bar' ,
940+ text : [ 100 , - 100 , 100 ] ,
941+ textposition : 'outside' ,
942+ } ] ;
943+ var layout = {
944+ barmode : 'relative' ,
945+ barnorm : 'percent'
946+ } ;
943947
944948 Plotly . plot ( gd , data , layout ) . then ( function ( ) {
945949 var traceNodes = getAllTraceNodes ( gd ) ,
@@ -964,8 +968,7 @@ describe('A bar plot', function() {
964968 } ) ;
965969
966970 it ( 'should be able to restyle' , function ( done ) {
967- var gd = createGraphDiv ( ) ,
968- mock = Lib . extendDeep ( { } , require ( '@mocks/bar_attrs_relative' ) ) ;
971+ var mock = Lib . extendDeep ( { } , require ( '@mocks/bar_attrs_relative' ) ) ;
969972
970973 Plotly . plot ( gd , mock . data , mock . layout ) . then ( function ( ) {
971974 var cd = gd . calcdata ;
@@ -1114,27 +1117,26 @@ describe('A bar plot', function() {
11141117 } ) ;
11151118
11161119 it ( 'should coerce text-related attributes' , function ( done ) {
1117- var gd = createGraphDiv ( ) ,
1118- data = [ {
1119- y : [ 10 , 20 , 30 , 40 ] ,
1120- type : 'bar' ,
1121- text : [ 'T1P1' , 'T1P2' , 13 , 14 ] ,
1122- textposition : [ 'inside' , 'outside' , 'auto' , 'BADVALUE' ] ,
1123- textfont : {
1124- family : [ '"comic sans"' ] ,
1125- color : [ 'red' , 'green' ] ,
1126- } ,
1127- insidetextfont : {
1128- size : [ 8 , 12 , 16 ] ,
1129- color : [ 'black' ] ,
1130- } ,
1131- outsidetextfont : {
1132- size : [ null , 24 , 32 ]
1133- }
1134- } ] ,
1135- layout = {
1136- font : { family : 'arial' , color : 'blue' , size : 13 }
1137- } ;
1120+ var data = [ {
1121+ y : [ 10 , 20 , 30 , 40 ] ,
1122+ type : 'bar' ,
1123+ text : [ 'T1P1' , 'T1P2' , 13 , 14 ] ,
1124+ textposition : [ 'inside' , 'outside' , 'auto' , 'BADVALUE' ] ,
1125+ textfont : {
1126+ family : [ '"comic sans"' ] ,
1127+ color : [ 'red' , 'green' ] ,
1128+ } ,
1129+ insidetextfont : {
1130+ size : [ 8 , 12 , 16 ] ,
1131+ color : [ 'black' ] ,
1132+ } ,
1133+ outsidetextfont : {
1134+ size : [ null , 24 , 32 ]
1135+ }
1136+ } ] ;
1137+ var layout = {
1138+ font : { family : 'arial' , color : 'blue' , size : 13 }
1139+ } ;
11381140
11391141 var expected = {
11401142 y : [ 10 , 20 , 30 , 40 ] ,
@@ -1194,6 +1196,72 @@ describe('A bar plot', function() {
11941196 . catch ( fail )
11951197 . then ( done ) ;
11961198 } ) ;
1199+
1200+ it ( 'can change orientation and correctly sets axis types' , function ( done ) {
1201+ function checkBarsMatch ( dims , msg ) {
1202+ var bars = d3 . selectAll ( '.bars .point' ) ;
1203+ var bbox1 = bars . node ( ) . getBoundingClientRect ( ) ;
1204+ bars . each ( function ( d , i ) {
1205+ if ( ! i ) return ;
1206+ var bbox = this . getBoundingClientRect ( ) ;
1207+ [ 'left' , 'right' , 'top' , 'bottom' , 'width' , 'height' ] . forEach ( function ( dim ) {
1208+ negateIf ( expect ( bbox [ dim ] ) , dims . indexOf ( dim ) === - 1 )
1209+ . toBeWithin ( bbox1 [ dim ] , 0.1 , msg + ' (' + i + '): ' + dim ) ;
1210+ } ) ;
1211+ } ) ;
1212+ }
1213+
1214+ Plotly . newPlot ( gd , [ {
1215+ x : [ 'a' , 'b' , 'c' ] ,
1216+ y : [ 1 , 2 , 3 ] ,
1217+ type : 'bar'
1218+ } ] , {
1219+ width : 400 , height : 400
1220+ } )
1221+ . then ( function ( ) {
1222+ checkTicks ( 'x' , [ 'a' , 'b' , 'c' ] , 'initial x' ) ;
1223+ checkTicks ( 'y' , [ '0' , '0.5' , '1' , '1.5' , '2' , '2.5' , '3' ] , 'initial y' ) ;
1224+
1225+ checkBarsMatch ( [ 'bottom' , 'width' ] , 'initial' ) ;
1226+
1227+ // turn implicit "v" into explicit "v" - a noop but specifically
1228+ // for orientation this was broken at one point...
1229+ return Plotly . restyle ( gd , { orientation : 'v' } ) ;
1230+ } )
1231+ . then ( function ( ) {
1232+ checkTicks ( 'x' , [ 'a' , 'b' , 'c' ] , 'explicit v x' ) ;
1233+ checkTicks ( 'y' , [ '0' , '0.5' , '1' , '1.5' , '2' , '2.5' , '3' ] , 'explicit v y' ) ;
1234+
1235+ checkBarsMatch ( [ 'bottom' , 'width' ] , 'explicit v' ) ;
1236+
1237+ // back to implicit v
1238+ return Plotly . restyle ( gd , { orientation : null } ) ;
1239+ } )
1240+ . then ( function ( ) {
1241+ checkTicks ( 'x' , [ 'a' , 'b' , 'c' ] , 'implicit v x' ) ;
1242+ checkTicks ( 'y' , [ '0' , '0.5' , '1' , '1.5' , '2' , '2.5' , '3' ] , 'implicit v y' ) ;
1243+
1244+ checkBarsMatch ( [ 'bottom' , 'width' ] , 'implicit v' ) ;
1245+
1246+ return Plotly . restyle ( gd , { orientation : 'h' } ) ;
1247+ } )
1248+ . then ( function ( ) {
1249+ checkTicks ( 'x' , [ '0' , '1' , '2' , '3' ] , 'h x' ) ;
1250+ checkTicks ( 'y' , [ 'a' , 'b' , 'c' ] , 'h y' ) ;
1251+
1252+ checkBarsMatch ( [ 'left' , 'height' ] , 'initial' ) ;
1253+
1254+ return Plotly . restyle ( gd , { orientation : 'v' } ) ;
1255+ } )
1256+ . then ( function ( ) {
1257+ checkTicks ( 'x' , [ 'a' , 'b' , 'c' ] , 'final x' ) ;
1258+ checkTicks ( 'y' , [ '0' , '0.5' , '1' , '1.5' , '2' , '2.5' , '3' ] , 'final y' ) ;
1259+
1260+ checkBarsMatch ( [ 'bottom' , 'width' ] , 'final' ) ;
1261+ } )
1262+ . catch ( fail )
1263+ . then ( done ) ;
1264+ } ) ;
11971265} ) ;
11981266
11991267describe ( 'bar hover' , function ( ) {
0 commit comments