I am using MPAndroidChart to generate a simple bar chart, inside a collapsing toolbar. When I add entries, the value of the entry is visible at the correct height on the chart, but I can't see the bar.
This is how I create my 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 my app. When I draw linecharts inside the collapsing toolbar, it also works fine.
What is MPAndroidChart? MPAndroidChart is a powerful Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
A barplot (or barchart) is one of the most common types of graphic. It shows the relationship between a numeric and a categoric variable. Each entity of the categoric variable is represented as a bar. The size of the bar represents its numeric value.
A bar chart is a way of summarizing a set of categorical data (continuous data can be made categorical by auto-binning). The bar chart displays data using a number of bars, each representing a particular category.
Had the same issue, solved it thanks to OumaSyuu.
For some reason the BarChart
has a difficulty with milliseconds.. convert your milliseconds to minutes: TimeUnit.MILLISECONDS.toMinutes(millisecondValue)
and your life will be honey!
setBarWidth(float mBarWidth)
, the input mBarWidth
represent values not pixels.i was facing the same issue when i used timestamp for X axis values which was reported as a bug in the library, so i came out with the following solution
first create an arraylist:
ArrayList<String> xAxis_stringArray = new ArrayList<>();
and then while you add the entries to the entry list, set the x value to be the index of the data source (string value) and add the string value to the string array like that:
jsonObject = results.getJSONObject(index);
String s = jsonObject.getString(x);
xAxis_stringArray.add(s);
entries.add(new Entry(Float.valueOf(index), Float.valueOf(jsonObject.getString(y))));
and finally refer the 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));
}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With