0

I do not know how to accurately describe the problem. It is now displayed like this: https://i.stack.imgur.com/wW5W8.png

Values and XAxis in BarChart not in center of the row. I need what was like this: https://i.stack.imgur.com/v0aU2.png

How to achieve this?

  public void setDataForChart() {

            barChart.setDrawBarShadow(false);
            barChart.setDrawValueAboveBar(true);
            barChart.setMaxVisibleValueCount(50);
            barChart.setPinchZoom(false);
            barChart.setDrawGridBackground(true);
            barChart.setExtraOffsets(0, 0, 0, 20);
            barChart.setDescription(description);

 ArrayList<BarEntry> barEntries = new ArrayList<>();
        barEntries.add(new BarEntry(1, 41f));
        barEntries.add(new BarEntry(2, 42f));
        barEntries.add(new BarEntry(3, 43f));
        barEntries.add(new BarEntry(4, 44f));
        barEntries.add(new BarEntry(5, 45f));
        barEntries.add(new BarEntry(6, 46f));
        barEntries.add(new BarEntry(7, 47f));
        barEntries.add(new BarEntry(8, 48f));
        barEntries.add(new BarEntry(9, 49f));
        barEntries.add(new BarEntry(10, 50f));
        barEntries.add(new BarEntry(11, 51f));
        barEntries.add(new BarEntry(12, 52f));

            BarDataSet barDataSet = new BarDataSet(barEntries, "Data Set1");
            barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);

            BarData data1 = new BarData(barDataSet);
            data1.setBarWidth(0.7f);

            barChart.setData(data1);
            barChart.notifyDataSetChanged();

            String[] months = new String[]{"Янв", "Фев", "Мар", "Апр", "Май", "Июн", "Июл", "Авг", "Сен", "Окт", "Ноя", "Дек"};

            XAxis xAxis = barChart.getXAxis();
            xAxis.setValueFormatter(new MyXAxisValueFormatter(months));
            xAxis.setPosition(XAxis.XAxisPosition.TOP);
            xAxis.setGranularity(1);
            xAxis.setCenterAxisLabels(true);
            xAxis.setLabelCount(12);

            YAxis yAxis = barChart.getAxisLeft();
            yAxis.setEnabled(false);
        }

        public class MyXAxisValueFormatter implements IAxisValueFormatter {

            public String[] values;

            public MyXAxisValueFormatter(String[] values) {
                this.values = values;
            }

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if (values.length > (int) value) {
                    return values[(int) value];
                } else {
                    return "";
                }

            }
        }
Hazex
  • 41
  • 4

1 Answers1

0

can you please check after remove or set 0 in place of 20 in below line.

barChart.setExtraOffsets(0, 0, 0, 20); 
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50