@@ -15,25 +15,20 @@ var tinycolor = require('tinycolor2');
1515var Color = require ( '../../components/color' ) ;
1616var helpers = require ( './helpers' ) ;
1717
18- module . exports = function calc ( gd , trace ) {
18+ exports . calc = function calc ( gd , trace ) {
1919 var vals = trace . values ;
2020 var hasVals = isArrayOrTypedArray ( vals ) && vals . length ;
2121 var labels = trace . labels ;
2222 var colors = trace . marker . colors || [ ] ;
2323 var cd = [ ] ;
2424 var fullLayout = gd . _fullLayout ;
25- var colorWay = fullLayout . colorway ;
2625 var colorMap = fullLayout . _piecolormap ;
2726 var allThisTraceLabels = { } ;
2827 var vTotal = 0 ;
2928 var hiddenLabels = fullLayout . hiddenlabels || [ ] ;
3029
3130 var i , v , label , hidden , pt ;
3231
33- if ( ! fullLayout . _piecolorway && colorWay !== Color . defaults ) {
34- fullLayout . _piecolorway = generateDefaultColors ( colorWay ) ;
35- }
36-
3732 if ( trace . dlabel ) {
3833 labels = new Array ( vals . length ) ;
3934 for ( i = 0 ; i < vals . length ; i ++ ) {
@@ -79,7 +74,7 @@ module.exports = function calc(gd, trace) {
7974 cd . push ( {
8075 v : v ,
8176 label : label ,
82- color : pullColor ( colors [ i ] ) ,
77+ color : pullColor ( colors [ i ] , label ) ,
8378 i : i ,
8479 pts : [ i ] ,
8580 hidden : hidden
@@ -99,29 +94,6 @@ module.exports = function calc(gd, trace) {
9994
10095 if ( trace . sort ) cd . sort ( function ( a , b ) { return b . v - a . v ; } ) ;
10196
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-
12597 // include the sum of all values in the first point
12698 if ( cd [ 0 ] ) cd [ 0 ] . vTotal = vTotal ;
12799
@@ -151,34 +123,66 @@ module.exports = function calc(gd, trace) {
151123 return cd ;
152124} ;
153125
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
157132 */
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 ;
159138
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 ) ;
165141 }
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+ } ;
166164
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 = { } ;
170170
171- function generateDefaultColors ( colorList ) {
171+ function generateExtendedColors ( colorList ) {
172172 var i ;
173+ var colorString = JSON . stringify ( colorList ) ;
174+ var pieColors = extendedColorWays [ colorString ] ;
175+ if ( ! pieColors ) {
176+ pieColors = colorList . slice ( ) ;
173177
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+ }
179181
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 ;
182186 }
183187
184188 return pieColors ;
0 commit comments