9 Replies Latest reply on Feb 3, 2009 5:35 PM by Newsgroup_User

    Dynamic charting...very tricky

    G009
      Hello Friends,

      I am working on code. i have two XML files, one contains header information, mapping and all. The other XML file contains actual data. What I'm trying to do is, access one XML check for mapping, and from mapping i'll come to know which tags in second XML to access. And access those tags. Now i tried to do my best, but i ended up doing some stupid thing. I been trying this complete weekend. But i couldn't do it. I am posting both XMLs and my MXML just to give idea about the logic which i explained above. But i believe my mxml code is wrong.

      MXML Code: :

      <?xml version="1.0"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
      creationComplete="create()">
      <mx:HTTPService id="head" url="header.do"
      showBusyCursor="true" result="xmlHandler(event)" resultFormat="e4x"/>
      <mx:HTTPService id="data" url="test.do"
      showBusyCursor="true" result="xmlHandler(event)" resultFormat="e4x"/>
      <mx:Script>
      <![CDATA[
      import mx.collections.XMLListCollection;
      import mx.charts.ColumnChart;
      import mx.charts.series.ColumnSeries;
      import mx.charts.CategoryAxis;
      import mx.charts.Legend;
      import mx.rpc.events.ResultEvent;

      [Bindable]public var expenses:XMLListCollection;
      [Bindable]public var header:XMLListCollection;

      public function xmlHandler(evt:ResultEvent):void{
      // Sets testInfo's root as the student. Everything else
      // referenced in respect to this.
      var list:XMLList = evt.result..VALUES;
      var list1:XMLList = evt.result..COUNT;
      var list2:XMLList = evt.result..COLUMN;
      expenses = new XMLListCollection(list);
      header = new XMLListCollection(list);
      for each(var item:XML in expenses){
      trace(item.toString());
      trace(item.toXMLString());
      }
      init();
      }

      public var myChart:ColumnChart;
      public var series1:ColumnSeries;
      public var series2:ColumnSeries;
      public var legend1:Legend;

      public function create():void {
      head.send();
      data.send();
      }

      public function init():void {
      var i:int;
      var max:int;
      var ids:Array;
      var values:Array;
      for each(var item:XML in expenses){
      max = item.child("COUNT");
      ids = item.elements("ID").toXMLString();
      values = item.elements("VALUE").toXMLString();
      }


      // Create the chart object and set some basic properties.
      myChart = new ColumnChart();
      myChart.showDataTips = true;
      myChart.dataProvider = expenses;

      // Define the category axis.
      var hAxis:CategoryAxis = new CategoryAxis();
      hAxis.categoryField = ids[0] ;
      hAxis.dataProvider = expenses;
      myChart.horizontalAxis = hAxis;

      // Add the series.
      var mySeries:Array=new Array();
      series1 = new ColumnSeries();
      series1.xField=ids[0];
      series1.yField=ids[1];
      series1.displayName = "Merchant Amount Average";
      mySeries.push(series1);

      series2 = new ColumnSeries();
      series2.xField=ids[0];
      series2.yField=ids[1];
      series2.displayName = "Account Count";
      mySeries.push(series2);

      myChart.series = mySeries;

      // Create a legend.
      legend1 = new Legend();
      legend1.dataProvider = myChart;

      // Attach chart and legend to the display list.
      p1.addChild(myChart);
      p1.addChild(legend1);
      }
      ]]>
      </mx:Script>
      <mx:Panel id="p1" title="Column Chart Created in ActionScript"/>
      </mx:Application>
      ------------------------------------------------------------------------------------------ ----------------------------------------------------------------------------
      test.xml :

      <?xml version="1.0" encoding="utf-8"?>
      <XMLDocument>
      <VALUES><c10>Nov 2005</c10><c11>205.2052</c11><c12>463290.84</c12></VALUES>
      <VALUES><c10>Dec 2005</c10><c11>241.817</c11><c12>465985.98</c12></VALUES>
      <VALUES><c10>May 2006</c10><c11>204.8433</c11><c12>479181.54</c12></VALUES>
      <VALUES><c10>Jun 2006</c10><c11>221.7901</c11><c12>481662.78</c12></VALUES>
      <VALUES><c10>Nov 2006</c10><c11>205.7652</c11><c12>497132.58</c12></VALUES>
      <VALUES><c10>Dec 2006</c10><c11>238.6818</c11><c12>500185.14</c12></VALUES>
      <VALUES><c10>Jan 2007</c10><c11>226.5511</c11><c12>503381.22</c12></VALUES>
      </XMLDocument>
      ------------------------------------------------------------------------------------------ -----------------------------------------------------------------------------

      header.xml

      <?xml version="1.0" encoding="utf-8"?>
      <XMLDocument>
      <HEADER>
      <COUNT>3</COUNT>
      <COLUMN><ID>c10</ID><VALUE>Bill Month</VALUE></COLUMN>
      <COLUMN><ID>c11</ID><VALUE>Merchant Amount Average</VALUE></COLUMN>
      <COLUMN><ID>c12</ID><VALUE>Account Count</VALUE></COLUMN>
      </HEADER>
      </XMLDocument>
      ------------------------------------------------------------------------------------------ -------------------------------------------------------------------

        • 1. Re: Dynamic charting...very tricky
          matthew horn Level 3
          I am confused by having the same handler handle both XML result events. Can you break that into two separate handlers?

          matt horn
          flex docs
          • 2. Re: Dynamic charting...very tricky
            G009 Level 1
            Ya, no problem breaking it, but as i already mentioned, my mxml is just to give idea about how my logic is.
            Logic:
            1) Read header.xml
            2)Search the mapping
            3) use the mapping values from header.xml to get values from test.xml

            As per my knowledge to read any xml we should know tags. In this case only tags we know will be the tags in header.xml, because the tags in test.xml may vary or change. Thats why header.xml is there to guide.

            I have no problem changing mxml. What i want to do in mxml, i mentioned above.....:)
            Thanks in advance.
            • 3. Dynamic charting...very tricky
              G009 Level 1
              OK i edited my mxml....but still its not working..

              <?xml version="1.0"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
              creationComplete="create()">
              <mx:HTTPService id="head" url="header.do"
              showBusyCursor="true" result="xmlheaderHandler(event)" resultFormat="e4x"/>
              <mx:HTTPService id="data" url="test.do"
              showBusyCursor="true" result="xmlHandler(event)" resultFormat="e4x"/>
              <mx:Script>
              <![CDATA[
              import mx.collections.XMLListCollection;
              import mx.charts.ColumnChart;
              import mx.charts.series.ColumnSeries;
              import mx.charts.CategoryAxis;
              import mx.charts.Legend;
              import mx.rpc.events.ResultEvent;

              [Bindable]public var expenses:XMLListCollection;
              [Bindable]public var header:XMLListCollection;

              public function xmlheaderHandler(evt:ResultEvent):void{
              // Sets testInfo's root as the student. Everything else
              // referenced in respect to this.
              var list:XMLList = evt.result..HEADER;
              var list1:XMLList = evt.result..COUNT;
              var list2:XMLList = evt.result..COLUMN;
              header = new XMLListCollection(list);

              }

              public function xmlHandler(evt:ResultEvent):void{
              // Sets testInfo's root as the student. Everything else
              // referenced in respect to this.
              var list:XMLList = evt.result..VALUES;
              expenses = new XMLListCollection(list);
              for each(var item:XML in expenses){
              trace(item.toString());
              trace(item.toXMLString());
              }
              init();
              }

              public var myChart:ColumnChart;
              public var series1:ColumnSeries;
              public var series2:ColumnSeries;
              public var legend1:Legend;

              public function create():void {
              head.send();
              data.send();
              }

              public function init():void {
              var i:int;
              var max:int;
              var ids:Array;
              var values:Array;
              for each(var item:XML in header){
              max = item.COUNT;
              ids = item.ID.toXMLString();
              values = item.VALUE.toXMLString();
              }


              // Create the chart object and set some basic properties.
              myChart = new ColumnChart();
              myChart.showDataTips = true;
              myChart.dataProvider = expenses;

              // Define the category axis.
              var hAxis:CategoryAxis = new CategoryAxis();
              hAxis.categoryField = ids[0]
              hAxis.dataProvider = expenses;
              myChart.horizontalAxis = hAxis;

              // Add the series.
              var mySeries:Array=new Array();
              series1 = new ColumnSeries();
              series1.xField=ids[0];
              series1.yField=ids[1];
              series1.displayName = "Merchant Amount Average";
              mySeries.push(series1);

              series2 = new ColumnSeries();
              series2.xField=ids[0];
              series2.yField=ids[2];
              series2.displayName = "Account Count";
              mySeries.push(series2);

              myChart.series = mySeries;

              // Create a legend.
              legend1 = new Legend();
              legend1.dataProvider = myChart;

              // Attach chart and legend to the display list.
              p1.addChild(myChart);
              p1.addChild(legend1);
              }
              ]]>
              </mx:Script>
              <mx:Panel id="p1" title="Column Chart Created in ActionScript"/>
              </mx:Application>
              • 4. Re: Dynamic charting...very tricky
                matthew horn Level 3
                I think the problem lies in iterating over the header:

                for each(var item:XML in header){

                There is only one "item" in header, which is the entire header. I don't know enough about working with XML to fix this, so maybe someone else can chime in.

                matt horn
                flex docs
                • 5. Re: Dynamic charting...very tricky
                  G009 Level 1
                  ya even i have the same problem. I dont know enough to play with XMLList or XMLListCollection in actionscript3. So is there any1 who can help me with this problem. Please.
                  • 6. Dynamic charting...very tricky
                    Start small and get that to work first - then add more functionality one piece at a time.Try something like this, to create a mapping of column id's to column header names:
                    • 7. Dynamic charting...very tricky
                      G009 Level 1
                      Well i dont want to do any mapping. The mapping is already there in header.xml. What i want to do is, i want to read what are the values between <ID></ID> tags in header.xml. And using those values i want to access values in test.xml . Cause whatever the tags in test.xml are the values in header.xml. Right now i just wanna do this. The logic is simple, but m not sure how to implement it in Actionscript3.
                      • 8. Dynamic charting...very tricky
                        G009 Level 1
                        I'm not sure, but is it possible to take values from XML and put it in array. And if it is possible, how to do it?
                        • 9. Re: Dynamic charting...very tricky
                          Level 7

                          "G009" <webforumsuser@macromedia.com> wrote in message
                          news:gm9vkj$bfv$1@forums.macromedia.com...
                          > hey guys someone guide me with me my problem....m desperately in need of
                          > help....

                          http://dispatchevent.org/roger/as3-e4x-rundown/