title: Update Line Chart every second
body: <p>I want to use one of the classes in <a href="http://www.achartengine.org/content/demo.html" rel="nofollow noreferrer">this example</a> </p>
  
  <p>I'm displaying the line chart <img src="https://i.stack.imgur.com/Jt7oV.png" alt="like this"></p>
  
  <p>In my case the x-axis represents time so I want to update the chart each second by calling execute function bellow and doing the following: 1-adding more points 2-Moving the Pan to my current time</p>
  
  <p>so in the end I'm expecting to see the lines on the plot moving with time</p>
  
  <p>I tried creating a function called update which create a new thread and calling it from onResume but the application crashes and I get </p>
  
  <pre><code>AndroidRuntime(23861): FATAL EXCEPTION: Thread-11775
  AndroidRuntime(23861): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
  AndroidRuntime(23861): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
  AndroidRuntime(23861):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908)
  AndroidRuntime(23861):  at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:837)
  AndroidRuntime(23861):  at android.view.View.requestLayout(View.java:15792)
  AndroidRuntime(23861):  at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:358)
  AndroidRuntime(23861):  at android.view.ViewGroup.removeAllViews(ViewGroup.java:3902)
  </code></pre>
  
  <p>here is my main class</p>
  
  <pre><code>public class ChartDemo extends Activity {
      private AverageCubicTemperatureChart TCh = new AverageCubicTemperatureChart();
      RelativeLayout LayoutToDisplayChart;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          LayoutToDisplayChart=(RelativeLayout)findViewById(R.id.relative_layout1);
      }
  
    @Override
     protected void onResume() {
      super.onResume();
      Intent achartIntent = TCh.execute(this,LayoutToDisplayChart);
      AverageCubicTemperatureChart.update(this,LayoutToDisplayChart);
  
      }
   }
  </code></pre>
  
  <p>The code for the class <strong>AverageCubicTemperatureChart</strong> </p>
  
  <pre><code>/**
    * Average temperature demo chart.
   */
  public class AverageCubicTemperatureChart implements IDemoChart  {
   /**
   * Returns the chart name.
   * 
  * @return the chart name
  */
      public String getName() {
          return "Average temperature";
      }
  
      public static void update(Context c, RelativeLayout p)
      {
         thread = new Thread() {
        public void run() {
            while(true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
  
                parent.removeAllViews();
                execute(c,p);
  
            }
        }
    };
    thread.start();
       }
  
      /**
      * Returns the chart description.
      * 
      * @return the chart description
      */
      public String getDesc() {
          return "The average temperature in 4 Greek islands (cubic line chart)";
      }
  
       /**
       * Executes the chart demo.
       * 
       * @param context the context
       * @return the built intent
       */
      public Intent execute(Context context,RelativeLayout parent) {
          String[] titles = new String[] { "Crete", "Corfu", "Thassos", "Skiathos" };
          List&lt;double[]&gt; x = new ArrayList&lt;double[]&gt;();
          for (int i = 0; i &lt; titles.length; i++) {
             x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
          }
          List&lt;double[]&gt; values = new ArrayList&lt;double[]&gt;();
          values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,
                                   13.9 });
          values.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 });
          values.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 });
          values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });
          int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW };
          PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND,
                                                  PointStyle.TRIANGLE, PointStyle.SQUARE };
          XYMultipleSeriesRenderer renderer = AbstractDemoChart.buildRenderer(colors, styles);
          int length = renderer.getSeriesRendererCount();
          for (int i = 0; i &lt; length; i++) {
               ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
          }
          AbstractDemoChart.setChartSettings(renderer, "Average temperature", "Month", "Temperature", 0, 100, 0, 100,
          Color.LTGRAY, Color.LTGRAY);
          renderer.setXLabels(12);
          renderer.setYLabels(10);
          renderer.setShowGrid(true);
          renderer.setXLabelsAlign(Align.RIGHT);
          renderer.setYLabelsAlign(Align.RIGHT);
          renderer.setZoomButtonsVisible(true);
          renderer.setPanLimits(new double[] { 0, 200, 0, 100 });
          renderer.setZoomLimits(new double[] { 0, 200, 0, 100 });
          Intent intent = ChartFactory.getCubicLineChartIntent(context, AbstractDemoChart.buildDataset(titles, x, values),
          renderer, 0.33f, "Average temperature");
          mChartView2=ChartFactory.getCubeLineChartView(context,  AbstractDemoChart.buildDataset(titles, x, values), renderer, 0.33f); 
          parent.addView(mChartView2);
          return intent;
      }
  }
  </code></pre>