9
9
10
10
'use strict' ;
11
11
12
+ var d3 = require ( 'd3' ) ;
12
13
var Plotly = require ( '../plotly' ) ;
13
14
var Registry = require ( '../registry' ) ;
14
15
var Plots = require ( '../plots/plots' ) ;
@@ -24,6 +25,21 @@ exports.layoutStyles = function(gd) {
24
25
return Lib . syncOrAsync ( [ Plots . doAutoMargin , exports . lsInner ] , gd ) ;
25
26
} ;
26
27
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
+
27
43
exports . lsInner = function ( gd ) {
28
44
var fullLayout = gd . _fullLayout ,
29
45
gs = fullLayout . _size ,
@@ -43,8 +59,73 @@ exports.lsInner = function(gd) {
43
59
44
60
gd . _context . setBackground ( gd , fullLayout . paper_bgcolor ) ;
45
61
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
+
46
127
var freefinished = [ ] ;
47
- fullLayout . _paper . selectAll ( 'g.subplot' ) . each ( function ( subplot ) {
128
+ subplotSelection . each ( function ( subplot ) {
48
129
var plotinfo = fullLayout . _plots [ subplot ] ,
49
130
xa = Plotly . Axes . getFromId ( gd , subplot , 'x' ) ,
50
131
ya = Plotly . Axes . getFromId ( gd , subplot , 'y' ) ;
@@ -58,7 +139,8 @@ exports.lsInner = function(gd) {
58
139
. call ( Drawing . setRect ,
59
140
xa . _offset - gs . p , ya . _offset - gs . p ,
60
141
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 ) ;
62
144
}
63
145
64
146
// Clip so that data only shows up on the plot area.
0 commit comments