0

I'm trying to integrate a graphs in an application I'm building, but I met a lot of difficulties when I try to integrate elements from achartEngine in fragments.

I found here a few discussions on the subject but no one helped me (such this and this).

Can anyone help or give direction?


Jackyto, This is my code according to your recommendation, but still not working for me (crash). : (

Thanks a lot!

public class LayoutOne extends Fragment {

private GraphicalView mChart;

private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();

private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

private XYSeries mCurrentSeries;

private XYSeriesRenderer mCurrentRenderer;

LinearLayout layout;

private void initChart() {
    mCurrentSeries = new XYSeries("Sample Data");
    mDataset.addSeries(mCurrentSeries);
    mCurrentRenderer = new XYSeriesRenderer();
    mRenderer.addSeriesRenderer(mCurrentRenderer);
}

private void addSampleData() {
    mCurrentSeries.add(1, 2);
    mCurrentSeries.add(2, 3);
    mCurrentSeries.add(3, 2);
    mCurrentSeries.add(4, 5);
    mCurrentSeries.add(5, 4);
}

public static Fragment newInstance(Context context) {
    LayoutOne f = new LayoutOne();  

    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
    layout = (LinearLayout) root.findViewById(R.id.chart);
    return root;
}

 public void onResume() {
        super.onResume();
        if (mChart == null) {
            initChart();
            addSampleData();
            mChart = ChartFactory.getCubeLineChartView(getActivity(), mDataset, mRenderer, 0.3f);
            layout.addView(mChart);
        } else {
            mChart.repaint();
        }
    }

}

Of course, I was define - android: id = "@ + id / chart" in my layout.

Community
  • 1
  • 1
  • 1
    Do you have a particular problem or you just can't see how to do that? – Jackyto Jun 04 '13 at 08:51
  • 1
    I am looking for a clear example to explain how to do it or something like that, I tried to build one myself, based on what I found online but it's not that simple. : ( Thank you. – user1836560 Jun 04 '13 at 10:48
  • 1
    Try here : http://www.javaadvent.com/2012/12/achartengine-charting-library-for.html . Then i just put my chart layout into my Fragment layout and it was done. If you have any question :) – Jackyto Jun 04 '13 at 11:24
  • 1
    Or this: http://jaxenter.com/effort-free-graphs-on-android-with-achartengine-46199.html – Dan D. Jun 04 '13 at 12:36
  • Jackyto, I updated the question in accordance with the code you gave me, but still it doesn't work. :( Dan, I don't understand where to look in the link you gave me. – user1836560 Jun 04 '13 at 20:45
  • layout = (LinearLayout) root.findViewById(R.id.chart); this line must be within onResume() function. – Jackyto Jun 05 '13 at 09:22
  • I cant.... I got "The method findViewById(int) is undefined for the type LayoutOne" when I do that. – user1836560 Jun 05 '13 at 09:28
  • and when I define field "root" before and to that - the program crash. – user1836560 Jun 05 '13 at 09:34

1 Answers1

0

Put this code in onActivityCreated

 initChart();
 addSampleData();
 mChart = ChartFactory.getCubeLineChartView(getActivity(), mDataset, mRenderer, 0.3f);
 layout.addView(mChart);`
upenpat
  • 685
  • 3
  • 12