簡體   English   中英

是否可以從單獨的ActionScript類更改彈性圖表的數據提供者?

[英]is it possible to change data provider of flex chart from a seperate ActionScript class?

我正在嘗試從單獨的動作腳本類中更改彈性圖表數據提供者? 可能嗎。 我沒有找到任何方法可以做到這一點。 有什么想法嗎?

是的你可以。 為此,您需要參考chart的dataprovider,並且它應該是可綁定的。 這意味着當您更新dataProvider時,您查看的視圖也將被更新(在這種情況下為圖表)。 如果沒有幫助,我會看到您的代碼。

是的你可以。 一個不錯的干凈方法是擁有一個自定義事件調度程序外觀:

package com.app.facades
{
    import flash.events.Event;
    import flash.events.EventDispatcher;

    [Event(name="showDataInGrid"   , type="com.app.events.GridEvent")]
    public class GridFacade extends EventDispatcher
    {
        private static var _instance:GridFacade;

        public function GridFacade(lock:SingletonLock, target:IEventDispatcher=null) {
            super(target);
            if(!(lock is SingletonLock)) {
                throw(new Error("GridFacade is a singleton, please do not make foreign instances of it"));
            }
        }

        public static function getInstance():GridFacade {
            if(!_instance) {
                _instance = new GridFacade(new SingletonLock());
            }
            return _instance;
        }
    }
}
class SingletonLock{}

創建一個可調度事件,如下所示:

package com.app.events {

    import flash.events.Event;
    import flash.events.IEventDispatcher;

    public class DispatchableEvent extends Event implements IDispatchableEvent {
        protected var _dispatcher:IEventDispatcher;

        public function DispatchableEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);
        }

        public function dispatch():void
        {
            _dispatcher.dispatchEvent(this);
        }
    }
}

然后有一個自定義GridEvent,如下所示:

package com.app.events {
    public class GridEvent extends DispatchableEvent {
        public static const SHOW_DATA_IN_GRID:String = "showDataInGrid";
        public var data:Object;
        public function GridEvent(type:String, 
                                  data:ArrayCollection = null, 
                                  bubbles:Boolean=false, 
                                  cancelable:Boolean=false) {
            super(type,bubbles,cancelable);
            _dispatcher = GridFacade.getInstance();
            this.data = data;
        }
    }
}

然后在網格組件范圍內偵聽showDataInGrid事件:

...
GridFacade.getInstance().addEventListener(GridEvent.SHOW_DATA_IN_GRID, onShowDataInGrid);
...

protected function onShowDataInGrid(event:GridEvent):void {
    myGrid.dataProvider = event.data;
    // refresh the collection so that the component will display the new data
    event.data.refresh();
    // remember to reset any data specific stuff you may have set in the grid component before doing this.
}

要實際更改數據,請在任何類中執行以下操作:

(new GridEvent(
    GridEvent.SHOW_DATA_IN_GRID,
    someCollectionToShowInTheGrid,
)).dispatch();

並觀看魔術! :)

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM