MPAndroidChart BarChart问题

时间:2019-01-15 17:52:03

标签: android mpandroidchart

我试图使用条形图(而不是水平图)显示一些数据。但是事实证明,这些条是不可见的,并且x,y轴似乎是倒置的。图例显示正确的颜色,但条形不能显示。 我的代码有什么错误或遗漏吗?

private void initBarChart(){
    // Graph
    barChart.setBackgroundColor(Color.WHITE);
    barChart.setDrawGridBackground(false);
    barChart.setDrawBarShadow(false);
    barChart.setHighlightFullBarEnabled(false);
    barChart.setDrawBorders(false);
    barChart.animateY(1000, Easing.Linear);
    barChart.animateX(1000, Easing.Linear);
    barChart.getDescription().setEnabled(false);

    // Axis
    xAxis = barChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setAxisMinimum(0f);
    xAxis.setGranularity(1f);
    xAxis.setTextColor(ContextCompat.getColor(getContext(), android.R.color.darker_gray));

    leftAxis = barChart.getAxisLeft();
    rightAxis = barChart.getAxisRight();
    leftAxis.setAxisMinimum(0f);
    rightAxis.setAxisMinimum(0f);

    // Legend
    legend = barChart.getLegend();
    legend.setForm(Legend.LegendForm.LINE);
    legend.setTextSize(11f);
    legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
    legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    legend.setDrawInside(false);
}

private void initValues(){
    yVals_spending = new ArrayList<>();          
    yVals_income = new ArrayList<>();            
    //xVals = new ArrayList<>();                  

    // Add months
    for (int i = 0; i < 12; i++){
        //xVals.add(new DateFormatSymbols().getMonths()[i]);
        yVals_spending.add(new BarEntry(dataManagerClass.month_spending(i+1, 0).floatValue(), i));
        yVals_income.add(new BarEntry(dataManagerClass.month_spending(i+1, 1).floatValue(), i));
    }
}

private void initBarDataSet(){
    // BarDataSet
    BarDataSet spendingDataSet = new BarDataSet(yVals_spending, "Spending");
    BarDataSet incomeDataSet = new BarDataSet(yVals_income, "Income");
    spendingDataSet.setColor(ContextCompat.getColor(getContext(), R.color.MyTheme_colorPrimary));
    incomeDataSet.setColor(ContextCompat.getColor(getContext(), R.color.MyTheme_colorSecondary));

    // IBarDataSets
    ArrayList<IBarDataSet> iBarDataSets = new ArrayList<>();
    iBarDataSets.add(spendingDataSet);
    iBarDataSets.add(incomeDataSet);

    // BarData
    BarData barData = new BarData(iBarDataSets);
    barChart.setData(barData);
}

Bar Chart Picture

0 个答案:

没有答案