Skip to content

Commit 102d67e

Browse files
jsantirsoRollAdvantage
authored andcommitted
Handle chartRangeMin/Max in stacked bar graphs
Fixes gwatts#69 Example with chartRangeMin: 0 and values: [[20, 20], [10, 10], [5, 5]] Notice the clipping that occurs in the "before" image: http://imgur.com/a/ay6e8 Signed-off-by: Chad Keating <[email protected]> gwatts#131
1 parent ea51eb0 commit 102d67e

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/chart-bar.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
chartRangeClip = options.get('chartRangeClip'),
1313
stackMin = Infinity,
1414
stackMax = -Infinity,
15-
isStackString, groupMin, groupMax, stackRanges,
15+
isStackString, groupMin, groupMax, stackRanges, stackRangesNeg, stackTotals, actualMin, actualMax,
1616
numValues, i, vlen, range, zeroAxis, xaxisOffset, min, max, clipMin, clipMax,
1717
stacked, vlist, j, slen, svals, val, yoffset, yMaxCalc, canvasHeightEf;
1818
bar._super.init.call(this, el, values, options, width, height);
@@ -51,12 +51,16 @@
5151
clipMin = chartRangeMin === undefined ? -Infinity : chartRangeMin;
5252
clipMax = chartRangeMax === undefined ? Infinity : chartRangeMax;
5353
}
54+
if (stacked) {
55+
actualMin = chartRangeMin === undefined ? stackMin : Math.min(stackMin, chartRangeMin);
56+
actualMax = chartRangeMax === undefined ? stackMax : Math.max(stackMax, chartRangeMax);
57+
}
5458

5559
numValues = [];
5660
stackRanges = stacked ? [] : numValues;
57-
var stackTotals = [];
58-
var stackRangesNeg = [];
59-
for (i = 0, vlen = values.length; i < vlen; i++) {
61+
stackTotals = [];
62+
stackRangesNeg = [];
63+
for (i = 0, vlen = values.length; i < vlen; i++) {
6064
if (stacked) {
6165
vlist = values[i];
6266
values[i] = svals = [];
@@ -76,12 +80,12 @@
7680
stackRanges[i] += val;
7781
}
7882
} else {
79-
stackRanges[i] += Math.abs(val - (val < 0 ? stackMax : stackMin));
83+
stackRanges[i] += Math.abs(val - (val < 0 ? actualMax : actualMin));
8084
}
8185
numValues.push(val);
8286
stacksInThisOffset++;
8387
}
84-
}
88+
}
8589
} else {
8690
val = chartRangeClip ? clipval(values[i], clipMin, clipMax) : values[i];
8791
val = values[i] = normalizeValue(val);
@@ -91,18 +95,18 @@
9195
}
9296
}
9397

94-
this.max = max = Math.max.apply(Math, numValues);
95-
this.min = min = Math.min.apply(Math, numValues);
96-
this.stackMax = stackMax = stacked ? Math.max.apply(Math, stackTotals) : max;
97-
this.stackMin = stackMin = stacked ? Math.min.apply(Math, numValues) : min;
98+
this.max = max = Math.max.apply(Math, numValues);
99+
this.min = min = Math.min.apply(Math, numValues);
100+
this.stackMax = stackMax = stacked ? Math.max.apply(Math, stackTotals) : max;
101+
this.stackMin = stackMin = stacked ? Math.min.apply(Math, numValues) : min;
98102
// this.stackRanges = stackRanges;
99103
this.stackTotals = stackTotals;
100104

101-
if (options.get('chartRangeMin') !== undefined && (options.get('chartRangeClip') || options.get('chartRangeMin') < min)) {
102-
min = options.get('chartRangeMin');
105+
if (chartRangeMin !== undefined && (chartRangeClip || chartRangeMin < min)) {
106+
min = chartRangeMin;
103107
}
104-
if (options.get('chartRangeMax') !== undefined && (options.get('chartRangeClip') || options.get('chartRangeMax') > max)) {
105-
max = options.get('chartRangeMax');
108+
if (chartRangeMax !== undefined && (chartRangeClip || chartRangeMax > max)) {
109+
max = chartRangeMax;
106110
}
107111

108112
this.zeroAxis = zeroAxis = options.get('zeroAxis', true);
@@ -120,7 +124,7 @@
120124
if (zeroAxis) {
121125
range = stacked ? stackMax : max;
122126
} else {
123-
range = stacked ? (Math.max.apply(Math, stackRanges) + Math.max.apply(Math, stackRangesNeg)) : max - min;
127+
range = stacked ? (Math.max.apply(Math, stackRanges) + Math.max.apply(Math, stackRangesNeg)) : max - min;
124128
}
125129
// as we plot zero/min values a single pixel line, we add a pixel to all other
126130
// values - Reduce the effective canvas size to suit
@@ -236,8 +240,8 @@
236240
}
237241

238242
if (allMin && minPlotted) {
239-
continue;
240-
}
243+
continue;
244+
}
241245

242246
if (stacked && val === stackTotals[valuenum]) {
243247
minPlotted = true;
@@ -249,9 +253,9 @@
249253
if (range > 0) {
250254
if (range - reserve == 1) {
251255
height = (canvasHeightEf * (Math.abs( ( val / stackTotals[valuenum] )))) + 1;
252-
} else {
256+
} else {
253257
height = ((canvasHeightEf / (range - reserve) ) * (val - xaxisOffset));
254-
}
258+
}
255259
} else {
256260
// range is 0 - all values are the same.
257261
height = Math.ceil(canvasHeightEf / (valcount || 1) );

0 commit comments

Comments
 (0)