0

I've used the BarChart provided by MPAndroidChart library to display a bar graph.

bar graph showing correct values

bar graph showing duplicate values

When there is data for more than one values on the x-axis (in this case the date), the output is shown as expected. However, when there is data for only a single date, for some reason that date is displayed twice on the x-axis.

I would like to prevent one of the values from being displayed. Below is the code that is displayed the results in the image.

private void showBarGraph(List<Object> response) {
    barChart.setNoDataText(getResources().getString(R.string.sorry_data_not_available));
    barChart.clear();
    if(response.size()>0) {
        int highestValue = response != null && response.size() > 0 ? response.size() : 0;
        barChart.getAxisLeft().setAxisMaximum(highestValue);
        barChart.getAxisLeft().setAxisMinimum(0);
        barChart.animateY(1000);
        List<BarEntry> entries = new ArrayList<>();
        List<String> xAxisValues = new ArrayList<>();
        if (response != null) {
            for (int i = 0; i < respones.size(); i++) {
                entries.add(new BarEntry(i, response.get(i).getCount(), ContextCompat.getColor(getActivity(), R.color.graph)));
                xAxisValues.add(response.get(i).getTimekey());
            }
            barChart.getAxisLeft().setLabelCount(highestValue > 10 ? 10 : highestValue, false);
            barChart.getXAxis().setLabelCount(response.size() <= 12 ? response.size() : 12, false);
            if (response.size() >= 4) {
                barChart.getXAxis().setLabelRotationAngle(-45);
            } else {
                barChart.getXAxis().setLabelRotationAngle(0);
            }
        }

        IndexAxisValueFormatter xAxisValueFormatter = new IndexAxisValueFormatter(xAxisValues);
        barChart.getXAxis().setValueFormatter(xAxisValueFormatter);
        BarXYMarkerView xymv = new BarXYMarkerView(getActivity(), xAxisValueFormatter);
        // Set the marker to the chart
        xymv.setChartView(barChart);
        barChart.setMarker(xymv);
        BarDataSet set = new BarDataSet(entries, null);
        set.setDrawValues(false);
        List<Integer> graphColors = new ArrayList<>();
        graphColors.add(ContextCompat.getColor(getActivity(), R.color.graph));
        set.setColors(graphColors);
        BarData data = new BarData(set);
        barChart.setData(data);
        barChart.invalidate();
    }
}

The code is good for data with entries on multiple dates. Can someone guide me on how I can prevent the duplicate date value when there is data for only one date.

Abd Maj
  • 197
  • 1
  • 13
  • Still looking for a fix on this issue, if anyone is able to figure it will be greatly appreciated. – Abd Maj Apr 15 '22 at 14:14
  • You can find the answer here https://stackoverflow.com/questions/43890668/getting-duplicate-values-at-xaxis-mpchart – Vu Thai Duy Jul 02 '22 at 14:45

0 Answers0