2011-08-01 1 views
1

다음 코드는 그림설정 그래프

public class MyActivity extends Activity { 
      Prefs myprefs = null; 
      private XYPlot mySimpleXYPlot; 
      Number[] series1Numbers=new Number[10]; 
      Number[] series2Numbers=new Number[10]; 
      int a,b,c,d,ee,f,g; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.graphval); 

      // Initialize our XYPlot reference: 
      mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot); 

      // Create two arrays of y-values to plot: 

      this.myprefs = new Prefs(getApplicationContext()); 

      ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>(); 
       JSONObject json = JSONfunctions.getJSONfromURL("............"); 


        // decrement the time remaining and update the display 

        try{ 

         JSONArray earthquakes = json.getJSONArray("graphs"); 

         for(int i=0;i<earthquakes.length();i++){       
          HashMap<String, Object> map = new HashMap<String, Object>(); 
          JSONObject e = earthquakes.getJSONObject(i); 


         a = Integer.parseInt(e.getString("sensor_a")); 
         b = Integer.parseInt(e.getString("sensor_b")); 
         c = Integer.parseInt(e.getString("sensor_c")); 
         d = Integer.parseInt(e.getString("sensor_d")); 
         ee = Integer.parseInt(e.getString("sensor_e")); 
         f = Integer.parseInt(e.getString("sensor_f")); 
         g = Integer.parseInt(e.getString("sensor_g")); 

         } 






         series1Numbers[0]=a; 
         series1Numbers[1]=b; 
         series1Numbers[2]=c; 
         series1Numbers[3]=d; 
         series1Numbers[4]=ee; 
         series1Numbers[5]=f; 
         series1Numbers[6]=g; 

         // Turn the above arrays into XYSeries: 
         XYSeries series1 = new SimpleXYSeries(
           Arrays.asList(series1Numbers),   // SimpleXYSeries takes a List so turn our array into a List 
           SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value 
           "Series1");        // Set the display title of the series 

         // Same as above, for series2 
         XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, 
           "Series2"); 

         // Create a formatter to use for drawing a series using LineAndPointRenderer: 
         LineAndPointFormatter series1Format = new LineAndPointFormatter(
           Color.rgb(0, 200, 0),     // line color 
           Color.rgb(0, 100, 0),     // point color 
           Color.rgb(150, 190, 150));    // fill color (optional) 

         // Add series1 to the xyplot: 
         mySimpleXYPlot.addSeries(series1, series1Format); 

         // Same as above, with series2: 
         mySimpleXYPlot.addSeries(series2, new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100), 
           Color.rgb(150, 150, 190))); 


         // Reduce the number of range labels 
         mySimpleXYPlot.setTicksPerRangeLabel(3); 

         // By default, AndroidPlot displays developer guides to aid in laying out your plot. 
         // To get rid of them call disableAllMarkup(): 
         mySimpleXYPlot.disableAllMarkup(); 





        }catch(JSONException e)  { 
         Log.e("log_tag", "Error parsing data "+e.toString()); 
        } 

     } 

enter image description here 과 같이 그래프를 그릴 수 있지만, u는 다음 코드 그것을 엉망 전체 그래프 모든 초만큼 그래프를 새로 고침 할 때와 같이

public class MyActivity extends Activity { 
    public int currentimageindex=0; 
     Timer timer; 
     TimerTask task; 
     private XYPlot mySimpleXYPlot; 
     Number[] series1Numbers=new Number[10]; 
     Number[] series2Numbers=new Number[10]; 
     int a,b,c,d,ee,f,g; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.graphval); 

     // Initialize our XYPlot reference: 
     mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot); 

     final Handler mHandler = new Handler(); 

     // Create runnable for posting 
     final Runnable mUpdateResults = new Runnable() { 
      public void run() { 

       AnimateandSlideShow(); 

      } 
     }; 

     int delay = 1000; // delay for 1 sec. 

     int period = 8000; // repeat every 4 sec. 

     Timer timer = new Timer(); 

     timer.scheduleAtFixedRate(new TimerTask() { 

     public void run() { 

       mHandler.post(mUpdateResults); 

     } 

     }, delay, period); 

    } 

    public void onClick(View v) { 

     finish(); 
     android.os.Process.killProcess(android.os.Process.myPid()); 
     } 
    private void AnimateandSlideShow() { 

      ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>(); 
      JSONObject json = JSONfunctions.getJSONfromURL("............"); 

      try{ 

      JSONArray earthquakes = json.getJSONArray("graphs"); 

      for(int i=0;i<earthquakes.length();i++){       
       HashMap<String, Object> map = new HashMap<String, Object>(); 
       JSONObject e = earthquakes.getJSONObject(i); 


      a = Integer.parseInt(e.getString("sensor_a")); 
      b = Integer.parseInt(e.getString("sensor_b")); 
      c = Integer.parseInt(e.getString("sensor_c")); 
      d = Integer.parseInt(e.getString("sensor_d")); 
      ee = Integer.parseInt(e.getString("sensor_e")); 
      f = Integer.parseInt(e.getString("sensor_f")); 
      g = Integer.parseInt(e.getString("sensor_g")); 

      } 






      series1Numbers[0]=a; 
      series1Numbers[1]=b; 
      series1Numbers[2]=c; 
      series1Numbers[3]=d; 
      series1Numbers[4]=ee; 
      series1Numbers[5]=f; 
      series1Numbers[6]=g; 

      // Turn the above arrays into XYSeries: 
      XYSeries series1 = new SimpleXYSeries(
        Arrays.asList(series1Numbers),   // SimpleXYSeries takes a List so turn our array into a List 
        SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value 
        "Series1");        // Set the display title of the series 

      // Same as above, for series2 
      XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, 
        "Series2"); 

      // Create a formatter to use for drawing a series using LineAndPointRenderer: 
      LineAndPointFormatter series1Format = new LineAndPointFormatter(
        Color.rgb(0, 200, 0),     // line color 
        Color.rgb(0, 100, 0),     // point color 
        Color.rgb(150, 190, 150));    // fill color (optional) 

      // Add series1 to the xyplot: 
      mySimpleXYPlot.addSeries(series1, series1Format); 

      // Same as above, with series2: 
      mySimpleXYPlot.addSeries(series2, new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100), 
        Color.rgb(150, 150, 190))); 


      // Reduce the number of range labels 
      mySimpleXYPlot.setTicksPerRangeLabel(3); 

      // By default, AndroidPlot displays developer guides to aid in laying out your plot. 
      // To get rid of them call disableAllMarkup(): 
      mySimpleXYPlot.disableAllMarkup(); 





      }catch(JSONException e)  { 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 

    } 




}  

enter image description here

+0

단계별 디버깅을 수행 했습니까? –

답변

1

mySimpleXYPlot.disableAllMarkup()처럼 보이는 그림이 호출되지 않습니다. 나는 try catch로 감싸 인 것을 본다. JSONException이 있습니까? 어쨌든이 작업은 활동을 만들 때 한 번만 호출해야합니다.