Barchart with mpandroidchart: bar chart not visible

I'm using MPa Android chart to generate a simple bar chart in the collapsed toolbar. When I add an item, the value of the item is visible at the correct height on the chart, but I can't see the bar

This is how I create variables:

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

This is how I populate the data and refresh the chart:

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();

I have no problem creating bar charts in other parts of the application. It works when I draw a line chart in the collapsed toolbar

resolvent:

When I used time stamps for X-axis values, I encountered the same problem, which was reported as an error in the library, so I proposed the following solution

First create an ArrayList:

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

Then, when adding an entry to the entry list, set the x value to the index (string value) of the data source, and then add the string value to the string array, as follows:

jsonObject = results.getJSONObject(index); String s = jsonobject. GetString (x); xAxis_ stringArray.add(s); entry.add(new Entry(Float.valueOf(index),Float.valueOf(jsonObject.getString(y))));;

Finally, reference xaxis to the value formatter class:

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

Timeaxisvalueformatter class:

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));
}}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>