3

I am working on an android app and using AChartEngine as charting library. I want that when bar graph is drawn it draws slowly so it would give us a look and feel that it is drawing with some kind of animation. How to implement such kind of behaviour?

   values.add(0, InCycleDuration);
    values.add(1, UnknownDowntimeDuration);
    int[] colors = new int[EqSDLenght] ;
     colors[0] = Color.parseColor("#008000");
    colors[1] = Color.parseColor("#FFFF00");





              for(int j = EqSDLenght - 1 ; j >= 0;j--)
              {

              if(EquipmentRKey[j].equals("EquipmentStatusTypeName.UnknownDowntime"))
                 {

                  titles [j] =  "Unknown Downtime" ;

                 }

                 if(EquipmentRKey[j].equals("EquipmentStatusTypeName.InCycle"))
                 {

                      titles [j] =  "In Cycle" ;
                 }
               }

             renderer = buildBarRenderer(colors);
            setChartSettings(renderer, "Comparison Chart", "", "Total Time Consumed", 0,
                6, 0, 100, Color.DKGRAY, Color.DKGRAY);
            for(int i=0 ; i < titles.length; i++)
            {
                x.add(new double[]{ 1,2,3,4,5});
            }

            for(int i=0;i<name.length;i++)
            {
                renderer.addTextLabel(i+1, name[i]);

            }


            renderer.setXLabels(0);

            renderer.setLabelsTextSize(12);

            renderer.getSeriesRendererAt(0).setDisplayChartValues(false);
            renderer.getSeriesRendererAt(1).setDisplayChartValues(false);
            renderer.setShowGridX(true);
            renderer.setGridColor(Color.parseColor("#359ACD32"));
            final int orientation = this.getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_PORTRAIT)   
            {
            renderer.setYLabels(11);
            }
            if (orientation == Configuration.ORIENTATION_LANDSCAPE)   
            {
            renderer.setYLabels(6);
            }
            renderer.setXLabelsAlign(Align.CENTER);
            renderer.setYLabelsAlign(Align.CENTER);
            renderer.setXLabelsColor(Color.DKGRAY);
            renderer.setYLabelsColor(0, Color.DKGRAY); 
            renderer.setPanEnabled(false, false);
            renderer.setZoomRate(1.1f);
            renderer.setBarSpacing(0.8f);
            renderer.setMargins( new int []{55, 40, 35, 30});
            renderer.setApplyBackgroundColor(true);

            renderer.setBackgroundColor(Color.WHITE);
            renderer.setMarginsColor(Color.WHITE);    
            renderer.setLegendHeight(40);

            renderer.setChartTitleTextSize(30);
            renderer.getXTextLabelLocations();
            renderer.setClickEnabled(true);


            mChartView =  ChartFactory.getBarChartView(SalesBar.this, buildBarDataset(titles, values), renderer,
                org.achartengine.chart.BarChart.Type.STACKED);

           layout.addView(mChartView, LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT);
             renderer.setSelectableBuffer(100);

             mChartView.setOnTouchListener(new OnTouchListener() {

                    public boolean onTouch(View v, MotionEvent event) {
                         if (MotionEvent.ACTION_DOWN == event.getAction()) {
                            xaxis =  event.getRawX();
                                yaxis =  event.getRawY(); 
                              xx = (int) xaxis;
                            yy = (int) yaxis;
                        } else if (MotionEvent.ACTION_UP == event.getAction()) {
                            xaxis =  event.getX();
                              yaxis =  event.getY(); 
                              xx = (int) xaxis;
                            yy = (int) yaxis;
                        }
                        else{}


                        return false;
                    }
                });


             mChartView.setOnClickListener(new View.OnClickListener() {
                 Toast test;
                public void onClick(View v) {
                  SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();




                  if (seriesSelection != null) {


                      int status_index = seriesSelection.getSeriesIndex();
                      int equipment_index = seriesSelection.getPointIndex();
                      double duration_value = seriesSelection.getValue();

                      if(status_index == 0)
                      {
                          if(equipment_index == 0)
                          {
                              duration_value = EquipmentHoursDuration[0][1];
                          }
                          if(equipment_index == 1)
                          {
                              duration_value = EquipmentHoursDuration[1][1];
                          }
                          if(equipment_index == 2)
                          {
                              duration_value = EquipmentHoursDuration[2][1];
                          }
                          if(equipment_index == 3)
                          {
                              duration_value = EquipmentHoursDuration[3][1];
                          }
                          if(equipment_index == 4)
                          {
                              duration_value = EquipmentHoursDuration[4][1];
                          }
                          String final_status = titles[status_index];
                         String final_equipment = EquipmentNamePartial[equipment_index];
                        test = Toast.makeText(
                                SalesBar.this,
                                  "Status: " + final_status + '\n'
                                + "Equipment: " + final_equipment + '\n'
                                + "Duration: " + duration_value + " hours", Toast.LENGTH_SHORT);
                       ((TextView)((LinearLayout)test.getView()).getChildAt(0))
                      .setGravity(Gravity.LEFT);

                         test.setGravity(Gravity.TOP|Gravity.LEFT, xx, yy);

                          test.show();
                      }

                      else
                      {
                     String final_status = titles[status_index];
                     String final_equipment = EquipmentNamePartial[equipment_index];
                     if(equipment_index == 0)
                  {
                      duration_value = EquipmentHoursDuration[0][0];
                  }
                  if(equipment_index == 1)
                  {
                      duration_value = EquipmentHoursDuration[1][0];
                  }
                  if(equipment_index == 2)
                  {
                      duration_value = EquipmentHoursDuration[2][0];
                  }
                  if(equipment_index == 3)
                  {
                      duration_value = EquipmentHoursDuration[3][0];
                  }
                  if(equipment_index == 4)
                  {
                      duration_value = EquipmentHoursDuration[4][0];
                  }



                    test = Toast.makeText(
                            SalesBar.this,
                              "Status: " + final_status + '\n'
                            + "Equipment: " + final_equipment + '\n'
                            + "Duration: " + duration_value + " hours", Toast.LENGTH_SHORT);


                         test.setGravity(Gravity.TOP|Gravity.LEFT, xx, yy);
                        ((TextView)((LinearLayout)test.getView()).getChildAt(0))
                          .setGravity(Gravity.LEFT);

                          test.show();
                      }
                  }
                }
              });

    protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
                XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
                renderer.setAxisTitleTextSize(16);
                renderer.setChartTitleTextSize(20);
                renderer.setLabelsTextSize(15);
                renderer.setLegendTextSize(15);
                int length = colors.length;
                for (int i = 0; i < length; i++) {
                  SimpleSeriesRenderer r = new SimpleSeriesRenderer();
                  r.setColor(colors[i]);
                  renderer.addSeriesRenderer(r);
                }
                return renderer;
              }


    protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) {
                renderer.setChartTitle(title);
                renderer.setXTitle(xTitle);
                renderer.setYTitle(yTitle);
                renderer.setXAxisMin(xMin);
                renderer.setXAxisMax(xMax);
                renderer.setYAxisMin(yMin);
                renderer.setYAxisMax(yMax);
                renderer.setAxesColor(axesColor);
                renderer.setLabelsColor(labelsColor);
              }

          protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
                XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
                int length = titles.length;
                for (int i = 0; i < length; i++) {
                  CategorySeries series = new CategorySeries(titles[i]);
                  double[] v = values.get(i);
                  int seriesLength = v.length;
                  for (int k = 0; k < seriesLength; k++) {
                    series.add(v[k]);
                  }
                  dataset.addSeries(series.toXYSeries());
                }

                return dataset;
              }
}
Fahad Abid Janjua
  • 1,024
  • 3
  • 14
  • 35
  • 1
    You could use the sleep() method from the Thread class and sleep a few seconds or so after painting each dot.http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html – calderonmluis Oct 08 '12 at 05:42

1 Answers1

4

You could display the initial view using some values and then gradually change the values until they reach the end values. During this time, you would modify the dataset with new values and call mChartView.repaint() in a separate thread that would indeed sleep for 100ms or so.

It should be as simple as:

new Thread(new Runnable() {
  public void run() {
    while (!stopped) {
      try {
        Thread.sleep(100);
      } catch (Exception e) {
        // ignore
      }
      // modify the contents of your series here
      ...
      mChartView.repaint();
    }
  }
}).start();
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • thanx for the response Dan, if i show you my code can you tell me how to do it in code?? – Fahad Abid Janjua Oct 08 '12 at 06:22
  • ok @Dan i have added the code to the question. give it your shot please and thanx..Appreciating the fact tat you always try to help me :) – Fahad Abid Janjua Oct 08 '12 at 06:39
  • by modifying contents of series you mean i should update my titles, colors and values? – Fahad Abid Janjua Oct 08 '12 at 10:55
  • Take a look at the buildBarDataset() method. You will have to change the values of your bars there, such as it may look like they are moving. – Dan D. Oct 08 '12 at 11:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17698/discussion-between-fahad-abid-janjua-and-dan) – Fahad Abid Janjua Oct 08 '12 at 11:34
  • Have you tried anything or do you need me to give you all the code you need? I don't have time now, I may have some time later this week. In the meanwhile, try something, don't wait for me to show you even how to add and remove data from an XYSeries object. – Dan D. Oct 08 '12 at 16:10