-1

I am trying to integrate Achart engine in my activity class to show a line chart but the problem is i have to make menu on the chart activity.And i don't have access to that activity. the code i am using is below.

       public class AchartActivity extends Activity implements OnClickListener
      {
        Button b1;
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       b1=(Button)findViewById(R.id.button1);
       b1.setOnClickListener(this);
      }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    double[] a1 = new double[] { 18, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 };
    double[] a2 = 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 };
    showChart(a1,a2);
}


void showChart(double[] a1,double[] a2 )
{
    String[] titles = new String[] { "Crete", "Corfu" };
    List<double[]> x = new ArrayList<double[]>();
    for (int i = 0; i < titles.length; i++) {
      x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
    }
    List<double[]> values = new ArrayList<double[]>();
    values.add(a1);
    values.add(a2);

    int[] colors = new int[] { Color.BLUE, Color.GREEN};
    PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND,
        };
    XYMultipleSeriesRenderer renderer;
    renderer = new XYMultipleSeriesRenderer();
    renderer.setAxisTitleTextSize(16);
    renderer.setChartTitleTextSize(20);
    renderer.setLabelsTextSize(15);
    renderer.setLegendTextSize(15);
    renderer.setPointSize(5f);
    renderer.setMargins(new int[] { 20, 30, 15, 20 });
    int length = colors.length;
    for (int i = 0; i < length; i++) {
      XYSeriesRenderer r = new XYSeriesRenderer();
      r.setColor(colors[i]);
      r.setPointStyle(styles[i]);
      renderer.addSeriesRenderer(r);
    }
    int length1 = renderer.getSeriesRendererCount();
    for (int i = 0; i < length1; i++) {
      ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
    }

    renderer.setChartTitle("Average temperature");
    renderer.setXTitle("Month");
    renderer.setYTitle("Temperature(degree celcius)");
    renderer.setXAxisMin(0.5);
    renderer.setXAxisMax(12.5);
    renderer.setYAxisMin(-10);
    renderer.setYAxisMax( 40);
    renderer.setAxesColor(Color.LTGRAY);
    renderer.setLabelsColor(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[] { -10, 20, -10, 40 });
    renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
    XYMultipleSeriesDataset dataset=null;
    dataset = new XYMultipleSeriesDataset();

    int length2 = titles.length;
    for (int i = 0; i < length2; i++) {
      XYSeries series = new XYSeries(titles[i], 0);
      double[] xV = x.get(i);
      double[] yV = values.get(i);
      int seriesLength = xV.length;
      for (int k = 0; k < seriesLength; k++) {
        series.add(xV[k], yV[k]);
      }
      dataset.addSeries(series);
    }

    Intent intent = ChartFactory.getLineChartIntent(this, dataset,
        renderer, "Average temperature");
    startActivity(intent);
}

}

Saurabh Verma
  • 1,534
  • 1
  • 14
  • 28
  • Try to look here. http://stackoverflow.com/questions/8682751/menu-and-a-contextmenu-for-a-achartengine-chart – dimetil Jun 01 '12 at 05:49
  • but the problem is that i would have to use the onDraw and other complicated things which are unnecessary.isn't there any simpler way to do that.well thanks for that. – Saurabh Verma Jun 01 '12 at 06:00
  • Well i got the answer. the solution was to make the line chart in my own activity by making my own xml. So that i could freely make my menu or options menu in my activity as required. i got this answer on this link----->>>>>>http://stackoverflow.com/questions/9258785/line-chart-using-achartengine – Saurabh Verma Jun 01 '12 at 06:51

1 Answers1

2

this was done easily by just changing the method used above. i.e

     ChartFactory.getLineChartIntent(this, dataset,
    renderer, "Average temperature");

i used the method

      ChartFactory.getLineChartView(this,dataset, renderer);

the above function returned an object of

   GraphicalView

now this graphical view object can be easily set in our layout and hence i was able to draw the chart in my own activity class.

Saurabh Verma
  • 1,534
  • 1
  • 14
  • 28