From e0413b93087c383dd29ef9092ef3c2f33a05133f Mon Sep 17 00:00:00 2001 From: David Schnur Date: Tue, 28 May 2013 22:24:50 -0400 Subject: [PATCH] Factor out duplicate variable definitions. --- jquery.flot.canvas.js | 2 +- jquery.flot.errorbars.js | 14 ++++++++------ jquery.flot.js | 35 ++++++++++++++++++++++------------- jquery.flot.pie.js | 26 ++++++++++++++------------ 4 files changed, 45 insertions(+), 32 deletions(-) diff --git a/jquery.flot.canvas.js b/jquery.flot.canvas.js index db36d1b..4a2369e 100644 --- a/jquery.flot.canvas.js +++ b/jquery.flot.canvas.js @@ -322,7 +322,7 @@ browser, but needs to redraw with canvas text when exporting as an image. // Fill in the x & y positions of each line, adjusting them // individually for horizontal alignment. - for (var i = 0, line; line = lines[i]; i++) { + for (var j = 0, line; line = lines[j]; j++) { if (halign === "center") { position.lines.push([Math.round(x - line.width / 2), y]); } else if (halign === "right") { diff --git a/jquery.flot.errorbars.js b/jquery.flot.errorbars.js index 7ffe8a4..3515ca8 100644 --- a/jquery.flot.errorbars.js +++ b/jquery.flot.errorbars.js @@ -181,21 +181,23 @@ shadowSize and lineWidth are derived as well from the points series. ps = s.datapoints.pointsize, ax = [s.xaxis, s.yaxis], radius = s.points.radius, - err = [s.points.xerr, s.points.yerr]; + err = [s.points.xerr, s.points.yerr], + invertX = false, + invertY = false, + tmp; //sanity check, in case some inverted axis hack is applied to flot - var invertX = false; + if (ax[0].p2c(ax[0].max) < ax[0].p2c(ax[0].min)) { invertX = true; - var tmp = err[0].lowerCap; + tmp = err[0].lowerCap; err[0].lowerCap = err[0].upperCap; err[0].upperCap = tmp; } - var invertY = false; if (ax[1].p2c(ax[1].min) < ax[1].p2c(ax[1].max)) { invertY = true; - var tmp = err[1].lowerCap; + tmp = err[1].lowerCap; err[1].lowerCap = err[1].upperCap; err[1].upperCap = tmp; } @@ -249,7 +251,7 @@ shadowSize and lineWidth are derived as well from the points series. //sanity check, in case some inverted axis hack is applied to flot if ((err[e].err === "x" && invertX) || (err[e].err === "y" && invertY)) { //swap coordinates - var tmp = lower; + tmp = lower; lower = upper; upper = tmp; tmp = drawLower; diff --git a/jquery.flot.js b/jquery.flot.js index bc35340..1a4e261 100644 --- a/jquery.flot.js +++ b/jquery.flot.js @@ -459,6 +459,7 @@ Licensed under the MIT license. // Angle is currently unused, it will be implemented in the future. Canvas.prototype.removeText = function(layer, x, y, text, font, angle) { + var i, positions, position; if (text == null) { var layerCache = this._textCache[layer]; if (layerCache != null) { @@ -467,8 +468,8 @@ Licensed under the MIT license. var styleCache = layerCache[styleKey]; for (var key in styleCache) { if (hasOwnProperty.call(styleCache, key)) { - var positions = styleCache[key].positions; - for (var i = 0, position; position = positions[i]; i++) { + positions = styleCache[key].positions; + for (i = 0; position = positions[i]; i++) { position.active = false; } } @@ -477,8 +478,8 @@ Licensed under the MIT license. } } } else { - var positions = this.getTextInfo(layer, text, font, angle).positions; - for (var i = 0, position; position = positions[i]; i++) { + positions = this.getTextInfo(layer, text, font, angle).positions; + for (i = 0; position = positions[i]; i++) { if (position.x === x && position.y === y) { position.active = false; } @@ -1549,11 +1550,13 @@ Licensed under the MIT license. } function setupGrid() { - var i, axes = allAxes(), showGrid = options.grid.show; + var axes = allAxes(), + showGrid = options.grid.show, + i, a; // Initialize the plot's offset from the edge of the canvas - for (var a in plotOffset) { + for (a in plotOffset) { var margin = options.grid.margin || 0; plotOffset[a] = typeof margin === "number" ? margin : margin[a] || 0; } @@ -1562,7 +1565,7 @@ Licensed under the MIT license. // If the grid is visible, add its border width to the offset - for (var a in plotOffset) { + for (a in plotOffset) { if(typeof(options.grid.borderWidth) === "object") { plotOffset[a] += showGrid ? options.grid.borderWidth[a] : 0; } else { @@ -2750,11 +2753,11 @@ Licensed under the MIT license. } var fragments = [], entries = [], rowStarted = false, - lf = options.legend.labelFormatter, s, label; + lf = options.legend.labelFormatter, s, label, i; // Build a list of legend entries, with each having a label and a color - for (var i = 0; i < series.length; ++i) { + for (i = 0; i < series.length; ++i) { s = series[i]; if (s.label) { label = lf ? lf(s.label, s) : s.label; @@ -2786,7 +2789,7 @@ Licensed under the MIT license. // Generate markup for the list of entries, in their final order - for (var i = 0; i < entries.length; ++i) { + for (i = 0; i < entries.length; ++i) { var entry = entries[i]; @@ -2878,7 +2881,8 @@ Licensed under the MIT license. mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster my = axisy.c2p(mouseY), maxx = maxDistance / axisx.scale, - maxy = maxDistance / axisy.scale; + maxy = maxDistance / axisy.scale, + x, y; ps = s.datapoints.pointsize; // with inverse transforms, we can't use the maxx/maxy @@ -2892,7 +2896,10 @@ Licensed under the MIT license. if (s.lines.show || s.points.show) { for (j = 0; j < points.length; j += ps) { - var x = points[j], y = points[j + 1]; + + x = points[j]; + y = points[j + 1]; + if (x == null) { continue; } @@ -2924,7 +2931,9 @@ Licensed under the MIT license. barRight = barLeft + s.bars.barWidth; for (j = 0; j < points.length; j += ps) { - var x = points[j], y = points[j + 1], b = points[j + 2]; + x = points[j]; + y = points[j + 1]; + var b = points[j + 2]; if (x == null) { continue; } diff --git a/jquery.flot.pie.js b/jquery.flot.pie.js index 4ba2b27..1b38c5e 100644 --- a/jquery.flot.pie.js +++ b/jquery.flot.pie.js @@ -165,13 +165,14 @@ More detail and specific examples can be found in the included HTML file. combined = 0, numCombined = 0, color = options.series.pie.combine.color, - newdata = []; + newdata = [], + i, value; // Fix up the raw data from Flot, ensuring the data is numeric - for (var i = 0; i < data.length; ++i) { + for (i = 0; i < data.length; ++i) { - var value = data[i].data; + value = data[i].data; // If the data is an array, we'll assume that it's a standard // Flot x-y pair, and are concerned only with the second value. @@ -202,15 +203,15 @@ More detail and specific examples can be found in the included HTML file. // Sum up all the slices, so we can calculate percentages for each - for (var i = 0; i < data.length; ++i) { + for (i = 0; i < data.length; ++i) { total += data[i].data[0][1]; } // Count the number of slices with percentages below the combine // threshold; if it turns out to be just one, we won't combine. - for (var i = 0; i < data.length; ++i) { - var value = data[i].data[0][1]; + for (i = 0; i < data.length; ++i) { + value = data[i].data[0][1]; if (value / total <= options.series.pie.combine.threshold) { combined += value; numCombined++; @@ -220,8 +221,8 @@ More detail and specific examples can be found in the included HTML file. } } - for (var i = 0; i < data.length; ++i) { - var value = data[i].data[0][1]; + for (i = 0; i < data.length; ++i) { + value = data[i].data[0][1]; if (numCombined < 2 || value / total > options.series.pie.combine.threshold) { newdata.push({ data: [[1, value]], @@ -373,8 +374,9 @@ More detail and specific examples can be found in the included HTML file. function drawPie() { - var startAngle = Math.PI * options.series.pie.startAngle; - var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius; + var startAngle = Math.PI * options.series.pie.startAngle, + radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius, + i; // center and rotate to starting position @@ -387,7 +389,7 @@ More detail and specific examples can be found in the included HTML file. ctx.save(); var currentAngle = startAngle; - for (var i = 0; i < slices.length; ++i) { + for (i = 0; i < slices.length; ++i) { slices[i].startAngle = currentAngle; drawSlice(slices[i].angle, slices[i].color, true); } @@ -399,7 +401,7 @@ More detail and specific examples can be found in the included HTML file. ctx.save(); ctx.lineWidth = options.series.pie.stroke.width; currentAngle = startAngle; - for (var i = 0; i < slices.length; ++i) { + for (i = 0; i < slices.length; ++i) { drawSlice(slices[i].angle, options.series.pie.stroke.color, false); } ctx.restore();