其他分享
首页 > 其他分享> > 带MPAndroidChart的BarChart:条形图不可见

带MPAndroidChart的BarChart:条形图不可见

作者:互联网

我正在使用MPAndroidChart在折叠的工具栏中生成一个简单的条形图.当我添加条目时,条目的值在图表上正确的高度处可见,但看不到该条.

这就是我创建变量的方式:

final List<BarEntry> barEntries = new ArrayList<>();
final BarDataSet barDataSet = new BarDataSet(barEntries, getResources().getString(R.string.cycles_profiled));
final BarData barData = new BarData(barDataSet);

这就是我填充数据和刷新图表的方式:

final Calendar cal = getFirstDayOfWeek();
for (int i = 0; i < NUMBER_OF_DAYS; i++) {
    long date = cal.getTimeInMillis();
    barEntries.add(new BarEntry(date, map.get(date)));
    cal.add(Calendar.DAY_OF_WEEK, 1);
}
barDataSet.setValues(barEntries);
barChart.setData(barData);
barData.notifyDataChanged();
barChart.notifyDataSetChanged();
barChart.invalidate();

Graph

我在应用程序的其他部分创建条形图没有问题.当我在折叠的工具栏内绘制折线图时,它也可以正常工作.

解决方法:

当我将时间戳用于X轴值时遇到了同样的问题,这被报告为库中的错误,因此我提出了以下解决方案

首先创建一个arraylist:

ArrayList<String> xAxis_stringArray = new ArrayList<>();

然后将条目添加到条目列表中时,将x值设置为数据源的索引(字符串值),然后将字符串值添加到字符串数组中,如下所示:

jsonObject = results.getJSONObject(index);
 字符串s = jsonObject.getString(x);
 xAxis_stringArray.add(s);
 entry.add(new Entry(Float.valueOf(index),Float.valueOf(jsonObject.getString(y))));;

最后将xAxis引用到值格式化程序类:

XAxis xAxis = sum_chart.getXAxis();
xAxis.setValueFormatter(new TimeAxisValueFormatter(xAxis_stringArray));

TimeAxisValueFormatter类:

public class TimeAxisValueFormatter implements IAxisValueFormatter {

ArrayList<String> arrayList = new ArrayList<>();

public TimeAxisValueFormatter(ArrayList<String> list) {
    this.arrayList = list;
}

@Override
public String getFormattedValue(float value, AxisBase axis) {
    return arrayList.get(Math.round(value));
}}

标签:mpandroidchart,android
来源: https://codeday.me/bug/20191118/2024560.html