MPAndroid Chart沒有顯示xAxis的任何標籤,缺少什麼?




android-layout charts (2)

我正在實施一項功能,用於顯示物業的收入/支出,並提供1個月,3個月,6個月和12個月的過濾器。

因此,我必鬚根據選擇的過濾器顯示月份標籤,例如,如果選擇3個月過濾器,則需要顯示3月,2月和1月,但當前標籤未顯示在XAxis上。

這是我的代碼,請糾正任何問題:

private void setChart() {

    ArrayList<BarEntry> incomeEntries = getIncomeEntries();
    ArrayList<BarEntry> expenseEntries = getExpenseEntries();

    BarDataSet set1, set2;

    set1 = new BarDataSet(incomeEntries, "Income");
    set1.setColor(Color.rgb(65, 168, 121));
    set1.setValueTextColor(Color.rgb(55, 70, 73));
    set1.setValueTextSize(10f);

    set2 = new BarDataSet(expenseEntries, "Expense");
    set2.setColors(Color.rgb(241, 107, 72));
    set2.setValueTextColor(Color.rgb(55, 70, 73));
    set2.setValueTextSize(10f);

    ArrayList<IBarDataSet> dataSets = new ArrayList<>();
    dataSets.add(set1);
    dataSets.add(set2);

    BarData data = new BarData(dataSets);
    barChart.setData(data);


    barChart.getDescription().setEnabled(false);
    barChart.setDrawBarShadow(false);
    barChart.setDrawValueAboveBar(true);
    barChart.setMaxVisibleValueCount(10);
    barChart.setPinchZoom(false);
    barChart.setDrawGridBackground(false);
    barChart.animateY(1400, Easing.EaseInOutQuad);
    barChart.animateXY(3000, 3000);

    Legend l = barChart.getLegend();
    l.setWordWrapEnabled(true);
    l.setTextSize(14);
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setForm(Legend.LegendForm.CIRCLE);


    XAxis xAxis = barChart.getXAxis();
    xAxis.setGranularity(1f);
    xAxis.setCenterAxisLabels(true);
    xAxis.setDrawGridLines(false);
    xAxis.setLabelRotationAngle(-45);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setAxisMaximum(data.getXMax() + 0.25f);

    ArrayList<String> formatter = getFormattter();

    Log.d(TAG,"Labels Received :"+formatter.size()); // Printing 3

    barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(formatter));

    barChart.getXAxis().setLabelCount(formatter.size(),true);

    Log.d(TAG,"Labels Count :"+xAxis.getLabelCount()); // Printing 3

    YAxis leftAxis = barChart.getAxisLeft();
    leftAxis.removeAllLimitLines();
    leftAxis.setTypeface(Typeface.DEFAULT);
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setTextColor(Color.BLACK);
    leftAxis.setDrawGridLines(false);
    leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true
    barChart.getAxisRight().setEnabled(false);

    float maxValue = 0, minValue = 0;

    for (int i = 0; i < incomeEntries.size(); i++) {

        if (maxValue < incomeEntries.get(i).getY()) {
            maxValue = incomeEntries.get(i).getY();
        }

        if (maxValue < expenseEntries.get(i).getY()) {
            maxValue = expenseEntries.get(i).getY();
        }

    }

    maxValue = maxValue + 100;
    Log.e(TAG, "==================== MAX VALUE = " + maxValue);

    leftAxis.setAxisMaximum(maxValue);
    leftAxis.setAxisMinimum(minValue);

    leftAxis.setAxisMaxValue(maxValue);
    leftAxis.setStartAtZero(true);

    data.setValueFormatter(new LargeValueFormatter());

    //data
    float groupSpace = 0.25f;
    float barSpace = 0.05f; // x2 dataset
    float barWidth = 0.35f; // x2 dataset


    barChart.getBarData().setBarWidth(barWidth);
    barChart.getXAxis().setAxisMinValue(10f);
    barChart.groupBars(10, groupSpace, barSpace);
    barChart.invalidate();

}

private ArrayList<String> getFormattter() {

    Log.e(TAG, "GET FORMATTED VALUE");

    switch (chartType) {

        case AppConstants.CHART_TYPE_1_MONTH:
            monthFormatter();
            return oneMonthLabels;

        case AppConstants.CHART_TYPE_3_MONTH:
            threeMonthFormatter();
            return threeMonthLabels;

        case AppConstants.CHART_TYPE_6_MONTH:
            sixMonthFormatter();
            return sixMonthLabels;

        case AppConstants.CHART_TYPE_12_MONTH:
            yearFormatter();
            return yearLabels;
    }
    return null;
}

private void threeMonthFormatter() {

    Log.e(TAG, "threeMonthFormatter , label list size : " + threeMonthLabels.size());
    if (null != threeMonthLabels) {
        threeMonthLabels.clear();
    }

    Calendar calendar = Calendar.getInstance();
    String month = new SimpleDateFormat("MMM").format(calendar.getTime());
    threeMonthLabels.add(month);

    calendar.add(Calendar.MONTH, -1);
    month = new SimpleDateFormat("MMM").format(calendar.getTime());
    threeMonthLabels.add(month);

    calendar.add(Calendar.MONTH, -1);
    month = new SimpleDateFormat("MMM").format(calendar.getTime());
    threeMonthLabels.add(month);

    for (int i = 0; i < threeMonthLabels.size(); i++) {
        Log.e(TAG, "Label : " + threeMonthLabels.get(i));
    }
}

我已經附加了截至目前已實現的屏幕。正如你可以看到的那樣,按照三個月的過濾器,它顯示每3個月的標尺,但是沒有標籤,甚至monthFormatter創建正好3個月的標籤。

這是顯示從Formatter方法接收日誌的標籤的另一個屏幕截圖。

從Formatter方法登錄

主屏幕


這兩行造成了這個問題:

barChart.getXAxis().setAxisMinValue(10f);     
barChart.groupBars(10, groupSpace, barSpace);

你正在將x軸的最小值設置為10,而標籤將以索引0開始,因此內部的IndexOut的綁定異常不斷發生所以,

改成:

barChart.getXAxis().setAxisMinValue(0f);     // Better remove setAxisMinValue(), as it deprecated
barChart.groupBars(0, groupSpace, barSpace);

如果您仍希望將10作為初始間距,則覆蓋 IAxisValueFormatter (不推薦)

barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(formatter) {

 @Override
        public String getFormattedValue(float value) {
            int x = (int)value / 10
            if(x > = 0 && x < formatter.size())
               return formatter.get(x);
            else
               return "";
        }
});

由於您有固定的組空間,條形空間和寬度。 您需要為多個數據集進行大量自定義。

但是,對於這樣的動態圖表,我不會使用您的解決方案,

我重寫了你的解決方案以適應不斷變化的寬度。 (這是結果)

Github項目鏈接

Apk Link

BarChart barChart;
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
float defaultBarWidth = -1;
List<String> xAxisValues = new ArrayList<>(Arrays.asList("Jan", "Feb", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"));


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    barChart = findViewById(R.id.barchart);
    setChart(3);
    RadioGroup radioGroup = findViewById(R.id.radio_group);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int switchId) {

            switch (switchId) {
                case R.id.one_month:
                    setChart(1);
                    break;
                case R.id.three_month:
                    setChart(3);
                    break;
                case R.id.six_month:
                    setChart(6);
                    break;
                case R.id.tweleve_month:
                    setChart(12);
                    break;
            }
        }
    });
}


private void setChart(int size) {

    List<BarEntry> incomeEntries = getIncomeEntries(size);
    List<BarEntry> expenseEntries = getExpenseEntries(size);
    dataSets = new ArrayList<>();
    BarDataSet set1, set2;

    set1 = new BarDataSet(incomeEntries, "Income");
    set1.setColor(Color.rgb(65, 168, 121));
    set1.setValueTextColor(Color.rgb(55, 70, 73));
    set1.setValueTextSize(10f);

    set2 = new BarDataSet(expenseEntries, "Expense");
    set2.setColors(Color.rgb(241, 107, 72));
    set2.setValueTextColor(Color.rgb(55, 70, 73));
    set2.setValueTextSize(10f);

    dataSets.add(set1);
    dataSets.add(set2);

    BarData data = new BarData(dataSets);
    barChart.setData(data);
    barChart.getAxisLeft().setAxisMinimum(0);

    barChart.getDescription().setEnabled(false);
    barChart.getAxisRight().setAxisMinimum(0);
    barChart.setDrawBarShadow(false);
    barChart.setDrawValueAboveBar(true);
    barChart.setMaxVisibleValueCount(10);
    barChart.setPinchZoom(false);
    barChart.setDrawGridBackground(false);

    Legend l = barChart.getLegend();
    l.setWordWrapEnabled(true);
    l.setTextSize(14);
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setForm(Legend.LegendForm.CIRCLE);

    XAxis xAxis = barChart.getXAxis();
    xAxis.setGranularity(1f);
    xAxis.setCenterAxisLabels(true);
    xAxis.setDrawGridLines(false);
    xAxis.setLabelRotationAngle(-45);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setAxisMaximum(getExpenseEntries(size).size());

    barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisValues));

    YAxis leftAxis = barChart.getAxisLeft();
    leftAxis.removeAllLimitLines();
    leftAxis.setTypeface(Typeface.DEFAULT);
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setTextColor(Color.BLACK);
    leftAxis.setDrawGridLines(false);
    barChart.getAxisRight().setEnabled(false);

    setBarWidth(data, size);
    barChart.invalidate();

}

private void setBarWidth(BarData barData, int size) {
    if (dataSets.size() > 1) {
        float barSpace = 0.02f;
        float groupSpace = 0.3f;
        defaultBarWidth = (1 - groupSpace) / dataSets.size() - barSpace;
        if (defaultBarWidth >= 0) {
            barData.setBarWidth(defaultBarWidth);
        } else {
            Toast.makeText(getApplicationContext(), "Default Barwdith " + defaultBarWidth, Toast.LENGTH_SHORT).show();
        }
        int groupCount = getExpenseEntries(size).size();
        if (groupCount != -1) {
            barChart.getXAxis().setAxisMinimum(0);
            barChart.getXAxis().setAxisMaximum(0 + barChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
            barChart.getXAxis().setCenterAxisLabels(true);
        } else {
            Toast.makeText(getApplicationContext(), "no of bar groups is " + groupCount, Toast.LENGTH_SHORT).show();
        }

        barChart.groupBars(0, groupSpace, barSpace); // perform the "explicit" grouping
        barChart.invalidate();
    }
}

private List<BarEntry> getExpenseEntries(int size) {
    ArrayList<BarEntry> expenseEntries = new ArrayList<>();

    expenseEntries.add(new BarEntry(1,1710));
    expenseEntries.add(new BarEntry(2,2480));
    expenseEntries.add(new BarEntry(3,242));
    expenseEntries.add(new BarEntry(4,2409));
    expenseEntries.add(new BarEntry(5,8100));
    expenseEntries.add(new BarEntry(6,1200));
    expenseEntries.add(new BarEntry(7,6570));
    expenseEntries.add(new BarEntry(8,5455));
    expenseEntries.add(new BarEntry(9,15000));
    expenseEntries.add(new BarEntry(10,11340));
    expenseEntries.add(new BarEntry(11,9100));
    expenseEntries.add(new BarEntry(12,6300));
    return expenseEntries.subList(0, size);
}

private List<BarEntry> getIncomeEntries(int size) {
    ArrayList<BarEntry> incomeEntries = new ArrayList<>();

    incomeEntries.add(new BarEntry(1, 11300));
    incomeEntries.add(new BarEntry(2, 1390));
    incomeEntries.add(new BarEntry(3, 1190));
    incomeEntries.add(new BarEntry(4, 7200));
    incomeEntries.add(new BarEntry(5, 4790));
    incomeEntries.add(new BarEntry(6, 4500));
    incomeEntries.add(new BarEntry(7, 8000));
    incomeEntries.add(new BarEntry(8, 7034));
    incomeEntries.add(new BarEntry(9, 4307));
    incomeEntries.add(new BarEntry(10, 8762));
    incomeEntries.add(new BarEntry(11, 4355));
    incomeEntries.add(new BarEntry(12, 6000));
    return incomeEntries.subList(0, size);
}

請注意, setBarWidth() 函數是最重要的,它起著調整圖形條寬度和大小的作用。


替換下面的行

barChart.getXAxis().setValueFormatter(new IAxisValueFormatter(formatter));

代替

barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(formatter));

並檢查。