MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Data set values on Horizontal Bar chart disappear on zooming along y-axis

Open charan1998 opened this issue 7 years ago • 13 comments

The values next to the bar in horizontal bar graph disappear on scaling along y-axis. It reappears when scrolled to far left

charan1998 avatar Jul 03 '18 01:07 charan1998

I am having the same issue...

Ezerous avatar Nov 14 '18 09:11 Ezerous

Could one of you please explain this issue further, perhaps with a video if you can?

almic avatar Nov 15 '18 17:11 almic

Starting from this initially and zooming just a bit till zero/start goes off the chart makes the values on top of the bars disappear (as seen here). While zoomed in, any time the start appears in the chart again, the values also reappear.

Deleting the useless parts, the minimal dummy code below can still reproduce the issue:

HorizontalBarChart horizontalBarChart;                
List<BarEntry> barEntries = new ArrayList<>();
barEntries.add(new BarEntry(1, 1));
barEntries.add(new BarEntry(2, 2));
barEntries.add(new BarEntry(3, 3));
BarDataSet dataSet = new BarDataSet(barEntries, "Label");
YAxis yAxisLeft = horizontalBarChart.getAxisLeft();
yAxisLeft.setAxisMinimum(0);
BarData barData = new BarData(dataSet);
horizontalBarChart.setData(barData);
int chartHeightDp = 10 + 30 * entries.length;
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
horizontalBarChart.setMinimumHeight((int) (chartHeightDp * (metrics.densityDpi / 160f)));
horizontalBarChart.invalidate();

Upon investigating it quickly, it seems like this block of code is triggered:

if (!mViewPortHandler.isInBoundsX(buffer.buffer[j]))
  continue;

and the drawing of the values is skipped.

Ezerous avatar Nov 15 '18 18:11 Ezerous

Yeah that definitely looks like the cause. It should check the Y-value on horizontal bar charts instead of the X-value. I'll fix this soon.

almic avatar Nov 19 '18 23:11 almic

I'm using version v3.1.0 and seeing a very similar issue. But it is happening in the vertical version of the Bar chart. A few columns disappear in a given zoom level and drag position. Very weird.

Maybe it's related?

bmacedo avatar Jan 02 '20 10:01 bmacedo

I have this problem too. I did a video. Android Emulator - Pixel_2_API_26_5554 2020-03-31 16-34-32.zip

parisisal avatar Mar 31 '20 14:03 parisisal

I found the problem seen in the video posted some minutes ago.

The BarEntry values must be added ordered (ascendent) by "X" value. This code will generate the issue:

ArrayList<BarEntry> binClassEntries = new ArrayList<BarEntry>(); for(int i = 0; i <20; i++) binClassEntries.add(new BarEntry((float)20-i,(float)new Random().nextInt(2000))); This code works:

ArrayList<BarEntry> binClassEntries = new ArrayList<BarEntry>(); for(int i = 0; i <20; i++) binClassEntries.add(new BarEntry((float)i,(float)new Random().nextInt(2000)));

parisisal avatar Mar 31 '20 15:03 parisisal

Any update or resolution?

LukeDaniel16 avatar Dec 16 '20 18:12 LukeDaniel16

I'm using version v3.1.0 and seeing a very similar issue. But it is happening in the vertical version of the Bar chart. A few columns disappear in a given zoom level and drag position. Very weird.

Maybe it's related?

You can resolve it?

LukeDaniel16 avatar Dec 16 '20 19:12 LukeDaniel16

I got this problem, too...but it only happens when my inserted entries aren't in ascending order.

cocok30 avatar Feb 07 '21 04:02 cocok30

I have no idea why it works, but yeah. Sorting my values fixed the disappearing bars issue

TangoBloom avatar Aug 12 '22 22:08 TangoBloom

I still didn't find any solution for the original issue. The best I could possibly do is disable scaling on the X axis (.setScaleXEnabled(false)).

Razvann232 avatar Mar 29 '23 11:03 Razvann232

Based on the comments, I managed to formulate my solution to the rendering issue of bars when zooming. I created a class called 'DataXY' which has 2 attributes, 'valueX' and 'valueY'. Then, I iterate through the list of data that I want to display in the chart and add them to a 'mutableListOf<DataXY>()'. This option solves the case where there are different X values with different Y values, and the Y values need to be accumulated for the same X value. By creating the necessary logic, the data can be accumulated in a list of type 'DataXY'. If this list is sorted (ascending), the visual bug is resolved. After sorting this list, it is iterated through, and the data from the 'DataXY' objects are added to a new list of 'BarEntry', which is displayed in the chart.

DaumasDev avatar May 27 '23 03:05 DaumasDev