1
 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
    }

这是我的代码,我正在尝试使用 2 个组项和 6 个条形绘制图表,并且我已经定义了 6 种类型的 {"TYPE 1"、"TYPE 2"、"TYPE 3"、"TYPE 4"、"TYPE 5"," TYPE 6"} 值,但是当我尝试运行并看到所有 x 轴值没有打印并且它也没有与组项对齐时显示。

下面是使用给定代码的图表的输出:

在此处输入图像描述

虽然我想显示与组栏对齐的所有 x 轴值,但请帮助我在代码中做错了什么。

4

1 回答 1

2
  • 由于条形空间和组空间的原因,需要正确更改宽度并将 setVisibleXRangeMaximum 设置为 6。您可以尝试一下,希望对您有所帮助

    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);
    

在此处输入图像描述

于 2022-01-23T18:23:39.707 回答