MPAndroid 图表未显示 xAxis 的任何标签,缺少什么? [英] MPAndroid Chart not displaying any labels for xAxis , what is missing?
问题描述
我正在实施一项功能,用于显示财产和支出的收入/支出.还提供了 1 个月、3 个月、6 个月和 6 个月的过滤器12 个月.
因此,我必须根据选择的过滤器显示月份标签,例如,如果选择了 3 个月过滤器,则需要显示 March , Feb &Jan 但是当前标签没有显示在 XAxis 上.
这是我的代码,请纠正任何问题:
private void setChart() {ArrayList收入条目 = getIncomeEntries();ArrayList费用条目 = getExpenseEntries();BarDataSet set1, set2;set1 = new BarDataSet(incomeEntries, "收入");set1.setColor(Color.rgb(65, 168, 121));set1.setValueTextColor(Color.rgb(55, 70, 73));set1.setValueTextSize(10f);set2 = new BarDataSet(expenseEntries, "Expense");set2.setColors(Color.rgb(241, 107, 72));set2.setValueTextColor(Color.rgb(55, 70, 73));set2.setValueTextSize(10f);ArrayList数据集 = 新的 ArrayList<>();dataSets.add(set1);dataSets.add(set2);BarData 数据 = new BarData(dataSets);barChart.setData(data);barChart.getDescription().setEnabled(false);barChart.setDrawBarShadow(false);barChart.setDrawValueAboveBar(true);barChart.setMaxVisibleValueCount(10);barChart.setPinchZoom(false);barChart.setDrawGridBackground(false);barChart.animateY(1400, Easing.EaseInOutQuad);barChart.animateXY(3000, 3000);图例 l = barChart.getLegend();l.setWordWrapEnabled(true);l.setTextSize(14);l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);l.setOrientation(Legend.LegendOrientation.HORIZONTAL);l.setDrawInside(false);l.setForm(Legend.LegendForm.CIRCLE);XAxis xAxis = barChart.getXAxis();xAxis.setGranularity(1f);xAxis.setCenterAxisLabels(true);xAxis.setDrawGridLines(false);xAxis.setLabelRotationAngle(-45);xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);xAxis.setAxisMaximum(data.getXMax() + 0.25f);ArrayList格式化程序 = getFormatter();Log.d(TAG,"收到的标签:"+formatter.size());//打印 3barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(formatter));barChart.getXAxis().setLabelCount(formatter.size(),true);Log.d(TAG,"标签计数:"+xAxis.getLabelCount());//打印 3YAxis leftAxis = barChart.getAxisLeft();leftAxis.removeAllLimitLines();leftAxis.setTypeface(Typeface.DEFAULT);leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);leftAxis.setTextColor(Color.BLACK);leftAxis.setDrawGridLines(false);leftAxis.setAxisMinValue(0f);//这将替换 setStartAtZero(truebarChart.getAxisRight().setEnabled(false);浮动 maxValue = 0, minValue = 0;for (int i = 0; i
我已经附加了截至目前已实现的屏幕.正如您所看到的每三个月 Filter ,它显示每 3 个月的条形,但没有标签,即使 monthFormatter 创建正好 3 个月的标签.
这是另一个屏幕截图,用于显示从 Formatter 方法接收到的标签日志.
Log From Formatter 方法
主屏幕
这两行导致了问题:
<块引用>barChart.getXAxis().setAxisMinValue(10f);barChart.groupBars(10, groupSpace, barSpace);
您将 x 轴的最小值设置为 10,而标签将从索引 0 开始,因此内部 IndexOut 的绑定异常不断发生所以,
更改为:
barChart.getXAxis().setAxisMinValue(0f);//最好删除 setAxisMinValue(),因为它已被弃用barChart.groupBars(0, groupSpace, barSpace);
如果您仍然希望将 10 作为初始间距,请覆盖 IAxisValueFormatter(不推荐)
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(formatter) {@覆盖公共字符串getFormattedValue(浮点值){int x = (int) 值/10if(x > = 0 && x < formatter.size())返回 formatter.get(x);别的返回 "";}});
因为您有固定的组空间、条空间和宽度.您将需要针对多个数据集进行大量自定义.
<块引用>但是,对于这样的动态图,我不会采用您的解决方案,
我重新编写了您的解决方案以适应不断变化的宽度.(这是结果)
BarChart barChart;ArrayList数据集 = 新的 ArrayList<>();浮动 defaultBarWidth = -1;列表<字符串>xAxisValues = new ArrayList<>(Arrays.asList("Jan", "Feb", "March", "April", "May", "June", "July", "August", "September", "October, 十一月十二月"));@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);barChart = findViewById(R.id.barchart);设置图表(3);RadioGroup radioGroup = findViewById(R.id.radio_group);radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@覆盖public void onCheckedChanged(RadioGroup radioGroup, int switchId) {开关(开关 ID){案例 R.id.one_month:设置图表(1);休息;案例 R.id.three_month:设置图表(3);休息;案例 R.id.six_month:设置图表(6);休息;案例 R.id.tweleve_month:设置图表(12);休息;}}});}私有无效 setChart(整数大小){列表收入条目 = getIncomeEntries(size);列表费用条目 = getExpenseEntries(size);数据集 = 新的 ArrayList<>();BarDataSet set1, set2;set1 = new BarDataSet(incomeEntries, "收入");set1.setColor(Color.rgb(65, 168, 121));set1.setValueTextColor(Color.rgb(55, 70, 73));set1.setValueTextSize(10f);set2 = new BarDataSet(expenseEntries, "Expense");set2.setColors(Color.rgb(241, 107, 72));set2.setValueTextColor(Color.rgb(55, 70, 73));set2.setValueTextSize(10f);dataSets.add(set1);dataSets.add(set2);BarData 数据 = new BarData(dataSets);barChart.setData(data);barChart.getAxisLeft().setAxisMinimum(0);barChart.getDescription().setEnabled(false);barChart.getAxisRight().setAxisMinimum(0);barChart.setDrawBarShadow(false);barChart.setDrawValueAboveBar(true);barChart.setMaxVisibleValueCount(10);barChart.setPinchZoom(false);barChart.setDrawGridBackground(false);图例 l = barChart.getLegend();l.setWordWrapEnabled(true);l.setTextSize(14);l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);l.setOrientation(Legend.LegendOrientation.HORIZONTAL);l.setDrawInside(false);l.setForm(Legend.LegendForm.CIRCLE);XAxis xAxis = barChart.getXAxis();xAxis.setGranularity(1f);xAxis.setCenterAxisLabels(true);xAxis.setDrawGridLines(false);xAxis.setLabelRotationAngle(-45);xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);xAxis.setAxisMaximum(getExpenseEntries(size).size());barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisValues));YAxis leftAxis = barChart.getAxisLeft();leftAxis.removeAllLimitLines();leftAxis.setTypeface(Typeface.DEFAULT);leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);leftAxis.setTextColor(Color.BLACK);leftAxis.setDrawGridLines(false);barChart.getAxisRight().setEnabled(false);setBarWidth(数据,大小);barChart.invalidate();}私有无效 setBarWidth(BarData barData, int size) {如果 (dataSets.size() > 1) {浮动条空间 = 0.02f;浮动组空间 = 0.3f;defaultBarWidth = (1 - groupSpace)/dataSets.size() - barSpace;如果(defaultBarWidth >= 0){barData.setBarWidth(defaultBarWidth);} 别的 {Toast.makeText(getApplicationContext(), "Default Barwdith " + defaultBarWidth, Toast.LENGTH_SHORT).show();}int groupCount = getExpenseEntries(size).size();如果(组计数!= -1){barChart.getXAxis().setAxisMinimum(0);barChart.getXAxis().setAxisMaximum(0 + barChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);barChart.getXAxis().setCenterAxisLabels(true);} 别的 {Toast.makeText(getApplicationContext(), "条形组数为" + groupCount, Toast.LENGTH_SHORT).show();}barChart.groupBars(0, groupSpace, barSpace);//执行显式"分组barChart.invalidate();}}私人列表getExpenseEntries(整数大小){ArrayList费用条目 = 新的 ArrayList<>();费用条目.添加(新的 BarEntry(1,1710));费用条目.添加(新的 BarEntry(2,2480));费用条目.添加(新的 BarEntry(3,242));费用条目.添加(新的 BarEntry(4,2409));费用条目.添加(新的 BarEntry(5,8100));费用条目.添加(新的 BarEntry(6,1200));费用条目.添加(新的 BarEntry(7,6570));费用条目.添加(新的 BarEntry(8,5455));费用条目.添加(新的 BarEntry(9,15000));费用条目.添加(新的 BarEntry(10,11340));费用条目.添加(新的 BarEntry(11,9100));费用条目.添加(新的 BarEntry(12,6300));返回费用条目.subList(0, size);}私人列表getIncomeEntries(整数大小){ArrayList收入条目 = 新的 ArrayList<>();incomeEntries.add(new BarEntry(1, 11300));incomeEntries.add(new BarEntry(2, 1390));incomeEntries.add(new BarEntry(3, 1190));incomeEntries.add(new BarEntry(4, 7200));incomeEntries.add(new BarEntry(5, 4790));incomeEntries.add(new BarEntry(6, 4500));incomeEntries.add(new BarEntry(7, 8000));incomeEntries.add(new BarEntry(8, 7034));incomeEntries.add(new BarEntry(9, 4307));incomeEntries.add(new BarEntry(10, 8762));incomeEntries.add(new BarEntry(11, 4355));incomeEntries.add(new BarEntry(12, 6000));返回incomeEntries.subList(0, size);}
注意setBarWidth()
函数是最重要的,它的作用是调整图形条的宽度和大小.
I'm implementing a feature for displaying Incomes/Expenses for properties & also provided Filter for 1 Month , 3 Months , 6 Months & 12 Months .
So, I have to display Months labels according to filter selected for example if 3 Month filter selected then need to display March , Feb & Jan but what currently labels are not displaying on XAxis.
Here is my code , please rectify any issue:
private void setChart() {
ArrayList<BarEntry> incomeEntries = getIncomeEntries();
ArrayList<BarEntry> expenseEntries = getExpenseEntries();
BarDataSet set1, set2;
set1 = new BarDataSet(incomeEntries, "Income");
set1.setColor(Color.rgb(65, 168, 121));
set1.setValueTextColor(Color.rgb(55, 70, 73));
set1.setValueTextSize(10f);
set2 = new BarDataSet(expenseEntries, "Expense");
set2.setColors(Color.rgb(241, 107, 72));
set2.setValueTextColor(Color.rgb(55, 70, 73));
set2.setValueTextSize(10f);
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
dataSets.add(set2);
BarData data = new BarData(dataSets);
barChart.setData(data);
barChart.getDescription().setEnabled(false);
barChart.setDrawBarShadow(false);
barChart.setDrawValueAboveBar(true);
barChart.setMaxVisibleValueCount(10);
barChart.setPinchZoom(false);
barChart.setDrawGridBackground(false);
barChart.animateY(1400, Easing.EaseInOutQuad);
barChart.animateXY(3000, 3000);
Legend l = barChart.getLegend();
l.setWordWrapEnabled(true);
l.setTextSize(14);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
l.setForm(Legend.LegendForm.CIRCLE);
XAxis xAxis = barChart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setDrawGridLines(false);
xAxis.setLabelRotationAngle(-45);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAxisMaximum(data.getXMax() + 0.25f);
ArrayList<String> formatter = getFormattter();
Log.d(TAG,"Labels Received :"+formatter.size()); // Printing 3
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(formatter));
barChart.getXAxis().setLabelCount(formatter.size(),true);
Log.d(TAG,"Labels Count :"+xAxis.getLabelCount()); // Printing 3
YAxis leftAxis = barChart.getAxisLeft();
leftAxis.removeAllLimitLines();
leftAxis.setTypeface(Typeface.DEFAULT);
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setTextColor(Color.BLACK);
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true
barChart.getAxisRight().setEnabled(false);
float maxValue = 0, minValue = 0;
for (int i = 0; i < incomeEntries.size(); i++) {
if (maxValue < incomeEntries.get(i).getY()) {
maxValue = incomeEntries.get(i).getY();
}
if (maxValue < expenseEntries.get(i).getY()) {
maxValue = expenseEntries.get(i).getY();
}
}
maxValue = maxValue + 100;
Log.e(TAG, "==================== MAX VALUE = " + maxValue);
leftAxis.setAxisMaximum(maxValue);
leftAxis.setAxisMinimum(minValue);
leftAxis.setAxisMaxValue(maxValue);
leftAxis.setStartAtZero(true);
data.setValueFormatter(new LargeValueFormatter());
//data
float groupSpace = 0.25f;
float barSpace = 0.05f; // x2 dataset
float barWidth = 0.35f; // x2 dataset
barChart.getBarData().setBarWidth(barWidth);
barChart.getXAxis().setAxisMinValue(10f);
barChart.groupBars(10, groupSpace, barSpace);
barChart.invalidate();
}
private ArrayList<String> getFormattter() {
Log.e(TAG, "GET FORMATTED VALUE");
switch (chartType) {
case AppConstants.CHART_TYPE_1_MONTH:
monthFormatter();
return oneMonthLabels;
case AppConstants.CHART_TYPE_3_MONTH:
threeMonthFormatter();
return threeMonthLabels;
case AppConstants.CHART_TYPE_6_MONTH:
sixMonthFormatter();
return sixMonthLabels;
case AppConstants.CHART_TYPE_12_MONTH:
yearFormatter();
return yearLabels;
}
return null;
}
private void threeMonthFormatter() {
Log.e(TAG, "threeMonthFormatter , label list size : " + threeMonthLabels.size());
if (null != threeMonthLabels) {
threeMonthLabels.clear();
}
Calendar calendar = Calendar.getInstance();
String month = new SimpleDateFormat("MMM").format(calendar.getTime());
threeMonthLabels.add(month);
calendar.add(Calendar.MONTH, -1);
month = new SimpleDateFormat("MMM").format(calendar.getTime());
threeMonthLabels.add(month);
calendar.add(Calendar.MONTH, -1);
month = new SimpleDateFormat("MMM").format(calendar.getTime());
threeMonthLabels.add(month);
for (int i = 0; i < threeMonthLabels.size(); i++) {
Log.e(TAG, "Label : " + threeMonthLabels.get(i));
}
}
I have attached screen which is achieved as of now.As you can see as per three month Filter , it's displaying bars as per 3 months but there are no labels even monthFormatter create exactly 3 months labels.
Here is another screenshot to display labels received log from Formatter method.
Log From Formatter method
Main Screen
These two lines are causing the issue :
barChart.getXAxis().setAxisMinValue(10f); barChart.groupBars(10, groupSpace, barSpace);
you are setting the minimum value of x axis to 10, whereas the labels will begin with index 0, So internally IndexOut of bound exception keeps occurring So,
Change To:
barChart.getXAxis().setAxisMinValue(0f); // Better remove setAxisMinValue(), as it deprecated
barChart.groupBars(0, groupSpace, barSpace);
If you still want to have 10 as the initial spacing, override IAxisValueFormatter (Not recommended)
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(formatter) {
@Override
public String getFormattedValue(float value) {
int x = (int)value / 10
if(x > = 0 && x < formatter.size())
return formatter.get(x);
else
return "";
}
});
As you have fixed group space, bar space and width. you will need to heavily customise for multiple dataset.
However I wouldn't go with your solution for such a dynamic graph,
I have re-written your solution to adapt to changing width. (This are the results)
BarChart barChart;
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
float defaultBarWidth = -1;
List<String> xAxisValues = new ArrayList<>(Arrays.asList("Jan", "Feb", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barChart = findViewById(R.id.barchart);
setChart(3);
RadioGroup radioGroup = findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int switchId) {
switch (switchId) {
case R.id.one_month:
setChart(1);
break;
case R.id.three_month:
setChart(3);
break;
case R.id.six_month:
setChart(6);
break;
case R.id.tweleve_month:
setChart(12);
break;
}
}
});
}
private void setChart(int size) {
List<BarEntry> incomeEntries = getIncomeEntries(size);
List<BarEntry> expenseEntries = getExpenseEntries(size);
dataSets = new ArrayList<>();
BarDataSet set1, set2;
set1 = new BarDataSet(incomeEntries, "Income");
set1.setColor(Color.rgb(65, 168, 121));
set1.setValueTextColor(Color.rgb(55, 70, 73));
set1.setValueTextSize(10f);
set2 = new BarDataSet(expenseEntries, "Expense");
set2.setColors(Color.rgb(241, 107, 72));
set2.setValueTextColor(Color.rgb(55, 70, 73));
set2.setValueTextSize(10f);
dataSets.add(set1);
dataSets.add(set2);
BarData data = new BarData(dataSets);
barChart.setData(data);
barChart.getAxisLeft().setAxisMinimum(0);
barChart.getDescription().setEnabled(false);
barChart.getAxisRight().setAxisMinimum(0);
barChart.setDrawBarShadow(false);
barChart.setDrawValueAboveBar(true);
barChart.setMaxVisibleValueCount(10);
barChart.setPinchZoom(false);
barChart.setDrawGridBackground(false);
Legend l = barChart.getLegend();
l.setWordWrapEnabled(true);
l.setTextSize(14);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
l.setForm(Legend.LegendForm.CIRCLE);
XAxis xAxis = barChart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setDrawGridLines(false);
xAxis.setLabelRotationAngle(-45);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAxisMaximum(getExpenseEntries(size).size());
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisValues));
YAxis leftAxis = barChart.getAxisLeft();
leftAxis.removeAllLimitLines();
leftAxis.setTypeface(Typeface.DEFAULT);
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setTextColor(Color.BLACK);
leftAxis.setDrawGridLines(false);
barChart.getAxisRight().setEnabled(false);
setBarWidth(data, size);
barChart.invalidate();
}
private void setBarWidth(BarData barData, int size) {
if (dataSets.size() > 1) {
float barSpace = 0.02f;
float groupSpace = 0.3f;
defaultBarWidth = (1 - groupSpace) / dataSets.size() - barSpace;
if (defaultBarWidth >= 0) {
barData.setBarWidth(defaultBarWidth);
} else {
Toast.makeText(getApplicationContext(), "Default Barwdith " + defaultBarWidth, Toast.LENGTH_SHORT).show();
}
int groupCount = getExpenseEntries(size).size();
if (groupCount != -1) {
barChart.getXAxis().setAxisMinimum(0);
barChart.getXAxis().setAxisMaximum(0 + barChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
barChart.getXAxis().setCenterAxisLabels(true);
} else {
Toast.makeText(getApplicationContext(), "no of bar groups is " + groupCount, Toast.LENGTH_SHORT).show();
}
barChart.groupBars(0, groupSpace, barSpace); // perform the "explicit" grouping
barChart.invalidate();
}
}
private List<BarEntry> getExpenseEntries(int size) {
ArrayList<BarEntry> expenseEntries = new ArrayList<>();
expenseEntries.add(new BarEntry(1,1710));
expenseEntries.add(new BarEntry(2,2480));
expenseEntries.add(new BarEntry(3,242));
expenseEntries.add(new BarEntry(4,2409));
expenseEntries.add(new BarEntry(5,8100));
expenseEntries.add(new BarEntry(6,1200));
expenseEntries.add(new BarEntry(7,6570));
expenseEntries.add(new BarEntry(8,5455));
expenseEntries.add(new BarEntry(9,15000));
expenseEntries.add(new BarEntry(10,11340));
expenseEntries.add(new BarEntry(11,9100));
expenseEntries.add(new BarEntry(12,6300));
return expenseEntries.subList(0, size);
}
private List<BarEntry> getIncomeEntries(int size) {
ArrayList<BarEntry> incomeEntries = new ArrayList<>();
incomeEntries.add(new BarEntry(1, 11300));
incomeEntries.add(new BarEntry(2, 1390));
incomeEntries.add(new BarEntry(3, 1190));
incomeEntries.add(new BarEntry(4, 7200));
incomeEntries.add(new BarEntry(5, 4790));
incomeEntries.add(new BarEntry(6, 4500));
incomeEntries.add(new BarEntry(7, 8000));
incomeEntries.add(new BarEntry(8, 7034));
incomeEntries.add(new BarEntry(9, 4307));
incomeEntries.add(new BarEntry(10, 8762));
incomeEntries.add(new BarEntry(11, 4355));
incomeEntries.add(new BarEntry(12, 6000));
return incomeEntries.subList(0, size);
}
Note that the setBarWidth()
function is the most important and plays the role of adjusting the width and size of graph bar.
这篇关于MPAndroid 图表未显示 xAxis 的任何标签,缺少什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!