MPAndroidChart-无法在条形图中添加第二组数据

时间:2015-06-09 16:12:58

标签: android mpandroidchart

我正在尝试将两组数据(即BarDataSet)添加到条形图中,这两个集合都可以正常工作,但是当我尝试添加第二个时,图形显示不正确。以下是数据集 (0到9作为x轴值)

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 个答案:

没有答案