Показать сообщение отдельно
Старый 17.03.2008, 12:54
Захаров вне форума Посмотреть профиль Отправить личное сообщение для Захаров Найти все сообщения от Захаров
  № 2  
Ответить с цитированием
Захаров

Регистрация: Oct 2007
Сообщений: 66
КОД декларативного содания олапа(Рабочий)
Код:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        creationComplete="creationCompleteHandler();" layout="absolute">

    <mx:Script>
      <![CDATA[
        import mx.rpc.AsyncResponder;
        import mx.rpc.AsyncToken;
        import mx.olap.OLAPQuery;
        import mx.olap.OLAPSet;
        import mx.olap.IOLAPQuery;
        import mx.olap.IOLAPQueryAxis;
        import mx.olap.IOLAPCube;
        import mx.olap.OLAPResult;
        import mx.events.CubeEvent;
        import mx.controls.Alert;
        import mx.collections.ArrayCollection;
        
        
        //
        // Format of Objects in the ArrayCollection:
        //
        //  data:Object = {
        //    customer:"AAA", 
        //    product:"ColdFusion",
        //    quarter:"Q1"
        //    revenue: "100.00" 
        //  }
        //

        [Bindable]
        private var flatData:ArrayCollection = new ArrayCollection(
        [
        {otd:"Отдел 1",pos:"Программист",year:2007,quarter:"Кв1",zp:20000},
        {otd:"Отдел 1",pos:"Стажер",year:2007,quarter:"Кв1",zp:15000},
        {otd:"Отдел 2",pos:"Программист",year:2007,quarter:"Кв1",zp:25000},
        {otd:"Отдел 2",pos:"Стажер",year:2007,quarter:"Кв1",zp:23000},
        {otd:"Отдел 1",pos:"Программист",year:2007,quarter:"Кв2",zp:21000},
        {otd:"Отдел 1",pos:"Стажер",year:2007,quarter:"Кв2",zp:16000},
        {otd:"Отдел 2",pos:"Программист",year:2007,quarter:"Кв2",zp:27000},
        {otd:"Отдел 2",pos:"Стажер",year:2007,quarter:"Кв2",zp:24000},
                 ]);
    
        private function creationCompleteHandler():void {
            // You must initialize the cube before you 
            // can execute a query on it.
            myMXMLCube.refresh();
        }

        // Create the OLAP query.
        private function getQuery(cube:IOLAPCube):IOLAPQuery {
            // Create an instance of OLAPQuery to represent the query. 
            var query:OLAPQuery = new OLAPQuery;
            
            // Get the row axis from the query instance.
            var rowQueryAxis:IOLAPQueryAxis = 
                query.getAxis(OLAPQuery.ROW_AXIS);
            // Create an OLAPSet instance to configure the axis.
            var otdSet:OLAPSet = new OLAPSet;
            // Add the Product to the row to aggregate data 
            // by the Product dimension.
            otdSet.addElements(
                cube.findDimension("DivDim").findAttribute("Otd").children);
            // Add the OLAPSet instance to the axis.
            var posSet:OLAPSet = new OLAPSet;
            // Add the Product to the row to aggregate data 
            // by the Product dimension.
            posSet.addElements(
                cube.findDimension("DivDim").findAttribute("Pos").children);
            // Add the OLAPSet instance to the axis.
            rowQueryAxis.addSet(otdSet.crossJoin(posSet));
            
            // Get the column axis from the query instance, and configure it
            // to aggregate the columns by the Quarter dimension. 
            var colQueryAxis:IOLAPQueryAxis = query.getAxis(OLAPQuery.COLUMN_AXIS);         
            var yearSet:OLAPSet= new OLAPSet;
            yearSet.addElements(
                cube.findDimension("DateTime").findAttribute("Year").children);
                var quarterSet:OLAPSet= new OLAPSet;
            quarterSet.addElements(
                cube.findDimension("DateTime").findAttribute("Quarter").children);
            colQueryAxis.addSet(yearSet.crossJoin(quarterSet));
            
            return query;       
        }

        // Event handler to execute the OLAP query 
        // after the cube completes initialization.
        private function runQuery(event:CubeEvent):void {
            // Get cube.
            var cube:IOLAPCube = IOLAPCube(event.currentTarget);
            // Create a query instance.
            var query:IOLAPQuery = getQuery(cube);
            // Execute the query.
            var token:AsyncToken = cube.execute(query);
            // Setup handlers for the query results.
            token.addResponder(new AsyncResponder(showResult, showFault));
        }

        // Handle a query fault.
        private function showFault(result:Object, token:Object):void {
            Alert.show("Error in query.");
        }

        // Handle a successful query by passing the query results to 
        // the OLAPDataGrid control..
        private function showResult(result:Object, token:Object):void {
            if (!result) {
                Alert.show("No results from query.");
                return;
            }
            myOLAPDG.dataProvider= result as OLAPResult;            
        }        
      ]]>
    </mx:Script>

    <mx:OLAPCube name="FlatSchemaCube" 
        dataProvider="{flatData}" 
        id="myMXMLCube"
        complete="runQuery(event);">
         
        <!--mx:OLAPDimension name="CustomerDim">
            <mx:OLAPAttribute name="Customer" dataField="customer"/>
            <mx:OLAPHierarchy name="CustomerHier" hasAll="true">
                <mx:OLAPLevel attributeName="Customer"/>
            </mx:OLAPHierarchy>
        </mx:OLAPDimension-->
        
        <mx:OLAPDimension name="DateTime">
            <mx:OLAPAttribute name="Year" dataField="year"/>
             <mx:OLAPAttribute name="Quarter" dataField="quarter"/>
            <mx:OLAPHierarchy name="DateTimeHier" hasAll="true">
                <mx:OLAPLevel attributeName="Year"/>
                <mx:OLAPLevel attributeName="Quarter"/>
            </mx:OLAPHierarchy>
        </mx:OLAPDimension>
    
        <mx:OLAPDimension name="DivDim">
        	<mx:OLAPAttribute name="Otd" dataField="otd"/>
            <mx:OLAPAttribute name="Pos" dataField="pos"/>
            
            <mx:OLAPHierarchy name="DivHier" hasAll="true">
                <mx:OLAPLevel attributeName="Otd"/>
                 <mx:OLAPLevel attributeName="Pos"/>
            </mx:OLAPHierarchy> 
        </mx:OLAPDimension>
        
        <mx:OLAPMeasure name="ZP" dataField="zp" aggregator="SUM"/>
    </mx:OLAPCube>
    
    <mx:Panel title="OLAPCube Control Example"
        height="75%" width="75%" layout="horizontal"
        paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10" x="69.5" y="35">

        <mx:OLAPDataGrid id="myOLAPDG" width="100%" height="100%"/>
    </mx:Panel>
    <mx:ApplicationControlBar x="40" y="22" dock="true">
        <mx:VBox height="100%" width="100%">
            <mx:HBox width="100%">
                <mx:Button label="Button"/>
                <mx:CheckBox label="Checkbox"/>
            </mx:HBox>
        </mx:VBox>
    </mx:ApplicationControlBar>
</mx:Application>