@@ -12,13 +12,17 @@ var destroyGraphDiv = require('../assets/destroy_graph_div');
12
12
var failTest = require ( '../assets/fail_test' ) ;
13
13
var checkTicks = require ( '../assets/custom_assertions' ) . checkTicks ;
14
14
var supplyAllDefaults = require ( '../assets/supply_defaults' ) ;
15
+ var color = require ( '../../../src/components/color' ) ;
16
+ var rgb = color . rgb ;
15
17
16
18
var customAssertions = require ( '../assets/custom_assertions' ) ;
17
19
var assertClip = customAssertions . assertClip ;
18
20
var assertNodeDisplay = customAssertions . assertNodeDisplay ;
19
21
20
22
var d3 = require ( 'd3' ) ;
21
23
24
+ var BAR_TEXT_SELECTOR = '.bars .bartext' ;
25
+
22
26
describe ( 'Bar.supplyDefaults' , function ( ) {
23
27
'use strict' ;
24
28
@@ -122,28 +126,58 @@ describe('Bar.supplyDefaults', function() {
122
126
expect ( traceOut . constraintext ) . toBeUndefined ( ) ;
123
127
} ) ;
124
128
125
- it ( 'should default textfont to layout.font' , function ( ) {
129
+ it ( 'should default textfont to layout.font except for insidetextfont.color ' , function ( ) {
126
130
traceIn = {
127
131
textposition : 'inside' ,
128
132
y : [ 1 , 2 , 3 ]
129
133
} ;
130
-
131
134
var layout = {
132
135
font : { family : 'arial' , color : '#AAA' , size : 13 }
133
136
} ;
137
+ var layoutFontMinusColor = { family : 'arial' , size : 13 } ;
134
138
135
139
supplyDefaults ( traceIn , traceOut , defaultColor , layout ) ;
136
140
137
141
expect ( traceOut . textposition ) . toBe ( 'inside' ) ;
138
142
expect ( traceOut . textfont ) . toEqual ( layout . font ) ;
139
143
expect ( traceOut . textfont ) . not . toBe ( layout . font ) ;
140
- expect ( traceOut . insidetextfont ) . toEqual ( layout . font ) ;
144
+ expect ( traceOut . insidetextfont ) . toEqual ( layoutFontMinusColor ) ;
141
145
expect ( traceOut . insidetextfont ) . not . toBe ( layout . font ) ;
142
146
expect ( traceOut . insidetextfont ) . not . toBe ( traceOut . textfont ) ;
143
147
expect ( traceOut . outsidetexfont ) . toBeUndefined ( ) ;
144
148
expect ( traceOut . constraintext ) . toBe ( 'both' ) ;
145
149
} ) ;
146
150
151
+ it ( 'should not default insidetextfont.color to layout.font.color' , function ( ) {
152
+ traceIn = {
153
+ textposition : 'inside' ,
154
+ y : [ 1 , 2 , 3 ]
155
+ } ;
156
+ var layout = {
157
+ font : { family : 'arial' , color : '#AAA' , size : 13 }
158
+ } ;
159
+
160
+ supplyDefaults ( traceIn , traceOut , defaultColor , layout ) ;
161
+
162
+ expect ( traceOut . insidetextfont . family ) . toBe ( 'arial' ) ;
163
+ expect ( traceOut . insidetextfont . color ) . toBeUndefined ( ) ;
164
+ expect ( traceOut . insidetextfont . size ) . toBe ( 13 ) ;
165
+ } ) ;
166
+
167
+ it ( 'should default insidetextfont.color to textfont.color' , function ( ) {
168
+ traceIn = {
169
+ textposition : 'inside' ,
170
+ y : [ 1 , 2 , 3 ] ,
171
+ textfont : { family : 'arial' , color : '#09F' , size : 20 }
172
+ } ;
173
+
174
+ supplyDefaults ( traceIn , traceOut , defaultColor , { } ) ;
175
+
176
+ expect ( traceOut . insidetextfont . family ) . toBe ( 'arial' ) ;
177
+ expect ( traceOut . insidetextfont . color ) . toBe ( '#09F' ) ;
178
+ expect ( traceOut . insidetextfont . size ) . toBe ( 20 ) ;
179
+ } ) ;
180
+
147
181
it ( 'should inherit layout.calendar' , function ( ) {
148
182
traceIn = {
149
183
x : [ 1 , 2 , 3 ] ,
@@ -802,6 +836,9 @@ describe('Bar.crossTraceCalc (formerly known as setPositions)', function() {
802
836
describe ( 'A bar plot' , function ( ) {
803
837
'use strict' ;
804
838
839
+ var DARK = rgb ( '#444' ) ;
840
+ var LIGHT = rgb ( '#fff' ) ;
841
+
805
842
var gd ;
806
843
807
844
beforeEach ( function ( ) {
@@ -849,19 +886,13 @@ describe('A bar plot', function() {
849
886
expect ( pathBB . right ) . not . toBeGreaterThan ( textBB . left ) ;
850
887
}
851
888
852
- var colorMap = {
853
- 'rgb(0, 0, 0)' : 'black' ,
854
- 'rgb(255, 0, 0)' : 'red' ,
855
- 'rgb(0, 128, 0)' : 'green' ,
856
- 'rgb(0, 0, 255)' : 'blue'
857
- } ;
858
- function assertTextFont ( textNode , textFont , index ) {
859
- expect ( textNode . style . fontFamily ) . toBe ( textFont . family [ index ] ) ;
860
- expect ( textNode . style . fontSize ) . toBe ( textFont . size [ index ] + 'px' ) ;
889
+ function assertTextFont ( textNode , expectedFontProps , index ) {
890
+ expect ( textNode . style . fontFamily ) . toBe ( expectedFontProps . family [ index ] ) ;
891
+ expect ( textNode . style . fontSize ) . toBe ( expectedFontProps . size [ index ] + 'px' ) ;
861
892
862
- var color = textNode . style . fill ;
863
- if ( ! colorMap [ color ] ) colorMap [ color ] = color ;
864
- expect ( colorMap [ color ] ) . toBe ( textFont . color [ index ] ) ;
893
+ var actualColorRGB = textNode . style . fill ;
894
+ var expectedColorRGB = rgb ( expectedFontProps . color [ index ] ) ;
895
+ expect ( actualColorRGB ) . toBe ( expectedColorRGB ) ;
865
896
}
866
897
867
898
function assertTextIsBeforePath ( textNode , pathNode ) {
@@ -871,6 +902,36 @@ describe('A bar plot', function() {
871
902
expect ( textBB . right ) . not . toBeGreaterThan ( pathBB . left ) ;
872
903
}
873
904
905
+ function assertTextFontColors ( expFontColors ) {
906
+ return function ( ) {
907
+ var selection = d3 . selectAll ( BAR_TEXT_SELECTOR ) ;
908
+ expect ( selection . size ( ) ) . toBe ( expFontColors . length ) ;
909
+ selection . each ( function ( d , i ) {
910
+ expect ( this . style . fill ) . toBe ( expFontColors [ i ] , 'element ' + i ) ;
911
+ } ) ;
912
+ } ;
913
+ }
914
+
915
+ function assertTextFontFamilies ( expFontFamilies ) {
916
+ return function ( ) {
917
+ var selection = d3 . selectAll ( BAR_TEXT_SELECTOR ) ;
918
+ expect ( selection . size ( ) ) . toBe ( expFontFamilies . length ) ;
919
+ selection . each ( function ( d , i ) {
920
+ expect ( this . style . fontFamily ) . toBe ( expFontFamilies [ i ] ) ;
921
+ } ) ;
922
+ } ;
923
+ }
924
+
925
+ function assertTextFontSizes ( expFontSizes ) {
926
+ return function ( ) {
927
+ var selection = d3 . selectAll ( BAR_TEXT_SELECTOR ) ;
928
+ expect ( selection . size ( ) ) . toBe ( expFontSizes . length ) ;
929
+ selection . each ( function ( d , i ) {
930
+ expect ( this . style . fontSize ) . toBe ( expFontSizes [ i ] + 'px' ) ;
931
+ } ) ;
932
+ } ;
933
+ }
934
+
874
935
it ( 'should show bar texts (inside case)' , function ( done ) {
875
936
var data = [ {
876
937
y : [ 10 , 20 , 30 ] ,
@@ -999,6 +1060,84 @@ describe('A bar plot', function() {
999
1060
. then ( done ) ;
1000
1061
} ) ;
1001
1062
1063
+ var insideTextTestsTraceDef = {
1064
+ x : [ 'giraffes' , 'orangutans' , 'monkeys' , 'elefants' , 'spiders' , 'snakes' ] ,
1065
+ y : [ 20 , 14 , 23 , 10 , 59 , 15 ] ,
1066
+ text : [ 20 , 14 , 23 , 10 , 59 , 15 ] ,
1067
+ type : 'bar' ,
1068
+ textposition : 'auto' ,
1069
+ marker : {
1070
+ color : [ '#ee1' , '#eee' , '#333' , '#9467bd' , '#dda' , '#922' ] ,
1071
+ }
1072
+ } ;
1073
+
1074
+ it ( 'should use inside text colors contrasting to bar colors by default' , function ( done ) {
1075
+ Plotly . plot ( gd , [ insideTextTestsTraceDef ] )
1076
+ . then ( assertTextFontColors ( [ DARK , DARK , LIGHT , LIGHT , DARK , LIGHT ] ) )
1077
+ . catch ( failTest )
1078
+ . then ( done ) ;
1079
+ } ) ;
1080
+
1081
+ it ( 'should use defined textfont.color for inside text instead of the contrasting default' , function ( done ) {
1082
+ var data = Lib . extendFlat ( { } , insideTextTestsTraceDef , { textfont : { color : '#09f' } } ) ;
1083
+
1084
+ Plotly . plot ( gd , [ data ] )
1085
+ . then ( assertTextFontColors ( Lib . repeat ( rgb ( '#09f' ) , 6 ) ) )
1086
+ . catch ( failTest )
1087
+ . then ( done ) ;
1088
+ } ) ;
1089
+
1090
+ it ( 'should use matching color from textfont.color array for inside text, contrasting otherwise' , function ( done ) {
1091
+ var data = Lib . extendFlat ( { } , insideTextTestsTraceDef , { textfont : { color : [ '#09f' , 'green' ] } } ) ;
1092
+
1093
+ Plotly . plot ( gd , [ data ] )
1094
+ . then ( assertTextFontColors ( [ rgb ( '#09f' ) , rgb ( 'green' ) , LIGHT , LIGHT , DARK , LIGHT ] ) )
1095
+ . catch ( failTest )
1096
+ . then ( done ) ;
1097
+ } ) ;
1098
+
1099
+ it ( 'should use defined insidetextfont.color for inside text instead of the contrasting default' , function ( done ) {
1100
+ var data = Lib . extendFlat ( { } , insideTextTestsTraceDef , { insidetextfont : { color : '#09f' } } ) ;
1101
+
1102
+ Plotly . plot ( gd , [ data ] )
1103
+ . then ( assertTextFontColors ( Lib . repeat ( rgb ( '#09f' ) , 6 ) ) )
1104
+ . catch ( failTest )
1105
+ . then ( done ) ;
1106
+ } ) ;
1107
+
1108
+ it ( 'should use matching color from insidetextfont.color array instead of the contrasting default' , function ( done ) {
1109
+ var data = Lib . extendFlat ( { } , insideTextTestsTraceDef , { insidetextfont : { color : [ 'yellow' , 'green' ] } } ) ;
1110
+
1111
+ Plotly . plot ( gd , [ data ] )
1112
+ . then ( assertTextFontColors ( [ rgb ( 'yellow' ) , rgb ( 'green' ) , LIGHT , LIGHT , DARK , LIGHT ] ) )
1113
+ . catch ( failTest )
1114
+ . then ( done ) ;
1115
+ } ) ;
1116
+
1117
+ it ( 'should fall back to textfont array values if insidetextfont array values don\'t ' +
1118
+ 'cover all bars' , function ( done ) {
1119
+ var trace = Lib . extendFlat ( { } , insideTextTestsTraceDef , {
1120
+ textfont : {
1121
+ color : [ 'blue' , 'blue' , 'blue' ] ,
1122
+ family : [ 'Arial' , 'serif' ] ,
1123
+ size : [ 8 , 24 ]
1124
+ } ,
1125
+ insidetextfont : {
1126
+ color : [ 'yellow' , 'green' ] ,
1127
+ family : [ 'Arial' ] ,
1128
+ size : [ 16 ]
1129
+ }
1130
+ } ) ;
1131
+ var layout = { font : { family : 'Roboto' , size : 12 } } ;
1132
+
1133
+ Plotly . plot ( gd , [ trace ] , layout )
1134
+ . then ( assertTextFontColors ( [ rgb ( 'yellow' ) , rgb ( 'green' ) , rgb ( 'blue' ) , LIGHT , DARK , LIGHT ] ) )
1135
+ . then ( assertTextFontFamilies ( [ 'Arial' , 'serif' , 'Roboto' , 'Roboto' , 'Roboto' , 'Roboto' ] ) )
1136
+ . then ( assertTextFontSizes ( [ 16 , 24 , 12 , 12 , 12 , 12 ] ) )
1137
+ . catch ( failTest )
1138
+ . then ( done ) ;
1139
+ } ) ;
1140
+
1002
1141
it ( 'should be able to restyle' , function ( done ) {
1003
1142
var mock = Lib . extendDeep ( { } , require ( '@mocks/bar_attrs_relative' ) ) ;
1004
1143
@@ -1170,6 +1309,9 @@ describe('A bar plot', function() {
1170
1309
font : { family : 'arial' , color : 'blue' , size : 13 }
1171
1310
} ;
1172
1311
1312
+ // Note: insidetextfont.color does NOT inherit from textfont.color
1313
+ // since insidetextfont.color should be contrasting to bar's fill by default.
1314
+ var contrastingLightColorVal = color . contrast ( 'black' ) ;
1173
1315
var expected = {
1174
1316
y : [ 10 , 20 , 30 , 40 ] ,
1175
1317
type : 'bar' ,
@@ -1182,7 +1324,7 @@ describe('A bar plot', function() {
1182
1324
} ,
1183
1325
insidetextfont : {
1184
1326
family : [ '"comic sans"' , 'arial' , 'arial' ] ,
1185
- color : [ 'black' , 'green' , 'blue' ] ,
1327
+ color : [ 'black' , 'green' , contrastingLightColorVal ] ,
1186
1328
size : [ 8 , 12 , 16 ]
1187
1329
} ,
1188
1330
outsidetextfont : {
0 commit comments