Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 499
apat Avatar asked Dec 20 '16 09:12

apat


People also ask

What is MPAndroidChart?

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.

Is a Barplot a bar graph?

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.

What is bar chart in Spotfire?

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.


2 Answers

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.
like image 128
Hadas Avatar answered Sep 19 '22 08:09

Hadas


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));
}}
like image 25
md-soft Avatar answered Sep 23 '22 08:09

md-soft