Create Project
Create new project in Android Studio with steps as below:
Step 1: Input Project Name and Select Project Location
Step 2: Select SDK for Android App
Step 3: Select Default Activity for App
Step 4: Finish create project
Add Strings
Open res\values\strings.xml file and add new string as below:
<resources>
<string name="app_name">Learn Android with Real Apps</string>
<string name="oil_price">Oil Price</string>
<string name="price_in_last_12_days">Price in last 12 days</string>
</resources>
Add Library as Dependencies
Open build.gradle file in Gradle Scripts and add new library as below:
repositories {
maven { url 'http://repo1.maven.org/maven2' }
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}
Main Activity Layout
Open res\layout\activity_main.xml file and create layout as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
tools:ignore="HardcodedText" >
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/lineChart"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Main Activity Class
Add code to MainActivity.java in android.demo.learnandroidwithrealapps package as below:
package android.demo.learnandroidwithrealapps;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawLineChart();
}
private void drawLineChart() {
LineChart lineChart = findViewById(R.id.lineChart);
List<Entry> lineEntries = getDataSet();
LineDataSet lineDataSet = new LineDataSet(lineEntries, getString(R.string.oil_price));
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setHighlightEnabled(true);
lineDataSet.setLineWidth(2);
lineDataSet.setColor(Color.RED);
lineDataSet.setCircleColor(Color.YELLOW);
lineDataSet.setCircleRadius(6);
lineDataSet.setCircleHoleRadius(3);
lineDataSet.setDrawHighlightIndicators(true);
lineDataSet.setHighLightColor(Color.RED);
lineDataSet.setValueTextSize(12);
lineDataSet.setValueTextColor(Color.DKGRAY);
LineData lineData = new LineData(lineDataSet);
lineChart.getDescription().setText(getString(R.string.price_in_last_12_days));
lineChart.getDescription().setTextSize(12);
lineChart.setDrawMarkers(true);
lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTH_SIDED);
lineChart.animateY(1000);
lineChart.getXAxis().setGranularityEnabled(true);
lineChart.getXAxis().setGranularity(1.0f);
lineChart.getXAxis().setLabelCount(lineDataSet.getEntryCount());
lineChart.setData(lineData);
}
private List<Entry> getDataSet() {
List<Entry> lineEntries = new ArrayList<Entry>();
lineEntries.add(new Entry(0, 1));
lineEntries.add(new Entry(1, 2));
lineEntries.add(new Entry(2, 3));
lineEntries.add(new Entry(3, 4));
lineEntries.add(new Entry(4, 2));
lineEntries.add(new Entry(5, 3));
lineEntries.add(new Entry(6, 1));
lineEntries.add(new Entry(7, 5));
lineEntries.add(new Entry(8, 7));
lineEntries.add(new Entry(9, 6));
lineEntries.add(new Entry(10, 4));
lineEntries.add(new Entry(11, 5));
return lineEntries;
}
}
Structure of Project
Run App
Load Main Activity