android line chart

Android Line Chart-How To Draw Line Chart In Android Studio[2020]

Hi, In this post we are going to learn, How to draw a line chart in the android studio. The Line chart is very important for data analysis because it is easy to understand.

We can create a Line chart by using MP Android chart dependency. MP Android chart is an easy and powerful way to design many types of charts.

As we previously discussed how to create a pie chart in android studio using MP Android chart.

Without wasting your time, we are going to explain to you how to make charts.

Draw a Line Chart In Android Studio

Step 1: Adding Dependency

The first things you need to do is to add the dependency.

Dependencies allow us to include an external library or local jar files or other library modules in Android project

Go to Android then build.gradle(Module: app) and add this inside dependency.

dependencies {
 implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}

Now add following line of code in your repository in build.gradle(Project: App Name)

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' } // add line which is not present
        
    }
}

Till now, we included the dependency of the chart and we are set to go start making our line chart. Now sync your project.

Step 2: Adding Line Chart in .xml

After we add a dependency, our second step is to add the pie chart in .xml. MP Android chart provide us many types of the chart such as Pie, Line, Bar Curve, etc.

<com.github.mikephil.charting.charts.LineChart
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lineChart"/>

Step 3: Adding values in Line Chart

After adding line chart in xml we need to pass data in line chart, we do this in java file by using following code

public class MainActivity extends AppCompatActivity {

LineChart lineChart;
LineData lineData;
List<Entry> entryList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lineChart = findViewById(R.id.lineChart);
        entryList.add(new Entry(10,20));
        entryList.add(new Entry(5,10));
        entryList.add(new Entry(7,31));
        entryList.add(new Entry(3,14));
        LineDataSet lineDataSet = new LineDataSet(entryList,"country");
        lineDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
        lineDataSet.setFillAlpha(110);
        lineData = new LineData(lineDataSet);
        lineChart.setData(lineData);
        lineChart.setVisibleXRangeMaximum(10);
        lineChart.invalidate();

    }

}

If you read our previous post, you will able to understand each line of code without explanation, But again we are going to explain you.

First, we create a reference variable of the Line chart, Line data. We also create a list of Entry by following the line of code.

LineChart lineChart;
LineData lineData;
List<Entry> entryList = new ArrayList<>();

Line chart uses line data to represent values, and line Data uses a list of entries to represent values.

After this inside onCreate() we connect the Line chart to its XML view.

lineChart = findViewById(R.id.lineChart);

Now we add our data to the entry list by creating an object of Entry and passing x and y to its constructor.

        entryList.add(new Entry(10,20));
        entryList.add(new Entry(5,10));
        entryList.add(new Entry(7,31));
        entryList.add(new Entry(3,14));

Finally we create a line data set and attach to line chart by this code

        LineDataSet lineDataSet = new LineDataSet(entryList,"country");
        lineDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
        lineDataSet.setFillAlpha(110);
        lineData = new LineData(lineDataSet);
        lineChart.setData(lineData);
        lineChart.setVisibleXRangeMaximum(10);
        lineChart.invalidate();

setData() sets the data and invalidate() is used if we update our values then it automatically changes the values in the pie chart.

If you run your code you see something like this

line chart in android studio

Conclusion

I hope you learn How to draw line chart in android studio from this post. If you find this helpful please share this article and comment if any error persist in the code

Sudhanshu is Technology geek and also a pro pubg player. I like to create content in my free time that helps others. Currently pursuing BCA from Noida and operating Geekstocode

Leave a Comment

Your email address will not be published. Required fields are marked *