99
1010'use strict' ;
1111
12+ var d3 = require ( 'd3' ) ;
1213var Plotly = require ( '../plotly' ) ;
1314var Registry = require ( '../registry' ) ;
1415var Plots = require ( '../plots/plots' ) ;
@@ -24,6 +25,21 @@ exports.layoutStyles = function(gd) {
2425 return Lib . syncOrAsync ( [ Plots . doAutoMargin , exports . lsInner ] , gd ) ;
2526} ;
2627
28+ function overlappingDomain ( xDomain , yDomain , domains ) {
29+ for ( var i = 0 ; i < domains . length ; i ++ ) {
30+ var existingX = domains [ i ] [ 0 ] ,
31+ existingY = domains [ i ] [ 1 ] ;
32+
33+ if ( existingX [ 0 ] >= xDomain [ 1 ] || existingX [ 1 ] <= xDomain [ 0 ] ) {
34+ continue ;
35+ }
36+ if ( existingY [ 0 ] < yDomain [ 1 ] && existingY [ 1 ] > yDomain [ 0 ] ) {
37+ return true ;
38+ }
39+ }
40+ return false ;
41+ }
42+
2743exports . lsInner = function ( gd ) {
2844 var fullLayout = gd . _fullLayout ,
2945 gs = fullLayout . _size ,
@@ -43,8 +59,73 @@ exports.lsInner = function(gd) {
4359
4460 gd . _context . setBackground ( gd , fullLayout . paper_bgcolor ) ;
4561
62+ var subplotSelection = fullLayout . _paper . selectAll ( 'g.subplot' ) ;
63+
64+ // figure out which backgrounds we need to draw, and in which layers
65+ // to put them
66+ var lowerBackgroundIDs = [ ] ;
67+ var lowerDomains = [ ] ;
68+ subplotSelection . each ( function ( subplot ) {
69+ var plotinfo = fullLayout . _plots [ subplot ] ;
70+
71+ if ( plotinfo . mainplot ) {
72+ // mainplot is a reference to the main plot this one is overlaid on
73+ // so if it exists, this is an overlaid plot and we don't need to
74+ // give it its own background
75+ if ( plotinfo . bg ) {
76+ plotinfo . bg . remove ( ) ;
77+ }
78+ plotinfo . bg = undefined ;
79+ return ;
80+ }
81+
82+ var xa = Plotly . Axes . getFromId ( gd , subplot , 'x' ) ,
83+ ya = Plotly . Axes . getFromId ( gd , subplot , 'y' ) ,
84+ xDomain = xa . domain ,
85+ yDomain = ya . domain ,
86+ plotgroupBgData = [ ] ;
87+
88+ if ( overlappingDomain ( xDomain , yDomain , lowerDomains ) ) {
89+ plotgroupBgData = [ 0 ] ;
90+ }
91+ else {
92+ lowerBackgroundIDs . push ( subplot ) ;
93+ lowerDomains . push ( [ xDomain , yDomain ] ) ;
94+ }
95+
96+ // create the plot group backgrounds now, since
97+ // they're all independent selections
98+ var plotgroupBg = plotinfo . plotgroup . selectAll ( '.bg' )
99+ . data ( plotgroupBgData ) ;
100+
101+ plotgroupBg . enter ( ) . append ( 'rect' )
102+ . classed ( 'bg' , true ) ;
103+
104+ plotgroupBg . exit ( ) . remove ( ) ;
105+
106+ plotgroupBg . each ( function ( ) {
107+ plotinfo . bg = plotgroupBg ;
108+ var pgNode = plotinfo . plotgroup . node ( ) ;
109+ pgNode . insertBefore ( this , pgNode . childNodes [ 0 ] ) ;
110+ } ) ;
111+ } ) ;
112+
113+ // now create all the lower-layer backgrounds at once now that
114+ // we have the list of subplots that need them
115+ var lowerBackgrounds = fullLayout . _bgLayer . selectAll ( '.bg' )
116+ . data ( lowerBackgroundIDs ) ;
117+
118+ lowerBackgrounds . enter ( ) . append ( 'rect' )
119+ . classed ( 'bg' , true ) ;
120+
121+ lowerBackgrounds . exit ( ) . remove ( ) ;
122+
123+ lowerBackgrounds . each ( function ( subplot ) {
124+ fullLayout . _plots [ subplot ] . bg = d3 . select ( this ) ;
125+ } ) ;
126+
46127 var freefinished = [ ] ;
47- fullLayout . _paper . selectAll ( 'g.subplot' ) . each ( function ( subplot ) {
128+ subplotSelection . each ( function ( subplot ) {
48129 var plotinfo = fullLayout . _plots [ subplot ] ,
49130 xa = Plotly . Axes . getFromId ( gd , subplot , 'x' ) ,
50131 ya = Plotly . Axes . getFromId ( gd , subplot , 'y' ) ;
@@ -58,7 +139,8 @@ exports.lsInner = function(gd) {
58139 . call ( Drawing . setRect ,
59140 xa . _offset - gs . p , ya . _offset - gs . p ,
60141 xa . _length + 2 * gs . p , ya . _length + 2 * gs . p )
61- . call ( Color . fill , fullLayout . plot_bgcolor ) ;
142+ . call ( Color . fill , fullLayout . plot_bgcolor )
143+ . style ( 'stroke-width' , 0 ) ;
62144 }
63145
64146 // Clip so that data only shows up on the plot area.
0 commit comments