Skip to content

Instantly share code, notes, and snippets.

@scothis
Created August 13, 2010 21:52
Show Gist options
  • Save scothis/523609 to your computer and use it in GitHub Desktop.
Save scothis/523609 to your computer and use it in GitHub Desktop.
dojo.provide("dojox.charting.axis2d.Titled");
dojo.require("dojox.charting.axis2d.Default");
dojo.require("dojox.gfx");
dojo.require("dojox.gfx.matrix");
(function(){
var dc = dojox.charting,
g = dojox.gfx,
m = dojox.gfx.matrix,
labelGap = 4; // in pixels
dojo.declare("dojox.charting.axis2d.Titled", dojox.charting.axis2d.Default, {
// summary:
// Adds title support to the Default axis
//
// Inspired by Brian Forbes, http://www.reigndropsfall.net/2010/08/12/dojox-charting-axis-titles/
getOffsets: function(){
// summary:
// Get the physical offset values for this axis (used in drawing data series).
// returns: Object
// The calculated offsets in the form of { l, r, t, b } (left, right, top, bottom).
var offsets = this.inherited(arguments),
o = this.opt;
if(o.title){
var ta = this.chart.theme.axis,
taFont = o.font || (ta.majorTick && ta.majorTick.font) || (ta.tick && ta.tick.font),
size = taFont ? g.normalizedLength(g.splitFontString(taFont).size) : 0,
leftBottom = o.leftBottom;
if(this.vertical){
offsets[leftBottom ? "l" : "r"] += size + labelGap;
}else{
offsets[leftBottom ? "b" : "t"] += size + labelGap;
}
}
return offsets; // Object
},
render: function(dim, offsets){
// summary:
// Render/draw the axis.
// dim: Object
// An object of the form { width, height}.
// offsets: Object
// An object of the form { l, r, t, b }.
// returns: dojox.charting.axis2d.Titled
// The reference to the axis for functional chaining.
if(!this.dirty){
return this; // dojox.charting.axis2d.Titled
}
this.inherited(arguments);
try {
var o = this.opt,
title = o.title,
ta = this.chart.theme.axis,
taFont = o.font || (ta.majorTick && ta.majorTick.font) || (ta.tick && ta.tick.font),
taFontColor = o.fontColor || (ta.majorTick && ta.majorTick.fontColor) || (ta.tick && ta.tick.fontColor) || "black",
size = taFont ? g.normalizedLength(g.splitFontString(taFont).size) : 0,
cm = this.chart.margins,
leftBottom = o.leftBottom;
if(title){
var x, y, rotate;
// If the axis is vertical, rotate it
if(this.vertical){
rotate = leftBottom ? 270 : 90;
x = leftBottom ? size + cm.l : dim.width - size - cm.r;
y = (offsets.t + dim.height - offsets.b) / 2;
}else{
rotate = 0;
x = (offsets.l + dim.width - offsets.r) / 2;
y = leftBottom ? dim.height - cm.b : cm.t;
}
// Render the text at the origin, transform into place later
var elem = this.group.createText({x: 0, y: 0, text: title, align: 'middle'});
elem.setFont(taFont).setFill(taFontColor);
// If the axis is vertical, rotate and move into position,
// otherwise just move into position.
if(rotate){
elem.setTransform([
m.translate(x, y),
m.rotateg(rotate)
]);
}else{
elem.setTransform(m.translate(x, y));
}
}
}catch(e){
// squelch
}
return this; // dojox.charting.axis2d.Titled
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment