When we are looking for a view to represent some statistical data or looking for a UI for displaying a graph in your app so in this article, we will take a look at creating a LineGraphView and adding and prefix to the label of the graph in our Android App. In this article, we will be building a simple Line Graph View in our Android app and we will be displaying some sample data in our application and adding suffix and prefix to the label.
Add Suffix and Prefix to the Label
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 this into build.gradle file
implementation ‘com.jjoe64:graphview:4.2.2’
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" ?> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < com.jjoe64.graphview.GraphView android:id = "@+id/graphview" android:layout_width = "match_parent" android:layout_height = "match_parent" /> </ RelativeLayout > |
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.graphics.Color; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import com.jjoe64.graphview.DefaultLabelFormatter; import com.jjoe64.graphview.GraphView; import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.PointsGraphSeries; public class MainActivity extends AppCompatActivity { GraphView graphView; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); graphView = findViewById(R.id.graphview); // For creating Point Graph Series We use PointGraphSeries PointsGraphSeries<DataPoint> series = new PointsGraphSeries<>(getDataPoint()); graphView.addSeries(series); // we use this method to define the // shape that will be used for data points series.setShape(PointsGraphSeries.Shape.TRIANGLE); // we use this method to // define the size of the shape series.setSize( 50 ); // we use this method // to set the color series.setColor(Color.RED); // adding the prefix and suffix here using gridlabelrenderer graphView.getGridLabelRenderer().setLabelFormatter( new DefaultLabelFormatter() { @Override public String formatLabel( double value, boolean isValueX) { // if valuex then add the prefix // return "$"+super.formatLabel(value, isValueX); // if valuex then add the suffix // return super.formatLabel(value, isValueX)+"$"; if (isValueX) { return "$" + super .formatLabel(value, isValueX); } return super .formatLabel(value, isValueX); } }); } private DataPoint[] getDataPoint() { DataPoint[] dp = new DataPoint[]{ new DataPoint( 0 , 1 ), new DataPoint( 2 , 7 ), new DataPoint( 3 , 5 ), new DataPoint( 5 , 2 ), new DataPoint( 6 , 7 ), }; return dp; } } |
Output:
Add Legend
To add legend 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.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; 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.DataPointInterface; import com.jjoe64.graphview.series.PointsGraphSeries; public class MainActivity extends AppCompatActivity { GraphView graphView; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); graphView = findViewById(R.id.graphview); PointsGraphSeries<DataPoint> series = new PointsGraphSeries<>(getDataPoint()); graphView.addSeries(series); series.setShape(PointsGraphSeries.Shape.TRIANGLE); series.setSize( 50 ); series.setColor(Color.RED); // setting custom shape series.setCustomShape( new PointsGraphSeries.CustomShape() { @Override public void draw(Canvas canvas, Paint paint, float x, float y, DataPointInterface dataPoint) { paint.setStrokeWidth( 5 ); canvas.drawLine(x - 20 , y, x, y - 20 , paint); canvas.drawLine(x, y - 20 , x + 20 , y, paint); canvas.drawLine(x + 20 , y, x, y + 20 , paint); canvas.drawLine(x - 20 , y, x, y + 20 , paint); } }); // adding title series.setTitle( "Title" ); // setting visibility to true graphView.getLegendRenderer().setVisible( true ); // setting fix position for the title graphView.getLegendRenderer().setFixedPosition( 4 , 5 ); // graphView.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP); graphView.getLegendRenderer().setTextColor(Color.BLUE); graphView.getLegendRenderer().setTextSize( 40 ); } private DataPoint[] getDataPoint() { DataPoint[] dp = new DataPoint[]{ new DataPoint( 0 , 1 ), new DataPoint( 2 , 1 ), new DataPoint( 3 , 5 ), new DataPoint( 6 , 2 ), new DataPoint( 7 , 8 ), }; return dp; } } |
Output: