1. Andy Clayton
  2. jqplot-gnucash

Commits

Andy Clayton  committed 80599fa

Improve certain default tick label positioning and CategoryAxisRenderer.pack cleanup.

Improves x2axis positioning, yaxis positioning when angle is 90 or -90, and possibly other cases.

  • Participants
  • Parent commits 058e50b
  • Branches default

Comments (0)

Files changed (1)

File src/plugins/jqplot.categoryAxisRenderer.js

View file
                 return p * unitlength / pixellength + max;
             };
         }
-        
+
+        var xaxis = (this.name == 'xaxis' || this.name == 'x2axis');
+        var primaryAxis = (this.name == 'xaxis' || this.name == 'yaxis');
+
+        var outerSizeParallel = function(o, includeMargin) {
+            return xaxis ? o.outerWidth(includeMargin) : o.outerHeight(includeMargin);
+        };
+
+        var outerSizePerpendicular = function(o, includeMargin) {
+            return xaxis ? o.outerHeight(includeMargin) : o.outerWidth(includeMargin);
+        };
+       
+        var edge;
+        var labelEdge;
+        if (xaxis) {
+           edge = 'left';
+           labelEdge = primaryAxis ? 'bottom' : 'top';
+        }
+        else {
+           edge = 'top';
+           labelEdge = primaryAxis ? 'left' : 'right';
+        } 
+
         if (this.show) {
-            if (this.name == 'xaxis' || this.name == 'x2axis') {
-                for (i=0; i<ticks.length; i++) {
-                    var t = ticks[i];
-                    if (t.show && t.showLabel) {
-                        var shim;
-                        
-                        if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
-                            // will need to adjust auto positioning based on which axis this is.
-                            var temp = (this.name == 'xaxis') ? 1 : -1;
-                            switch (t.labelPosition) {
-                                case 'auto':
-                                    // position at end
-                                    if (temp * t.angle < 0) {
-                                        shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
-                                    }
-                                    // position at start
-                                    else {
-                                        shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
-                                    }
-                                    break;
-                                case 'end':
-                                    shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
-                                    break;
-                                case 'start':
-                                    shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
-                                    break;
-                                case 'middle':
-                                    shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
-                                    break;
-                                default:
-                                    shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
-                                    break;
+            for (i=0; i<ticks.length; i++) {
+                var t = ticks[i];
+                if (t.show && t.showLabel) {
+                    var positionOffset;
+                    var shim = 0;
+                    var labelPosition = 'middle';
+                    if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
+                        // will need to adjust auto positioning based on which axis this is.
+                        var temp = primaryAxis ? 1 : -1;
+                        var side = (xaxis ? t._textRenderer.height : t._textRenderer.width); 
+                        shim = -temp * side * Math.sin(t._textRenderer.angle) / 2; 
+                        labelPosition = t.labelPosition;
+                        if (labelPosition == 'auto') {
+                            if (t.angle % 90 == 0) {
+                                labelPosition = 'middle';
+                            }
+                            else if (temp * t.angle < 0) {
+                                labelPosition = 'end';
+                            }
+                            else {
+                                labelPosition = 'start';
                             }
                         }
-                        else {
-                            shim = -t.getWidth()/2;
+                    }
+                    if (xaxis) {
+                        switch (labelPosition) {
+                            case 'end':
+                                positionOffset = -t.getWidth();
+                                break;
+                            case 'start':
+                                positionOffset = 0;
+                                break;
+                            case 'middle':
+                            default:
+                                positionOffset = -t.getWidth()/2;
+                                shim = 0;
+                                break;
                         }
-                        var val = this.u2p(t.value) + shim + 'px';
-                        t._elem.css('left', val);
-                        t.pack();
+                    } else {
+                        positionOffset = -t.getHeight()/2;
+                        switch (labelPosition) {
+                            case 'end':
+                            case 'start':
+                                // nothing
+                                break;
+                            case 'middle':
+                            default:
+                                shim = 0;
+                                break;
+                        }
+                    }
+                    var val = this.u2p(t.value) + positionOffset + shim + 'px';
+                    t._elem.css(edge, val);
+                    t.pack();
+                }
+            }
+          
+            var labelEdgeOffset = 0;
+            // draw the axis label
+            if (lshow) {
+                var labelsize = outerSizeParallel(this._label._elem, true);
+                var start = (xaxis ? offmin : offmax);
+                this._label._elem.css(edge, start + Math.abs(pixellength)/2 - labelsize/2 + 'px');
+                this._label._elem.css(labelEdge, labelEdgeOffset + 'px');
+                labelEdgeOffset += outerSizePerpendicular(this._label._elem, true);
+                this._label.pack();
+            }
+            
+            // draw the group labels
+            var step = parseInt(this._ticks.length/this.groups, 10);
+            for (i=0; i<this._groupLabels.length; i++) {
+                var mid = 0;
+                var count = 0;
+                for (var j=i*step; j<=(i+1)*step; j++) {
+                    if (this._ticks[j]._elem && this._ticks[j].label != " ") {
+                        var t = this._ticks[j]._elem;
+                        var p = t.position();
+                        mid += (xaxis ? p.left : p.top) + outerSizeParallel(t, true)/2;
+                        count++;
                     }
                 }
-                
-                var labeledge=['bottom', 0];
-                if (lshow) {
-                    var w = this._label._elem.outerWidth(true);
-                    this._label._elem.css('left', offmin + pixellength/2 - w/2 + 'px');
-                    if (this.name == 'xaxis') {
-                        this._label._elem.css('bottom', '0px');
-                        labeledge = ['bottom', this._label._elem.outerHeight(true)];
-                    }
-                    else {
-                        this._label._elem.css('top', '0px');
-                        labeledge = ['top', this._label._elem.outerHeight(true)];
-                    }
-                    this._label.pack();
-                }
-                
-                // draw the group labels
-                var step = parseInt(this._ticks.length/this.groups, 10);
-                for (i=0; i<this._groupLabels.length; i++) {
-                    var mid = 0;
-                    var count = 0;
-                    for (var j=i*step; j<=(i+1)*step; j++) {
-                        if (this._ticks[j]._elem && this._ticks[j].label != " ") {
-                            var t = this._ticks[j]._elem;
-                            var p = t.position();
-                            mid += p.left + t.outerWidth(true)/2;
-                            count++;
-                        }
-                    }
-                    mid = mid/count;
-                    this._groupLabels[i].css({'left':(mid - this._groupLabels[i].outerWidth(true)/2)});
-                    this._groupLabels[i].css(labeledge[0], labeledge[1]);
-                }
-            }
-            else {
-                for (i=0; i<ticks.length; i++) {
-                    var t = ticks[i];
-                    if (t.show && t.showLabel) {                        
-                        var shim;
-                        if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
-                            // will need to adjust auto positioning based on which axis this is.
-                            var temp = (this.name == 'yaxis') ? 1 : -1;
-                            switch (t.labelPosition) {
-                                case 'auto':
-                                    // position at end
-                                case 'end':
-                                    if (temp * t.angle < 0) {
-                                        shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
-                                    }
-                                    else {
-                                        shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
-                                    }
-                                    break;
-                                case 'start':
-                                    if (t.angle > 0) {
-                                        shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
-                                    }
-                                    else {
-                                        shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
-                                    }
-                                    break;
-                                case 'middle':
-                                    // if (t.angle > 0) {
-                                    //     shim = -t.getHeight()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
-                                    // }
-                                    // else {
-                                    //     shim = -t.getHeight()/2 - t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
-                                    // }
-                                    shim = -t.getHeight()/2;
-                                    break;
-                                default:
-                                    shim = -t.getHeight()/2;
-                                    break;
-                            }
-                        }
-                        else {
-                            shim = -t.getHeight()/2;
-                        }
-                        
-                        var val = this.u2p(t.value) + shim + 'px';
-                        t._elem.css('top', val);
-                        t.pack();
-                    }
-                }
-                
-                var labeledge=['left', 0];
-                if (lshow) {
-                    var h = this._label._elem.outerHeight(true);
-                    this._label._elem.css('top', offmax - pixellength/2 - h/2 + 'px');
-                    if (this.name == 'yaxis') {
-                        this._label._elem.css('left', '0px');
-                        labeledge = ['left', this._label._elem.outerWidth(true)];
-                    }
-                    else {
-                        this._label._elem.css('right', '0px');
-                        labeledge = ['right', this._label._elem.outerWidth(true)];
-                    }   
-                    this._label.pack();
-                }
-                
-                // draw the group labels, position top here, do left after label position.
-                var step = parseInt(this._ticks.length/this.groups, 10);
-                for (i=0; i<this._groupLabels.length; i++) {
-                    var mid = 0;
-                    var count = 0;
-                    for (var j=i*step; j<=(i+1)*step; j++) {
-                        if (this._ticks[j]._elem && this._ticks[j].label != " ") {
-                            var t = this._ticks[j]._elem;
-                            var p = t.position();
-                            mid += p.top + t.outerHeight()/2;
-                            count++;
-                        }
-                    }
-                    mid = mid/count;
-                    this._groupLabels[i].css({'top':mid - this._groupLabels[i].outerHeight()/2});
-                    this._groupLabels[i].css(labeledge[0], labeledge[1]);
-                    
-                }
+                mid = mid/count;
+                this._groupLabels[i].css(edge, mid - outerSizeParallel(this._groupLabels[i], true)/2);
+                this._groupLabels[i].css(labelEdge, labelEdgeOffset);
             }
         }
     };    
     
     
-})(jQuery);
+})(jQuery)