Link to home
Create AccountLog in
Avatar of karinos57
karinos57Flag for Afghanistan

asked on

Get the data from selected row and show it in the graphics

Hi,
i am trying to show a graph when the user selects a row in the data grid but the chart is not showing up.  i am able to pull the data from the backend using php/mysql and populating the data grid successfully but i would like to show a chart based on what the user selects in the data grid.  pls. help.
Note, i am using flash builder 4
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:summarybyallservice="services.summarybyallservice.*"
			   creationComplete="initApp()">
	
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.collections.ArrayCollection;
			import mx.events.FlexEvent;
			import mx.rpc.events.ResultEvent;
			import mx.utils.ArrayUtil;
			import mx.events.ListEvent;
			
			
			
			[Bindable] 
			private var mydata:ArrayCollection = new ArrayCollection();
			
			[Bindable]
			private var ac:ArrayCollection;
			
			/* Array for moving up and down */
			private var pos:Array = [1,2,3];
			
			/* Radius for the pie chart */       
			[Bindable]
			private var explRadius:Array = new Array();
			
			
			private function initApp():void
			{	
				summarybyallService.getAllSummary_by_all();
			}
			
			
			protected function summarybyallService_resultHandler(event:ResultEvent):void
			
			   {
				
				mydata = event.result as ArrayCollection;
			   }
			
			
			private function selectRow(e:ListEvent):void
			{    
				var j:int;
				
				try{	
					for(j;j< explRadius.length; j++)
					{
						explRadius[j]=0;
					}				
					explRadius[e.columnIndex -1]=.2;
					
					ac = new ArrayCollection([
						{Years:"Act FY10", Spend:e.itemRenderer.data.Actual_FY10, Values:e.itemRenderer.data.Actual_FY10 /2 },
						{Years:"Act FY11", Spend:e.itemRenderer.data.Actual_FY11 , Values:e.itemRenderer.data.Actual_FY11 /2},
						{Years:"Ann FY11", Spend:e.itemRenderer.data.Anualized_FY11, Values:e.itemRenderer.data.Anualized_FY11 /2 }
						
						
					]);
					
					
					theColumnChart.dataProvider =ac;
						
					
					/*thePieSeries.perWedgeExplodeRadius = explRadius; */
				}
				catch(er:*)
				{
					Alert.show(er.toString(),"Error");
				}
			}

			
		]]>
	</fx:Script>
	<fx:Declarations>
		
		<s:CallResponder id="getAllSummary_by_allResult"/>
		<summarybyallservice:SummarybyallService id="summarybyallService" 
												 fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" 
												 showBusyCursor="true"
												 result="summarybyallService_resultHandler(event)"/>
		
		<!-- Place non-visual elements (e.g., services, value objects) here -->
		<!-- Define chart effects -->
		
		<!--Column chart effect-->
		<mx:SeriesSlide id="slideIn" duration="1000" direction="up"/>
		<mx:SeriesSlide id="slideOut" duration="1000" direction="down" />
		<mx:SolidColor id="sc0" color="red" alpha=".3"/>
		<mx:Stroke id="stroke0" color="red" weight="1" alpha=".8"/>
	
	</fx:Declarations>
	
	<mx:DataGrid x="76" y="68" id="dGrid" dataProvider="{mydata}" width="427"
				 itemClick="selectRow(event)"
				 selectedIndex="0">
		<mx:columns>
			<mx:DataGridColumn headerText="Category" dataField="category"/>
			<mx:DataGridColumn headerText="Actual FY10" dataField="Actual_FY10"/>
			<mx:DataGridColumn headerText="Actual FY11" dataField="Actual_FY11"/>
			<mx:DataGridColumn headerText="Annualized FY11" dataField="Anualized_FY11"/>
		</mx:columns>
	</mx:DataGrid>
	
	
	
	
	<!--ColumnChart-->
	<mx:ColumnChart id="theColumnChart" 
					height="121" width="411"
					showDataTips="true" 
					selectionMode="single"
					type="clustered" y="238" x="76">
		<mx:horizontalAxis>
			<mx:CategoryAxis 
				dataProvider="{ac}" 
				categoryField="Years"/>
		</mx:horizontalAxis>
		
		<mx:verticalAxis>
			<mx:LinearAxis minimum="0" maximum="1500"/>
		</mx:verticalAxis>
		
		<mx:series>
			<mx:ColumnSeries 
				id="theColSeries"
				yField="Spend"
				xField="Years"
				stroke="{stroke0}"
				
				showDataEffect="slideIn"
				hideDataEffect="slideOut"/>
		</mx:series>
	</mx:ColumnChart>

