How to set Viewport Range via values at MpAndroidChart? - android

I have dataset values at range [0-10000]. And I want to see range [500-600] at chart with automatically calculated scroll and zoom values. How it can be done in MPAndroidChart?
Is there any method to setXRange(leftXValue, rightXValue)?

First set the max visible range.
So if you want to display the range of 500-600, set a max visible range of 100
mChart.setVisibleXRangeMaximum(100);
than you have to move your view so that the values are visible from the 500th position
mChart.moveViewToX(499);

Related

Android MP Chart library get X-axis height

Let's say I have an Activity containing a ChartView (blue rectangle)
Now, I want to place another View on top of it that will stretch from the X-axis all the way to the top of ChartView.
I tried to do that by setting the margin-bottom equal to the height of the X-axis (red area), but I don't know how to get its height
How can I achieve this effect? Is there a way to get X-axis height in Android MP Chart? Or maybe there is a walkaround that will have a similar result?
Desirable outcome (the gray area is a view placed on top of the chart):
chart.getXAxis().mLabelHeight returns the height of x-Axis by pixel.
You can get it after OnGlobalLayoutListener, because it is calculated after x-Axis is drawn.
----------Updated Jun 22nd
another way is to use "getHeight() - mViewPortHandler.contentBottom()", but mViewPortHandler is protected, so an extended chart is necessary to provide access to mViewPortHandler

How to set recyclerView item offset from percentage of visible item height in Android?

We are getting the index of the item and visible height percentage to show for the item as below:
As shown in the image, from the server, we are getting index 1 with visiblePercentage 20 which means, show 2nd item (index 1) 20%. I know the method linearLayoutManager.scrollToPositionWithOffset but it requires offset. How can we convert percentage into offset?
Also, the visiblePercent is coming as double like 20.456 whereas the offset is an int value, which means, there can be some difference.
So, if there is a way to use the double to avoid loss of decimal part, it will be more accurate.

How to solve below chart problems with `MPChartAndroid`?

I have 3 issue related to MPChartAndroid.
How to make chart to see selected values.
1. Add space to the left of LeftAxis and to the right of rightAxis to fully see the values.
2. how to make first and last chart value to fully see the values.
3. Add space under xAxis to see the cut text.
To overcome issue with cutoff x-Axis label, you need to set some extra space on bottom of your chart view.
mLineChart.setExtraOffsets(left,top,right,bottom); try code below
mLineChart.setExtraOffsets(0,0,0,10);
For overcome issue with overlapping value on axis line you need to set some spacing (start position of lines) on x-Axis, try code below
xAxis.setSpaceMax(0.01f);
xAxis.setSpaceMin(0.01f);
also try xAxis.setAvoidFirstLastClipping(true);

Customizing the Grid or x-axis in Android Graph View

I am trying to display 13 labels on the x-axis such as 0,1,2,3,4...12. However, I only get labels in intervals of two 0,2,4,6,8...12.
The labels show up as 0,1,2,3... if I set horizontal labels to 14 but then the labels continue after 13th label and shows half of 14th label even though the maxX of viewport is set to 13.
Is there any method that I can call that will display the vertical grid lines and horizontal labels at intervals of 1?
My code is as follows (The series only has 12 data points with x=1,2,3...12):
BarGraphSeries<DataPoint> series = new BarGraphSeries<>(values);
graph.addSeries(series);
// Graph properties
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(13);
graph.setTitle("Yearly Word Count");
graph.getGridLabelRenderer().setHorizontalAxisTitle("Month");
graph.getGridLabelRenderer().setVerticalAxisTitle("Word Count");
graph.getGridLabelRenderer().setNumHorizontalLabels(13);
series.setSpacing(20);
graph.getViewport().setScrollable(true);
The first picture is with NumHorizontalLabels set to 13. The next is set to 14. I want something like the first picture with the grids showing intervals of 1 on the x-axis.
This is your Problem
graph.getViewport().setMaxX(13);
you are going to display 0...13
Do this instead:
graph.getViewport().setMaxX(12);

MPAndroidChart how to make y-axis to start at non-zero value?

I am using MPAndroidChart library and have a set of data entries where the Y values are between 20 - 30 and I'm currently displaying it in a Line chart. However, I notice that there is a big gap because the Y-axis starts at 0 instead of 20.
How can I make Y-axis to start at the 20 instead of 0? Or even better the minimum of all y-values?
Thank you for your time!
You can find this in the documentation.
yAxis.setAxisMinValue(...)
allows you to adjust the lower limit of the axis to your needs.
The last version includes a left and right axis. You can set to zero like this (Kotlin):
chart.axisLeft.axisMinimum = 0f
chart.axisRight.axisMinimum = 0f
You can set zero in Bar Graph like this in Java
barChart.getAxisLeft().setAxisMinValue(0f);
barChart.getAxisRight().setAxisMinValue(0f);

Categories

Resources