Posts Tagged ‘flex’

Column Chart v.s. Bar Chart

Wednesday, October 1st, 2008

Well, it was confusing. I was thinking bar charts and column charts were doing the same thing. The distinct appearance of them is the direction of the “bar”. Actually, you could call columns “bars” if you want. Thus, I event didn’t get this critical idea that a bar chart CANNOT draw bars vertically. You know what? I struggled with this for hours. Finally I realized it.

So, please remember:
A Column Chart is showing number of columns vertically, which seems to “stand” on the horizontal axis.
A Bar Chart is showing number of bars horizontally, like a rotation column chart.
However, they are logically different.

測量文字框

Wednesday, September 17th, 2008

有個很方便的方法:
UIComponent#measureText( text:String ):TextLineMetrics
先看看官方說的:
Measures the specified text, assuming that it is displayed in a single-line UITextField using a UITextFormat determined by the styles of this UIComponent.

簡單來說,這個方法會根據你當下使用的文字樣式,測量一段文字的長寬高。舉例來說, “Taiwan” 這幾個字,在各種字體、字型大小設定下,顯示出來的結果當然不同(比如字體大小11pt時高度為21px; 14pt時高度為34px…) ,為了要得到正確的文字框數據,就可以運用該方法。
那麼,到底什麼時候會用到呢? 很多時候,在開發與TextField相關的自訂元件時,會大量地運用到,譬如 Button,裡頭的Label和icon要對齊,label的位置就利用了此方法測量出來,然後位移之。

看看原始碼,應該會常常看到這種寫法:
因為大寫W是最高的字, 小寫j是最低的字, 如此可以算出最大的文字框高度
measureText("Wj");

算出現有的文字框高度
measureText(textField.text);


UIComponent#measureText(text:String):TextLineMetrics
It is a very handy method for determining the bounds of a specific text. To illustrate, with different text format, the appearance of text must be different. In other words, the height, width and other values will be changed as well. Therefore, to make sure we get the correct and precise values of it, we can call this method to get a TextLineMetrics object, which provides all the properties. Given that object, we could update positions of components precisely.

For example, while the label of a button changes, we could measure it by
measureText(label)
or
to get a safe size of it, use following code:
measureText("Wj")
Here is a thing, the capital w and lowercase j are the latter with highest baseline and the lowest. That’s why Adobe developer often writes up code in this way.

FYI

Hide Chart Elements

Sunday, August 24th, 2008

First of all, a visual chart, generated via Flex-Charting framework, is constructed with many layers of elements. They are series(data) layer, axis layer, background elements and annotation elements. Besides, you are able to create your own chart elements by implementing IChartElement2 interface.

Here are some quick tips:

  • To kill the grid lines behind charts:
    backgroundElements=[]
  • To make the grid lines drawn with larger interval:
    GridLines#horizontalChangeCount = larger_number;
  • hide elements of axises
    AxisRenderer#showLabels = false;
    AxisRenderer#showLine = false;
    AxisRenderer#tickPlacements = “none”;

用Filter Function作簡單線性圖Drill-Down

Monday, July 28th, 2008

首先,想像有一張線性圖(Line Chart),上頭的線是由許多資料點所構成。在Flex中,一旦對該資料及和作了過濾的動作,圖表會重繪。然後,Flex3的圖表都有支援selectedItems:Array的方法,以獲取你圈選的資料點,因此,我們可以藉由選取的資料點,得到一個範圍,利用範圍來過濾原資料集合,就可以做到類似Drill-Down的功能了。

這是範例(線性圖的drill down)
這是原始碼


Drill down into data in a line chart.

With Flex3 data visualization package, we are able to get the selectedItems by
ChartBase.selectionMode = "multiple";
ChartBase.selectedItems

Therefore, it is handy to measure a range for filtering out our data. We have to know that every series in a chart contains a data provider. The data providers are ListCollectionView objects. So to filter out those objects will update the display of chart immediately. Those lines will be redrawn. You could see the line scaled just like drilling-down into it.

Here is a sample and source code.
Simple Drill Down into Line Chart
Source

提昇Flex圖表效能

Friday, July 25th, 2008

提昇Flex圖表元件 (Charting Components) 的效能,當你遇到圖表元件的繪製速度慢到想殺人的時候,建議你先從資料開始檢查,再看看圖表元件本身。

  • 如果可以,移除掉圖表效果,像是陰影

    ChartBase.seriesFilters=[]

  • 圖表的data-provider別餵太複雜的物件,尤其是很肥大的XML應該盡量避免,真的必要,可以作adapter物件來繪製之。
  • 當需要畫出很多條不同的資料線時,不要來一筆資料餵一次series (chartBase.series),能的話,讀完了再一次餵,因為一旦執行

    ChartBase.series = your_new_series_list

    會逼迫ChartBase重繪*所有的*的視覺元件,包含標籤、矩陣線、圖表線、軸線…等,很恐怖唷!尤其是你的資料又長到個不行時,老闆不開心、自己也難過。

  • 作線性圖( Line Chart )時,別輕易地變更線的樣式,尤其”form”, “miter”, “joint”這幾個,你可以看看官方的原始碼,對於這些屬性有非常不同的繪製演算法。用預設值吧,我相信Flex開發團隊已經先最佳化過了。
  • Gutter (gutter top, left, right, bottom) 能的話先給預設值,可以省下運算時間
  • 小心使用軸線的單位(unit),針對你的資料多寡,給予適當的單位值,如果你的資料有一千年,把單位切成秒數就不是個明智的作法。

