@@ -15,25 +15,20 @@ var tinycolor = require('tinycolor2');
15
15
var Color = require ( '../../components/color' ) ;
16
16
var helpers = require ( './helpers' ) ;
17
17
18
- module . exports = function calc ( gd , trace ) {
18
+ exports . calc = function calc ( gd , trace ) {
19
19
var vals = trace . values ;
20
20
var hasVals = isArrayOrTypedArray ( vals ) && vals . length ;
21
21
var labels = trace . labels ;
22
22
var colors = trace . marker . colors || [ ] ;
23
23
var cd = [ ] ;
24
24
var fullLayout = gd . _fullLayout ;
25
- var colorWay = fullLayout . colorway ;
26
25
var colorMap = fullLayout . _piecolormap ;
27
26
var allThisTraceLabels = { } ;
28
27
var vTotal = 0 ;
29
28
var hiddenLabels = fullLayout . hiddenlabels || [ ] ;
30
29
31
30
var i , v , label , hidden , pt ;
32
31
33
- if ( ! fullLayout . _piecolorway && colorWay !== Color . defaults ) {
34
- fullLayout . _piecolorway = generateDefaultColors ( colorWay ) ;
35
- }
36
-
37
32
if ( trace . dlabel ) {
38
33
labels = new Array ( vals . length ) ;
39
34
for ( i = 0 ; i < vals . length ; i ++ ) {
@@ -79,7 +74,7 @@ module.exports = function calc(gd, trace) {
79
74
cd . push ( {
80
75
v : v ,
81
76
label : label ,
82
- color : pullColor ( colors [ i ] ) ,
77
+ color : pullColor ( colors [ i ] , label ) ,
83
78
i : i ,
84
79
pts : [ i ] ,
85
80
hidden : hidden
@@ -99,29 +94,6 @@ module.exports = function calc(gd, trace) {
99
94
100
95
if ( trace . sort ) cd . sort ( function ( a , b ) { return b . v - a . v ; } ) ;
101
96
102
- /**
103
- * now go back and fill in colors we're still missing
104
- * this is done after sorting, so we pick defaults
105
- * in the order slices will be displayed
106
- */
107
-
108
- for ( i = 0 ; i < cd . length ; i ++ ) {
109
- pt = cd [ i ] ;
110
- if ( pt . color === false ) {
111
- // have we seen this label and assigned a color to it in a previous trace?
112
- if ( colorMap [ pt . label ] ) {
113
- pt . color = colorMap [ pt . label ] ;
114
- }
115
- else {
116
- colorMap [ pt . label ] = pt . color = nextDefaultColor (
117
- fullLayout . _piedefaultcolorcount ,
118
- fullLayout . _piecolorway
119
- ) ;
120
- fullLayout . _piedefaultcolorcount ++ ;
121
- }
122
- }
123
- }
124
-
125
97
// include the sum of all values in the first point
126
98
if ( cd [ 0 ] ) cd [ 0 ] . vTotal = vTotal ;
127
99
@@ -151,34 +123,66 @@ module.exports = function calc(gd, trace) {
151
123
return cd ;
152
124
} ;
153
125
154
- /**
155
- * pick a default color from the main default set, augmented by
156
- * itself lighter then darker before repeating
126
+ /*
127
+ * `calc` filled in (and collated) explicit colors.
128
+ * Now we need to propagate these explicit colors to other traces,
129
+ * and fill in default colors.
130
+ * This is done after sorting, so we pick defaults
131
+ * in the order slices will be displayed
157
132
*/
158
- var pieDefaultColors ;
133
+ exports . crossTraceCalc = function ( gd ) {
134
+ var fullLayout = gd . _fullLayout ;
135
+ var calcdata = gd . calcdata ;
136
+ var pieColorWay = fullLayout . piecolorway ;
137
+ var colorMap = fullLayout . _piecolormap ;
159
138
160
- function nextDefaultColor ( index , pieColorWay ) {
161
- if ( ! pieDefaultColors ) {
162
- // generate this default set on demand (but then it gets saved in the module)
163
- var mainDefaults = Color . defaults ;
164
- pieDefaultColors = generateDefaultColors ( mainDefaults ) ;
139
+ if ( fullLayout . extendpiecolors ) {
140
+ pieColorWay = generateExtendedColors ( pieColorWay ) ;
165
141
}
142
+ var dfltColorCount = 0 ;
143
+
144
+ var i , j , cd , pt ;
145
+ for ( i = 0 ; i < calcdata . length ; i ++ ) {
146
+ cd = calcdata [ i ] ;
147
+ if ( cd [ 0 ] . trace . type !== 'pie' ) continue ;
148
+
149
+ for ( j = 0 ; j < cd . length ; j ++ ) {
150
+ pt = cd [ j ] ;
151
+ if ( pt . color === false ) {
152
+ // have we seen this label and assigned a color to it in a previous trace?
153
+ if ( colorMap [ pt . label ] ) {
154
+ pt . color = colorMap [ pt . label ] ;
155
+ }
156
+ else {
157
+ colorMap [ pt . label ] = pt . color = pieColorWay [ dfltColorCount % pieColorWay . length ] ;
158
+ dfltColorCount ++ ;
159
+ }
160
+ }
161
+ }
162
+ }
163
+ } ;
166
164
167
- var pieColors = pieColorWay || pieDefaultColors ;
168
- return pieColors [ index % pieColors . length ] ;
169
- }
165
+ /**
166
+ * pick a default color from the main default set, augmented by
167
+ * itself lighter then darker before repeating
168
+ */
169
+ var extendedColorWays = { } ;
170
170
171
- function generateDefaultColors ( colorList ) {
171
+ function generateExtendedColors ( colorList ) {
172
172
var i ;
173
+ var colorString = JSON . stringify ( colorList ) ;
174
+ var pieColors = extendedColorWays [ colorString ] ;
175
+ if ( ! pieColors ) {
176
+ pieColors = colorList . slice ( ) ;
173
177
174
- var pieColors = colorList . slice ( ) ;
175
-
176
- for ( i = 0 ; i < colorList . length ; i ++ ) {
177
- pieColors . push ( tinycolor ( colorList [ i ] ) . lighten ( 20 ) . toHexString ( ) ) ;
178
- }
178
+ for ( i = 0 ; i < colorList . length ; i ++ ) {
179
+ pieColors . push ( tinycolor ( colorList [ i ] ) . lighten ( 20 ) . toHexString ( ) ) ;
180
+ }
179
181
180
- for ( i = 0 ; i < colorList . length ; i ++ ) {
181
- pieColors . push ( tinycolor ( colorList [ i ] ) . darken ( 20 ) . toHexString ( ) ) ;
182
+ for ( i = 0 ; i < colorList . length ; i ++ ) {
183
+ pieColors . push ( tinycolor ( colorList [ i ] ) . darken ( 20 ) . toHexString ( ) ) ;
184
+ }
185
+ extendedColorWays [ colorString ] = pieColors ;
182
186
}
183
187
184
188
return pieColors ;
0 commit comments