BottomAxisLayout.js revision a5057260e5538ddf2faca20fa81271eeff2bf892
/**
* Contains algorithms for rendering a bottom axis.
*
* @class BottomAxisLayout
* @Constructor
*/
BottomAxisLayout = function(){};
BottomAxisLayout.prototype = {
/**
* Default margins for text fields.
*
* @private
* @method _getDefaultMargins
* @return Object
*/
_getDefaultMargins: function()
{
return {
top: 4,
left: 0,
right: 0,
bottom: 0
};
},
/**
* Sets the length of the tick on either side of the axis line.
*
* @method setTickOffsets
* @protected
*/
setTickOffsets: function()
{
var host = this,
majorTicks = host.get("styles").majorTicks,
tickLength = majorTicks.length,
halfTick = tickLength * 0.5,
display = majorTicks.display;
host.set("leftTickOffset", 0);
host.set("rightTickOffset", 0);
switch(display)
{
case "inside" :
host.set("topTickOffset", tickLength);
host.set("bottomTickOffset", 0);
break;
case "outside" :
host.set("topTickOffset", 0);
host.set("bottomTickOffset", tickLength);
break;
case "cross":
host.set("topTickOffset", halfTick);
host.set("bottomTickOffset", halfTick);
break;
default:
host.set("topTickOffset", 0);
host.set("bottomTickOffset", 0);
break;
}
},
/**
* Calculates the coordinates for the first point on an axis.
*
* @method getLineStart
* @protected
*/
getLineStart: function()
{
var style = this.get("styles"),
padding = style.padding,
majorTicks = style.majorTicks,
tickLength = majorTicks.length,
display = majorTicks.display,
pt = {x:0, y:padding.top};
if(display === "inside")
{
pt.y += tickLength;
}
else if(display === "cross")
{
pt.y += tickLength/2;
}
return pt;
},
/**
* Draws a tick
*
* @method drawTick
* @param {Object} pt hash containing x and y coordinates
* @param {Object} tickStyles hash of properties used to draw the tick
* @protected
*/
drawTick: function(pt, tickStyles)
{
var host = this,
style = host.get("styles"),
padding = style.padding,
tickLength = tickStyles.length,
start = {x:pt.x, y:padding.top},
end = {x:pt.x, y:tickLength + padding.top};
host.drawLine(start, end, tickStyles);
},
/**
* Calculates the point for a label.
*
* @method getLabelPoint
* @param {Object} pt Object containing x and y coordinates
* @return Object
* @protected
*/
getLabelPoint: function(point)
{
return {x:point.x, y:point.y + this.get("bottomTickOffset")};
},
/**
* Updates the value for the <code>maxLabelSize</code> for use in calculating total size.
*
* @method updateMaxLabelSize
* @param {HTMLElement} label to measure
* @protected
*/
updateMaxLabelSize: function(label)
{
var host = this,
props = this._labelRotationProps,
rot = props.rot,
absRot = props.absRot,
sinRadians = props.sinRadians,
cosRadians = props.cosRadians,
m11 = props.m11,
m12 = props.m12,
m21 = props.m21,
m22 = props.m22,
max;
if(!DOCUMENT.createElementNS)
{
label.style.filter = 'progid:DXImageTransform.Microsoft.Matrix(M11=' + m11 + ' M12=' + m12 + ' M21=' + m21 + ' M22=' + m22 + ' sizingMethod="auto expand")';
host.set("maxLabelSize", Math.max(host.get("maxLabelSize"), label.offsetHeight));
}
else
{
label.style.msTransform = "rotate(0deg)";
if(rot === 0)
{
max = label.offsetHeight;
}
else if(absRot === 90)
{
max = label.offsetWidth;
}
else
{
max = (sinRadians * label.offsetWidth) + (cosRadians * label.offsetHeight);
}
host.set("maxLabelSize", Math.max(host.get("maxLabelSize"), max));
}
},
/**
* Rotate and position title.
*
* @method positionTitle
* @param {HTMLElement} label to rotate position
* @protected
*/
positionTitle: function(label)
{
var host = this,
max,
styles = host.get("styles").title,
props = this._getTextRotationProps(styles),
rot = props.rot,
absRot = props.absRot,
sinRadians = props.sinRadians,
cosRadians = props.cosRadians,
x = this.get("width")/2,
y = this.get("maxLabelSize") + this.get("styles").label.margin.top + styles.margin.top + this.get("bottomTickOffset"),
leftOffset = 0,
topOffset = 0,
labelWidth = label.offsetWidth,
labelHeight = label.offsetHeight;
if(Y.config.doc.createElementNS)
{
if(rot === 0)
{
max = labelHeight;
leftOffset -= labelWidth * 0.5;
}
else if(absRot === 90)
{
max = labelWidth;
if(rot === -90)
{
topOffset += labelWidth;
leftOffset -= labelHeight;
}
}
else
{
max = (sinRadians * labelWidth) + (cosRadians * labelHeight);
if(rot > 0)
{
leftOffset -= (cosRadians * labelWidth)/2 - (sinRadians * labelHeight)/2;
}
else
{
topOffset += (sinRadians * labelWidth) - (cosRadians * labelHeight)/2;
leftOffset -= (cosRadians * labelWidth)/2 + (sinRadians * labelHeight)/2;
}
}
}
else
{
if(rot === 0)
{
leftOffset -= labelWidth * 0.5;
max = labelHeight;
}
else if(rot === 90)
{
leftOffset -= labelHeight * 0.5;
max = labelWidth;
}
else if(rot === -90)
{
leftOffset -= labelHeight * 0.5;
max = labelWidth;
}
else
{
max = (sinRadians * labelWidth) + (cosRadians * labelHeight);
leftOffset -= ((cosRadians * labelWidth) + (sinRadians * labelHeight))/2;
}
}
x += leftOffset;
y += topOffset;
label.style.left = x + "px";
label.style.top = y + "px";
this._titleSize = max;
this._rotate(label, props);
},
/**
* Rotate and position labels.
*
* @method positionLabel
* @param {HTMLElement} label to rotate position
* @param {Object} pt hash containing the x and y coordinates in which the label will be positioned
* against.
* @protected
*/
positionLabel: function(label, pt)
{
var host = this,
tickOffset = host.get("bottomTickOffset"),
style = host.get("styles").label,
margin = 0,
props = this._labelRotationProps,
rot = props.rot,
absRot = props.absRot,
sinRadians = props.sinRadians,
cosRadians = props.cosRadians,
leftOffset = Math.round(pt.x),
topOffset = Math.round(pt.y),
labelWidth = Math.round(label.offsetWidth),
labelHeight = Math.round(label.offsetHeight);
if(style.margin && style.margin.top)
{
margin = style.margin.top;
}
if(!DOCUMENT.createElementNS)
{
label.style.filter = null;
labelWidth = Math.round(label.offsetWidth);
labelHeight = Math.round(label.offsetHeight);
if(absRot === 90)
{
leftOffset -= labelHeight * 0.5;
}
else if(rot < 0)
{
leftOffset -= cosRadians * labelWidth;
leftOffset -= sinRadians * (labelHeight * 0.5);
}
else if(rot > 0)
{
leftOffset -= sinRadians * (labelHeight * 0.5);
}
else
{
leftOffset -= labelWidth * 0.5;
}
topOffset += margin;
topOffset += tickOffset;
label.style.left = Math.round(leftOffset) + "px";
label.style.top = Math.round(topOffset) + "px";
this._rotate(label, props);
return;
}
label.style.msTransform = "rotate(0deg)";
labelWidth = Math.round(label.offsetWidth);
labelHeight = Math.round(label.offsetHeight);
if(rot === 0)
{
leftOffset -= labelWidth * 0.5;
}
else if(absRot === 90)
{
if(rot === 90)
{
leftOffset += labelHeight * 0.5;
}
else
{
topOffset += labelWidth;
leftOffset -= labelHeight * 0.5;
}
}
else
{
if(rot < 0)
{
leftOffset -= (cosRadians * labelWidth) + (sinRadians * (labelHeight * 0.6));
topOffset += sinRadians * labelWidth;
}
else
{
leftOffset += Math.round(sinRadians * (labelHeight * 0.6));
}
}
topOffset += margin;
topOffset += tickOffset;
label.style.left = Math.round(leftOffset) + "px";
label.style.top = Math.round(topOffset) + "px";
this._rotate(label, props);
},
/**
* Calculates the size and positions the content elements.
*
* @method setSizeAndPosition
* @protected
*/
setSizeAndPosition: function()
{
var host = this,
labelSize = host.get("maxLabelSize"),
tickLength = host.get("bottomTickLength"),
style = host.get("styles"),
sz = tickLength + labelSize,
margin = style.label.margin;
if(margin && margin.top)
{
sz += margin.top;
}
sz = Math.round(sz);
host.set("height", sz);
},
/**
* Adjusts position for inner ticks.
*
* @method offsetNodeForTick
* @param {Node} cb contentBox of the axis
* @protected
*/
offsetNodeForTick: function(cb)
{
var host = this;
host.get("contentBox").setStyle("top", 0 - host.get("topTickOffset"));
},
/**
* Assigns a height based on the size of the contents.
*
* @method setCalculatedSize
* @protected
*/
setCalculatedSize: function()
{
var host = this,
style = host.get("styles").label,
ttl = Math.round(host.get("bottomTickOffset") + host.get("maxLabelSize") + style.margin.top + this.get("styles").title.margin.top + this._titleSize);
host.set("height", ttl);
}
};
Y.BottomAxisLayout = BottomAxisLayout;