From 5a86074fc587952902d607ab7b57e63b0048bd8b Mon Sep 17 00:00:00 2001 From: "olau@iola.dk" Date: Sun, 21 Sep 2008 21:10:00 +0000 Subject: [PATCH] Fixed two bugs with negative bars git-svn-id: https://flot.googlecode.com/svn/trunk@89 1e0a6537-2640-0410-bfb7-f154510ff394 --- NEWS.txt | 2 +- jquery.flot.js | 42 +++++++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/NEWS.txt b/NEWS.txt index 07af8cd..f8b0e4e 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -55,7 +55,7 @@ timothytoe). Fixed a bug in finding max values for all-negative data sets. Prevent the possibility of eternal looping in tick calculations. Fixed a bug when borderWidth is set to 0 (reported by Rob/sanchothefat). Fixed a bug with drawing bars extending below 0 -(reported by James Hewitt, convenient patch by Ryan Funduk). Fixed a +(reported by James Hewitt, patch by Ryan Funduk). Fixed a bug with line widths of bars (reported by MikeM). Fixed a bug with 'nw' and 'sw' legend positions. Improved the handling of axis auto-scaling with bars. Fixed a bug with multi-line x-axis tick diff --git a/jquery.flot.js b/jquery.flot.js index 27ad493..e75b057 100644 --- a/jquery.flot.js +++ b/jquery.flot.js @@ -1299,16 +1299,25 @@ if (data[i] == null) continue; - var x = data[i][0], y = data[i][1]; - var drawLeft = true, drawTop = true, drawRight = true; - - // determine the co-ordinates of the bar, account for negative bars having - // flipped top/bottom and draw/don't draw accordingly - var left = x + deltaLeft, right = x + deltaRight, bottom = (y < 0 ? y : 0), top = (y < 0 ? 0 : y); - if (right < axisx.min || left > axisx.max || top < axisy.min || bottom > axisy.max) - continue; - + var x = data[i][0], y = data[i][1], + drawLeft = true, drawRight = true, + drawTop = true, drawBottom = false, + left = x + deltaLeft, right = x + deltaRight, + bottom = 0, top = y; + + // account for negative bars + if (top < bottom) { + top = 0; + bottom = y + drawBottom = true; + drawTop = false; + } + // clip + if (right < axisx.min || left > axisx.max || + top < axisy.min || bottom > axisy.max) + continue; + if (left < axisx.min) { left = axisx.min; drawLeft = false; @@ -1319,9 +1328,11 @@ drawRight = false; } - if (bottom < axisy.min) + if (bottom < axisy.min) { bottom = axisy.min; - + drawBottom = false; + } + if (top > axisy.max) { top = axisy.max; drawTop = false; @@ -1338,7 +1349,7 @@ } // draw outline - if (drawLeft || drawRight || drawTop) { + if (drawLeft || drawRight || drawTop || drawBottom) { ctx.beginPath(); ctx.moveTo(axisx.p2c(left), axisy.p2c(bottom) + offset); if (drawLeft) @@ -1354,6 +1365,10 @@ ctx.lineTo(axisx.p2c(right), axisy.p2c(bottom) + offset); else ctx.moveTo(axisx.p2c(right), axisy.p2c(bottom) + offset); + if (drawBottom) + ctx.lineTo(axisx.p2c(left), axisy.p2c(bottom) + offset); + else + ctx.moveTo(axisx.p2c(left), axisy.p2c(bottom) + offset); ctx.stroke(); } } @@ -1519,7 +1534,8 @@ // For a bar graph, the cursor must be inside the bar // and no other point can be nearby if (!foundPoint && mx >= x + barLeft && - mx <= x + barRight && my <= y) + mx <= x + barRight && + (y > 0 ? my >= 0 && my <= y : my <= 0 && my >= y)) item = result(i, j); }