Learn How to Create Line Chart In Android

Introduction

 
This article explains how to prepare a Line Chart in Android using Android Studio. In this application, you will prepare a chart of income and expenses. You will see the graph showing the rise and fall of both income and expenses. First, you will download the Android Plot Library. In your XML file you will create the Android plot and use the id of this in the Android XY plot variable. Then you will create three arrays, one of String type and another of number type with the values you want to show on the chart. Second, you will get the object of XYplot returned by the SimpleXYSeries(). Now set the color for the line by the LineandPointFormatter class.
 
Step 1
 
Create an XML file and write this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.     <com.androidplot.xy.XYPlot  
  12.             android:id="@+id/plotxy"  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="fill_parent"  
  15.             title="Income / Expenditure Chart [ Year 2012 ]"/>  
  16.    
  17.    
  18. </RelativeLayout> 
Step 2 
  1. package com.myapplication;  
  2. import android.graphics.Color;  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import com.androidplot.series.XYSeries;  
  7. import com.androidplot.xy.LineAndPointFormatter;  
  8. import com.androidplot.xy.SimpleXYSeries;  
  9. import com.androidplot.xy.XYPlot;  
  10. import java.lang.reflect.Array;  
  11. import java.util.Arrays;  
  12. public class MainActivity extends Activity   
  13. {  
  14.  String[] months = new String[]   
  15.  {  
  16.   "A",  
  17.   "B",  
  18.   "C",  
  19.   "D",  
  20.   "E",  
  21.   "F",  
  22.   "G",  
  23.   "H"  
  24.  };  
  25.  Number[] incomes =   
  26.  {  
  27.   2000,  
  28.   2500,  
  29.   2700,  
  30.   3000,  
  31.   2800,  
  32.   3500,  
  33.   3700,  
  34.   3800  
  35.  };  
  36.  Number[] expences =   
  37.  {  
  38.   2200,  
  39.   2700,  
  40.   2900,  
  41.   2800,  
  42.   2600,  
  43.   3000,  
  44.   3300,  
  45.   3400  
  46.  };  
  47.  XYPlot xyplot;  
  48.  @Override  
  49.  protected void onCreate(Bundle savedInstanceState)   
  50.  {  
  51.    super.onCreate(savedInstanceState);  
  52.    setContentView(R.layout.activity_main);  
  53.    xyplot = (XYPlot) findViewById(R.id.plotxy);  
  54.    XYSeries incomePlot = new SimpleXYSeries(Arrays.asList(incomes), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Income");  
  55.    XYSeries expence = new SimpleXYSeries(Arrays.asList(expences), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Expence");  
  56.    LineAndPointFormatter IncomeFormatter = new LineAndPointFormatter(Color.#ff0000, Color.#00ff00,null);LineAndPointFormatter ExpenceFormatter= new LineAndPointFormatter(Color.#ff0000, Color.#00ff00,null);  
  57.        
  58.  }  
  59.  @Override  
  60.  public boolean onCreateOptionsMenu(Menu menu)  
  61.  {  
  62.     getMenuInflater().inflate(R.menu.main,menu);  
  63.     return true;  
  64.  }  
Step 3 Android Menifest.xml file 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.myapplication"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.myapplication.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application> </manifest>
Step 4
 
Clipboard05.jpg


Similar Articles