Open In App

Point Graph Series in Android

Last Updated : 04 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

We have seen using a simple line graph and BarChart implementation in Android to represent data in the graphical format. Another graphical format for representing data is a point graph series. In this article, we will take a look at the implementation of the Point Graph Series in Android. 

What we are going to build in this article? 

We will be building a simple application in which we will be displaying sample data in a point graph view in Android. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add dependency to build.gradle(Module:app)

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.    

implementation ‘com.jjoe64:graphview:4.2.2’

After adding the above dependency now sync your project and we will now move towards the implementation of our GraphView.  

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--graph view to display our graph-->
    <com.jjoe64.graphview.GraphView
        android:id="@+id/idGraphView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
      
</LinearLayout>


Step 4: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.PointsGraphSeries;
  
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing our variable for graph view.
        GraphView graphView = findViewById(R.id.idGraphView);
          
        // on below line we are creating a new data
        // point series for our point graph series.
        // we are calling get data point method to add 
        // data point to our point graph series.
        PointsGraphSeries<DataPoint> series = new PointsGraphSeries<>(getDataPoint());
          
        // below line is to add series
        // to our graph view.
        graphView.addSeries(series);
          
        // below line is to activate
        // horizontal scrolling.
        graphView.getViewport().setScrollable(true);
          
        // below line is to activate horizontal 
        // zooming and scrolling.
        graphView.getViewport().setScalable(true);
          
        // below line is to activate vertical and 
        // horizontal zoom with scrolling.
        graphView.getViewport().setScalableY(true);
          
        // below line is to activate vertical scrolling.
        graphView.getViewport().setScrollableY(true);
          
        // below line is to set shape 
        // for the point of graph view.
        series.setShape(PointsGraphSeries.Shape.TRIANGLE);
          
        // below line is to set 
        // the size of our shape.
        series.setSize(12);
          
        // below line is to add color 
        // to our shape of graph view.
        series.setColor(R.color.purple_200);
    }
  
    private DataPoint[] getDataPoint() {
        // creating a variable for data point.
        DataPoint[] dataPoints = new DataPoint[]
                {
                        // on below line we are adding a new
                        // data point to our Data Point class.
                        new DataPoint(0, 1),
                        new DataPoint(1, 2),
                        new DataPoint(2, 3),
                        new DataPoint(3, 5),
                        new DataPoint(4, 1),
                        new DataPoint(4, 3),
                        new DataPoint(5, 3),
                        new DataPoint(6, 2)
                };
        // at last we are returning
        // the data point class.
        return dataPoints;
    }
}


Now run your app and see the output of the app.

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads