Creating Line Chart Using GraphView

This example shows how you can create line chart in android using GraphView library.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it LineChartExample.

2.) Add GraphView.jar to Project->Properties->Java Build path-> Add external JARS and order it to highest in the tree.

3.) Write following into main.xml:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" android:id="@+id/layout"
android:orientation="vertical">
</LinearLayout>

 

 

4.) Run for output.

Steps:

1.) Create a project named LineChartExample and set the information as stated in the image.

Build Target: Android 4.3
Application Name: LineChartExample
Package Name: com.example. LineChartExample
Activity Name: LineChartExampleActivity

2.) Open LineChartExampleActivity.java file and write following code there:

 

package com.example.linechartexample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;

public class LineChartExampleActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

GraphViewSeries exampleSeries = new GraphViewSeries(new GraphViewData[] {
new GraphViewData(1, 40)
, new GraphViewData(2, 12)
, new GraphViewData(3, 7)
, new GraphViewData(2, 8)
, new GraphViewData(2, 10)
, new GraphViewData(3, 26)
, new GraphViewData(1, 37)
, new GraphViewData(1, 53)
, new GraphViewData(3, 253)

});

GraphView graphView = new LineGraphView(
this // context
, "Job Status Graph" // heading
);
graphView.addSeries(exampleSeries); // data

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.addView(graphView);
}
}

 

3.) Compile and build the project.

Output