How to draw CandleStickChart using MPAndroidChart in Android

In this android programming source code example, we are going to draw CandleStickChart using MPAndroidChart in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to draw CandleStickChart using MPAndroidChart in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AndroidChart.AndroidChartActivity6">

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

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;

import com.github.mikephil.charting.charts.CandleStickChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

public class AndroidChartActivity6 extends AppCompatActivity {
    private CandleStickChart candleStickChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_chart6);
        candleStickChart = (CandleStickChart)findViewById(R.id.candleStickChart);
        CandleDataSet candleDataSet = new CandleDataSet(getData(), "Inducesmile");
        candleDataSet.setColor(Color.rgb(80, 80, 80));
        candleDataSet.setShadowColor(getResources().getColor(R.color.colorPrimaryDark));
        candleDataSet.setShadowWidth(0.8f);
        candleDataSet.setDecreasingColor(getResources().getColor(R.color.colorPrimary));
        candleDataSet.setDecreasingPaintStyle(Paint.Style.FILL);
        candleDataSet.setIncreasingColor(getResources().getColor(R.color.colorAccent));
        candleDataSet.setIncreasingPaintStyle(Paint.Style.FILL);
        candleDataSet.setNeutralColor(Color.LTGRAY);
        candleDataSet.setDrawValues(false);
        CandleData candleData = new CandleData(candleDataSet);
        XAxis xAxis = candleStickChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        final String[] months = new String[]{"Jan", "Feb", "Mar", "Apr"};
        IndexAxisValueFormatter formatter = new IndexAxisValueFormatter(months);
        xAxis.setGranularity(1f);
        xAxis.setValueFormatter(formatter);
        candleStickChart.setData(candleData);
        candleStickChart.animateXY(5000, 5000);
        candleStickChart.invalidate();
    }

    private ArrayList getData(){
        ArrayList<CandleEntry> entries = new ArrayList<>();
        entries.add(new CandleEntry(0f, 225.0f, 219.84f, 224.94f, 221.07f));
        entries.add(new CandleEntry(1f, 228.35f, 222.57f, 223.52f, 226.41f));
        entries.add(new CandleEntry(2f, 226.84f, 222.52f, 225.75f, 223.07f));
        entries.add(new CandleEntry(3f, 222.95f, 217.27f, 222.15f, 217.88f));
        return entries;
    }
}

app/build.gradle

implementation 'com.github.Philjay:MPAndroidChart:v3.1.0'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment