summaryrefslogtreecommitdiff
path: root/jquery.flot.stack.js
diff options
context:
space:
mode:
authorIgor Sfiligoi <isfiligoi@ucsd.edu>2011-03-22 07:30:12 -0400
committerIgor Sfiligoi <isfiligoi@ucsd.edu>2011-03-22 07:30:12 -0400
commitcb2e3d2743c0f88b430f14b7a396aa1a583dc978 (patch)
treea01293736eef098deff6d1cc832dcba0731ebb3e /jquery.flot.stack.js
parentaf26aaa53d073a7b2d25caee93e1cb5ceb2e099c (diff)
flot-0.7
Diffstat (limited to 'jquery.flot.stack.js')
-rw-r--r--jquery.flot.stack.js84
1 files changed, 58 insertions, 26 deletions
diff --git a/jquery.flot.stack.js b/jquery.flot.stack.js
index 4dbd29f..a31d5dc 100644
--- a/jquery.flot.stack.js
+++ b/jquery.flot.stack.js
@@ -1,8 +1,14 @@
/*
Flot plugin for stacking data sets, i.e. putting them on top of each
-other, for accumulative graphs. Note that the plugin assumes the data
-is sorted on x. Also note that stacking a mix of positive and negative
-values in most instances doesn't make sense (so it looks weird).
+other, for accumulative graphs.
+
+The plugin assumes the data is sorted on x (or y if stacking
+horizontally). For line charts, it is assumed that if a line has an
+undefined gap (from a null point), then the line above it should have
+the same gap - insert zeros instead of "null" if you want another
+behaviour. This also holds for the start and end of the chart. Note
+that stacking a mix of positive and negative values in most instances
+doesn't make sense (so it looks weird).
Two or more series are stacked when their "stack" attribute is set to
the same key (which can be any number or string or just "true"). To
@@ -14,15 +20,15 @@ specify the default stack, you can set
or specify it for a specific series
- $.plot($("#placeholder"), [{ data: [ ... ], stack: true ])
+ $.plot($("#placeholder"), [{ data: [ ... ], stack: true }])
The stacking order is determined by the order of the data series in
the array (later series end up on top of the previous).
Internally, the plugin modifies the datapoints in each series, adding
an offset to the y value. For line series, extra data points are
-inserted through interpolation. For bar charts, the second y value is
-also adjusted.
+inserted through interpolation. If there's a second y value, it's also
+adjusted (e.g for bar charts or filled areas).
*/
(function ($) {
@@ -51,15 +57,20 @@ also adjusted.
var other = findMatchingSeries(s, plot.getData());
if (!other)
return;
-
+
var ps = datapoints.pointsize,
points = datapoints.points,
otherps = other.datapoints.pointsize,
otherpoints = other.datapoints.points,
newpoints = [],
px, py, intery, qx, qy, bottom,
- withlines = s.lines.show, withbars = s.bars.show,
+ withlines = s.lines.show,
+ 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) {
@@ -68,27 +79,40 @@ also adjusted.
l = newpoints.length;
- if (j >= otherpoints.length
- || otherpoints[j] == null
- || points[i] == null) {
- // degenerate cases
+ if (points[i] == null) {
+ // copy gaps
for (m = 0; m < ps; ++m)
newpoints.push(points[i + m]);
i += ps;
}
+ else if (j >= otherpoints.length) {
+ // for lines, we can't use the rest of the points
+ if (!withlines) {
+ for (m = 0; m < ps; ++m)
+ newpoints.push(points[i + m]);
+ }
+ i += ps;
+ }
+ else if (otherpoints[j] == null) {
+ // oops, got a gap
+ for (m = 0; m < ps; ++m)
+ newpoints.push(null);
+ fromgap = true;
+ j += otherps;
+ }
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;
@@ -98,9 +122,9 @@ also adjusted.
// 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;
@@ -108,21 +132,29 @@ also adjusted.
j += otherps;
}
- else {
+ else { // px < qx
+ if (fromgap && withlines) {
+ // if we come from a gap, we just skip this point
+ i += ps;
+ continue;
+ }
+
for (m = 0; m < ps; ++m)
newpoints.push(points[i + m]);
// we might be able to interpolate a point below,
// this can give us a better y
- if (withlines && j > 0 && otherpoints[j - ps] != null)
- bottom = qy + (otherpoints[j - ps + 1] - qy) * (px - qx) / (otherpoints[j - ps] - qx);
+ if (withlines && j > 0 && otherpoints[j - otherps] != null)
+ bottom = qy + (otherpoints[j - otherps + accumulateOffset] - qy) * (px - qx) / (otherpoints[j - otherps + keyOffset] - qx);
- newpoints[l + 1] += bottom;
+ newpoints[l + accumulateOffset] += bottom;
i += ps;
}
+
+ fromgap = false;
- if (l != newpoints.length && withbars)
+ if (l != newpoints.length && withbottom)
newpoints[l + 2] += bottom;
}
@@ -136,7 +168,7 @@ also adjusted.
newpoints[l + 1] = newpoints[l - ps + 1];
}
}
-
+
datapoints.points = newpoints;
}
@@ -147,6 +179,6 @@ also adjusted.
init: init,
options: options,
name: 'stack',
- version: '1.0'
+ version: '1.2'
});
})(jQuery);