RightAxisLayout.js revision 82d0cf8c731b23f6a2fbb31e3e696e629444363e
/**
* Contains algorithms for rendering a right axis.
*/
function RightAxisLayout(config)
{
RightAxisLayout.superclass.constructor.apply(this, arguments);
}
RightAxisLayout.ATTRS = {
axisRenderer: {
value: null
}
};
Y.extend(RightAxisLayout, Y.Base, {
/**
* Sets the length of the tick on either side of the axis line.
*/
setTickOffsets: function()
{
var ar = this.get("axisRenderer"),
majorTicks = ar.get("styles").majorTicks,
tickLength = majorTicks.length,
halfTick = tickLength * 0.5,
display = majorTicks.display;
ar.set("topTickOffset", 0);
ar.set("bottomTickOffset", 0);
switch(display)
{
case "inside" :
ar.set("leftTickOffset", tickLength);
break;
case "outside" :
ar.set("rightTickOffset", tickLength);
break;
case "cross":
ar.set("rightTickOffset", halfTick);
ar.set("leftTickOffset", halfTick);
break;
}
},
drawTick: function(pt, tickStyles)
{
var ar = this.get("axisRenderer"),
style = ar.get("styles"),
padding = style.padding,
tickLength = tickStyles.length,
start = {x:padding.left, y:pt.y},
end = {x:padding.left + tickLength, y:pt.y};
ar.drawLine(start, end, tickStyles);
},
/**
* Calculates the coordinates for the first point on an axis.
*/
getLineStart: function()
{
var ar = this.get("axisRenderer"),
style = ar.get("styles"),
padding = style.padding,
majorTicks = style.majorTicks,
tickLength = majorTicks.length,
display = majorTicks.display,
pt = {x:padding.left, y:padding.top};
if(display === "inside")
{
pt.x += tickLength;
}
else if(display === "cross")
{
pt.x += tickLength/2;
}
return pt;
},
/**
* Calculates the point for a label.
*/
getLabelPoint: function(point)
{
var ar = this.get("axisRenderer");
return {x:point.x + ar.get("rightTickOffset"), y:point.y};
},
updateMaxLabelSize: function(label)
{
var ar = this.get("axisRenderer"),
style = ar.get("styles").label,
rot = Math.min(90, Math.max(-90, style.rotation)),
absRot = Math.abs(rot),
radCon = Math.PI/180,
sinRadians = parseFloat(parseFloat(Math.sin(absRot * radCon)).toFixed(8)),
cosRadians = parseFloat(parseFloat(Math.cos(absRot * radCon)).toFixed(8)),
max;
if(Y.UA.ie)
{
label.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(rotation=" + rot + ")";
this.set("maxLabelSize", Math.max(this.get("maxLabelSize"), label.offsetWidth));
}
else
{
if(rot === 0)
{
max = label.offsetWidth;
}
else if(absRot === 90)
{
max = label.offsetHeight;
}
else
{
max = (cosRadians * label.offsetWidth) + (sinRadians * label.offsetHeight);
}
this.set("maxLabelSize", Math.max(this.get("maxLabelSize"), max));
}
},
positionLabel: function(label, pt)
{
var ar = this.get("axisRenderer"),
style = ar.get("styles").label,
labelAlpha = style.alpha,
filterString,
margin = 0,
leftOffset = pt.x,
topOffset = pt.y,
rot = Math.min(Math.max(style.rotation, -90), 90),
absRot = Math.abs(rot),
radCon = Math.PI/180,
sinRadians = parseFloat(parseFloat(Math.sin(absRot * radCon)).toFixed(8)),
cosRadians = parseFloat(parseFloat(Math.cos(absRot * radCon)).toFixed(8)),
m11 = cosRadians,
m12 = rot > 0 ? -sinRadians : sinRadians,
m21 = -m12,
m22 = m11;
if(style.margin && style.margin.right)
{
margin = style.margin.right;
}
if(Y.UA.ie)
{
label.style.filter = null;
if(rot === 0)
{
topOffset -= label.offsetHeight * 0.5;
}
else if(absRot === 90)
{
topOffset -= label.offsetWidth * 0.5;
}
else if(rot > 0)
{
topOffset -= (cosRadians * (label.offsetHeight * 0.5));
}
else
{
topOffset -= (sinRadians * label.offsetWidth) + (cosRadians * (label.offsetHeight * 0.5));
}
leftOffset += margin;
label.style.left = leftOffset + "px";
label.style.top = topOffset + "px";
if(Y.Lang.isNumber(labelAlpha))
{
filterString = "progid:DXImageTransform.Microsoft.Alpha(Opacity=" + Math.round(labelAlpha * 100) + ")";
}
if(rot !== 0)
{
if(filterString)
{
filterString += " ";
}
else
{
filterString = "";
}
filterString += 'progid:DXImageTransform.Microsoft.Matrix(M11=' + m11 + ' M12=' + m12 + ' M21=' + m21 + ' M22=' + m22 + ' sizingMethod="auto expand")';
}
if(filterString)
{
label.style.filter = filterString;
}
return;
}
if(rot === 0)
{
topOffset -= label.offsetHeight * 0.5;
}
else if(rot === 90)
{
leftOffset += label.offsetHeight;
topOffset -= label.offsetWidth * 0.5;
}
else if(rot === -90)
{
topOffset += label.offsetWidth * 0.5;
}
else if(rot < 0)
{
topOffset -= (cosRadians * (label.offsetHeight * 0.6));
}
else
{
topOffset -= cosRadians * (label.offsetHeight * 0.6);
leftOffset += sinRadians * label.offsetHeight;
}
leftOffset += margin;
label.style.left = leftOffset + "px";
label.style.top = topOffset + "px";
label.style.MozTransformOrigin = "0 0";
label.style.MozTransform = "rotate(" + rot + "deg)";
label.style.webkitTransformOrigin = "0 0";
label.style.webkitTransform = "rotate(" + rot + "deg)";
},
/**
* Calculates the size and positions the content elements.
*/
setSizeAndPosition: function()
{
var ar = this.get("axisRenderer"),
labelSize = this.get("maxLabelSize"),
style = ar.get("styles"),
sz = style.line.weight,
majorTicks = style.majorTicks,
display = majorTicks.display,
tickLen = majorTicks.length;
if(display === "outside")
{
sz += tickLen;
}
else if(display === "cross")
{
sz += tickLen * 0.5;
}
sz += labelSize;
ar.set("width", sz);
},
offsetNodeForTick: function(cb)
{
var ar = this.get("axisRenderer"),
majorTicks = ar.get("styles").majorTicks,
tickLength = majorTicks.length,
display = majorTicks.display;
if(display === "inside")
{
cb.setStyle("left", 0 - tickLength + "px");
}
else if (display === "cross")
{
cb.setStyle("left", 0 - (tickLength * 0.5) + "px");
}
},
setCalculatedSize: function()
{
var ar = this.get("axisRenderer"),
style = ar.get("styles").label,
ttl = ar.get("rightTickOffset") + this.get("maxLabelSize") + style.margin.left;
ar.set("width", ttl);
}
});
Y.RightAxisLayout = RightAxisLayout;