@@ -33,12 +33,81 @@ describe('legend defaults', function() {
3333 } ;
3434 } ) ;
3535
36+ function allShown ( fullData ) {
37+ return fullData . map ( function ( trace ) {
38+ return Lib . extendDeep ( {
39+ visible : true ,
40+ showlegend : true ,
41+ _dfltShowLegend : true ,
42+ _input : { }
43+ } , trace ) ;
44+ } ) ;
45+ }
46+
47+ it ( 'hides by default if there is only one legend item by default' , function ( ) {
48+ fullData = allShown ( [
49+ { type : 'scatter' } ,
50+ { type : 'scatter' , visible : false } , // ignored
51+ { type : 'contour' , _dfltShowLegend : false , showlegend : false } // hidden by default
52+ ] ) ;
53+
54+ supplyLayoutDefaults ( { } , layoutOut , fullData ) ;
55+ expect ( layoutOut . showlegend ) . toBe ( false ) ;
56+ } ) ;
57+
58+ it ( 'shows if there are two legend items by default but only one is shown' , function ( ) {
59+ fullData = allShown ( [
60+ { type : 'scatter' } ,
61+ { type : 'scatter' , showlegend : false } // not shown but still triggers legend
62+ ] ) ;
63+
64+ supplyLayoutDefaults ( { } , layoutOut , fullData ) ;
65+ expect ( layoutOut . showlegend ) . toBe ( true ) ;
66+ } ) ;
67+
68+ it ( 'hides if no items are actually shown' , function ( ) {
69+ fullData = allShown ( [
70+ { type : 'scatter' , showlegend : false } ,
71+ { type : 'scatter' , showlegend : false }
72+ ] ) ;
73+
74+ supplyLayoutDefaults ( { } , layoutOut , fullData ) ;
75+ expect ( layoutOut . showlegend ) . toBe ( false ) ;
76+ } ) ;
77+
78+ it ( 'shows with one visible pie' , function ( ) {
79+ fullData = allShown ( [
80+ { type : 'pie' }
81+ ] ) ;
82+
83+ supplyLayoutDefaults ( { } , layoutOut , fullData ) ;
84+ expect ( layoutOut . showlegend ) . toBe ( true ) ;
85+ } ) ;
86+
87+ it ( 'does not show with a hidden pie' , function ( ) {
88+ fullData = allShown ( [
89+ { type : 'pie' , showlegend : false }
90+ ] ) ;
91+
92+ supplyLayoutDefaults ( { } , layoutOut , fullData ) ;
93+ expect ( layoutOut . showlegend ) . toBe ( false ) ;
94+ } ) ;
95+
96+ it ( 'shows if even a default hidden single item is explicitly shown' , function ( ) {
97+ fullData = allShown ( [
98+ { type : 'contour' , _dfltShowLegend : false , _input : { showlegend : true } }
99+ ] ) ;
100+
101+ supplyLayoutDefaults ( { } , layoutOut , fullData ) ;
102+ expect ( layoutOut . showlegend ) . toBe ( true ) ;
103+ } ) ;
104+
36105 it ( 'should default traceorder to reversed for stack bar charts' , function ( ) {
37- fullData = [
38- { type : 'bar' } ,
39- { type : 'bar' } ,
40- { type : 'scatter' }
41- ] ;
106+ fullData = allShown ( [
107+ { type : 'bar' , visible : 'legendonly' } ,
108+ { type : 'bar' , visible : 'legendonly' } ,
109+ { type : 'scatter' }
110+ ] ) ;
42111
43112 supplyLayoutDefaults ( layoutIn , layoutOut , fullData ) ;
44113 expect ( layoutOut . legend . traceorder ) . toEqual ( 'normal' ) ;
@@ -50,20 +119,20 @@ describe('legend defaults', function() {
50119 } ) ;
51120
52121 it ( 'should default traceorder to reversed for filled tonext scatter charts' , function ( ) {
53- fullData = [
54- { type : 'scatter' } ,
55- { type : 'scatter' , fill : 'tonexty' }
56- ] ;
122+ fullData = allShown ( [
123+ { type : 'scatter' } ,
124+ { type : 'scatter' , fill : 'tonexty' }
125+ ] ) ;
57126
58127 supplyLayoutDefaults ( layoutIn , layoutOut , fullData ) ;
59128 expect ( layoutOut . legend . traceorder ) . toEqual ( 'reversed' ) ;
60129 } ) ;
61130
62131 it ( 'should default traceorder to grouped when a group is present' , function ( ) {
63- fullData = [
64- { type : 'scatter' , legendgroup : 'group' } ,
65- { type : 'scatter' }
66- ] ;
132+ fullData = allShown ( [
133+ { type : 'scatter' , legendgroup : 'group' } ,
134+ { type : 'scatter' }
135+ ] ) ;
67136
68137 supplyLayoutDefaults ( layoutIn , layoutOut , fullData ) ;
69138 expect ( layoutOut . legend . traceorder ) . toEqual ( 'grouped' ) ;
@@ -74,6 +143,27 @@ describe('legend defaults', function() {
74143 expect ( layoutOut . legend . traceorder ) . toEqual ( 'grouped+reversed' ) ;
75144 } ) ;
76145
146+ it ( 'does not consider invisible traces for traceorder default' , function ( ) {
147+ fullData = allShown ( [
148+ { type : 'bar' , visible : false } ,
149+ { type : 'bar' , visible : false } ,
150+ { type : 'scatter' }
151+ ] ) ;
152+
153+ layoutOut . barmode = 'stack' ;
154+
155+ supplyLayoutDefaults ( layoutIn , layoutOut , fullData ) ;
156+ expect ( layoutOut . legend . traceorder ) . toEqual ( 'normal' ) ;
157+
158+ fullData = allShown ( [
159+ { type : 'scatter' , legendgroup : 'group' , visible : false } ,
160+ { type : 'scatter' }
161+ ] ) ;
162+
163+ supplyLayoutDefaults ( layoutIn , layoutOut , fullData ) ;
164+ expect ( layoutOut . legend . traceorder ) . toEqual ( 'normal' ) ;
165+ } ) ;
166+
77167 it ( 'should default orientation to vertical' , function ( ) {
78168 supplyLayoutDefaults ( layoutIn , layoutOut , [ ] ) ;
79169 expect ( layoutOut . legend . orientation ) . toEqual ( 'v' ) ;
@@ -382,24 +472,6 @@ describe('legend getLegendData', function() {
382472describe ( 'legend helpers:' , function ( ) {
383473 'use strict' ;
384474
385- describe ( 'legendGetsTraces' , function ( ) {
386- var legendGetsTrace = helpers . legendGetsTrace ;
387-
388- it ( 'should return true when trace is visible and supports legend' , function ( ) {
389- expect ( legendGetsTrace ( { visible : true , showlegend : true } ) ) . toBe ( true ) ;
390- expect ( legendGetsTrace ( { visible : false , showlegend : true } ) ) . toBe ( false ) ;
391- expect ( legendGetsTrace ( { visible : 'legendonly' , showlegend : true } ) ) . toBe ( true ) ;
392-
393- expect ( legendGetsTrace ( { visible : true , showlegend : false } ) ) . toBe ( true ) ;
394- expect ( legendGetsTrace ( { visible : false , showlegend : false } ) ) . toBe ( false ) ;
395- expect ( legendGetsTrace ( { visible : 'legendonly' , showlegend : false } ) ) . toBe ( true ) ;
396-
397- expect ( legendGetsTrace ( { visible : true } ) ) . toBe ( false ) ;
398- expect ( legendGetsTrace ( { visible : false } ) ) . toBe ( false ) ;
399- expect ( legendGetsTrace ( { visible : 'legendonly' } ) ) . toBe ( false ) ;
400- } ) ;
401- } ) ;
402-
403475 describe ( 'isGrouped' , function ( ) {
404476 var isGrouped = helpers . isGrouped ;
405477
0 commit comments