From 569685fb200f8374e0f925732fe12ea44afce071 Mon Sep 17 00:00:00 2001 From: "olau@iola.dk" Date: Tue, 9 Feb 2010 15:36:24 +0000 Subject: [PATCH] Added support for stacking horizontal bar charts to the stacking plugin git-svn-id: https://flot.googlecode.com/svn/trunk@230 1e0a6537-2640-0410-bfb7-f154510ff394 --- NEWS.txt | 1 + jquery.flot.js | 12 ++++++++++-- jquery.flot.stack.js | 25 ++++++++++++++----------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/NEWS.txt b/NEWS.txt index 6ef6d54..082ad3f 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -12,6 +12,7 @@ Changes: example percentiles.html for a use case). - More predictable handling of gaps for the stacking plugin, now all undefined ranges are skipped. +- Stacking plugin can stack horizontal bar charts. Bug fixes: diff --git a/jquery.flot.js b/jquery.flot.js index 3c86774..663bb92 100644 --- a/jquery.flot.js +++ b/jquery.flot.js @@ -102,7 +102,7 @@ fill: true, fillColor: null, align: "left", // or "center" - horizontal: false // when horizontal, left is now top + horizontal: false }, shadowSize: 3 }, @@ -382,8 +382,13 @@ format.push({ x: true, number: true, required: true }); format.push({ y: true, number: true, required: true }); - if (s.bars.show || (s.lines.show && s.lines.fill)) + if (s.bars.show || (s.lines.show && s.lines.fill)) { format.push({ y: true, number: true, required: false, defaultValue: 0 }); + if (s.bars.horizontal) { + delete format[format.length - 1].y; + format[format.length - 1].x = true; + } + } s.datapoints.format = format; } @@ -1584,6 +1589,9 @@ drawLeft, drawRight, drawTop, drawBottom, tmp; + // in horizontal mode, we start the bar from the left + // instead of from the bottom so it appears to be + // horizontal rather than vertical if (horizontal) { drawBottom = drawRight = drawTop = true; drawLeft = false; diff --git a/jquery.flot.stack.js b/jquery.flot.stack.js index 41be965..21008d4 100644 --- a/jquery.flot.stack.js +++ b/jquery.flot.stack.js @@ -64,9 +64,12 @@ adjusted (e.g for bar charts or filled areas). newpoints = [], px, py, intery, qx, qy, bottom, withlines = s.lines.show, - withbottom = ps > 2 && datapoints.format[2].y, + horizontal = s.bars.horizontal, + withbottom = ps > 2 && (horizontal ? datapoints.format[2].x : datapoints.format[2].y), withsteps = withlines && s.lines.steps, fromgap = true, + keyOffset = horizontal ? 1 : 0, + accumulateOffset = horizontal ? 0 : 1, i = 0, j = 0, l; while (true) { @@ -98,17 +101,17 @@ adjusted (e.g for bar charts or filled areas). } else { // cases where we actually got two points - px = points[i]; - py = points[i + 1]; - qx = otherpoints[j]; - qy = otherpoints[j + 1]; + px = points[i + keyOffset]; + py = points[i + accumulateOffset]; + qx = otherpoints[j + keyOffset]; + qy = otherpoints[j + accumulateOffset]; bottom = 0; if (px == qx) { for (m = 0; m < ps; ++m) newpoints.push(points[i + m]); - newpoints[l + 1] += qy; + newpoints[l + accumulateOffset] += qy; bottom = qy; i += ps; @@ -118,9 +121,9 @@ adjusted (e.g for bar charts or filled areas). // we got past point below, might need to // insert interpolated extra point if (withlines && i > 0 && points[i - ps] != null) { - intery = py + (points[i - ps + 1] - py) * (qx - px) / (points[i - ps] - px); + intery = py + (points[i - ps + accumulateOffset] - py) * (qx - px) / (points[i - ps + keyOffset] - px); newpoints.push(qx); - newpoints.push(intery + qy) + newpoints.push(intery + qy); for (m = 2; m < ps; ++m) newpoints.push(points[i + m]); bottom = qy; @@ -141,9 +144,9 @@ adjusted (e.g for bar charts or filled areas). // we might be able to interpolate a point below, // this can give us a better y if (withlines && j > 0 && otherpoints[j - otherps] != null) - bottom = qy + (otherpoints[j - otherps + 1] - qy) * (px - qx) / (otherpoints[j - otherps] - qx); + bottom = qy + (otherpoints[j - otherps + accumulateOffset] - qy) * (px - qx) / (otherpoints[j - otherps + keyOffset] - qx); - newpoints[l + 1] += bottom; + newpoints[l + accumulateOffset] += bottom; i += ps; } @@ -175,6 +178,6 @@ adjusted (e.g for bar charts or filled areas). init: init, options: options, name: 'stack', - version: '1.1' + version: '1.2' }); })(jQuery);