</s:Application>

Open in new window

Avatar of petiex
petiex
Flag of United States of America image

It seems to work for me. You do have to CLICK a datagrid column to populate the graph. Maybe you want to change
itemClick="selectRow(event)"
to
change="selectRow(event)" ?

Because I don't have the summarybyallService code, to test this, I replaced the summarybyall service invocation with an assignment of a hard coded array collection to the one bound to the datagrid dataprovider:

initApp():void{
mydata =new ArrayCollection([
                {category:"Cheese",Actual_FY10:0,Actual_FY11:0,Anualized_FY11:4},
                {category:"Corn",Actual_FY10:522,Actual_FY11:123,Anualized_FY11:611},
                {category:"Okra",Actual_FY10:88,Actual_FY11:99,Anualized_FY11:90},
                {category:"Haggis",Actual_FY10:111,Actual_FY11:77,Anualized_FY11:100}]);
}
Avatar of karinos57

ASKER

thanks for your quick response.  i have tried your suggestion but still not working.  i can see the graph but without the bars or the spend; see attached.  thanks
Doc1.doc
The problem is that your data is larger than the limit for the graph.

You have
                <mx:verticalAxis>
                        <mx:LinearAxis minimum="0" maximum="1500"/>
                </mx:verticalAxis>
The smallest value in the screenshot you attached is like 200 million, so it is just to big to register on an axis whose maximum is 1500. Try changing the maximum to 1000000000.
it is working like a charm, thank you very much.  one last question:
how can i format the numbers with dolar signs and with no decimial.  I would like to see currency with no decimal.  thanks again for your time
Put this in your script area:

protected function myLabelFunction(item:Object, dgc:DataGridColumn):String{
     var str:String = item[dgc.dataField];
     return "$" + (str.indexOf(".") > -1) ? str.substring(0,str.indexOf(".")) : str;
}

Then, for each datagridcolumn that you want to format like that, add:
labelFunction="myLabelFunction"
Oops! Left out a crucial set of parentheses:

protected function myLabelFunction(item:Object, dgc:DataGridColumn):String{
     var str:String = item[dgc.dataField];
     return "$" + ((str.indexOf(".") > -1) ? str.substring(0,str.indexOf(".")) : str);
} 

Open in new window

it is working fine but the numbers are not rounded up.  is there a way to round up the numbers - i don't know if i have to use currency formatter class.  thanks again for your help.
ASKER CERTIFIED SOLUTION
Avatar of petiex
petiex
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
thank you very much.  it is working like a charm.  the only thing i would like to ask is to put comma in the proper place within the numbers but i already asked too much.  thanks again.
No, I think I can spare a numberformatter example:
        . . .
  private var nf:NumberFormatter;

  private function initApp():void {
            nf = new NumberFormatter();
            nf.useThousandsSeparator = true;
            nf.precision = 0;
            summarybyallService.getAllSummary_by_all();
  }
  protected function myLabelFunction(item:Object, dgc:DataGridColumn):String {
            return "$" + nf.format(Math.round(item[dgc.dataField]));
  }

Open in new window

i am getting 2 errors:
the first one at this line: private var nf:NumberFormatter;
and the error is
1046: Type was not found or was not a compile-time constant: NumberFormatter.      

2nd at this line:nf = new NumberFormatter();
and the error is :
"Call to a possibly undefined method NumberFormatter."
add
import mx.formatters.NumberFormatter;
up there with the other import statements.
you are the man.  thnx again.