4 Replies Latest reply on Mar 18, 2008 5:47 AM by Sreenivas R

    How to make OLAPDATAGRID use AS3

    zaharovsa
      How to make OLAPDATAGRID use AS3? I make like this and have a problem lease halp me type simple example!
      [Attach Code]
      package My
      {
      import mx.collections.ArrayCollection;
      import mx.collections.IList;
      import mx.controls.Alert;
      import mx.controls.OLAPDataGrid;
      import mx.olap.IOLAPCube;
      import mx.olap.IOLAPDimension;
      import mx.olap.IOLAPQuery;
      import mx.olap.IOLAPQueryAxis;
      import mx.olap.OLAPAttribute;
      import mx.olap.OLAPCube;
      import mx.olap.OLAPDimension;
      import mx.olap.OLAPHierarchy;
      import mx.olap.OLAPLevel;
      import mx.olap.OLAPMeasure;
      import mx.olap.OLAPQuery;
      import mx.olap.OLAPResult;
      import mx.olap.OLAPSet;
      import mx.rpc.AsyncResponder;
      import mx.rpc.AsyncToken;

      public class OlapDGCreator
      {
      [Bindable]
      private var flatData:ArrayCollection = new ArrayCollection(
      [
      {otd:"div1",pos:"programmer",year:2007,quarter:"q1",zp:20000},
      {otd:"div1",pos:"manager",year:2007,quarter:"q1",zp:15000},
      {otd:"div2",pos:"programmer",year:2007,quarter:"q1",zp:25000},
      {otd:"div2",pos:"manager",year:2007,quarter:"q1",zp:23000},
      {otd:"div1",pos:"programmer",year:2007,quarter:"q2",zp:21000},
      {otd:"div1",pos:"manager",year:2007,quarter:"q2",zp:16000},
      {otd:"div2",pos:"programmer",year:2007,quarter:"q2",zp:27000},
      {otd:"div2",pos:"manager",year:2007,quarter:"q2",zp:24000},
      ]);

      public var str:String = "";

      public var ODG:OLAPDataGrid = new OLAPDataGrid();
      public var MyCube:OLAPCube = new OLAPCube("otchet");
      public function OlapDGCreator()
      {

      MyCube.dataProvider=flatData;



      var m:OLAPMeasure = new OLAPMeasure("ZP");
      m.aggregator="SUM";
      m.dataField="zp";



      var a:IList = new ArrayCollection();
      a.addItem(m);



      var dimDateTime:OLAPDimension = new OLAPDimension("DateTime","DateTime");
      dimDateTime.cube=MyCube;
      dimDateTime.name="DateTime";
      var hierDataTime:OLAPHierarchy = new OLAPHierarchy("hierDataTime","displayHierDataTime");
      var l1:OLAPLevel = new OLAPLevel("Year","Year")
      var l2:OLAPLevel = new OLAPLevel("Quarter","Quarter")

      hierDataTime.levels.addItem(l1);
      hierDataTime.levels.addItem(l2);

      var att1:OLAPAttribute = new OLAPAttribute("Year","Year")
      att1.dataField="year";
      att1.name="Year";
      var att2:OLAPAttribute = new OLAPAttribute("Quarter","quarter")
      att2.dataField="quarter";
      att2.name="Quarter";

      dimDateTime.attributes.addItem(att1)
      dimDateTime.attributes.addItem(att2)
      dimDateTime.hierarchies.addItem(hierDataTime);
      //-----------------------------///
      var dimDivDim:OLAPDimension = new OLAPDimension("DivDim","DivDim");
      dimDivDim.cube=MyCube;
      dimDivDim.name="DivDim";
      var hierDivDim:OLAPHierarchy = new OLAPHierarchy("hierDivDim","displayHierDivDim");
      var dd_l1:OLAPLevel = new OLAPLevel("Otd","Otd")
      var dd_l2:OLAPLevel = new OLAPLevel("Pos","Pos")

      hierDivDim.levels.addItem(dd_l1);
      hierDivDim.levels.addItem(dd_l2);

      var dd_att1:OLAPAttribute = new OLAPAttribute("Otd","Otd")
      dd_att1.dataField="otd";
      dd_att1.dimension=dimDivDim;
      var dd_att2:OLAPAttribute = new OLAPAttribute("Pos","Pos")
      dd_att2.dataField="pos";
      dd_att2.dimension=dimDivDim;

      dimDivDim.attributes.addItem(dd_att1);
      dimDivDim.attributes.addItem(dd_att2);
      dimDivDim.hierarchies.addItem(hierDivDim);


      MyCube.dimensions.addItem(dimDateTime);
      MyCube.dimensions.addItem(dimDivDim);
      MyCube.measures = a
      MyCube.refresh();

      //MyCube.measures.addEventListener("www",test);
      //MyCube.addEventListener(CubeEvent.CUBE_COMPLETE,runQuery)

      runQuery();

      //запустили выражение




      }
      // 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.
      var tmp:IOLAPDimension = cube.findDimension("DivDim");
      var tmp2:OLAPDimension = cube.dimensions[0];
      otdSet.addElements(OLAPAttribute(OLAPDimension(cube.dimensions[1]).attributes[0]).childre n);//zah
      // 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(OLAPAttribute(OLAPDimension(cube.dimensions[1]).attributes[1]).childre n);//zah
      // 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(OLAPAttribute(OLAPDimension(cube.dimensions[0]).attributes[0]).childr en);//zah
      // yearSet.addElements(cube.findDimension("DateTime").findAttribute("Year").children);

      var quarterSet:OLAPSet= new OLAPSet;
      quarterSet.addElements(OLAPAttribute(OLAPDimension(cube.dimensions[0]).attributes[1]).chi ldren);//zah
      //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 {
      private function runQuery():void {
      // Get cube.
      //var cube:IOLAPCube = IOLAPCube(event.currentTarget);
      var cube:IOLAPCube = IOLAPCube(this.MyCube);
      // 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;
      }

      ODG.dataProvider= result as OLAPResult;
      }

      }
      }
      [/Attach Code]
        • 1. Re: How to make OLAPDATAGRID use AS3
          zaharovsa Level 1
          please help!
          • 2. Re: How to make OLAPDATAGRID use AS3
            Sreenivas R Adobe Employee
            Here is the fixed code. I would have loved to re-organize the code but left it as is so that you can diff the files and make a note of the differences.

            package
            {
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.controls.OLAPDataGrid;
            import mx.events.CubeEvent;
            import mx.olap.IOLAPCube;
            import mx.olap.IOLAPDimension;
            import mx.olap.IOLAPQuery;
            import mx.olap.IOLAPQueryAxis;
            import mx.olap.OLAPAttribute;
            import mx.olap.OLAPCube;
            import mx.olap.OLAPDimension;
            import mx.olap.OLAPHierarchy;
            import mx.olap.OLAPLevel;
            import mx.olap.OLAPMeasure;
            import mx.olap.OLAPQuery;
            import mx.olap.OLAPResult;
            import mx.olap.OLAPSet;
            import mx.rpc.AsyncResponder;
            import mx.rpc.AsyncToken;

            public class OlapDGCreator
            {
            [Bindable]
            private var flatData:ArrayCollection = new ArrayCollection(
            [
            {otd:"div1",pos:"programmer",year:2007,quarter:"q1",zp:20000},
            {otd:"div1",pos:"manager",year:2007,quarter:"q1",zp:15000},
            {otd:"div2",pos:"programmer",year:2007,quarter:"q1",zp:25000},
            {otd:"div2",pos:"manager",year:2007,quarter:"q1",zp:23000},
            {otd:"div1",pos:"programmer",year:2007,quarter:"q2",zp:21000},
            {otd:"div1",pos:"manager",year:2007,quarter:"q2",zp:16000},
            {otd:"div2",pos:"programmer",year:2007,quarter:"q2",zp:27000},
            {otd:"div2",pos:"manager",year:2007,quarter:"q2",zp:24000},
            ]);

            public var str:String = "";

            public var ODG:OLAPDataGrid = new OLAPDataGrid();
            public var MyCube:OLAPCube = new OLAPCube("otchet");
            public function OlapDGCreator()
            {

            MyCube.dataProvider=flatData;

            var m:OLAPMeasure = new OLAPMeasure("ZP");
            m.aggregator="SUM";
            m.dataField="zp";

            //var a:IList = new ArrayCollection();
            //a.addItem(m);

            var dimDateTime:OLAPDimension = new OLAPDimension("DateTime","DateTime");
            //dimDateTime.cube=MyCube;
            //dimDateTime.name="DateTime";
            var hierDataTime:OLAPHierarchy = new OLAPHierarchy("hierDataTime","displayHierDataTime");
            var l1:OLAPLevel = new OLAPLevel("Year","Year")
            l1.attributeName = "Year";
            var l2:OLAPLevel = new OLAPLevel("Quarter","Quarter")
            l2.attributeName = "Quarter";

            //hierDataTime.levels.addItem(l1);
            //hierDataTime.levels.addItem(l2);
            hierDataTime.levels = new ArrayCollection([l1, l2]);

            var att1:OLAPAttribute = new OLAPAttribute("Year","Year")
            att1.dataField="year";
            //att1.name="Year";
            var att2:OLAPAttribute = new OLAPAttribute("Quarter","quarter")
            att2.dataField="quarter";
            //att2.name="Quarter";

            //dimDateTime.attributes.addItem(att1)
            //dimDateTime.attributes.addItem(att2)
            dimDateTime.attributes = new ArrayCollection([att1, att2]);
            //dimDateTime.hierarchies.addItem(hierDataTime);
            dimDateTime.hierarchies = new ArrayCollection([hierDataTime]);
            //-----------------------------///
            var dimDivDim:OLAPDimension = new OLAPDimension("DivDim","DivDim");
            //dimDivDim.cube=MyCube;
            //dimDivDim.name="DivDim";
            var hierDivDim:OLAPHierarchy = new OLAPHierarchy("hierDivDim","displayHierDivDim");
            var dd_l1:OLAPLevel = new OLAPLevel("Otd","Otd")
            dd_l1.attributeName = "Otd";
            var dd_l2:OLAPLevel = new OLAPLevel("Pos","Pos")
            dd_l2.attributeName = "Pos";

            //hierDivDim.levels.addItem(dd_l1);
            //hierDivDim.levels.addItem(dd_l2);
            hierDivDim.levels = new ArrayCollection([dd_l1, dd_l2]);

            var dd_att1:OLAPAttribute = new OLAPAttribute("Otd","Otd")
            dd_att1.dataField="otd";
            //dd_att1.dimension=dimDivDim;
            var dd_att2:OLAPAttribute = new OLAPAttribute("Pos","Pos")
            dd_att2.dataField="pos";
            //dd_att2.dimension=dimDivDim;

            //dimDivDim.attributes.addItem(dd_att1);
            //dimDivDim.attributes.addItem(dd_att2);
            //dimDivDim.hierarchies.addItem(hierDivDim);
            dimDivDim.attributes = new ArrayCollection([dd_att1, dd_att2]);
            dimDivDim.hierarchies = new ArrayCollection([hierDivDim]);

            //MyCube.dimensions.addItem(dimDateTime);
            //MyCube.dimensions.addItem(dimDivDim);
            //MyCube.measures = a
            MyCube.elements = [dimDateTime, dimDivDim, m];
            MyCube.refresh();

            //MyCube.measures.addEventListener("www",test);
            MyCube.addEventListener(CubeEvent.CUBE_COMPLETE,runQuery)


            }

            // 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.
            var tmp:IOLAPDimension = cube.findDimension("DivDim");
            var tmp2:OLAPDimension = cube.dimensions[0];
            otdSet.addElements(OLAPAttribute(OLAPDimension(cube.dimensions[1]).attributes[0]).children );//zah
            // 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(OLAPAttribute(OLAPDimension(cube.dimensions[1]).attributes[1]).children );//zah
            // 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(OLAPAttribute(OLAPDimension(cube.dimensions[0]).attributes[0]).childre n);//zah
            // yearSet.addElements(cube.findDimension("DateTime").findAttribute("Year").children);

            var quarterSet:OLAPSet= new OLAPSet;
            quarterSet.addElements(OLAPAttribute(OLAPDimension(cube.dimensions[0]).attributes[1]).chil dren);//zah
            //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 {
            private function runQuery(event:Event):void {
            // Get cube.
            //var cube:IOLAPCube = IOLAPCube(event.currentTarget);
            var cube:IOLAPCube = IOLAPCube(this.MyCube);
            // 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;
            }

            ODG.dataProvider= result as OLAPResult;
            }

            }
            }
            • 3. Re: How to make OLAPDATAGRID use AS3
              zaharovsa Level 1
              Thanks it's perfect but why dont work .addItem(); in attributes, hierarchies?
              • 4. Re: How to make OLAPDATAGRID use AS3
                Sreenivas R Adobe Employee
                The addItem etc cause the underlying list to change without the knowledge of the cube. For example it is very similar to DataGrid.columns property or List.selectedIndices property. The getter of the property would return you the IList and when you do a addItem to it there is no way the DataGrid or List would come to know about the addition to carry out extra steps required.