28

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?

James Z
  • 12,209
  • 10
  • 24
  • 44
pitnal
  • 551
  • 1
  • 5
  • 17
  • Why does it have to be in xml? – Falmarri Oct 31 '10 at 07:33
  • I have customized xml resource file for defining my layout... So i have added on empty linear layout in xml file and in java code I added graphical view of achartengine to this empty linear layout.. this solved my problem.. – pitnal Oct 31 '10 at 17:18
  • I recently found a very nice tutorial for the AChartEngine: http://coffeedrivendevelopment.wordpress.com/2012/01/17/achartengine-xy-date-plot-example/ Hope it helps someone else too in the future :) – Ahmed Faisal Aug 07 '12 at 19:53

4 Answers4

45

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();
}
logray
  • 2,332
  • 1
  • 29
  • 30
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • Hi Dan , In above coding mChartView is Graphicalview but the results of CharFactory.get ... is Intent Then how is possible to store the intent into graphicalview . will u explain this? Ok thanks pitnal.. I got it.. I got the solution thanks a lot.. – Lakshmanan Feb 07 '11 at 12:55
  • 1
    Thx as this helped. Pls note also that integrating a demo code like "MultipleTemperatureChart" (getting 2 Y axis for example) into an existing project requires not to forget to add " into the manifest.xml, even if java lib path is correctly defined. Otherwise one will get an fatal error at first call like : java.lang.NoClassDefFoundError: [Lorg.achartengine.chart.PointStyle; I also add to rename "lib" to "libs" as explained (here)[http://stackoverflow.com/a/9886768/461212] – hornetbzz Sep 23 '12 at 12:46
  • @Dan Can you please answer my question on AChartLibrary here: https://stackoverflow.com/questions/48581812/real-time-graph-with-achartengine-chart-plotting-last-retrieved-value-from-clou – Tia Feb 02 '18 at 12:19
15

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.

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
pitnal
  • 551
  • 1
  • 5
  • 17
7

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();
      }
    }
cHao
  • 84,970
  • 20
  • 145
  • 172
Taruni
  • 1,671
  • 4
  • 22
  • 43
2

So here is a self sufficient place to find all about AChartEngine. Which includes link to download,

  1. achartengine-1.1.0-demo-source.zip The AChartEngine 1.1.0 demo project source code. May 15 May 15 168 KB 8149
  2. achartengine-1.1.0-javadocs.zip The AChartEngine 1.1.0 javadoc documentation. May 15 May 15 374 KB 3610
  3. achartengine-1.1.0.jar The AChartEngine 1.1.0 binary build library. May 15 May 15 117 KB 5264

Which are latest for now(while posting this answer). Now follow these easy steps in order to add new chart file:

  • Download and add achartengine-1.1.0.jar to your project's lib section
  • Now right click on file and add it to build path, like in image

    Add jar to your project

  • Now as define in previous answers, add following code to your class:

    private GraphicalView mChartView;
    private XYMultipleSeriesDataset dataset;
    private XYMultipleSeriesRenderer renderer;
    private BarChart.Type type;
    
    
     protected void onResume() {
          super.onResume(); 
          if (mChartView == null) {
            LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
            /*getBarChartView(android.content.Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, BarChart.Type type)
              Creates a bar chart view.*/
            mChartView = ChartFactory.getBarChartView(this, dataset, renderer, type);//tView(this,getBarDemoDataset(values),renderer,Type.DEFAULT);
            layout.addView(mChartView);
          } else {
            mChartView.repaint();
          }
        }
    
    • To get detail about any class of AChartEngineAPI, download achartengine-1.1.0-javadocs.zip and open by clicking in index.html

    • achartengine-1.1.0-demo-source.zip will be helpful during further deepar development where you can take reference for all charts

rptwsthi
  • 10,094
  • 10
  • 68
  • 109