PhilJay/MPAndroidChart simple manager class for BarChart

Rohail Ahmad
3 min readJul 23, 2020
BarChart

A bar chart or bar graph is a chart/graph that presents data points with rectangular bars with heights or lengths proportional to the values that they represent.

Let’s jump to the coding part.

Add the following dependencies inbuild.gradle project level

allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}

Add the following dependencies in build.gradle app-level

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

xml file for the BarChart to be displayed

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/dataLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/margin_standard"
android:layout_marginBottom="@dimen/margin_standard">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/barChartTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/barTitleMinWidth"
android:textAlignment="center" />

<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barChart"
android:layout_width="match_parent"
android:layout_height="@dimen/graph_height_default"
android:layout_marginTop="@dimen/margin_standard" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
android:id="@+id/dataLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/margin_standard"
android:layout_marginBottom="@dimen/margin_standard">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/barChartTitle"
style="@style/eliqBodySmallText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/barTitleMinWidth"
android:textAlignment="center"
tools:text="kWh" />

<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barChart"
android:layout_width="match_parent"
android:layout_height="@dimen/graph_height_default"
android:layout_marginTop="@dimen/margin_standard" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Let’s jump to the Manager class.

lass BarChartManager(
private val context: Context,
private val barChart: BarChart,
labels: List<String>,
private val values: List<BarEntry>,
unit: String
) {
}

The unit parameter is for my custom marker view. Now, initialize BarChart properties.

init {
barChart.setDrawBarShadow(false)
barChart.setDrawValueAboveBar(false)

barChart.description.isEnabled = false

// if more than 60 entries are displayed in the chart, no values will be drawn
barChart.setMaxVisibleValueCount(values.size)

// scaling can now only be done on x- and y-axis separately
barChart.setPinchZoom(false)
barChart.isDoubleTapToZoomEnabled = false
barChart.setScaleEnabled(false)

barChart.setDrawGridBackground(false)

// barChart.setDrawYLabels(false);
val xAxisFormatter: ValueFormatter = IndexAxisValueFormatter(labels)
val xAxis = barChart.xAxis

xAxis.setLabelCount(values.size, true)
xAxis.valueFormatter = xAxisFormatter
xAxis.labelCount = values.size

xAxis.position = XAxis.XAxisPosition.BOTTOM
xAxis.setDrawGridLines(false)
xAxis.granularity = 1f // only intervals of 1 day
xAxis.textColor = ContextCompat.getColor(context, R.color.colorPrimary)
xAxis.axisLineColor = ContextCompat.getColor(context, R.color.colorDefaultIndicator)

val custom: ValueFormatter = MyValueFormatter(Locale.getDefault())

val leftAxis = barChart.axisLeft
leftAxis.setLabelCount(3, true)
leftAxis.valueFormatter = custom
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
leftAxis.spaceTop = 0f
leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true)
leftAxis.textColor = ContextCompat.getColor(context, R.color.colorPrimary)
leftAxis.axisLineColor = ContextCompat.getColor(context, R.color.colorDefaultIndicator)
leftAxis.zeroLineColor = ContextCompat.getColor(context, R.color.colorDefaultIndicator)

barChart.axisRight.isEnabled = false
/*val rightAxis = barChart.axisRight
rightAxis.setDrawGridLines(false)
rightAxis.setLabelCount(8, false)
rightAxis.valueFormatter = custom
rightAxis.spaceTop = 15f
rightAxis.axisMinimum = 0f // this replaces setStartAtZero(true)*/


val l = barChart.legend
l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
l.orientation = Legend.LegendOrientation.HORIZONTAL
l.setDrawInside(false)
l.form = Legend.LegendForm.NONE
l.formSize = 9f
l.textSize = 11f
l.xEntrySpace = 1f

val mv = CustomMarkerView(context, xAxisFormatter, unit)
mv.chartView = barChart // For bounds control

barChart.marker = mv // Set the marker to the chart

val roundedBarChartRenderer =
RoundedBarChartRenderer(barChart, barChart.animator, barChart.viewPortHandler)
roundedBarChartRenderer.setRadius(context.resources.getDimension(R.dimen.barCornerRadius))
barChart.renderer = roundedBarChartRenderer

setData()
}

I believe all properties are self-explanatory. Let’s set data to BarChart now.

private fun setData() {

val set1: BarDataSet
if (barChart.data != null &&
barChart.data.dataSetCount > 0
) {
set1 = barChart.data.getDataSetByIndex(0) as BarDataSet
set1.values = values
barChart.data.notifyDataChanged()
barChart.notifyDataSetChanged()
} else {
set1 = BarDataSet(values, null)

set1.setDrawIcons(false)
set1.setDrawValues(false)
//set color of bars val startColor1 = ContextCompat.getColor(context, R.color.colorPrimary)
val endColor1 = ContextCompat.getColor(context, R.color.colorPrimary)
val gradientColors: MutableList<GradientColor> = ArrayList()
gradientColors.add(GradientColor(startColor1, endColor1))
set1.gradientColors = gradientColors val dataSets = ArrayList<IBarDataSet>()
dataSets.add(set1)

val data = BarData(dataSets)
data.setDrawValues(false)
data.barWidth = 0.9f
barChart.data = data
}

}

I have one getter for BarChart at the end.

fun getBarChart() = barChart

That’s it, ladies and gentlemen.

Photo by Martin W. Kirst on Unsplash

If you enjoyed this article then smash the applause icon a couple of times for appreciation.

--

--

Rohail Ahmad

Android Engineer with @kivrasweden formerly @Eliq & @Inov8