 
package com.qiyou.mb.android.ui.charts;

import java.util.ArrayList;
import org.achartengine.ChartFactory;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.qiyou.mb.android.QiYouConstant;
import com.qiyou.mb.android.beans.Track;
import com.qiyou.mb.android.beans.basic.Statics_bean;
import com.qiyou.mb.android.utils.Utils;

/**
 * Average temperature demo chart.
 */
public class TrackStaticsChart extends AbstractChart {
	public static final String TAG ="com.qiyou.TrackStaticsChart";
	public static final String dTAG ="com.qiyou";
//    StatisticsTrack mTypeStatistics =this.getStrack();
  /**
   * Executes the chart demo.
   * 
   * @param context the context
   * @return the built intent
   */
  @Override
public Intent execute(Context context) {
	  
    buildChartData(); 
    Intent intent = null;
    try{
      intent = ChartFactory.getLineChartIntent(context, dataset, renderer,
        "距离-avs"); 
   
    }catch(Exception er)
    {
    	Utils.logStackTrace(er, TAG);
    }
     return intent;
  }
  
 /*
  *  (non-Javadoc)
  * @see com.qiyou.mb.android.ui.charts.AbstractChart#getChartView(android.content.Context)
  */
  @Override
 public View getChartView(Context context)
  {
	  View v = null;
	  try{
		  setFontSize(context);
		  buildChartData(); 
		  v= ChartFactory.getLineChartView(context, dataset, renderer);
	   
	    }catch(Exception er)
	    {
	    	Utils.logStackTrace(er, TAG);
	    }
	  
	  return v;
	  
	 
  }
 
 @Override
 public void buildChartData(){
	 if(mTypeStatistics==null) return;
	    getTitles();	   
	    getXYvalues();
	    dataset = buildDataset(titles, xValues, yValues);
	    
	    renderer = getRender(2, "", "记录编号", "   公里,     公里/小时");
	    renderer.setZoomButtonsVisible(false);
	   
//	    renderer.setApplyBackgroundColor(false);
//	    renderer.isApplyBackgroundColor();
	    renderer.setXLabels(10); //数字的个数
	    renderer.setYLabels(10);	    
//	    renderer.setLabelsColor(Color.BLUE); //周边标签字体颜色 

	    renderer.setPointSize(5f);
	    int left=  (int) (QiYouConstant.PV_INT_SCREEN_WIDTH/480f *30);
	    int bottom =(int) (QiYouConstant.PV_INT_SCREEN_HEIGHT/800f *24); 
	    bottom=QiYouConstant.PV_INT_SCREEN_HEIGHT==800?0:bottom;
	    bottom=bottom>0?bottom:0;
//	    renderer.setMargins(new int[] { 1, 80,60, 0 });//top,left,bottom,right
	    renderer.setMargins(new int[] { 1, left,bottom, 0 });//top,left,bottom,right
	   
	    
//	    renderer.setMargins(new int[] { 1, 55,48, 0 });//top,left,bottom,right
	    renderer.setXLabelsPadding(2);
	    
	    renderer.setPanEnabled(false);
	    renderer.setZoomEnabled(false);
//	    renderer.setShowLegend(false);
//	    renderer.setLabelsTextSize(8f);
//	    renderer.setLegendTextSize(8);
	     
// 	    renderer.setMarginsColor(DARKCYRAN);
 	    
 }
  
 
  void getTitles(){ 
	  if(mTypeStatistics==null)return;
	  titles = new String[2];
	  titles[0]="运动距离";
	  titles[1]="运动均速";
//	  titles[3]="最远距离";
	   
  
  }
  

  
  
