PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.54k stars 9.01k forks source link

Labels and bars in x-axis are not aligned #4598

Open anshul90 opened 5 years ago

anshul90 commented 5 years ago

Hi Team, I'm integrating StackedBar chart with latest version of library but it's not allowing me to make center aligned to the bars with x-axis label. I'm posting my code as well as image also.

    private void populateGraphData() {
    Float barWidth, barSpace, groupSpace;
    int groupCount = 12;

    barWidth = 0.35f;
    barSpace = 0.07f;
    groupSpace = 0.56f;

    List<String> xAxisValues = new ArrayList<String>();
    xAxisValues.add("Jan");
    xAxisValues.add("Feb");
    xAxisValues.add("Mar");
    xAxisValues.add("Apr");
    xAxisValues.add("May");
    xAxisValues.add("June");
    xAxisValues.add("Jul");
    xAxisValues.add("Aug");
    xAxisValues.add("Sep");
    xAxisValues.add("Oct");
    xAxisValues.add("Nov");
    xAxisValues.add("Dec");

    List<BarEntry> yValueGroup1 = new ArrayList<BarEntry>();
    List<BarEntry> yValueGroup2 = new ArrayList<BarEntry>();

    // draw the graph
    BarDataSet barDataSet1;
    BarDataSet barDataSet2;

    yValueGroup1.add(new BarEntry(1f, new float[]{9f, 3f}));
    yValueGroup2.add(new BarEntry(1f, new float[]{2f, 7f}));

    yValueGroup1.add(new BarEntry(2f, new float[]{3f, 3f}));
    yValueGroup2.add(new BarEntry(2f, new float[]{4f, 15f}));

    yValueGroup1.add(new BarEntry(3f, new float[]{3f, 3f}));
    yValueGroup2.add(new BarEntry(3f, new float[]{4f, 15f}));

    yValueGroup1.add(new BarEntry(4f, new float[]{3f, 3f}));
    yValueGroup2.add(new BarEntry(4f, new float[]{4f, 15f}));

    yValueGroup1.add(new BarEntry(5f, new float[]{9f, 3f}));
    yValueGroup2.add(new BarEntry(5f, new float[]{10f, 6f}));

    yValueGroup1.add(new BarEntry(6f, new float[]{11f, 1f}));
    yValueGroup2.add(new BarEntry(6f, new float[]{12f, 2f}));

    yValueGroup1.add(new BarEntry(7f, new float[]{11f, 7f}));
    yValueGroup2.add(new BarEntry(7f, new float[]{12f, 12f}));

    yValueGroup1.add(new BarEntry(8f, new float[]{11f, 9f}));
    yValueGroup2.add(new BarEntry(8f, new float[]{12f, 8f}));

    yValueGroup1.add(new BarEntry(9f, new float[]{11f, 13f}));
    yValueGroup2.add(new BarEntry(9f, new float[]{12f, 12f}));

    yValueGroup1.add(new BarEntry(10f, new float[]{11f, 2f}));
    yValueGroup2.add(new BarEntry(10f, new float[]{12f, 7f}));

    yValueGroup1.add(new BarEntry(11f, new float[]{11f, 6f}));
    yValueGroup2.add(new BarEntry(11f, new float[]{12f, 5f}));

    yValueGroup1.add(new BarEntry(12f, new float[]{11f, 2f}));
    yValueGroup2.add(new BarEntry(12f, new float[]{12f, 3f}));

    barDataSet1 = new BarDataSet(yValueGroup1, "");
    barDataSet1.setColors(Color.BLUE, Color.RED);
    barDataSet1.setLabel("2016");
    barDataSet1.setDrawIcons(false);
    barDataSet1.setDrawValues(false);

    barDataSet2 = new BarDataSet(yValueGroup2, "");

    barDataSet2.setLabel("2017");
    barDataSet2.setColors(Color.YELLOW, Color.RED);

    barDataSet2.setDrawIcons(false);
    barDataSet2.setDrawValues(false);

    BarData barData = new BarData(barDataSet2);

    stackedChart.getDescription().setEnabled(false);
    stackedChart.getDescription().setTextSize(0f);
    barData.setValueFormatter(new LargeValueFormatter());
    stackedChart.setData(barData);
    stackedChart.getBarData().setBarWidth(barWidth);
    stackedChart.getXAxis().setAxisMinimum(0);
    stackedChart.getXAxis().setAxisMaximum(5f);

    //  stackedChart.groupBars(0f, groupSpace, barSpace);
    //       stackedChart.setFitBars(true);
    stackedChart.getXAxis().setCenterAxisLabels(true);

    stackedChart.getData().setHighlightEnabled(false);
    stackedChart.invalidate();

    // set bar label
    Legend legend = stackedChart.getLegend();
    legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    legend.setDrawInside(false);

    List<LegendEntry> legenedEntries = new ArrayList<>();

    //     legenedEntries.add(new LegendEntry("2016", Legend.LegendForm.SQUARE, 8f, 8f, null, Color.RED));
    legenedEntries.add(new LegendEntry("2017", Legend.LegendForm.SQUARE, 8f, 8f, null, Color.YELLOW));

    legend.setCustom(legenedEntries);

    legend.setYOffset(2f);
    legend.setXOffset(2f);
    legend.setYEntrySpace(0f);
    legend.setTextSize(5f);

    XAxis xAxis = stackedChart.getXAxis();
    xAxis.setGranularity(1f);
    xAxis.setGranularityEnabled(true);
    xAxis.setCenterAxisLabels(true);
    xAxis.setDrawGridLines(false);
    xAxis.setTextSize(9f);

    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setValueFormatter(new IndexAxisValueFormatter(xAxisValues));

    xAxis.setLabelCount(12);
    xAxis.mAxisMaximum = 12f;
    xAxis.setCenterAxisLabels(true);
    xAxis.setAvoidFirstLastClipping(true);
    xAxis.setSpaceMin(4f);
    xAxis.setSpaceMax(4f);

    stackedChart.setVisibleXRangeMaximum(12f);
    stackedChart.setVisibleXRangeMinimum(12f);
    stackedChart.setDragEnabled(true);

    //Y-axis
    stackedChart.getAxisRight().setEnabled(false);
    stackedChart.setScaleEnabled(true);

    YAxis leftAxis = stackedChart.getAxisLeft();
    leftAxis.setValueFormatter(new LargeValueFormatter());
    leftAxis.setDrawGridLines(false);
    leftAxis.setSpaceTop(1f);
    leftAxis.setAxisMinimum(0f);

    stackedChart.setData(barData);
    stackedChart.setVisibleXRange(1f, 12f);
    stackedChart.setFitBars(true);
    stackedChart.invalidate();
  }

Image looks like as device-2019-07-03-095618

sanjeevkarat commented 4 years ago

Remove this xAxis.setCenterAxisLabels(true);

Deaful commented 1 year ago

Remove this xAxis.setCenterAxisLabels(true);

Agree,it works.