Chris Leonello avatar Chris Leonello committed bd3ca46

Fixes for stacked bar charts with negative values.

Comments (0)

Files changed (3)

src/jqplot.core.js

             }
             gridData = options.gridData || this.renderer.makeGridData.call(this, data, plot);
         
-            this.renderer.drawShadow.call(this, sctx, gridData, options);
+            this.renderer.drawShadow.call(this, sctx, gridData, options, plot);
         }
         
         for (j=0; j<$.jqplot.postDrawSeriesShadowHooks.length; j++) {
                         temp[k][0] += cd[k][0];
                         temp[k][1] += cd[k][1];
                         // only need to sum up the stack axis column of data
-                        plotdata[k][sidx] += cd[k][sidx];
+                        // and only sum if it is of same sign.
+                        if (series.data[k][sidx] * cd[k][sidx] >= 0) {
+                            plotdata[k][sidx] += cd[k][sidx];
+                        }
                     }
                 }
                 for (var i=0; i<plotdata.length; i++) {

src/plugins/jqplot.barRenderer.js

         }
         return ret;
     }
+
+    function getStart(sidx, didx, comp, plot, axis) {
+        // check if sign change
+        var seriesIndex = sidx,
+            prevSeriesIndex = sidx - 1,
+            start,
+            prevVal,
+            aidx = (axis === 'x') ? 0 : 1;
+
+        // is this not the first series?
+        if (seriesIndex > 0) {
+            prevVal = plot.series[prevSeriesIndex]._plotData[didx][aidx];
+
+            // is there a sign change
+            if ((comp * prevVal) < 0) {
+                start = getStart(prevSeriesIndex, didx, comp, plot, axis);
+            }
+
+            // no sign change.
+            else {
+                start = plot.series[prevSeriesIndex].gridData[didx][aidx];
+            }
+
+        }
+
+        // if first series, return value at 0
+        else {
+
+            start = (aidx === 0) ? plot.series[seriesIndex]._xaxis.series_u2p(0) : plot.series[seriesIndex]._yaxis.series_u2p(0);
+        }
+
+        return start;
+    }
+
     
     $.jqplot.BarRenderer.prototype.draw = function(ctx, gridData, options, plot) {
         var i;
 			var base;
 			var xstart; 
 			var ystart;
-
-            function getYStart(sidx, didx, comp) {
-                // check if sign change
-                var seriesIndex = sidx,
-                    prevSeriesIndex = sidx - 1,
-                    start,
-                    prevVal;
-
-                // is this not the first series?
-                if (seriesIndex > 0) {
-                    prevVal = plot.series[prevSeriesIndex]._plotData[didx][1];
-
-                    // is there a sign change
-                    if ((comp * prevVal) < 0) {
-                        start = getYStart(prevSeriesIndex, didx, comp);
-                    }
-
-                    // no sign change.
-                    else {
-                        start = plot.series[prevSeriesIndex].gridData[didx][1];
-                    }
-
-                }
-
-                // if first series, return value at 0
-                else {
-                    start = plot.series[seriesIndex]._yaxis.series_u2p(0);
-                }
-
-                return start;
-            }
-
-            function getXStart(sidx, didx, comp) {
-                // check if sign change
-                var seriesIndex = sidx,
-                    prevSeriesIndex = sidx - 1,
-                    start,
-                    prevVal;
-
-                // is this not the first series?
-                if (seriesIndex > 0) {
-                    prevVal = plot.series[prevSeriesIndex]._plotData[didx][0];
-
-                    // is there a sign change
-                    if ((comp * prevVal) < 0) {
-                        start = getYStart(prevSeriesIndex, didx, comp);
-                    }
-
-                    // no sign change.
-                    else {
-                        start = plot.series[prevSeriesIndex].gridData[didx][0];
-                    }
-
-                }
-
-                // if first series, return value at 0
-                else {
-                    start = plot.series[seriesIndex]._xaxis.series_u2p(0);
-                }
-
-                return start;
-            }
             
             if (this.barDirection == 'vertical') {
                 for (var i=0; i<gridData.length; i++) {
                     
                     // stacked
                     if (this._stack && this._prevGridData.length) {
+                        ystart = getStart(this.index, i, this._plotData[i][1], plot, 'y');
+                    }
 
-                        ystart = getYStart(this.index, i, this._plotData[i][1]);
-
-                    }
                     // not stacked and first series in stack
                     else {
                         if (this.fillToZero) {
                     xstart;
                     
                     if (this._stack && this._prevGridData.length) {
-                        xstart = getXStart(this.index, i, this._plotData[i][0]);
+                        xstart = getStart(this.index, i, this._plotData[i][0], plot, 'x');
                     }
                     // not stacked and first series in stack
                     else {
     
      
     // for stacked plots, shadows will be pre drawn by drawShadow.
-    $.jqplot.BarRenderer.prototype.drawShadow = function(ctx, gridData, options) {
+    $.jqplot.BarRenderer.prototype.drawShadow = function(ctx, gridData, options, plot) {
         var i;
         var opts = (options != undefined) ? options : {};
         var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
                         var ystart;
                     
                         if (this._stack && this._prevGridData.length) {
-                            ystart = this._prevGridData[i][1];
+                            ystart = getStart(this.index, i, this._plotData[i][1], plot, 'y');
                         }
                         else {
                             if (this.fillToZero) {
                         var xstart;
                     
                         if (this._stack && this._prevGridData.length) {
-                            xstart = this._prevGridData[i][0];
+                            xstart = getStart(this.index, i, this._plotData[i][0], plot, 'x');
                         }
                         else {
-                            xstart = 0;
+                            if (this.fillToZero) {
+                                xstart = this._xaxis.series_u2p(0);
+                            }
+                            else {
+                                xstart = 0;
+                            }
                         }
                     
                         points.push([xstart, base+this.barWidth/2]);

src/plugins/jqplot.pointLabels.js

                 }
             }
             else {
-                var d = this._plotData;
+                // var d = this._plotData;
+                var d = this.data;
                 if (this.renderer.constructor === $.jqplot.BarRenderer && this.waterfall) {
                     d = this._data;
                 }
             }
         
             var pd = this._plotData;
+            var ppd = this._prevPlotData;
             var xax = this._xaxis;
             var yax = this._yaxis;
             var elem, helem;
                 if ((this.fillToZero && pd[i][1] < 0) || (this.fillToZero && this._type === 'bar' && this.barDirection === 'horizontal' && pd[i][0] < 0) || (this.waterfall && parseInt(label, 10)) < 0) {
                     location = oppositeLocations[locationIndicies[location]];
                 }
+
+
                 var ell = xax.u2p(pd[i][0]) + p.xOffset(elem, location);
                 var elt = yax.u2p(pd[i][1]) + p.yOffset(elem, location);
+
+                // we have stacked chart but are not showing stacked values,
+                // place labels in center.
+                if (this._stack && !p.stackedValue) {
+                    if (this.barDirection === "vertical") {
+                        elt = (this._barPoints[i][0][1] + this._barPoints[i][1][1]) / 2 + plot._gridPadding.top - 0.5 * elem.outerHeight(true);
+                    }
+                    else {
+                        ell = (this._barPoints[i][2][0] + this._barPoints[i][0][0]) / 2 + plot._gridPadding.left - 0.5 * elem.outerWidth(true);
+                    }
+                }
+
                 if (this.renderer.constructor == $.jqplot.BarRenderer) {
                     if (this.barDirection == "vertical") {
                         ell += this._barNudge;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.