2017-06-21 60 views
0

我有一個條形圖,其中有4個條形,使用MPAndroidChart library分組成2個組。 這個工程到目前爲止。現在我想在每個組上面顯示一個標題。這是我當前的輸出(如您所見,標題未顯示在每個組上方)。MPAndroidChart分組條形圖:組標題未顯示在組上

enter image description here

我的代碼:

_overviewBarChart.setPinchZoom(false); 
    _overviewBarChart.getDescription().setEnabled(false); 
    _overviewBarChart.setDrawValueAboveBar(true); 
    _overviewBarChart.getXAxis().setCenterAxisLabels(true); 
    _overviewBarChart.getXAxis().setAxisMinimum(0); 
    _overviewBarChart.getXAxis().setDrawGridLines(false); 

private void updateOverviewBarChart() { 

    //Init calorie needs bars 

    BarEntry todayCalorieNeedsBarEntry = new BarEntry(0, _calorieEntry.getTarget()); 
    BarEntry yesterdayCalorieNeedsBarEntry = new BarEntry(0, 0); 
    if (_yesterdayCalorieEntry != null) 
     yesterdayCalorieNeedsBarEntry = new BarEntry(0, _yesterdayCalorieEntry.getTarget()); 

    ArrayList<BarEntry> calorieNeedsBarEntries = new ArrayList<>(); 
    calorieNeedsBarEntries.add(todayCalorieNeedsBarEntry); 
    calorieNeedsBarEntries.add(yesterdayCalorieNeedsBarEntry); 

    //Init consumed calories bars 

    BarEntry todayConsumedCaloriesBarEntry = new BarEntry(1, _calorieEntry.getConsumed()); 
    BarEntry yesterdayConsumedCaloriesBarEntry = new BarEntry(1, 0); 
    if (_yesterdayCalorieEntry != null) 
     yesterdayConsumedCaloriesBarEntry = new BarEntry(1, _yesterdayCalorieEntry.getConsumed()); 

    ArrayList<BarEntry> consumedCaloriesBarEntries = new ArrayList<>(); 
    consumedCaloriesBarEntries.add(todayConsumedCaloriesBarEntry); 
    consumedCaloriesBarEntries.add(yesterdayConsumedCaloriesBarEntry); 

    //Init BarDataSets 

    BarDataSet calorieNeedsBarDataSet = new BarDataSet(calorieNeedsBarEntries, getString(R.string.fragment_main_calorieneeds)); 
    calorieNeedsBarDataSet.setColor(Color.parseColor("#26A69A")); 
    BarDataSet consumedCaloriesBarDataSet = new BarDataSet(consumedCaloriesBarEntries, getString(R.string.fragment_main_consumed)); 
    consumedCaloriesBarDataSet.setColor(Color.parseColor("#E53935")); 

    //Init BarData, group BarEntrys, set group titles 

    BarData barData = new BarData(calorieNeedsBarDataSet, consumedCaloriesBarDataSet); 
    barData.setValueTextSize(14); 
    barData.setBarWidth(0.2f); 
    barData.groupBars(0, 0.15f, 0.1f); 

    ArrayList<String> groupTitles = new ArrayList<String>(); 
    groupTitles.add(getString(R.string.fragment_main_today)); 
    groupTitles.add(getString(R.string.fragment_main_yesterday)); 

    _overviewBarChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(groupTitles));// new BarChartStringFormatter(groupTitles)); 

    //Set data, redraw BarChart 

    _overviewBarChart.setData(barData); 
    _overviewBarChart.invalidate(); 
} 

你可以看到我做錯了嗎?

回答

0

最後,我找到了我的問題的解決方案。

首先你要添加的代碼這兩條線(注:該參數可以爲您的要求各不相同):

_overviewBarChart.getXAxis().setGranularity(1f); 
_overviewBarChart.getXAxis().setAxisMaximum(2); 

我的錯誤是,默認的X軸是要細分化(0.1,0.2,0.3 ...)。因此,IndexAxisValueFormatter將組標題應用於每個XAxis步驟。 setGranularity解決了這個問題。最後,setAxisMaximum用於限制XAxis上的步驟數(以前是3,因此標題顯示3次)。

,然後更改......

barData.groupBars(0, 0.15f, 0.1f); 

這個(注:該參數可以爲您的要求各不相同)..

barData.groupBars(0, 0.45f, 0.1f); 

結果:

enter image description here