apat
apat

Reputation: 186

BarChart with MPAndroidChart : bars are invisible

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

Graph

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.

Upvotes: 5

Views: 2809

Answers (4)

Hadas
Hadas

Reputation: 986

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!

  • If converting to minutes doesn't help try to a different (higher) time unit. The time unit that will be good for you is the average gap between the bars, for me it was hours.
    • Another interesting point that may help you style the chart - In the method setBarWidth(float mBarWidth), the input mBarWidth represent values not pixels.

Upvotes: 6

Simon
Simon

Reputation: 19948

I had the same problem just now, for me increasing the barwidth solved the problem. I think the problem lays with how spread out your data is which impacts the width of the lines. If you have a dataset that is only spread out over an hour and has entry for every minute of that hour, I think this problem won't exist. But if you are like me and have sparse data over 5 hours or so, then the barwidth just become smaller and smaller to accommodate all those entries and the bar just disappears.

Example:

        data.setBarWidth(2f);

enter image description here

When I increased the barwidth to 80f:

        data.setBarWidth(80f);

enter image description here

Upvotes: 1

md-soft
md-soft

Reputation: 41

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

Upvotes: 4

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

You are missing to set .setColor

Try with

barDataSet.setColor(Color.rgb(0, 155, 0)); //Put your RGB Color

Upvotes: 1

Related Questions