以下是本文的英文版:

Enhance Flex Charting Components Performance

  • Clear the beautiful shadows or any other filters.
    ChartBase.seriesFilters=[]
  • Optimize your data objects to be rendered. Do not pass a bunch of huge, complex objects to the data-provider.
  • While building a line chart, do NOT change the lineStroke attributes such as “miter”, “joint” if not necessary. Flex Data-Visualization components has optimized the drawing performance to default renderers. To change these attributes may bring up a terrible risk.
  • You can remove the grid lines.
  • Set explicit gutter values while defining a chart base object.
  • If you have to render multiple series in a single chart, try to finish loading all data-providers before you assign series list to chart. Calling this method:

    ChartBase.series = your_list

    will force the chart to remove *ALL* chart elements( label, grid, axis, series items…) and re-create them. It is really time consuming to refresh the chart if re-assign the series objects again and again.

  • Set a lower quality of your flash movie( not available for AIR)
  • Mind your axis units. Be very careful while showing up a time-based data set. It is very possible to be the key to skew up your chart performance if the unit is set too small. Given an appropriate unit is highly recommended.

Cool Down Button

Wednesday, May 7th, 2008

Blizzard公司出品的Warcraft III (魔獸爭霸III)遊戲介面中, 有些按鈕是有Cool-Down 的限制,也就是說,當你按下某個按鈕(通常是施展法術或是喝藥水),無法立刻按該按鈕,它會處於disabled的狀態,必須經過若干秒數(端看該法術設定)。

簡單地做了這個按鈕元件,沒什麼特別的,真的,以下是演示:
Flex Cool-Down Button Demo
( 進入Demo後按右鍵可以下載原始碼(view source) )

使用方法:

Actionscript:
  1. <controls:CoolDownButton
  2. width="64"
  3. height="64"
  4. coolDownSeconds="10"
  5. coolDownEnabled="true" 
  6. cornerRadius="0"
  7. />

keynote: 數位典藏

Tuesday, April 22nd, 2008

RIA 在數位典藏的實作應用:

投影片:
RIA Demo 投影片

原始檔:
RIA Demo 範例原始檔

取得HierarchicalCollectoinView的第一筆物件

Tuesday, April 15th, 2008

這個標題看起來很蠢,不過,HierarchicalCollectionView 提供的API本身並無 getItem() , getItemAt() 之類的方法,因此如何取得資料結構中第一筆?

方法一:
用ViewCursor, 新建立的cursor就是指向第一筆
myHCV.createCursor().current;

方法二: (由不願具名的魔人提供的高級作法)
myHCV.mx_internal::treeData.getItemAt(0);
treeData 其實是HCV真正在操作的資料集合,它是一個ICollectionView,因此可以引用getItem()...等方法。

拖拉物件從ListBase 到 AdvancedListBase

Monday, April 7th, 2008

把傳統的ListBase item 拖拉到 AdvancedListBase 基本上是不會成功的,因為在DragSource::dataFormat,一個是"items", 一個是"treeDataGridItems" ,我們可在DragDrop發生之前,用DragSource::addData()改變DragEvent::dragSource 內資料定義即可, 這樣ADG就會Accept Drop了(會顯示Drop Feedback)

Actionscript:
  1. function onDragEnter(event:DragEvent):void{
  2. event.dragSource.addData( myObject , "treeDataGridItems" );
  3. }

發現一些好用的方法:
可以得到目前拖放物件在List中的索引位置!
AdvancedListBase::calculateDropIndex ( event:DragEvent ) : int

藉由索引值取得對應的itemRenderer instance
AdvancedListBase::indexToItemRenderer( index :int ):IListItemRenderer

結合兩者:

Actionscript:
  1. function onDragDrop(event:DragEvent){
  2. var ir:IListItemRenderer = adg.indexToItemRenderer( adg.calculateDropIndex (event ) );
  3. var myData:Object = ir.data;
  4. var hv:HierarchicalCollectionView = adg.dataProvider as HierarchicalCollectionView;
  5. //取得父節點,插入新物件
  6. var parentNode : Object = hv.getParentItem( myData );
  7. var myNewData:Object = event.dragSource.dataForFormat("treeDataGridItems"); //取出新物件
  8. hv.addChildAt( parentNode, myData , 0 ); //放入
  9.  
  10. adg.validateNow();
  11. }

使ADG吃完資料且繪製完後自動展開

Tuesday, April 1st, 2008

答案出乎意料的簡單,特寫此文以表敬意, 請用:

AdvancedDataGrid::displayItemsExpanded = true;

官方文件說明:
If true, expand the navigation tree to show all items. If a new branch is added, it will be shown expanded.
The default value is false.

奧妙往往都在手冊之中呢。