MPAndroidChart-如何正确显示相关X轴位置下的数据
我正在尝试使用MPAndroidChart,我成功地做到了,直到我看到他们有了一个新版本。 自从我使用的版本(在互联网上的教程之后)以来,很多事情都发生了变化,然后我无法正确管理手机屏幕上的显示 我想显示3个条形图。以下是我的问题:MPAndroidChart-如何正确显示相关X轴位置下的数据,android,bar-chart,mpandroidchart,Android,Bar Chart,Mpandroidchart,我正在尝试使用MPAndroidChart,我成功地做到了,直到我看到他们有了一个新版本。 自从我使用的版本(在互联网上的教程之后)以来,很多事情都发生了变化,然后我无法正确管理手机屏幕上的显示 我想显示3个条形图。以下是我的问题: 钢筋与其相关轴点不一致(且应一致) 这两条线一个接一个地显示,我想把它们分开 对于2“Sep”,我将稍后检查我的代码 以下是我在列表中抓取的内容>: Month Loan Borrow 7 15 5 8
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 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;
}
}
公共类MonthAxisValueFormatter实现IAxisValueFormatter{
受保护的字符串[]个字符;
私人语境;
专用条形图基础图;
公共MonthAxisValueFormatter(BarLineChartBase图表,上下文){
this.chart=图表;
this.context=上下文;
mmoths=新字符串[]{
this.context.getResources().getString(R.string.january),
this.context.getResources(),
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.septer),
this.context.getResources().getString(R.string.十月),
this.context.getResources(),
this.context.getResources().getString(R.string.december)
};
}
@凌驾
公共字符串getFormattedValue(浮点值,AxisBase轴){
整数月份=(整数)值;
字符串monthName=months[月份%months.length];
返回月名;
}
}
看看这个例子:
要在线查看示例,请执行以下操作:
您需要添加如下行:
float groupSpace = 0.08f;
float barSpace = 0.03f; // x4 DataSet
int groupCount = mSeekBarX.getProgress() + 1;
int startMonth = 0;
mChart.groupBars(startYear, groupSpace, barSpace);
试试这个:XAxis XAxis=chart.getXAxis();xAxis.setGranularity(1f);xAxis.setGranularityEnabled(真);xAxis.setLabelCount(12,真);嗨,奥古斯丁,终于可以让我的数据与X轴对齐了,谢谢你。仅保留两个条形部分(红色和绿色应为不同的条形)。
float groupSpace = 0.08f;
float barSpace = 0.03f; // x4 DataSet
int groupCount = mSeekBarX.getProgress() + 1;
int startMonth = 0;
mChart.groupBars(startYear, groupSpace, barSpace);