5

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.

apat
  • 186
  • 2
  • 15
  • Have you tried `barChart.setHardwareAccelerationEnabled(false);`? – David Rawson Dec 23 '16 at 09:26
  • Thanks, I just tried it, but the result is the same – apat Dec 23 '16 at 09:43
  • What version of the MPAndroidChart is this? Is the result on all phones, or is it on just one phone? – David Rawson Dec 23 '16 at 09:45
  • Very hard to debug from here. Make sure you have the latest version of the library and try creating a [minimum, complete, verifiable example](http://stackoverflow.com/help/mcve) – David Rawson Dec 23 '16 at 09:49
  • I am using the latest version of the library, 3.0.1. The result is the same on all devices. Okay thanks for the advice I will try that! – apat Dec 23 '16 at 10:38
  • Also, how are you setting the background color to blue? – David Rawson Dec 23 '16 at 19:10
  • I am not setting it to blue, the background is part of the (collapsing) toolbar my chart is in. – apat Dec 26 '16 at 13:15
  • That was a really important detail you left out of your question! That's why it's important to create a MCVE – David Rawson Dec 26 '16 at 18:54
  • thank you, I edited the question. – apat Dec 27 '16 at 10:48
  • this is still not an MCVE. We don't mnow what class your collapsing toolbar is. So how can we help? If you want the issue to go further create the _minimum_ activity to reproduce the issue and post it here. Otherwise we just have to guess what is wrong – David Rawson Dec 27 '16 at 18:30
  • 1
    @apat: have you found the solution? I face the same issue... – androfan Jan 06 '17 at 15:20
  • 1
    @androfan No I haven't found the solution, sorry. Is your graph also in a collapsing toolbar? – apat Jan 09 '17 at 08:14

4 Answers4

6

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.
Hadas
  • 906
  • 12
  • 22
4

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));
}}
md-soft
  • 41
  • 2
1

You are missing to set .setColor

Try with

barDataSet.setColor(Color.rgb(0, 155, 0)); //Put your RGB Color
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • No actually I do it, I just havent posted the code, but I do : – apat Dec 20 '16 at 09:52
  • I do: barDataSet.setColor(Color.WHITE) – apat Dec 20 '16 at 09:54
  • This is not the same problem as mine, as my chart is being displayed. Moreover, the width and height of the chart are set in the XML. – apat Dec 20 '16 at 09:59
  • 1
    Color.WHITE is already an actual color. I have tried using one of my own colors and getResources().getColor(R.color.mycolor) but the result is the same. – apat Dec 20 '16 at 10:08
  • @apat any clue ?? – IntelliJ Amiya Dec 20 '16 at 11:17
  • No I have no idea. I draw other barcharts in my app and I don't have any problem. Just this one, in a collapsing toolbar, doesn't show the bars. – apat Dec 20 '16 at 12:57
1

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

Simon
  • 19,658
  • 27
  • 149
  • 217