@@ -15,21 +15,18 @@ var Color = require('../../components/color');
15
15
var helpers = require ( './helpers' ) ;
16
16
17
17
module . exports = function calc ( gd , trace ) {
18
- var vals = trace . values ,
19
- labels = trace . labels ,
20
- cd = [ ] ,
21
- fullLayout = gd . _fullLayout ,
22
- colorMap = fullLayout . _piecolormap ,
23
- allThisTraceLabels = { } ,
24
- needDefaults = false ,
25
- vTotal = 0 ,
26
- hiddenLabels = fullLayout . hiddenlabels || [ ] ,
27
- i ,
28
- v ,
29
- label ,
30
- color ,
31
- hidden ,
32
- pt ;
18
+ var vals = trace . values ;
19
+ var hasVals = Array . isArray ( vals ) && vals . length ;
20
+ var labels = trace . labels ;
21
+ var colors = trace . marker . colors ;
22
+ var cd = [ ] ;
23
+ var fullLayout = gd . _fullLayout ;
24
+ var colorMap = fullLayout . _piecolormap ;
25
+ var allThisTraceLabels = { } ;
26
+ var vTotal = 0 ;
27
+ var hiddenLabels = fullLayout . hiddenlabels || [ ] ;
28
+
29
+ var i , v , label , hidden , pt ;
33
30
34
31
if ( trace . dlabel ) {
35
32
labels = new Array ( vals . length ) ;
@@ -38,46 +35,60 @@ module.exports = function calc(gd, trace) {
38
35
}
39
36
}
40
37
41
- for ( i = 0 ; i < vals . length ; i ++ ) {
42
- v = vals [ i ] ;
43
- if ( ! isNumeric ( v ) ) continue ;
44
- v = + v ;
45
- if ( v < 0 ) continue ;
38
+ function pullColor ( color , label ) {
39
+ if ( ! color ) return false ;
40
+
41
+ color = tinycolor ( color ) ;
42
+ if ( ! color . isValid ( ) ) return false ;
43
+
44
+ color = Color . addOpacity ( color , color . getAlpha ( ) ) ;
45
+ if ( ! colorMap [ label ] ) colorMap [ label ] = color ;
46
+
47
+ return color ;
48
+ }
49
+
50
+ var seriesLen = ( hasVals ? vals : labels ) . length ;
51
+
52
+ for ( i = 0 ; i < seriesLen ; i ++ ) {
53
+ if ( hasVals ) {
54
+ v = vals [ i ] ;
55
+ if ( ! isNumeric ( v ) ) continue ;
56
+ v = + v ;
57
+ if ( v < 0 ) continue ;
58
+ }
59
+ else v = 1 ;
46
60
47
61
label = labels [ i ] ;
48
62
if ( label === undefined || label === '' ) label = i ;
49
63
label = String ( label ) ;
50
- // only take the first occurrence of any given label.
51
- // TODO: perhaps (optionally?) sum values for a repeated label?
52
- if ( allThisTraceLabels [ label ] === undefined ) allThisTraceLabels [ label ] = true ;
53
- else continue ;
54
-
55
- color = tinycolor ( trace . marker . colors [ i ] ) ;
56
- if ( color . isValid ( ) ) {
57
- color = Color . addOpacity ( color , color . getAlpha ( ) ) ;
58
- if ( ! colorMap [ label ] ) {
59
- colorMap [ label ] = color ;
60
- }
61
- }
62
- // have we seen this label and assigned a color to it in a previous trace?
63
- else if ( colorMap [ label ] ) color = colorMap [ label ] ;
64
- // color needs a default - mark it false, come back after sorting
65
- else {
66
- color = false ;
67
- needDefaults = true ;
68
- }
69
64
70
- hidden = hiddenLabels . indexOf ( label ) !== - 1 ;
65
+ var thisLabelIndex = allThisTraceLabels [ label ] ;
66
+ if ( thisLabelIndex === undefined ) {
67
+ allThisTraceLabels [ label ] = cd . length ;
71
68
72
- if ( ! hidden ) vTotal += v ;
69
+ hidden = hiddenLabels . indexOf ( label ) !== - 1 ;
73
70
74
- cd . push ( {
75
- v : v ,
76
- label : label ,
77
- color : color ,
78
- i : i ,
79
- hidden : hidden
80
- } ) ;
71
+ if ( ! hidden ) vTotal += v ;
72
+
73
+ cd . push ( {
74
+ v : v ,
75
+ label : label ,
76
+ color : pullColor ( colors [ i ] ) ,
77
+ i : i ,
78
+ pts : [ i ] ,
79
+ hidden : hidden
80
+ } ) ;
81
+ }
82
+ else {
83
+ pt = cd [ thisLabelIndex ] ;
84
+ pt . v += v ;
85
+ pt . pts . push ( i ) ;
86
+ if ( ! pt . hidden ) vTotal += v ;
87
+
88
+ if ( pt . color === false && colors [ i ] ) {
89
+ pt . color = pullColor ( colors [ i ] , label ) ;
90
+ }
91
+ }
81
92
}
82
93
83
94
if ( trace . sort ) cd . sort ( function ( a , b ) { return b . v - a . v ; } ) ;
@@ -88,10 +99,14 @@ module.exports = function calc(gd, trace) {
88
99
* in the order slices will be displayed
89
100
*/
90
101
91
- if ( needDefaults ) {
92
- for ( i = 0 ; i < cd . length ; i ++ ) {
93
- pt = cd [ i ] ;
94
- if ( pt . color === false ) {
102
+ for ( i = 0 ; i < cd . length ; i ++ ) {
103
+ pt = cd [ i ] ;
104
+ if ( pt . color === false ) {
105
+ // have we seen this label and assigned a color to it in a previous trace?
106
+ if ( colorMap [ pt . label ] ) {
107
+ pt . color = colorMap [ pt . label ] ;
108
+ }
109
+ else {
95
110
colorMap [ pt . label ] = pt . color = nextDefaultColor ( fullLayout . _piedefaultcolorcount ) ;
96
111
fullLayout . _piedefaultcolorcount ++ ;
97
112
}
@@ -103,17 +118,21 @@ module.exports = function calc(gd, trace) {
103
118
104
119
// now insert text
105
120
if ( trace . textinfo && trace . textinfo !== 'none' ) {
106
- var hasLabel = trace . textinfo . indexOf ( 'label' ) !== - 1 ,
107
- hasText = trace . textinfo . indexOf ( 'text' ) !== - 1 ,
108
- hasValue = trace . textinfo . indexOf ( 'value' ) !== - 1 ,
109
- hasPercent = trace . textinfo . indexOf ( 'percent' ) !== - 1 ,
110
- separators = fullLayout . separators ,
111
- thisText ;
121
+ var hasLabel = trace . textinfo . indexOf ( 'label' ) !== - 1 ;
122
+ var hasText = trace . textinfo . indexOf ( 'text' ) !== - 1 ;
123
+ var hasValue = trace . textinfo . indexOf ( 'value' ) !== - 1 ;
124
+ var hasPercent = trace . textinfo . indexOf ( 'percent' ) !== - 1 ;
125
+ var separators = fullLayout . separators ;
126
+
127
+ var thisText ;
112
128
113
129
for ( i = 0 ; i < cd . length ; i ++ ) {
114
130
pt = cd [ i ] ;
115
131
thisText = hasLabel ? [ pt . label ] : [ ] ;
116
- if ( hasText && trace . text [ pt . i ] ) thisText . push ( trace . text [ pt . i ] ) ;
132
+ if ( hasText ) {
133
+ var texti = helpers . getFirstFilled ( trace . text , pt . pts ) ;
134
+ if ( texti ) thisText . push ( texti ) ;
135
+ }
117
136
if ( hasValue ) thisText . push ( helpers . formatPieValue ( pt . v , separators ) ) ;
118
137
if ( hasPercent ) thisText . push ( helpers . formatPiePercent ( pt . v / vTotal , separators ) ) ;
119
138
pt . text = thisText . join ( '<br>' ) ;
0 commit comments