Skip to content

Commit 21a6bb6

Browse files
committed
Replace measureText with helpers.measureText
1 parent caec142 commit 21a6bb6

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

src/core/core.scale.js

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,6 @@ function getPixelForGridLine(scale, index, offsetGridLines) {
8383
return lineValue;
8484
}
8585

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-
9786
/**
9887
* Returns {width, height, offset} objects for the first, last, widest, highest tick
9988
* labels where offset indicates the anchor point offset from the top in pixels.
@@ -103,43 +92,50 @@ function computeLabelSizes(ctx, tickFonts, ticks, cache) {
10392
var widths = [];
10493
var heights = [];
10594
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+
});
109100

110101
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;
114108
width = height = 0;
115-
ctx.font = tickFont.string;
116109
// Undefined labels and arrays should not be measured
117110
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;
120113
} else if (helpers.isArray(label)) {
121114
// if it is an array lets measure each element
122115
for (j = 0, jlen = label.length; j < jlen; ++j) {
123116
nestedLabel = label[j];
124117
// Undefined labels and arrays should not be measured
125118
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;
128121
}
129122
}
130123
}
131124
widths.push(width);
132125
heights.push(height);
133-
offsets.push(tickFont.lineHeight / 2);
126+
offsets.push(lineHeight / 2);
134127
}
135128

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);
140137
}
141-
gc.splice(0, gcLen);
142-
}
138+
});
143139

144140
widest = widths.indexOf(Math.max.apply(null, widths));
145141
highest = heights.indexOf(Math.max.apply(null, heights));

0 commit comments

Comments
 (0)