简体   繁体   中英

How to display all x-axis value align with group bar in mpchart android

 private  void displayGraph(){

        barChart.setDrawBarShadow(false);
        barChart.setDrawValueAboveBar(true);
        barChart.setMaxVisibleValueCount(50);
        barChart.setPinchZoom(false);
        barChart.setDrawGridBackground(false);
        barChart.getDescription().setEnabled(false);
        Legend l = barChart.getLegend();
        l.setEnabled(false);

        ArrayList<BarEntry> barEntries = new ArrayList<>();
        ArrayList<BarEntry> barEntries1 = new ArrayList<>();

        barEntries.add(new BarEntry(1,50f));
        barEntries.add(new BarEntry(2,42f));
        barEntries.add(new BarEntry(3,34f));
        barEntries.add(new BarEntry(4,24f));
        barEntries.add(new BarEntry(1,50f));
        barEntries.add(new BarEntry(2,42f));

        barEntries1.add(new BarEntry(1,17f));
        barEntries1.add(new BarEntry(2,19f));
        barEntries1.add(new BarEntry(3,19f));
        barEntries1.add(new BarEntry(4,18f));
        barEntries1.add(new BarEntry(1,17f));
        barEntries1.add(new BarEntry(2,19f));

        BarDataSet barDataSet = new BarDataSet(barEntries,"");
        barDataSet.setColor(Color.parseColor("#F44336"));
        barDataSet.setDrawValues(false);

        BarDataSet barDataSet1 = new BarDataSet(barEntries1,"");
        barDataSet1.setColors(Color.parseColor("#9C27B0"));
        barDataSet1.setDrawValues(false);

        String[] months = new String[] {"TYPE 1", "TYPE 2", "TYPE 3", "TYPE 4","TYPE 5","TYPE 6"};
        BarData data = new BarData(barDataSet,barDataSet1);
        barChart.setData(data);

        XAxis xAxis = barChart.getXAxis();
        xAxis.setValueFormatter(new IndexAxisValueFormatter(months));
        barChart.getAxisLeft().setAxisMinimum(0);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setGranularity(1);
        xAxis.setGranularityEnabled(true);
        xAxis.setLabelCount(5);
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMaximum(6);
        xAxis.setAvoidFirstLastClipping(true);

        float barSpace = 0.02f;
        float groupSpace = 0.3f;
        int groupCount = 6;
        data.setBarWidth(0.15f);
        barChart.getXAxis().setAxisMinimum(0);
        barChart.getXAxis().setAxisMaximum(0 + barChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
        barChart.groupBars(0, groupSpace, barSpace); // perform the "explicit" grouping
    }

This is my code i am trying plot graph with 2 group item with 6 bar and i have defined 6 type of {"TYPE 1", "TYPE 2", "TYPE 3", "TYPE 4","TYPE 5","TYPE 6"} value but when i try run and seeing all x-axis value is not printing and also its not displaying with aligned with group item.

below is output of my graph using given code:

在此处输入图像描述

while i want to display all x-axis value aligned with group bar please help me what i am doing wrong in code.

You can try this,hope this helps

String[] days = new String[]{"TYPE 1", "TYPE 2", "TYPE 3", "TYPE 4", "TYPE 5", "TYPE 6"};

        ArrayList<BarEntry> barEntries = new ArrayList<>();
        ArrayList<BarEntry> barEntries2 = new ArrayList<>();

        barEntries.add(new BarEntry(1,50f));
        barEntries.add(new BarEntry(2,42f));
        barEntries.add(new BarEntry(3,34f));
        barEntries.add(new BarEntry(4,24f));
        barEntries.add(new BarEntry(5,50f));
        barEntries.add(new BarEntry(6,42f));

        barEntries2.add(new BarEntry(1,17f));
        barEntries2.add(new BarEntry(2,19f));
        barEntries2.add(new BarEntry(3,19f));
        barEntries2.add(new BarEntry(4,18f));
        barEntries2.add(new BarEntry(5,17f));
        barEntries2.add(new BarEntry(6,19f));

        BarDataSet barDataSet = new BarDataSet(barEntries,"");
        barDataSet.setColor(Color.parseColor("#F44336"));

        BarDataSet barDataSet1 = new BarDataSet(barEntries2,"");
        barDataSet1.setColors(Color.parseColor("#9C27B0"));

        BarData data = new BarData(barDataSet,barDataSet1);
        chart7.setData(data);

        chart7.setDrawBarShadow(false);
        chart7.setDrawValueAboveBar(true);
        chart7.setMaxVisibleValueCount(50);
        chart7.setPinchZoom(false);
        chart7.setDrawGridBackground(false);
        chart7.getDescription().setEnabled(false);
        Legend l = chart7.getLegend();
        l.setEnabled(false);

        XAxis xAxis = chart7.getXAxis();
        xAxis.setValueFormatter(new IndexAxisValueFormatter(days));
        xAxis.setCenterAxisLabels(true);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setGranularity(1);
        xAxis.setGranularityEnabled(true);
        chart7.setDragEnabled(true);
        chart7.setVisibleXRangeMaximum(6);
        float barSpace = 0.02f;
        float groupSpace = 0.55f;
        data.setBarWidth(0.20f);
        chart7.getXAxis().setAxisMinimum(0);
        chart7.getXAxis().setAxisMaximum(6f);
        chart7.groupBars(0, groupSpace, barSpace);

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM