<?xml version="1.0" encoding="utf-8"?>
<!--
////////////////////////////////////////////////////////////////////////////////
// 株式会社　アイエルエス
// Copyright 2008 ILS
// All Rights Reserved.
//
// NOTICE:棒グラフ、円グラフ
////////////////////////////////////////////////////////////////////////////////
-->
<GcPanel 
	xmlns="bizLibrary.sharedComponent.*"
	xmlns:mx="http://www.adobe.com/2006/mxml"
	width="{Application.application.width}"
	height="{Application.application.height}" 
	creationComplete="onCreationComplete()"
	resize="true"
	>

    <!-- データモデルの定義 -->
	
	<mx:Script>
		<![CDATA[
			import mx.controls.RadioButton;
			import mx.core.Application;
			import mx.controls.Image;
			import mx.charts.HitData;
			import mx.charts.DateTimeAxis;
			import mx.charts.CategoryAxis;
			import mx.charts.chartClasses.Series;
			import mx.charts.chartClasses.ChartBase;
			import mx.collections.ArrayCollection;
			import mx.events.ItemClickEvent;
			import mx.charts.series.ColumnSeries;
			import mx.charts.Legend;
			import mx.core.DragSource;
			import mx.containers.Canvas;
			import mx.charts.events.LegendMouseEvent;
			import mx.charts.LegendItem;
			import mx.controls.Alert;
			import mx.core.UIComponent;
			import mx.charts.series.LineSeries;
			import mx.managers.DragManager;
			import mx.events.DragEvent;
			import mx.managers.PopUpManager;

			//一覧情報
			[Bindable]
			public var dataModel:XML;
	        public var selData:XML;
			
			[Bindable]
	        public var keyField:String;

			[Bindable]
			private var _selectedViewIndex:Number = 0;
			[Bindable]
			private var dataProvider:Array;

	        private var scimg:Image = new Image();

			//public var itemList:Array = new Array;
			private var ccRegionSeries:Array = new Array;

			private var chart:ChartBase;
			private var series:Series;
			
        	//表示フラグ
        	private var _openFlg:Boolean = false;

			public function set openFlg(value:Boolean):void{
				_openFlg = value;
			}
			
			public function get openFlg():Boolean{
				return _openFlg;	
			}
			
			//検索用セッションＩＤ
			private var _SearchJobID:String;
			
			public function set SearchJobID(value:String):void{
				_SearchJobID = value;
			}
			public function get SearchJobID():String{
				return _SearchJobID;	
			}		

			/**
			 * 共通初期化処理
			 * 
			 */		
			private function onCreationComplete():void{
				//グラフの切替（円グラフ）
				_selectedViewIndex = 1;
	            //外部設定ファイルの読込      
	            getXMLFile(setChart);			
			}

			/**
			 * グラフの設定
			 * 
			 */	
			private function setChart():void
			{
				//グラフデータの設定
			    dataModel = xmlData;
			    selData = XML(dataModel.data.chart);

				//グラフタイトルのセット
	        	lblTitle.text = dataModel.data.grapfTitle.toString();
	        	if(lblTitle.text==""){
	        		boxTitleArea.visible = false;
	        	}
	        	//X軸
	        	xAxis.title = dataModel.data.grapfXTitle.toString();
	        	//Y軸
	        	yAxis.title = dataModel.data.grapfYTitle.toString();
				
				//カテゴリリストの作成
	            createCategoryList(sortSeq(selData.item));
	            //期間ラジオボタンの追加
	            for (var i:int=0; i<categorylist.length; i++) {
	            	var rbtn:RadioButton = new RadioButton;
	            	rbtn.label = categorylist[i].toString();
	            	rbtn.value = categorylist[i].toString();
	            	rbtn.groupName = "grpKikan";
	            	if(i==0){
	            		rbtn.selected = true;	
	            	}
	            	boxKikan.addChild(rbtn);
	            }

				makeChart();
			}

			
			/**
			 * グラフの作成
			 * 
			 */	
			private function makeChart():void{

				ccRegion.series = null;
				//itemList = new Array;
				ccRegionSeries = new Array;
				
				var fltcategory:String;
				if(grpKikan.selectedValue == null){
					fltcategory = categorylist[0].toString();
				}else{
					fltcategory = grpKikan.selectedValue.toString();
				}

	            ccRegion.dataProvider = selData.axis.(category==fltcategory);

	            //for each(var property:XML in selData.item.name) {
	            var KikanXML:XMLList = sortSeq(selData.item.(category==fltcategory));
				//var KikanXML:XMLList = selData.item;

				//名前リスト作成
	            createNameList(KikanXML);	           
	
	            // Iterate over categorys and create a new series 
	            // for each one.
	            for each(var s:String in namelist) {
	                // Use all items namese category matches s.
	                var localXML:XMLList = selData.item.(name==s).(category==fltcategory);
					//var localXML:XMLList = selData.item.(name==s);

					//ｸﾞﾗﾌ
		            var localSeries2:ColumnSeries = new ColumnSeries();
		            localSeries2.dataProvider = localXML;
		            localSeries2.yField="value";
		            localSeries2.xField="category";
		            
	                // dataTips and Legendに表示する値を設定
		            localSeries2.displayName = s;
	                // Series オブジェクトの配列に新しいSeriesを追加する
					ccRegionSeries.push(localSeries2);						
	                // グラフに新しいSeries オブジェクトの配列を追加
		            ccRegion.series = ccRegionSeries;

	            }
	            
	            pcRegion.dataProvider = sortSeq(selData.item.(category==fltcategory));
	            
			}
			
			
	        private function formatDataTip(hitData:HitData):String
	        {
	            var name:String = hitData.item.name;
	            var category:String = hitData.item.category;
	            var value:Number = hitData.item.value;
	            return "<b>"+name+"</b><br>"+category+"<br>"+suDisplay.format(value);
	        }
            


	        private function getSliceLabel(item:Object, arg2:String, arg3:Number, arg4:Number):String
	        {
	            return item==null?"":item.name.slice(0,30);
	        }


			//グラフの切替
    		private function chartOnClickHandler(event:ItemClickEvent):void {
	            switch(event.index){
	                case 0:
	                	//円グラフ
	                	_selectedViewIndex = 1;
	                    break;
	                case 1:
	                	//棒グラフ
	                	_selectedViewIndex = 0;
	                    break;
	            }
            }


		]]>
	</mx:Script>
	<mx:NumberFormatter id="suDisplay" 
		decimalSeparatorTo="."
		thousandsSeparatorTo=","
		useThousandsSeparator="true"/>
    <mx:Array id="chartData">
        <mx:Object icon="@Embed('assets/icon_chart2.png')"/>
        <mx:Object icon="@Embed('assets/icon_chart3.png')"/>
    </mx:Array>
    <mx:VBox id="boxChart" left="0" right="0" verticalGap="0" height="100%">
	    <mx:HBox horizontalGap="0" width="100%">
		    <mx:HBox id="boxTitleArea" horizontalGap="0" >
		    	<mx:Label id="lblTitle" fontWeight="bold" paddingLeft="20" fontSize="12" />
		    </mx:HBox>
			<mx:Spacer width="100%"/>
			<mx:ButtonBar id="barChart" dataProvider="{chartData}" itemClick="chartOnClickHandler(event);">
			</mx:ButtonBar>
	    </mx:HBox>
	    <mx:HBox width="100%" height="100%">
			<mx:ViewStack id="vsChart" width="100%" height="100%" creationPolicy="all"
				selectedIndex="{_selectedViewIndex}"
				>
				<mx:Canvas id="cnv1" label="cv1" width="100%" height="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off">
					<mx:HBox width="100%" height="100%">
						<mx:ColumnChart id="ccRegion" width="85%" height="{cnv1.height}"
							dataProvider="{dataProvider}" 
					        paddingLeft="5" paddingRight="5"
					        showDataTips="true"
					        dataTipFunction="formatDataTip"
							>
							<mx:horizontalAxis>
						       <mx:CategoryAxis id="xAxis" categoryField="category"/>
						    </mx:horizontalAxis>
						    <mx:verticalAxis>
						    	<mx:LinearAxis id="yAxis"/>
						    </mx:verticalAxis>
						    
						</mx:ColumnChart>
						<mx:HBox  height="{cnv1.height}" width="15%" verticalScrollPolicy="auto" horizontalScrollPolicy="off">
							<mx:Legend id="legend1" dataProvider="{ccRegion}" width="180"/>
						</mx:HBox>
					</mx:HBox>
				</mx:Canvas>
				
				<mx:Canvas id="cnv2" label="cv2" width="100%" height="100%">
				    <mx:PieChart id="pcRegion" width="100%" height="{cnv2.height}"
				    	showDataTips="true"
						paddingTop="0"
				    	paddingBottom="0"
				        dataTipFunction="formatDataTip"
				        >
				        <mx:series>
				            <mx:Array>
				                <mx:PieSeries  startAngle="90" id="psView" nameField="name" field="value" labelPosition="callout" labelFunction="getSliceLabel">
				                </mx:PieSeries>
				            </mx:Array>
				        </mx:series>
				    </mx:PieChart>
				</mx:Canvas>
			</mx:ViewStack>
	    </mx:HBox>
	    
		<mx:FormItem label="[期間]" labelWidth="50" width="100%">
			<mx:HBox width="100%" horizontalGap="0" id="boxKikan">
				<mx:RadioButtonGroup id="grpKikan" change="makeChart()"/>
			</mx:HBox>
		</mx:FormItem>
    </mx:VBox>
</GcPanel>