 @Override
public double getMaxY(){
//	  double maxY =0;
// 
//		  if(mTypeStatistics ==null)return 0;
////		  maxSportTime = tr.getTrackBean().getSportTime()>maxSportTime? tr.getTrackBean().getSportTime():maxSportTime;
//		 
//		  maxY = mTypeStatistics.getStatTrack().getMaxDist()/1000.0>maxY?  mTypeStatistics.getStatTrack().getMaxDist()/1000.0:maxY;
//		  maxY =  mTypeStatistics.getStatTrack().getMaxAvSpeed()>maxY? mTypeStatistics.getStatTrack().getMaxAvSpeed():maxY;
//	 
		 
	  return maxY;
 }
 

	public ArrayList<Track> getTrs() { 
		return trs;
	}
	
	public  double getTimePeriod()
	  {
		  double  timePeriod =0.0;
		  if(mTypeStatistics!=null && mTypeStatistics.getSbs().size()>0){
//			  long endTime =mTypeStatistics.getStaticsTrackBeans().get(mTypeStatistics.getStaticsTrackBeans().size()-1).getEndTime();
//		 	  long start =mTypeStatistics.getStaticsTrackBeans().get(0).getStartTime();
//		 	  timePeriod =endTime -start;
		 	 timePeriod =mTypeStatistics.getStart() -mTypeStatistics.getEnd();
		 	  timePeriod = timePeriod/1000/60.0;
		  }
	 	 
		  return timePeriod ;
	  }
 
 
	
	void getXYvalues (){  
		   
	      xValues = new ArrayList<double[]>();
		  
		  double maxTime = getTimePeriod(); 
		  XstepValue = maxTime/mTypeStatistics.getSbs().size();
//		  long startTime = mTypeStatistics.getStaticsTrackBeans().get(0).getStartTime();
//		  double[]  xD ; 
		  ArrayList<Double>  xD ;
		  ArrayList<Double>   xS ; 
	   
		  yValues = new ArrayList<double[]>();    
//		  double[]  yD; 
		  ArrayList<Double>  yD;
		  ArrayList<Double>   yS;  
		  
		  
		   yD =new ArrayList<Double>();
//	       yD.add(0,  0D);
	       yS =new ArrayList<Double>();
//	       yS.add(0,  0D);
	       xD = new ArrayList<Double>();//[Xcnt+1];	       
//		   xD.add(0,  0D);;
		   xS =new ArrayList<Double>();
//		   xS.add(0,  0D);
		   
		   int i=0;
		   Statics_bean tr;
		    
       for(Integer key :mTypeStatistics.getSbs().keySet()){
		  tr=mTypeStatistics.getSbs().get(key);
//	  for(Statics_bean tr:mTypeStatistics.getSbs()){
		  if(tr==null ||tr.getStartTime()==0)continue;
		  if(i==0)minX=tr.getTrackid();
		  yD.add(tr.getDist()/1000d); 
//		  xD.add((tr.getStartTime()-startTime)/1000/60.0);
		  xD.add(tr.getTrackid()/1.0);
//		  xD.add(tr.getTrackid()/1.0);
		  maxY=maxY>tr.getDist()/1000d?maxY:tr.getDist()/1000d;
		  
		  yS.add((double) tr.getAvs()); 
//		  xS.add((tr.getStartTime()-startTime)/1000/60.0);
		  xS.add(tr.getTrackid()/1.0);
//		  xS.add(tr.getTrackid()/1.0);
		  maxY=maxY>tr.getAvs()?maxY:(double) tr.getAvs();
		  
		   i++;
		   minX = minX>tr.getTrackid()?tr.getTrackid():minX;
		   
	  }
	       yValues.add( getDv(yD) );   
	       yValues.add(getDv(yS));
	       xValues.add(getDv(xD,true)); //distance
	       xValues.add(getDv(xS,true)); //speed 
//	       maxXvalue =  maxXvalue>10? maxXvalue:10; 
	       maxXvalue=i<QiYouConstant.TRACK_NUMBERS_IN_STATISTICS_CHART?maxXvalue+QiYouConstant.TRACK_NUMBERS_IN_STATISTICS_CHART-i:maxXvalue+2;
//	       maxXvalue=maxXvalue+2;
	  }

}
	    
  


