Skip to content

Commit 1eb0ea3

Browse files
author
benrubson
committed
Correctly stack series with null and missing values
1 parent becb630 commit 1eb0ea3

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

public/vendor/flot/jquery.flot.stack.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,38 @@ charts or filled areas).
7272
horizontal = s.bars.horizontal,
7373
withbottom = ps > 2 && (horizontal ? datapoints.format[2].x : datapoints.format[2].y),
7474
withsteps = withlines && s.lines.steps,
75-
fromgap = true,
7675
keyOffset = horizontal ? 1 : 0,
7776
accumulateOffset = horizontal ? 0 : 1,
7877
i = 0, j = 0, l, m;
7978

8079
while (true) {
81-
if (i >= points.length)
80+
if (i >= points.length && j >= otherpoints.length)
8281
break;
8382

8483
l = newpoints.length;
8584

86-
if (points[i] == null) {
85+
if (i < points.length && points[i] == null) {
8786
// copy gaps
8887
for (m = 0; m < ps; ++m)
8988
newpoints.push(points[i + m]);
9089
i += ps;
9190
}
91+
else if (i >= points.length) {
92+
// take the remaining points from the previous series
93+
for (m = 0; m < ps; ++m)
94+
newpoints.push(otherpoints[j + m]);
95+
if (withbottom)
96+
newpoints[l + 2] = otherpoints[j + accumulateOffset];
97+
j += otherps;
98+
}
9299
else if (j >= otherpoints.length) {
93-
// for lines, we can't use the rest of the points
94-
if (!withlines) {
95-
for (m = 0; m < ps; ++m)
96-
newpoints.push(points[i + m]);
97-
}
100+
// take the remaining points from the current series
101+
for (m = 0; m < ps; ++m)
102+
newpoints.push(points[i + m]);
98103
i += ps;
99104
}
100-
else if (otherpoints[j] == null) {
101-
// oops, got a gap
102-
for (m = 0; m < ps; ++m)
103-
newpoints.push(null);
104-
fromgap = true;
105+
else if (j < otherpoints.length && otherpoints[j] == null) {
106+
// ignore point
105107
j += otherps;
106108
}
107109
else {
@@ -123,9 +125,15 @@ charts or filled areas).
123125
j += otherps;
124126
}
125127
else if (px > qx) {
128+
// take the point from the previous series so that next series will correctly stack
129+
if (i == 0) {
130+
for (m = 0; m < ps; ++m)
131+
newpoints.push(otherpoints[j + m]);
132+
bottom = qy;
133+
}
126134
// we got past point below, might need to
127135
// insert interpolated extra point
128-
if (withlines && i > 0 && points[i - ps] != null) {
136+
if (i > 0 && points[i - ps] != null) {
129137
intery = py + (points[i - ps + accumulateOffset] - py) * (qx - px) / (points[i - ps + keyOffset] - px);
130138
newpoints.push(qx);
131139
newpoints.push(intery + qy);
@@ -137,18 +145,12 @@ charts or filled areas).
137145
j += otherps;
138146
}
139147
else { // px < qx
140-
if (fromgap && withlines) {
141-
// if we come from a gap, we just skip this point
142-
i += ps;
143-
continue;
144-
}
145-
146148
for (m = 0; m < ps; ++m)
147149
newpoints.push(points[i + m]);
148150

149151
// we might be able to interpolate a point below,
150152
// this can give us a better y
151-
if (withlines && j > 0 && otherpoints[j - otherps] != null)
153+
if (j > 0 && otherpoints[j - otherps] != null)
152154
bottom = qy + (otherpoints[j - otherps + accumulateOffset] - qy) * (px - qx) / (otherpoints[j - otherps + keyOffset] - qx);
153155

154156
newpoints[l + accumulateOffset] += bottom;
@@ -159,7 +161,7 @@ charts or filled areas).
159161
fromgap = false;
160162

161163
if (l != newpoints.length && withbottom)
162-
newpoints[l + 2] += bottom;
164+
newpoints[l + 2] = bottom;
163165
}
164166

165167
// maintain the line steps invariant

0 commit comments

Comments
 (0)