我正在使用MPAndroidChart来展示BarChart。
我想显示GroupBars的标签,而不是单个条形码。
这是我的两个BarDataSet
。 (我使用SQLite来获取值,因此我可以显示此代码)
我添加了这些是我的代码,用于添加两个BarDataSet
。
BarDataSet expenseBarDataSet = new BarDataSet(expenseAmounts, "Expense");
expenseBarDataSet.setColors(Color.RED);
expenseBarDataSet.setValueTextColor(Color.WHITE);
BarDataSet incomeBarDataSet = new BarDataSet(incomeAmounts, "Income");
incomeBarDataSet.setColors(Color.GREEN);
incomeBarDataSet.setValueTextColor(Color.WHITE);
BarData barData = new BarData(expenseBarDataSet, incomeBarDataSet);
barData.setValueTextSize(12f);
barData.setValueFormatter(new LargeValueFormatter());
这是BarChart的代码
customDatesBarChart.setData(barChartExpenseData.getBarData()); //getBarData will return the above BarData
customDatesBarChart.groupBars(0f, 0.5f, 0f);
customDatesBarChart.getData().setHighlightEnabled(false);
customDatesBarChart.setDescription(null);
customDatesBarChart.setPinchZoom(false);
customDatesBarChart.setScaleEnabled(false);
customDatesBarChart.setDrawBarShadow(false);
customDatesBarChart.setDrawGridBackground(false);
customDatesBarChart.getXAxis().setAxisMaximum(0 + customDatesBarChart.getBarData().getGroupWidth(0.5f, 0f) * barChartExpenseData.count());
customDatesBarChart.invalidate();
Legend barChartLegend = customDatesBarChart.getLegend();
barChartLegend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
barChartLegend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
barChartLegend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
barChartLegend.setDrawInside(true);
barChartLegend.setYOffset(20f);
barChartLegend.setXOffset(0f);
barChartLegend.setYEntrySpace(0f);
barChartLegend.setTextSize(12f);
barChartLegend.setTextColor(Color.WHITE);
XAxis xAxis = customDatesBarChart.getXAxis();
xAxis.setAxisLineColor(Color.WHITE);
xAxis.setGridColor(Color.WHITE);
xAxis.setTextColor(Color.WHITE);
xAxis.setTextSize(12f);
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
xAxis.setCenterAxisLabels(true);
xAxis.setDrawGridLines(false);
xAxis.setAxisMinimum(0);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setValueFormatter(new IndexAxisValueFormatter(barChartExpenseData.getCategories())); // getCategories will return the labels, for example cat1, cat2
xAxis.setLabelCount(barChartExpenseData.getBarData().getEntryCount());
customDatesBarChart.getAxisRight().setEnabled(false);
YAxis yAxis = customDatesBarChart.getAxisLeft();
yAxis.setAxisLineColor(Color.WHITE);
yAxis.setGridColor(Color.WHITE);
yAxis.setTextColor(Color.WHITE);
yAxis.setValueFormatter(new LargeValueFormatter());
yAxis.setDrawGridLines(true);
yAxis.setTextSize(12f);
yAxis.setSpaceTop(35f);
yAxis.setAxisMinimum(0f);
这是我的输出
正如您所看到的,cat1和cat2是GroupBars的标签,但它显示的是单个条形图。
如何在BarChart中为GroupBars设置标签?
答案 0 :(得分:0)
使用版本3.0.1并按照以下示例操作:
barWidth = 0.3f;
barSpace = 0f;
groupSpace = 0.4f;
chart = (BarChart)findViewById(R.id.barChart);
chart.setDescription(null);
chart.setPinchZoom(false);
chart.setScaleEnabled(false);
chart.setDrawBarShadow(false);
chart.setDrawGridBackground(false);
int groupCount = 6;
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("Jan");
xVals.add("Feb");
xVals.add("Mar");
xVals.add("Apr");
xVals.add("May");
xVals.add("Jun");
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
yVals1.add(new BarEntry(1, (float) 1));
yVals2.add(new BarEntry(1, (float) 2));
yVals1.add(new BarEntry(2, (float) 3));
yVals2.add(new BarEntry(2, (float) 4));
yVals1.add(new BarEntry(3, (float) 5));
yVals2.add(new BarEntry(3, (float) 6));
yVals1.add(new BarEntry(4, (float) 7));
yVals2.add(new BarEntry(4, (float) 8));
yVals1.add(new BarEntry(5, (float) 9));
yVals2.add(new BarEntry(5, (float) 10));
yVals1.add(new BarEntry(6, (float) 11));
yVals2.add(new BarEntry(6, (float) 12));
BarDataSet set1, set2;
set1 = new BarDataSet(yVals1, "A");
set1.setColor(Color.RED);
set2 = new BarDataSet(yVals2, "B");
set2.setColor(Color.BLUE);
BarData data = new BarData(set1, set2);
data.setValueFormatter(new LargeValueFormatter());
//X-axis
XAxis xAxis = chart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
xAxis.setCenterAxisLabels(true);
xAxis.setDrawGridLines(false);
xAxis.setAxisMaximum(6);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));
//Y-axis
chart.getAxisRight().setEnabled(false);
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setValueFormatter(new LargeValueFormatter());
leftAxis.setDrawGridLines(true);
leftAxis.setSpaceTop(35f);
leftAxis.setAxisMinimum(0f);
// create a MarkerView
// MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker);
// chart.setMarker(mv);
chart.setData(data);
chart.getBarData().setBarWidth(barWidth);
chart.getXAxis().setAxisMinimum(0);
chart.getXAxis().setAxisMaximum(0 + chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
chart.groupBars(0, groupSpace, barSpace);
chart.getData().setHighlightEnabled(false);
chart.invalidate();
从代码中取消注释以下行并检查它是否发生了任何变化:
// customDatesBarChart.getBarData().setBarWidth(barWidth);