Hot questions for Chart Y-Axis in MPAndroidChart

MPAndroidChart: constant position of Y-axis lines independent of data

Question: I'm using MPAndroid chart to display a line chart.

The chart is updated with different data and of course, when the data changes, the y-axis lines and the zoom level is changed because of the different sets of input data.

Is there any way to maintain the position of the y-axis lines and only change their labels according to the new data?

Answer: You used the setLabelCount function before but never the one with the force parameter. The solution is:

mChart.getAxisLeft().setLabelCount(visibleYCount, true);

Forcing the label count will assure the number of y-axis lines and thus, their position is stable with different data sets.


MPAndroidChart: Customise graduation steps on Y axis

Question: I have create a simple MPAndroidChart Line Chart with values from 0 to 5.

yAxis.setAxisMinimum(0.0f);
yAxis.setAxisMaximum(5.0f);

I have added my values (0 for every point in my example)

And I also have set the formatter just to display the value:

yAxis.setValueFormatter(new IAxisValueFormatter() {
                    @Override
                    public String getFormattedValue(float value, AxisBase axis) {
                        return "" + value;
                    }
                });

WHAT I EXPECTED: To get an Y axis with values from 0 to 5 every unit (0-1-2-3-4-5)

WHAT I GOT: Values spaced every 0.8

WHAT I TRIED: I tried to scaled to chart till 4.8, which is a good start as it doesn't make the line "overshoot" the last line But I still have 6 graduations instead of 5

How is it possible to force only 5 graduations or decide the steps in these graduations...

Answer: You have to use setGranularity:

yAxis.setAxisMinimum(0.0f);
yAxis.setAxisMaximum(5.0f);
yAxis.setGranularityEnabled(true);
yAxis.setGranularity(1.0f);

Display y axis value with B(billion) or M(million) in MPAndroidChart

Question: I have some values on Y-axis(look at picture)! I want to them be like 100M, 200M,...

I'm using MPAndroidchart. Is there any solution?

Answer: You can achieve this with an AxisValueFormatter. You can write your own pretty easily if you need to, but MPAndroidChart already comes with a LargeValueFormatter that does what you are describing.

From the AxisValueFormatter help page section on predefined formatters:

LargeValueFormatter: Can be used for formatting large values > "1,000". It will turn values like "1,000" into "1k", "1,000,000" will be "1m" (million), "1,000,000,000" will be "1b" (billion) and values like one trillion will be e.g. "1t".

Example use:

YAxis left = chart.getAxisLeft();
left.setValueFormatter(new LargeValueFormatter());

How to set custom value axisLeft on mpandroidchart

Question: I created a bar chart by MPAndroidChart:v3.0.3. as you can see on my picture

On AxisLeft, there are a big numbers. this is my codes:

YAxis yAxis = mBinding.barChartIncome.getAxisLeft();
yAxis.setDrawGridLines(true);
yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
yAxis.setSpaceTop(15f);
yAxis.setTextSize(12f);
yAxis.setAxisMinimum(1f);
yAxis.setLabelCount(10, false);

Now, I want to dived these big numbers into 1,000,000 and then shown on chart?How could i do that?

Answer: You need IAxisValueFormatter for this. Create class that implements it like this:

public class MyYAxisValueFormatter implements IAxisValueFormatter {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return String.valueOf(value/1000000);  // Format value, and get any string here
        }
}

And set it to your chart's Left axis :

chart.getAxisLeft().setValueFormatter(new MyYAxisValueFormatter());

How to remove value from the Y axis in MP Android BarChart

Question: I have created a bar graph with MPAndroid, but i want the chart minimal as possible so i removed most the description, grid and Ledgers. But i can't figure out how to remove these values from the chart.

Answer: Try with this code:

chart.getAxisLeft().setDrawLabels(false);  
chart.getAxisRight().setDrawLabels(false);

How to automatically scale graph to fit it on Y axis?

Question: I have almost finished Line graph, and I want to fit screen it on Y axis. MIN\MAX Peak values should touch top and bottom corner of the graph. How to do this?

Answer: Find minimum and maximum in your DataSet and use

chart.getAxisLeft().setAxisMinimum(0f);
chart.getAxisLeft().setAxisMaximum(100f);

to set the boundaries accordingly.


Show Y-Value of clicked slice/bar in MPAndroidChart piechart/barchart

Question: I'm using the MPAndroidChart library to display some graphs in my app (pie chart and bar chart). I don't want any textual description for the bars and slices, unless the user clicks on a slice/bar. Then only the description for this exact slice/bar should be displayed.

Right now I got some kind of workaround, which displays/undisplays all Y-values as soon as the user clicks on a slice/bar:

sessionsPieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {

                sessionsPieChart.setUsePercentValues(true);
                pieDataSet.setValueTextSize(12);
            }

            @Override
            public void onNothingSelected() {

                sessionsPieChart.setUsePercentValues(false);
                pieDataSet.setValueTextSize(0);
            }
        });

How can I only display the Y-Value of the clicked slice/bar?

Answer: First of all create MyMarkerView.java class in your project as:

public class MyMarkerView extends MarkerView {

private TextView tvContent;

public MyMarkerView(Context context, int layoutResource) {
    super(context, layoutResource);
    // this markerview only displays a textview
    tvContent = (TextView) findViewById(R.id.tvContent);
}

// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)

@Override
public void refreshContent(Entry e, Highlight highlight)
{
    // here you can change whatever you want to show in following line as x/y or both
    tvContent.setText("x: " + e.getX() + " , y: " + e.getY()); // set the entry-value as the display text
}

}

After that create following layout file custom_marker.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@color/colorPrimary"
>

<TextView
    android:id="@+id/tvContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:text=""
    android:textSize="12dp"
    android:textColor="@android:color/black"
    android:ellipsize="end"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceSmall" />

 </RelativeLayout>

After that where you are creating chart add following code:

   MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker);
   combinedChart.setMarker(mv);