From e3e8fb0ee99bdb2309f1cd48c0c8a28895fa5269 Mon Sep 17 00:00:00 2001 From: hy9be Date: Tue, 11 Apr 2017 19:34:40 -0400 Subject: [PATCH 1/2] Optimize the text measuring logic for large amount of traces --- src/components/legend/draw.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/components/legend/draw.js b/src/components/legend/draw.js index beb5f58a1c8..bcc45356feb 100644 --- a/src/components/legend/draw.js +++ b/src/components/legend/draw.js @@ -572,7 +572,16 @@ function computeTextDimensions(g, gd) { textLines = textSpans[0].length || 1; height = lineHeight * textLines; - width = text.node() && Drawing.bBox(text.node()).width; + + // If there are more than 20 traces (> 20 legend items), estimate the text width + // so that the overhead of measureing text is not too big + if(gd.data.length <= 20) { + width = text.node() && Drawing.bBox(text.node()).width; + } else { + // "7" is empirical number based on some case studies in Sketch + width = legendItem.trace.name.length * 7 * opts.font.size / 11 + } + // approximation to height offset to center the font // to avoid getBoundingClientRect From 0fedbd2a9a2231713f028cfb14d075da93a6e092 Mon Sep 17 00:00:00 2001 From: hy9be Date: Tue, 11 Apr 2017 19:58:04 -0400 Subject: [PATCH 2/2] Fix JSLint reported error --- src/components/legend/draw.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/legend/draw.js b/src/components/legend/draw.js index bcc45356feb..9674a3e3cc0 100644 --- a/src/components/legend/draw.js +++ b/src/components/legend/draw.js @@ -579,7 +579,7 @@ function computeTextDimensions(g, gd) { width = text.node() && Drawing.bBox(text.node()).width; } else { // "7" is empirical number based on some case studies in Sketch - width = legendItem.trace.name.length * 7 * opts.font.size / 11 + width = legendItem.trace.name.length * 7 * opts.font.size / 11; }