Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to show Distribution Bar Graphs in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 748
    Comment on it

    From here you can be able to display the Bar Graphs through android applications. First of all you need to add achartengine-1.0.0.jar to your project . Which can be easily downloaded from here

    Now you can use the following activity, in which i have tried to show the comparison between income,expenditure and savings throughout the year.

    package com.barchartexample;
    
    import org.achartengine.ChartFactory;
    import org.achartengine.chart.BarChart.Type;
    import org.achartengine.model.XYMultipleSeriesDataset;
    import org.achartengine.model.XYSeries;
    import org.achartengine.renderer.XYMultipleSeriesRenderer;
    import org.achartengine.renderer.XYSeriesRenderer;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Color;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
    private String[] monthArray = new String[] {
            "Jan", "Feb" , "Mar", "Apr", "May", "Jun",
            "Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"
        };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        showBarChart();
    }
    
    private void showBarChart(){
        int[] x = { 0,1,2,3,4,5,6,7 };
        int[] income = { 2000,2500,2700,3000,2800,3500,3700,3800};
        int[] expense = {2200, 2700, 2900, 2800, 2600, 3000, 3300, 3400 };
        int[] saving = {1800, 2000, 2200, 1600, 1400, 2500, 2800, 3000 };
    
    
    
    
        // Creating an  XYSeries for Income
        //CategorySeries incomeSeries = new CategorySeries("Income");
        XYSeries incomeSeries = new XYSeries("Income");
        // Creating an  XYSeries for Income
        XYSeries expenseSeries = new XYSeries("Expense");
        // Creating an  XYSeries for Saving
        XYSeries savingSeries = new XYSeries("Saving");
    
        // Adding data to Income and Expense Series
        for(int i=0;i<x.length;i++){            
            incomeSeries.add(i,income[i]);
            expenseSeries.add(i,expense[i]);
            savingSeries.add(i,saving[i]);
        }
    
    
        // Creating a dataset to hold each series
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        // Adding Income Series to the dataset
        dataset.addSeries(incomeSeries);
        // Adding Expense Series to dataset
        dataset.addSeries(expenseSeries);  
        // Adding Saving Series to dataset
        dataset.addSeries(savingSeries);
    
    
        // Creating XYSeriesRenderer to customize incomeSeries
        XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
        incomeRenderer.setColor(Color.rgb(130, 130, 230));
        incomeRenderer.setFillPoints(true);
        incomeRenderer.setLineWidth(5);
        incomeRenderer.setDisplayChartValues(true);
    
        // Creating XYSeriesRenderer to customize expenseSeries
        XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
        expenseRenderer.setColor(Color.rgb(220, 80, 80));
        expenseRenderer.setFillPoints(true);
        expenseRenderer.setLineWidth(5);
        expenseRenderer.setDisplayChartValues(true);
    
        XYSeriesRenderer savingRenderer = new XYSeriesRenderer();
        savingRenderer.setColor(Color.rgb(280, 100, 50));
        savingRenderer.setFillPoints(true);
        savingRenderer.setLineWidth(5);
        savingRenderer.setDisplayChartValues(true);
    
    
        // Creating a XYMultipleSeriesRenderer to customize the whole chart
        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
        multiRenderer.setXLabels(0);
        multiRenderer.setChartTitle("Income, Expense and Saving Chart");
        multiRenderer.setXTitle("Year 2014");
        multiRenderer.setYTitle("Amount in Rupee");
        multiRenderer.setZoomButtonsVisible(true);  
        multiRenderer.setBarSpacing(0.5);
    
        for(int i=0; i< x.length;i++){
            multiRenderer.addXTextLabel(i, monthArray[i]);  
        }       
    
        // Adding incomeRenderer and expenseRenderer to multipleRenderer
        // Note: The order of adding dataseries to dataset and renderers to multipleRenderer
        // should be same
    
        multiRenderer.addSeriesRenderer(incomeRenderer);
        multiRenderer.addSeriesRenderer(expenseRenderer);
        multiRenderer.addSeriesRenderer(savingRenderer);
    
        // Creating an intent to plot bar chart using dataset and multipleRenderer      
        Intent intent = ChartFactory.getBarChartIntent(getBaseContext(), dataset, multiRenderer, Type.DEFAULT);
    
        // Start Activity
        startActivity(intent);
    
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    }

    Do not forget to add the graph activity in your manifest :-

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.barchartexample.MainActivity"
            android:label="@string/app_name" >
            <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>
    

    It is a working example. Still if you have any query regarding this please feel free to ask. :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: