【问题标题】:MPAndroidChart - How to correctly display data under their related X axis positionMPAndroidChart - 如何在相关 X 轴位置下正确显示数据
【发布时间】:2016-12-14 13:53:36
【问题描述】:

我正在尝试使用 MPAndroidChart,并且我成功了,直到我看到他们有一个新版本。 自从我使用的版本(按照互联网上的教程)以来,很多东西都发生了变化,然后我无法正确管理手机屏幕上的显示。

我想显示 3 个条形图。这是我的问题:

  1. 条形与其相关的轴点不对齐(它们应该是),
  2. 2 个条形图一个接一个地显示,我想将它们分开,
  3. 对于 2“Sep”,我稍后会检查我的代码。

这是我进入我的列表的内容>:

Month     Loan     Borrow
7         15       5
8         0        5.7
10        102.4    2
11        15       5
12        25.5     35.5

这是显示我的条形图的代码。

private void moneyBarChart(){
        BarChart barChart = (BarChart) findViewById(R.id.chart);
        barChart.getDescription().setText(getResources().getString(R.string.money_bar_chart));
        MonthConverter monthConverter = new MonthConverter(this);
        //HorizontalBarChart barChart= (HorizontalBarChart) findViewById(R.id.chart);

        List<List<Float>> amountByMonth = dbHandlers.getDbMoneyHandler().getLastSixMonthsMoney();

        ArrayList<String> labels = new ArrayList<>();
        ArrayList<BarEntry> loan = new ArrayList<>();
        ArrayList<BarEntry> borrow = new ArrayList<>();

        List<Float> temp = amountByMonth.get(0);
        int position = 0;
        int currentMonth = calendar.get(Calendar.MONTH);
        int j = 5;

        barChart.getXAxis().setAxisMinimum((float) currentMonth - j);
        barChart.getXAxis().setAxisMaximum((float) currentMonth);

        for (int i=0; i<amountByMonth.size(); i++) {
            while (currentMonth - j != Math.round(temp.get(i * 3))-1){
                labels.add(monthConverter.convert(currentMonth - j));
                loan.add(new BarEntry(currentMonth - j, 0f));
                borrow.add(new BarEntry(currentMonth - j, 0f));
                j-=1;
                position++;
            }
            labels.add(monthConverter.convert(Math.round(temp.get(i * 3))));
            loan.add(new BarEntry(Math.round(temp.get(i * 3)-1), Math.round(temp.get(i * 3 + 1))));
            borrow.add(new BarEntry(Math.round(temp.get(i * 3)-1), Math.round(temp.get(i * 3 + 2))));
            position++;
            j-=1;
        }

        BarDataSet barDataSet1 = new BarDataSet(loan, getResources().getString(R.string.type_loan));
        barDataSet1.setColor(Color.rgb(0, 155, 0));
        //barDataSet1.setColors(ColorTemplate.COLORFUL_COLORS);

        BarDataSet barDataSet2 = new BarDataSet(borrow, getResources().getString(R.string.type_borrow));
        barDataSet2.setColor(Color.rgb(155, 0, 0));
        //barDataSet2.setColors(ColorTemplate.COLORFUL_COLORS);

        ArrayList<IBarDataSet> dataset = new ArrayList<>();
        dataset.add(barDataSet1);
        dataset.add(barDataSet2);

        //BarData data = new BarData(labels, dataset);
        BarData data = new BarData(dataset);
        barChart.setData(data);
        barChart.getXAxis().setValueFormatter(new MonthAxisValueFormatter(barChart, this));
        barChart.invalidate();
        barChart.animateY(2000);
        barChart.getBarData().setBarWidth(0.5f);


        data.setValueFormatter(new CustomYAxisValueFormatter());
    }

注意肯定有帮助,但下面是代码:

  • CustomYAxisValueFormatter,
  • MonthAxisValueFormatter

这里:

public class CustomYAxisValueFormatter implements IValueFormatter {

    private DecimalFormat mFormat;

    public CustomYAxisValueFormatter() {
        mFormat = new DecimalFormat("###,###,###,##0.0"); // sets precision to 1
    }

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        return mFormat.format(value);
    }
}

这里:

public class MonthAxisValueFormatter implements IAxisValueFormatter {

    protected String[] mMonths;
    private Context context;

    private BarLineChartBase<?> chart;

    public MonthAxisValueFormatter(BarLineChartBase<?> chart, Context context) {
        this.chart = chart;
        this.context = context;

        mMonths = new String[]{
                this.context.getResources().getString(R.string.january),
                this.context.getResources().getString(R.string.february),
                this.context.getResources().getString(R.string.march),
                this.context.getResources().getString(R.string.april),
                this.context.getResources().getString(R.string.may),
                this.context.getResources().getString(R.string.june),
                this.context.getResources().getString(R.string.july),
                this.context.getResources().getString(R.string.august),
                this.context.getResources().getString(R.string.september),
                this.context.getResources().getString(R.string.october),
                this.context.getResources().getString(R.string.november),
                this.context.getResources().getString(R.string.december)
        };
    }

    @Override
    public String getFormattedValue(float value, AxisBase axis) {

        int month = (int) value;

        String monthName = mMonths[month % mMonths.length];

        return monthName;
    }
}

【问题讨论】:

  • 试试这个:XAxis xAxis = chart.getXAxis(); xAxis.setGranularity(1f); xAxis.setGranularityEnabled(true); xAxis.setLabelCount(12, true);
  • 嗨 Augustin,终于可以将我的数据与我的 X 轴对齐了,谢谢。仅保留两个条形部分(红色和绿色应该是不同的条形)。

标签: android bar-chart mpandroidchart


【解决方案1】:

看看这个例子:https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java

在线查看示例:

https://android-arsenal.com/details/1/741#!liveDemo

你需要添加这样的行:

float groupSpace = 0.08f;
float barSpace = 0.03f; // x4 DataSet
int groupCount = mSeekBarX.getProgress() + 1;
int startMonth = 0;
mChart.groupBars(startYear, groupSpace, barSpace);

【讨论】:

    猜你喜欢
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2013-12-12
    • 1970-01-01
    相关资源
    最近更新 更多