Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: I am using AChartEngine library for graphs, but not able to integrate achartengine's graph view with android xml?

My application requires graph library and I am using achartengine graph library. My app requires graph to be only 50% of the screen and other part is used to display some other information.

Is it possible have xml resource file for achartengine's graph APIs and how to do it?

I tried to find an example but didn't find it. Is it supported or not?

like image 838
pitnal Avatar asked Oct 31 '10 07:10

pitnal


3 Answers

This is a FAQ for AChartEngine. The AChartEngine demo application is available for download here: AChartEngine demo

In the demo source code you can see an example on how to embed a chart into an existing view.

Basically, in the activity descriptor .xml file, we have defined the following as a placeholder for the chart. Of course, other user interface components go together with this layout:

ChartDemo/layout/xy_chart.xml near Line 27

<LinearLayout
    android:id="@+id/chart"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal" />

We define a local variable:

ChartDemo/src/org.achartengine.chartdemo.demo.chart/XYChartBuilder.java near Line 68

private GraphicalView mChartView;

We instantiate it on the onResume() method of the activity:

ChartDemo/src/org.achartengine.chartdemo.demo.chart/XYChartBuilder.java near Line 163

protected void onResume() {
  super.onResume();
  if (mChartView == null) {
    LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
    mChartView = ChartFactory.getLineChartView(this, mDataset,
mRenderer);
    layout.addView(mChartView, new LayoutParams
(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    ...
  } else {
    mChartView.repaint();
  }
}

Whenever new data is added (when the user presses the "Add" button in our case, a new point is added in the current series and:

ChartDemo/src/org.achartengine.chartdemo.demo.chart/XYChartBuilder.java near Line 147

if (mChartView != null) {
  mChartView.repaint();
}
like image 175
Dan D. Avatar answered Nov 04 '22 00:11

Dan D.


There are two sets of APIs in ChartFactory. For eg. getLineChartView() and getLineChartIntent(). So former is used to get a graphical view which can be added to customized layout and later is used to get the intent. In above example Dan has used getLineChartView() API which returns a GraphicalView.

like image 15
pitnal Avatar answered Nov 04 '22 01:11

pitnal


You can write your xml like this...

<LinearLayout 
android:layout_below="@+id/btn"
android:id="@+id/chart" android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_weight="1">

</LinearLayout>

and the java code snippet is

   protected void onResume() {
      super.onResume();
      if (mChartView == null) {
        LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
        mChartView = ChartFactory.getBarChartView(this,getBarDemoDataset(values),renderer,Type.DEFAULT);
        layout.addView(mChartView);


      } else {
        mChartView.repaint();
      }
    }
like image 8
Taruni Avatar answered Nov 04 '22 01:11

Taruni