I'm implementing a feature for displaying Incomes/Expenses for properties & also provided Filter for 1 Month , 3 Months , 6 Months & 12 Months .
So, I have to display Months labels according to filter selected for example if 3 Month filter selected then need to display March , Feb & Jan but what currently labels are not displaying on XAxis.
Here is my code , please rectify any issue:
private void setChart() {
ArrayList<BarEntry> incomeEntries = getIncomeEntries();
ArrayList<BarEntry> expenseEntries = getExpenseEntries();
BarDataSet set1, set2;
set1 = new BarDataSet(incomeEntries, "Income");
set1.setColor(Color.rgb(65, 168, 121));
set1.setValueTextColor(Color.rgb(55, 70, 73));
set1.setValueTextSize(10f);
set2 = new BarDataSet(expenseEntries, "Expense");
set2.setColors(Color.rgb(241, 107, 72));
set2.setValueTextColor(Color.rgb(55, 70, 73));
set2.setValueTextSize(10f);
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
dataSets.add(set2);
BarData data = new BarData(dataSets);
barChart.setData(data);
barChart.getDescription().setEnabled(false);
barChart.setDrawBarShadow(false);
barChart.setDrawValueAboveBar(true);
barChart.setMaxVisibleValueCount(10);
barChart.setPinchZoom(false);
barChart.setDrawGridBackground(false);
barChart.animateY(1400, Easing.EaseInOutQuad);
barChart.animateXY(3000, 3000);
Legend l = barChart.getLegend();
l.setWordWrapEnabled(true);
l.setTextSize(14);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
l.setForm(Legend.LegendForm.CIRCLE);
XAxis xAxis = barChart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setDrawGridLines(false);
xAxis.setLabelRotationAngle(-45);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAxisMaximum(data.getXMax() + 0.25f);
ArrayList<String> formatter = getFormattter();
Log.d(TAG,"Labels Received :"+formatter.size()); // Printing 3
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(formatter));
barChart.getXAxis().setLabelCount(formatter.size(),true);
Log.d(TAG,"Labels Count :"+xAxis.getLabelCount()); // Printing 3
YAxis leftAxis = barChart.getAxisLeft();
leftAxis.removeAllLimitLines();
leftAxis.setTypeface(Typeface.DEFAULT);
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setTextColor(Color.BLACK);
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true
barChart.getAxisRight().setEnabled(false);
float maxValue = 0, minValue = 0;
for (int i = 0; i < incomeEntries.size(); i++) {
if (maxValue < incomeEntries.get(i).getY()) {
maxValue = incomeEntries.get(i).getY();
}
if (maxValue < expenseEntries.get(i).getY()) {
maxValue = expenseEntries.get(i).getY();
}
}
maxValue = maxValue + 100;
Log.e(TAG, "==================== MAX VALUE = " + maxValue);
leftAxis.setAxisMaximum(maxValue);
leftAxis.setAxisMinimum(minValue);
leftAxis.setAxisMaxValue(maxValue);
leftAxis.setStartAtZero(true);
data.setValueFormatter(new LargeValueFormatter());
//data
float groupSpace = 0.25f;
float barSpace = 0.05f; // x2 dataset
float barWidth = 0.35f; // x2 dataset
barChart.getBarData().setBarWidth(barWidth);
barChart.getXAxis().setAxisMinValue(10f);
barChart.groupBars(10, groupSpace, barSpace);
barChart.invalidate();
}
private ArrayList<String> getFormattter() {
Log.e(TAG, "GET FORMATTED VALUE");
switch (chartType) {
case AppConstants.CHART_TYPE_1_MONTH:
monthFormatter();
return oneMonthLabels;
case AppConstants.CHART_TYPE_3_MONTH:
threeMonthFormatter();
return threeMonthLabels;
case AppConstants.CHART_TYPE_6_MONTH:
sixMonthFormatter();
return sixMonthLabels;
case AppConstants.CHART_TYPE_12_MONTH:
yearFormatter();
return yearLabels;
}
return null;
}
private void threeMonthFormatter() {
Log.e(TAG, "threeMonthFormatter , label list size : " + threeMonthLabels.size());
if (null != threeMonthLabels) {
threeMonthLabels.clear();
}
Calendar calendar = Calendar.getInstance();
String month = new SimpleDateFormat("MMM").format(calendar.getTime());
threeMonthLabels.add(month);
calendar.add(Calendar.MONTH, -1);
month = new SimpleDateFormat("MMM").format(calendar.getTime());
threeMonthLabels.add(month);
calendar.add(Calendar.MONTH, -1);
month = new SimpleDateFormat("MMM").format(calendar.getTime());
threeMonthLabels.add(month);
for (int i = 0; i < threeMonthLabels.size(); i++) {
Log.e(TAG, "Label : " + threeMonthLabels.get(i));
}
}
I have attached screen which is achieved as of now.As you can see as per three month Filter , it's displaying bars as per 3 months but there are no labels even monthFormatter create exactly 3 months labels.
Here is another screenshot to display labels received log from Formatter method.
Replace below line
instead of
and check.
These two lines are causing the issue :
you are setting the minimum value of x axis to 10, whereas the labels will begin with index 0, So internally IndexOut of bound exception keeps occurring So,
Change To:
If you still want to have 10 as the initial spacing, override IAxisValueFormatter (Not recommended)
As you have fixed group space, bar space and width. you will need to heavily customise for multiple dataset.
I have re-written your solution to adapt to changing width. (This are the results)
Github Project Link
Apk Link
Note that the
setBarWidth()
function is the most important and plays the role of adjusting the width and size of graph bar.