From cfc07ff28588a15ff06a04bb0aa6287e1694b308 Mon Sep 17 00:00:00 2001 From: David Schnur Date: Sun, 3 Feb 2013 13:29:40 -0500 Subject: [PATCH] Eliminate the plugin-global redraw flag. The redraw flag starts out as true, then is immediately set to false on draw. If labels are enabled, drawPie calls drawLabels, which calls drawLabel for each label. Any label that can't fit sets redraw back to true, so the whole process can repeat. This isn't the most obvious mechanism, and forces one to remember to do things like setting redraw back to true after drawing, so the plot can redraw itself on resize or when setting new data. Instead we now have drawPie return true when it drew successfully, and false otherwise, which the same happening in drawLabels and drawLabel. Instead of checking the flag, we now just check the return value. This has the added benefit of slightly improving performance in the case where several redraws are necessary, since it now short-circuits out of the draw loop as soon as one label fails to fit. --- jquery.flot.pie.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/jquery.flot.pie.js b/jquery.flot.pie.js index f0d5630..f9edc2a 100644 --- a/jquery.flot.pie.js +++ b/jquery.flot.pie.js @@ -74,7 +74,6 @@ More detail and specific examples can be found in the included HTML file. maxRadius = null, centerLeft = null, centerTop = null, - redraw = true, legendWidth = 0, processed = false, raw = false, @@ -302,11 +301,13 @@ More detail and specific examples can be found in the included HTML file. ctx = newCtx; setupPie(); - var slices = plot.getData(); - var attempts = 0; + var slices = plot.getData(), + attempts = 0; + + // Keep shrinking the pie's radius until drawPie returns true, + // indicating that all the labels fit, or we try too many times. - while (redraw && attempts < REDRAW_ATTEMPTS) { - redraw = false; + do { if (attempts > 0) { maxRadius *= REDRAW_SHRINK; } @@ -315,20 +316,13 @@ More detail and specific examples can be found in the included HTML file. if (options.series.pie.tilt <= 0.8) { drawShadow(); } - drawPie(); - } + } while (!drawPie() && attempts < REDRAW_ATTEMPTS) if (attempts >= REDRAW_ATTEMPTS) { clear(); target.prepend("
Could not draw pie with labels contained inside canvas
"); } - // Reset the redraw flag on success, so the loop above can run - // again in the event of a resize or other update. - // TODO: We should remove this redraw system entirely! - - redraw = true; - if (plot.setSeries && plot.insertLegend) { plot.setSeries(slices); plot.insertLegend(); @@ -413,14 +407,13 @@ More detail and specific examples can be found in the included HTML file. drawDonutHole(ctx); - // draw labels + ctx.restore(); - if (options.series.pie.label.show) { - drawLabels(); - } + // Draw the labels, returning true if they fit within the plot - // restore to original state - ctx.restore(); + if (options.series.pie.label.show) { + return drawLabels(); + } else return true; function drawSlice(angle, color, fill) { @@ -461,14 +454,19 @@ More detail and specific examples can be found in the included HTML file. for (var i = 0; i < slices.length; ++i) { if (slices[i].percent >= options.series.pie.label.threshold * 100) { - drawLabel(slices[i], currentAngle, i); + if (!drawLabel(slices[i], currentAngle, i)) { + return false; + } } currentAngle += slices[i].angle; } + return true; + function drawLabel(slice, startAngle, index) { + if (slice.data[0][1] == 0) { - return; + return true; } // format label text @@ -502,7 +500,7 @@ More detail and specific examples can be found in the included HTML file. // check to make sure that the label is not outside the canvas if (0 - labelTop > 0 || 0 - labelLeft > 0 || canvasHeight - (labelTop + label.height()) < 0 || canvasWidth - (labelLeft + label.width()) < 0) { - redraw = true; + return false; } if (options.series.pie.label.background.opacity != 0) { @@ -520,6 +518,8 @@ More detail and specific examples can be found in the included HTML file. .css("opacity", options.series.pie.label.background.opacity) .insertBefore(label); } + + return true; } // end individual label function } // end drawLabels function } // end drawPie function