MPAndroidChart- Not able to add second set of data in Bar graph

278 views Asked by At

I am trying to add two sets of data i.e. BarDataSet to Bar graph, independently both the sets work just fine, but when I try adding the second the graph is showing incorrect. Following are the data sets with (0 to 9 as x-axis values)

Set1->      15:5
Set2->      16:5
Set1->      0:2
Set2->      0:2
Set1->      0:1
Set2->      22:1
Set1->      40:0
Set2->      10:0

//DATA SET CREATION

ArrayList<BarDataSet> dataSets = null;

        ArrayList<BarEntry> valueSet1 = new ArrayList<>();
        ArrayList<BarEntry> valueSet2 = new ArrayList<>();
        for(int i=0;i<arrayList.size();i++) {
            if(Loop through size of arrayList) 
            {
                BarEntry v1e1 = new BarEntry(getData1, valueFromArrayList);
                if(!valueSet1.contains(v1e1)) {
                    valueSet1.add(v1e1);    
                }
                BarEntry v2e2 = new BarEntry(getData2, valueFromArrayList);
                if(!valueSet2.contains(v2e2)) {
                    valueSet2.add(v2e2);
                }
            }
        }

        BarDataSet barDataSet1 = new BarDataSet(valueSet1, TITLE1);
        barDataSet1.setColor(getResources().getColor(R.color.Dark_Blue));
        barDataSet1.setDrawValues(false);
        BarDataSet barDataSet2 = new BarDataSet(valueSet2, TITLE2);
        barDataSet2.setColor(getResources().getColor(R.color.Orange));
        barDataSet2.setDrawValues(false);


        dataSets = new ArrayList<>();
        dataSets.add(barDataSet1);
        dataSets.add(barDataSet2);


//Plotting graph

BarChart barChart = (BarChart) findViewById(ID OF GRAPH);
XAxis xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.DKGRAY);
xAxis.setValues(xVals);
xAxis.setAdjustXLabels(false);
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(true);

YAxis yAxis = barChart.getAxisLeft();
yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
yAxis.setDrawAxisLine(true);
yAxis.setDrawGridLines(false);

yAxis.setAxisMinValue(0);
yAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return String.valueOf((int) Math.floor(value));
}
});
yAxis.setDrawLabels(true);

YAxis yAxisRight = barChart.getAxisRight();
yAxisRight.setDrawAxisLine(true);
yAxisRight.setDrawGridLines(true);
yAxisRight.setDrawLabels(false);

BarData data = new BarData(setUpXAxisLabels(), getDataSet());
data.setGroupSpace(0f);
barChart.setData(data);

barChart.setPinchZoom(false);
barChart.setDoubleTapToZoomEnabled(false);
barChart.setScaleEnabled(false);
barChart.getLegend().setEnabled(true);
barChart.setEnabled(false);
barChart.setDrawBarShadow(false);
barChart.setDrawGridBackground(false);
barChart.getLegend().setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);

barChart.setDescription("");
barChart.invalidate();
barChart.animateXY(1000, 1000);
0

There are 0 answers