venerdì 2 novembre 2012

Creare grafici con AChartEngine in Android

Un altro metodo per creare grafici con Android e' quello di impiegare la libreria AChartEngine

Il problema con questa libreria e' che sostanzialmente priva di esempi e non e' cosi' immediato usarla
Si possono trovare dei video tipo questo


ma se si prova a seguire l'esempio il programma genera sempre un'eccezione

il trucco e' che in modo preliminare si devono modificare project.properties e Manifest.xml cosi' come evidenziato in giallo

project.properties
------------------------------------------------
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-15
manifestmerger.enabled=true 

------------------------------------------------


Manifest.xml
------------------------------------------------
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.graph2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="org.achartengine.GraphicalActivity" />
    </application>

</manifest>

------------------------------------------------
Dopo di cio' si puo' iniziare a scrivere il codice
Di fatto il grafico vive in una sua finestra per cui va lanciato mediante Intent

MainActivity
------------------------------------------------
package com.test.graph2;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
   
    public void lineGraphHandler (View view)
    {
        LineGraph line = new LineGraph();
        Intent lineIntent = line.getIntent(this);
        startActivity(lineIntent);
    }

}

------------------------------------------------
Per il resto il codice per generazione del grafico e' piuttosto autoesplicativa

LineGraph.java
------------------------------------------------
package com.test.graph2;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

public class LineGraph {
public Intent getIntent(Context context) {
       
        // Our first data
        int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // x values!
        int[] y =  { 30, 34, 45, 57, 77, 89, 100, 111 ,123 ,145 }; // y values!
        TimeSeries series = new TimeSeries("Line1");
        for( int i = 0; i < x.length; i++)
        {
            series.add(x[i], y[i]);
        }
       
        // Our second data
        int[] x2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // x values!
        int[] y2 =  { 145, 123, 111, 100, 89, 77, 57, 45, 34, 30}; // y values!
        TimeSeries series2 = new TimeSeries("Line2");
        for( int i = 0; i < x2.length; i++)
        {
            series2.add(x2[i], y2[i]);
        }
       
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        dataset.addSeries(series);
        dataset.addSeries(series2);
       
        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph
        XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used to customize line 1
        XYSeriesRenderer renderer2 = new XYSeriesRenderer(); // This will be used to customize line 2
        mRenderer.addSeriesRenderer(renderer);
        mRenderer.addSeriesRenderer(renderer2);
       
        // Customization time for line 1!
        renderer.setColor(Color.WHITE);
        renderer.setPointStyle(PointStyle.SQUARE);
        renderer.setFillPoints(true);
        // Customization time for line 2!
        renderer2.setColor(Color.YELLOW);
        renderer2.setPointStyle(PointStyle.DIAMOND);
        renderer2.setFillPoints(true);
       
        Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Line Graph Title");
        return intent;
       
    }
}
 

------------------------------------------------