@@ -83,17 +83,6 @@ function getPixelForGridLine(scale, index, offsetGridLines) {
83
83
return lineValue ;
84
84
}
85
85
86
- function measureText ( ctx , data , gc , string ) {
87
- var key = ctx . font + ';' + string ;
88
- var textWidth = data [ key ] ;
89
-
90
- if ( ! textWidth ) {
91
- textWidth = data [ key ] = ctx . measureText ( string ) . width ;
92
- gc . push ( key ) ;
93
- }
94
- return textWidth ;
95
- }
96
-
97
86
/**
98
87
* Returns {width, height, offset} objects for the first, last, widest, highest tick
99
88
* labels where offset indicates the anchor point offset from the top in pixels.
@@ -103,43 +92,50 @@ function computeLabelSizes(ctx, tickFonts, ticks, cache) {
103
92
var widths = [ ] ;
104
93
var heights = [ ] ;
105
94
var offsets = [ ] ;
106
- var data = cache . data = cache . data || { } ;
107
- var gc = cache . garbageCollect = cache . garbageCollect || [ ] ;
108
- var gcLen , i , j , jlen , tick , label , tickFont , width , height , nestedLabel , widest , highest ;
95
+ var i , j , jlen , label , tickFont , fontString , data , gc , lineHeight , width , height , nestedLabel , widest , highest ;
96
+
97
+ helpers . each ( tickFonts , function ( font ) {
98
+ cache [ font . string ] = cache [ font . string ] || { data : { } , garbageCollect : [ ] } ;
99
+ } ) ;
109
100
110
101
for ( i = 0 ; i < length ; ++ i ) {
111
- tick = ticks [ i ] ;
112
- label = tick . label ;
113
- tickFont = tick . major ? tickFonts . major : tickFonts . minor ;
102
+ label = ticks [ i ] . label ;
103
+ tickFont = ticks [ i ] . major ? tickFonts . major : tickFonts . minor ;
104
+ ctx . font = fontString = tickFont . string ;
105
+ data = cache [ fontString ] . data ;
106
+ gc = cache [ fontString ] . garbageCollect ;
107
+ lineHeight = tickFont . lineHeight ;
114
108
width = height = 0 ;
115
- ctx . font = tickFont . string ;
116
109
// Undefined labels and arrays should not be measured
117
110
if ( ! helpers . isNullOrUndef ( label ) && ! helpers . isArray ( label ) ) {
118
- width = measureText ( ctx , data , gc , label ) ;
119
- height = tickFont . lineHeight ;
111
+ width = helpers . measureText ( ctx , data , gc , width , label ) ;
112
+ height = lineHeight ;
120
113
} else if ( helpers . isArray ( label ) ) {
121
114
// if it is an array lets measure each element
122
115
for ( j = 0 , jlen = label . length ; j < jlen ; ++ j ) {
123
116
nestedLabel = label [ j ] ;
124
117
// Undefined labels and arrays should not be measured
125
118
if ( ! helpers . isNullOrUndef ( nestedLabel ) && ! helpers . isArray ( nestedLabel ) ) {
126
- width = Math . max ( width , measureText ( ctx , data , gc , nestedLabel ) ) ;
127
- height += tickFont . lineHeight ;
119
+ width = helpers . measureText ( ctx , data , gc , width , nestedLabel ) ;
120
+ height += lineHeight ;
128
121
}
129
122
}
130
123
}
131
124
widths . push ( width ) ;
132
125
heights . push ( height ) ;
133
- offsets . push ( tickFont . lineHeight / 2 ) ;
126
+ offsets . push ( lineHeight / 2 ) ;
134
127
}
135
128
136
- gcLen = gc . length / 2 ;
137
- if ( gcLen > length ) {
138
- for ( i = 0 ; i < gcLen ; ++ i ) {
139
- delete data [ gc [ i ] ] ;
129
+ helpers . each ( cache , function ( fontCache ) {
130
+ var garbageCollect = fontCache . garbageCollect ;
131
+ var gcLen = garbageCollect . length / 2 ;
132
+ if ( gcLen > length ) {
133
+ for ( i = 0 ; i < gcLen ; ++ i ) {
134
+ delete fontCache . data [ garbageCollect [ i ] ] ;
135
+ }
136
+ garbageCollect . splice ( 0 , gcLen ) ;
140
137
}
141
- gc . splice ( 0 , gcLen ) ;
142
- }
138
+ } ) ;
143
139
144
140
widest = widths . indexOf ( Math . max . apply ( null , widths ) ) ;
145
141
highest = heights . indexOf ( Math . max . apply ( null , heights ) ) ;
0 commit comments