我正在尝试使用MPAndroidChart,我成功了,直到我看到他们有了新版本。 自从我使用的版本(在互联网上的教程之后)以来,许多事情都发生了变化,然后我无法在手机屏幕上正确管理显示。
我想显示3条形图。这是我的问题:
以下是我在List>中的内容:
toTime
以下是显示我的条形图的代码。
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());
}
在这里:
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);
}
}
答案 0 :(得分:0)
要在线查看示例:
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);