I saw an interesting forum post in Adobe Forums today
ttp://forums.adobe.com/post!reply.jspa?message=2012407
It was about a person wanting to wrap the text but the standard Legends not allowing you to do so. I imagined the solution was something easy so I tried out myself and... NO, it was not as easy as I first thought. I extended LegendItem and managed to get the text wrap and the LegendItem measure itself somewhat correctly but I had a problem with it setting itself to the largest item there is.
Out of my own interest, I took on the problem personally. After couple of hours later I think I fixed it!!?!?!....
View source is enabled so you can see exactly what I have done
I finally discovered that problem was actually in the Legend Class will ALWAYS space out the LegendItems evenly every time!!! So natually I tried to extend Legend class but there were so much private variables it was impossible. So in order to overcome this issue I had to make a copy of the Legend class to src/mx/charts/Legend.as and everything that it references to replicate its behavious exactly. This way, flex compiler will take priority over the actual Legend Class in the framework.
Then I modified some of the code so it will handle the case when wrapping items were required.
The percentageHeight of each item were always being set to 100
Legend.as
private function addLegendItem(legendData:Object):void
{
var c:Class = legendItemClass;
var newItem:LegendItem = new c();
newItem.marker = legendData.marker;
if (legendData.label != "")
newItem.label = legendData.label;
if (legendData.element)
newItem.element = legendData.element;
if ("fill" in legendData)
newItem.setStyle("fill",legendData["fill"]);
newItem.markerAspectRatio = legendData.aspectRatio;
newItem.legendData = legendData;
newItem.percentWidth = 100;
newItem.percentHeight = 100;
addChild(newItem);
newItem.setStyle("backgroundColor", 0xEEEEFF);
}
so I eliminated this implicit perventHeight assignment in the WrapLegendItem class with a setter
// overriding to ALWAYS SET TO percentHeight to ZERO
override public function set percentHeight(value:Number):void
{
super.percentHeight = 0;
}
Now, I fixed the layoutVertical() function further down in the Legend class.
The lines with --- are the additional lines I put into the Legend class. I wanted to keep the modification to minimum so it wouldn't affect its use for non-wrapping LegendItems.
--- // I add flag a for a wrapping LegendItem
--- var wrapTextItem:Boolean;
if (child.percentHeight > 0)
{
childHeight = Math.min(cellHeight,
cellHeight * child.percentHeight / 100);
}
else
{
--- // This condition is only true if it is a wrapping text LegendItem
childHeight = child.getExplicitOrMeasuredHeight();
--- wrapTextItem = true;
}
child.setActualSize(childWidth, childHeight);
// Align the child in the cell.
var xOffset:Number =
Math.floor(calcHorizontalOffset(child.width, horizontalAlign));
var yOffset:Number =
Math.floor(calcVerticalOffset(child.height, verticalAlign));
child.move(xPos + xOffset, yPos + yOffset);
if ((i % rowCount) == rowCount - 1)
{
yPos = vm.top;
xPos += (colWidth + horizontalGap);
}
else
{
--- // this is where the problem occurs
--- if(wrapTextItem)
--- //increment height by the child's height
--- yPos += (childHeight + verticalGap);
--- else
// this is normal behaviour
yPos += (cellHeight + verticalGap);
--- }
It's not an elegant "fix", and more of a monkey patch, but it works for now and doesn't seem to break anything else in Legend Class. Anyway, I think it was a great personal exercise and maybe help someone in the process.