4 Replies Latest reply on Jan 28, 2011 4:59 AM by cmac457

    Pie Chart changing data provider ?

    cmac457

      Iv created a pie chart to which the dataprovider is often going to change , the problem is how can i draw the pie chart when the datprovider changes as currently the chart is drawn when the dataprovider hasnt been set and thereforee nothing is displayed?

       

      Thanks Chris

        • 1. Re: Pie Chart changing data provider ?
          jmessary

          <fx:Script>
                  <![CDATA[
                     
                      import mx.rpc.events.ResultEvent;
                      import mx.collections.XMLListCollection;          
                      import mx.events.ListEvent;
                      import mx.controls.Alert;
                     
                      [Bindable]
                      public var stats_list:XMLList;
                      [Bindable]
                      public var stats_list_collection:XMLListCollection;
                     
                      //Handler for stats list xml.           
                      public function stats_result(event:ResultEvent):void
                      {                       
                          stats_list=event.result.stats
                          stats_list_collection=new XMLListCollection(stats_list);                                                             
                      }                         
                  ]]>
              </fx:Script>

           

          <mx:PieChart id="piechart" dataProvider="{stats_list_collection}" showDataTips="true" color="#000000" name="Dynamic Pie Chart" right="0" left="0" top="40" bottom="10">
                      <mx:series>
                          <mx:PieSeries explodeRadius=".1" labelPosition="callout" displayName="Series 1" field="value" nameField="type" fills="{[sc1, sc2, sc3, sc4]}"/>
                      </mx:series>
          </mx:PieChart>

           

           

           

          Chris,

           

          I posted some information above that might be of some use to you.  Note that my PieChart dataProvider is: stats_list_collection 


          When the function "stats_result" is triggered it changes the data for the PieChart and I reset the value of stats_list_collection.  When that happens it changes the PieChart to contain the new values from your XMLListCollection.  Basically the way this application was setup is to have a dropdown that a user selects and option from and it populates the data in the PieChart depending on their selection.

           

          Another way of doing this (if you don't want to just update the dataProvider's content) is:

           

          <fx:Script>
                   <![CDATA[

                        //pseudo code

                    var dataprovider1:SOMETYPE = SOMETHING;

                    var dataprovider2:SOMETYPE = SOMETHING;

                    var dataprovider3:SOMETYPE = SOMETHING;

                      public function WHATEVER YOU WANT THE TRIGGER TO BE(event:ResultEvent):void
                       {                       
                              switch(SOME CONDITION)

                              {

                                   case "case 1":    piechart.dataProvider = dataprovider1;

                                                            break;

                                   case "case 2":    piechart.dataProvider = dataprovider2;

                                                            break;

                                   case "case 3":    piechart.dataProvider = dataprovider3;

                                                            break;

           

                              }                                                        
                       }                         
                   ]]>
               </fx:Script>

           

          <mx:PieChart  id="piechart">
                      <mx:series>
                           <mx:PieSeries explodeRadius=".1" labelPosition="callout"  displayName="Series 1" field="value" nameField="type" fills="{[sc1, sc2,  sc3, sc4]}"/>
                      </mx:series>
          </mx:PieChart>

           

           

          Hope this helps.  The secondary option I posted was mostly pseduo code but the idea should give you a starting point.

          • 2. Re: Pie Chart changing data provider ?
            cmac457 Level 1

                         [Bindable]
                        var muscles:ArrayCollection;

             

            protected function dropDownList_changeHandler(event:IndexChangeEvent):void
                        {
                                muscles = new ArrayCollection([
                                {Muscle: "Chest",Value:dropDownList.selectedItem.chest},
                                {Muscle: "Arms", Value:dropDownList.selectedItem.arms},
                                {Muscle: "Shoulders",Value:dropDownList.selectedItem.shoulders},
                                {Muscle: "Abs",Value:dropDownList.selectedItem.abs}
                            ]);
                           
                           
                            mychart.dataProvider = muscles;muscles.refresh();
                       
                        }

             

             

             

             

            <mx:PieChart x="676" y="94" id="mychart" dataProvider="{muscles}" showDataTips="true">
                                <mx:series>
                                    <mx:PieSeries displayName="Series 1" field="value" id="pieSeries" nameField="value"/>
                                </mx:series>
                            </mx:PieChart>

             

            This is the code i have so far but when loaded the pie chart doesnt show even when an item is clicked the pie chart still does not show  , any ideas?

            • 3. Re: Pie Chart changing data provider ?
              jmessary Level 1

              Can you post your entire code?  I will try to run and see if I can debug.

              • 4. Re: Pie Chart changing data provider ?
                cmac457 Level 1

                Thanks for your help , got it working ! Was a problem with the call to remote service thanks again