1

I have an Activity and a XML, inside the Activity I have a Button. I want to click in this button to call a new intent (another Activity and XML) with the achartengine inside the XML, so I can use TextViews, ActionBar... Is that possible?

My Activity's code where is the in the button (It's a normal activity, with onCreate(), XML...):

public void newGraph(View view){
    Graph graph= new Graph();
    Intent it = grafico.getIntent(this);
    startActivity(it);
}

My Graph's class code:

public class Grafico {

public Intent getIntent(Context context){
    int [] x = {1, 2, 3, 4, 5, 6};
    int[] y = {75, 82, 77, 90, 84, 82};

    TimeSeries series = new TimeSeries("Glicose");
    for(int i=0; i<x.length; i++){
        series.add(x[i], y[i]);
    }

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(series);

    XYSeriesRenderer renderer = new XYSeriesRenderer();
            XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
            mRenderer.addSeriesRenderer(renderer);

    Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Title");      
    return intent;
}

}

So, I'd like to use onCreate in this Graph class calling a XML file that I'd create. Is that possible?

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
user2600853
  • 101
  • 1
  • 12
  • You can't use `onCreate` method in graph class. `onCreate()` is Android's Activity method whereas your graph is a custom class. – YuDroid Jul 23 '13 at 05:38
  • So, how can I use a xml file with the graph? Because I don't want just a graph in my screen, I want to add more. – user2600853 Jul 23 '13 at 05:39

2 Answers2

0

It sounds like you need something similar to what the official AChartEngine demo does. Take a look at the code here.

You can also get some ideas from this post.

Community
  • 1
  • 1
Dan D.
  • 32,246
  • 5
  • 63
  • 79
0

You can't use getLineChartIntent if you want to display any other element(like textview,imageview,etc.,). For that you have to remove the getLineChartIntent and you have to use getLineChartView.

private GraphicalView mChartView;
LinearLayout layout;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

layout = (LinearLayout) findViewById(R.id.chart_layout);

}

@Override
    protected void onResume() {
        super.onResume();
        if (mChartView == null) {

            mChartView = execute(this);

            layout.addView(mChartView, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        }else {
            mChartView.repaint();
        }
    }


public GraphicalView execute() {
        titles = new String[] { "Crete", "Corfu" };
        List<double[]> x = new ArrayList<double[]>();
        for (int i = 0; i < tit.length; i++) {
            x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
        }
        values = new ArrayList<double[]>();
        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 });

        colors = new int[] { Color.BLUE, Color.GREEN};
        styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND};
        XYMultipleSeriesRenderer renderer = buildRenderer(col, styles);
        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
            ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
        }

        renderer.setXTitle("Month");
        renderer.setYTitle("Temperature");
        renderer.setXAxisMin(0.5);
        renderer.setXAxisMax(12.5);
        renderer.setYAxisMin(-10);
        renderer.setYAxisMax(40);
        renderer.setAxesColor(Color.BLACK);
        renderer.setLabelsColor(Color.BLUE);


        renderer.setXLabels(12);
        renderer.setYLabels(10);
        renderer.setShowGrid(true);
        renderer.setXLabelsAlign(Align.RIGHT);
        renderer.setYLabelsAlign(Align.RIGHT);
        renderer.setGridColor(Color.RED);
        renderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
        renderer.setXLabelsColor(Color.WHITE);
        renderer.setYLabelsColor(0, Color.WHITE);



        renderer.setApplyBackgroundColor(true);
        renderer.setBackgroundColor(Color.RED);
        renderer.setZoomButtonsVisible(true);
        renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
        renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });

        renderer.setInScroll(true);

        XYMultipleSeriesDataset dataset = buildDataset(tit, x, values);
        XYSeries series = dataset.getSeriesAt(0);
        series.addAnnotation("Vacation", 6, 30);

        //double maxX = series.getMaxX();
        //Log.v("maxX", ""+maxX);
       // double minX = maxX - 12; // deltaX is your required x-range
       // Log.v("minX", ""+minX);
       // double minY = series.getMinY();
       // Log.v("minY", ""+minY);
       // double maxY = series.getMaxY();       
       // Log.v("maxY", ""+maxY);

        renderer.setRange(new double[] { 0, 15, 0, 32 });

        //Intent intent = ChartFactory.getLineChartIntent(context, dataset, renderer, "Average temperature");
        return ChartFactory.getLineChartView(this, dataset, renderer);
    }


    protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues,
            List<double[]> yValues) {
        for(int k = 0; k < titles.length; k++)
            Log.v("titles--", ""+titles[k]);
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        addXYSeries(dataset, titles, xValues, yValues, 0);
        return dataset;
    }

    public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues,
            List<double[]> yValues, int scale) {
        int length = titles.length;
        for (int i = 0; i < length; i++) {
            XYSeries series = new XYSeries(titles[i], scale);
            double[] xV = xValues.get(i);
            double[] yV = yValues.get(i);
            int seriesLength = xV.length;
            for (int k = 0; k < seriesLength; k++) {
                series.add(xV[k], yV[k]);
            }
            dataset.addSeries(series);
        }
    }
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • This code I use in the main or use in the Graph Class (creating the xml file)? – user2600853 Jul 23 '13 at 16:37
  • Nevermind, I got it. You just forgot to show the intent in the main like: graph.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent it = new Intent(getApplicationContext(), Graph.class); startActivity(it); } }); – user2600853 Jul 23 '13 at 17:00