(MPAndroidChart)柱状图中有些标签没有显示

3
我正在使用MPAndroidChart(https://github.com/PhilJay/MPAndroidChart)技术制作柱状图,但是x轴的第二个值没有显示出来。我的输入值是:
x,y : 1周,3次
x,y : 2周,0次
x,y : 3周,0次
x,y : 4周,0次
x,y : 5周,0次
但是在我的手机上,第二个标签没有显示。我附上了源代码和结果截图。
ArrayList<BarEntry> entries = new ArrayList<>();
        ArrayList<String> labels = new ArrayList<String>();
        String[] labels2 = new String[ExRegList.length()];
        try {
            exerciseWholeCnt = ExRegList.length();
            for (int i = 0; i < ExRegList.length(); i++) {
                JSONObject obj = ExRegList.getJSONObject(i);
                String week = obj.getString("WEEK");
                int exCnt = obj.getInt("RESULT_COUNT");
                labels.add(week + "주");
                labels2[i] = week+"주";
                entries.add(new BarEntry(i,exCnt));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        XAxis xAxis = barChart.getXAxis();
        xAxis.setValueFormatter(new IndexAxisValueFormatter(labels2));
        XAxis bottomAxis = barChart.getXAxis();
        bottomAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        bottomAxis.setDrawLabels(true);
        bottomAxis.setDrawGridLines(false);
        bottomAxis.setDrawAxisLine(true);



        YAxis left = barChart.getAxisLeft();
        left.setAxisMinimum(0);
        left.setAxisMaximum(7);

        BarDataSet barDataSet = new BarDataSet(entries, "01");
        BarData barData = new BarData(barDataSet);

        YAxis rightYAxis = barChart.getAxisRight();
        rightYAxis.setEnabled(false);

        barChart.setData(barData);
        barChart.setDescription(null);
        barChart.setPinchZoom(false);
        barChart.setScaleEnabled(false);
        barChart.setDrawBarShadow(false);
        barChart.setDrawGridBackground(false);
        barChart.animateY(2000);
        barChart.getLegend().setEnabled(false);
        barChart.getData().setValueTextSize(10);

        barChart.getBarData().setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                if (value != 0) {
                    DecimalFormat format = new DecimalFormat("#");
                    return format.format(value) + "회";
                }
                return "";
            }
        });

//            barChart.getXAxis().setValueFormatter(new MyValueFormatter());

        barChart.invalidate();
    }

结果如下。

enter image description here 有人能帮我吗?谢谢。


ExRegList是什么?你如何填充它?没有这个列表数据,我无法运行你的代码。所以很难提供帮助。我认为它是JSON数组。你能发布JSON吗?@Adrian - Misha Akopov
@MishaAkopov ExRegList是基于json的数据。我从中获取了所有数据,例如计数事项。 - Adrian
好的,我差不多解决了这个问题。很快会回答你。 - Misha Akopov
@MishaAkopov 确定吗?谢谢!你可以帮我节省时间.. 我不知道我缺少了什么.. - Adrian
@Adrian 看看解决方案 :) - Misha Akopov
2个回答

15

在查看库的源代码后,最终找到了问题所在。您应该调用setLabelCount。在这行代码之后:

  XAxis bottomAxis = barChart.getXAxis();

将计数设置为X轴标签:

   bottomAxis.setLabelCount(entries.size());

它会起作用。

在这里输入图片描述

说明: 基本上,默认的标签计数是6(如果您不指定),并且它不正确地计算适当的标签。在您的情况下,您有5个项目,格式化程序获取值0、0.8、1.6、2.4、3.2和4.0-6个值。该库的此方法为第二个值给出了“”值:

   public String getFormattedValue(float value, AxisBase axis) {
        int index = Math.round(value);

        if (index < 0 || index >= mValueCount || index != (int)value)
            return "";

        return mValues[index];
    }
这是一个源代码库,可以为您提供“标签”。在您的情况下,在呈现第二个值时会给出 ""。

哇塞...你是我的英雄..!! 我不知道那个..;; 我之前使用的是MPAndroidChart v2.2.4,它一直很好用。哇塞 !!! 我真的非常感激你。再次感谢!!! - Adrian
即使我之前不知道那个问题 :) 我花了一些时间研究他们的源代码并找到了解决方法。很高兴你的问题得到了解决。 - Misha Akopov
谢谢。如果可以的话,你能再帮我看一下吗?这次是最后一个值的问题。 https://dev59.com/iqfja4cB1Zd3GeqP4P9E - Adrian
是的,当然。你有答案在那里,等着你 :) - Misha Akopov

1
根据你的代码,我猜测代码或数据中存在运行时错误。
 try {
            exerciseWholeCnt = ExRegList.length();
          for (int i = 0; i < ExRegList.length(); i++) {
            JSONObject obj = ExRegList.getJSONObject(i);
            String week = obj.getString("WEEK");
            int exCnt = obj.getInt("RESULT_COUNT");
            labels.add(week + "주");
            labels2[i] = week+"주";
            entries.add(new BarEntry(i,exCnt));
         }

您能检查循环第二次是否完美运行且日志中没有任何内容吗?

    } catch (Exception e) {
        **e.printStackTrace();**
    }

嗯...抱歉;; 但它运行良好。我检查了日志,但没有任何记录... 它运行良好。 - Adrian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接