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. <com.androidplot.xy.XYPlot
  11. android:id="@+id/plotxy"
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent"
  14. title="Income / Expenditure Chart [ Year 2012 ]"/>
  15. </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. @Override
  59. public boolean onCreateOptionsMenu(Menu menu)
  60. {
  61. getMenuInflater().inflate(R.menu.main,menu);
  62. return true;
  63. }
  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. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.myapplication.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application> </manifest>
Step 4
Clipboard05.jpg