消失的BarSeries標籤文字 - Missing Labels on Bar Series
[更新@2008.12.16]
關於更改Series上的label,修改InstanceCache宣告會更快更好:
*To manipulate label instances on series, overridding the instanceCache object is a better approach.
**/
labelCache = new InstanceCache( Label , labelContainer )
labelCache.property = { truncateToFit : false };
使用Bar Chart 的時候,有個現象:
當Bar上頭的標籤文字太長,以至於無法放進畫面中,BarChart 會清除掉裡面的文字內容:
label.text = “”; (請見BarChart.as line 662)
好在,它只是清掉renderData的cache,並非真正的dataProvider內容(如果是,就糟了),也因此,我們有機會把這些文字回覆,逼它無論如何都要顯示,方法很簡單,你可以自訂一個BarSeries,請見如下程式碼。走訪dataprovider,把值塞回去給Label 實體即可,但請確保文字框夠大,或是把truncateToFit改成false,這樣就不會被裁切掉了。
[English version]
A Bar-Chart renders several series where those labels show up. If you have read source of BarChart.as, you would aware of one thing: If by checking the TextField width against remaining space, there is no enough space to place the label , the label text will be eliminated. ( see source: BarChart.as Line 662 ) However, I think Adobe team might do too much here so that even if you set explicit textField width with a very large number, it won’t work. Fortunately, BarChart itself clear the renderCache instead of dataprovider so that we could recover the text of each label. Here is an example:
//override updateDisplayList()
override protected function updateDisplayList( w:Number , h:Number )
{
super.udpateDisplayList( w , h );
//here we get all created *Label* instances
var labels:Array = labelCache.instances;
// cursor is a IViewCursor, iterating the dataprovider so that cursor#view is just dataprovider object.
var dataList:Array = IList( cursor.view ).toArray();
var len:int = labels.length;
//iterate all label instance, if the text is removed, re-assign it and validate as well.
//one more thing, the length of labels and dataprovider should be the same.
var label:Label;
for(var i:int = 0 ; i < len ; i++ )
{
label = Label( labels[i] );
if( label )
{
label.truncateToFit = false; //optional
label.width = Math.max(w,300); //optional
if( label.text == "" && labelFunction != null )
{
var c:ChartItem = ChartItem( items[i] );
c.item = v[i];
var txt:String = this.labelFunction( c , this )
label.text = txt;
}
label.validateNow();
}
}
Tags: flex
