Mpandroidchart: setting labels on x axis in line chart

Created on 24 Aug 2016  ·  15Comments  ·  Source: PhilJay/MPAndroidChart

I don't know how to set labels in x axis in the new version of mpandroidchart. Have gone through the examples but didn't find any.

Most helpful comment

I kind of figured it out:

package com.lukechenshui.chart;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.AxisValueFormatter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LineChart chart = (LineChart) findViewById(R.id.chart);

        int[] numArr = {1,2,3,4,5,6};

        final HashMap<Integer, String>numMap = new HashMap<>();
        numMap.put(1, "first");
        numMap.put(2, "second");
        numMap.put(3, "third");
        numMap.put(4, "fourth");
        numMap.put(5, "fifth");
        numMap.put(6, "sixth");

        List<Entry> entries1 = new ArrayList<Entry>();

        for(int num : numArr){
            entries1.add(new Entry(num, num));
        }

        LineDataSet dataSet = new LineDataSet(entries1, "Numbers");


        LineData data = new LineData(dataSet);

        XAxis xAxis = chart.getXAxis();
        xAxis.setValueFormatter(new AxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {

                return numMap.get((int)value);
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        });
        chart.setData(data);
        chart.invalidate();

    }
}

screenshot 2016-10-03 11 00 58

All 15 comments

Same here bro

I kind of figured it out:

package com.lukechenshui.chart;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.AxisValueFormatter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LineChart chart = (LineChart) findViewById(R.id.chart);

        int[] numArr = {1,2,3,4,5,6};

        final HashMap<Integer, String>numMap = new HashMap<>();
        numMap.put(1, "first");
        numMap.put(2, "second");
        numMap.put(3, "third");
        numMap.put(4, "fourth");
        numMap.put(5, "fifth");
        numMap.put(6, "sixth");

        List<Entry> entries1 = new ArrayList<Entry>();

        for(int num : numArr){
            entries1.add(new Entry(num, num));
        }

        LineDataSet dataSet = new LineDataSet(entries1, "Numbers");


        LineData data = new LineData(dataSet);

        XAxis xAxis = chart.getXAxis();
        xAxis.setValueFormatter(new AxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {

                return numMap.get((int)value);
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        });
        chart.setData(data);
        chart.invalidate();

    }
}

screenshot 2016-10-03 11 00 58

Can not resolve symbol AxisValueFormatter.
Did the api change again?

It did. use IAxisValueFormatter
XAxis xAxis = chart.getXAxis(); xAxis.setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float value, AxisBase axis) { return null; } });

X axis value is not coming continuously. If I am using value 0 to 10 then it's coming as 0 ,2,4,6,8,10,
I am using IAxisValueFormatter to format x values.
In getFormattedValue(float value, AxisBase axis) methods values are coming as 0 ,2,4,6,8,10.
It should come as 0,1,2,3,4,5,6,7,8,9,10.
Can anyone help ?

Try to put this line
xAxis.setLabelCount(numArr.length);
representing the number of your labels.

how to set x axis value yearly basis

even I am having the same problem as @shaktipm . the float value is incrementing as 0,2,4,6,8,10 instead of 0,1,2,3,4 . I have five labels in my lables array. xAxis.setLabelCount(lables.length); does'nt show any effect the output is the same. can you please help

Yes I'm also facing this issue. When dataset increases, getFormattedValue value getting as 0,2,4,6, then 0,3,6,9... and 0,5,10,15.. Can anyone help me to get it as 0 to N without any intermediate interval?

I think you can use xAxis.setGranularity(1.0f) or xAxis.setLabelCount(N, true)

Hi, please someone explains, when I have below numbers as an array of Entries:

image

Why I receive completely different numbers as an argument for below method?
How can I have chart with XAxis lables in direct line with circles?

@Override public String getFormattedValue(float value, AxisBase axis) {}

For the com.github.PhilJay:MPAndroidChart:v3.0.3

I am using a label list:

final List list_x_axis_name = new ArrayList<>();
list_x_axis_name.add("label1");
list_x_axis_name.add("label2");
list_x_axis_name.add("label3");
list_x_axis_name.add("label4");
list_x_axis_name.add("label5");

and setting the label like this:

BarChart chartBar = (BarChart) findViewById(R.id.chartBar);
XAxis xAxis = chartBar.getXAxis();
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setLabelRotationAngle(-90);
xAxis.setValueFormatter(new IAxisValueFormatter() {
 @override
 public String getFormattedValue(float value, AxisBase axis) {
  if (value >= 0) {
   if (value <= list_x_axis_name.size() - 1) {
    return list_x_axis_name.get((int) value);
   }
   return "";
  }
  return "";
 }
});

Closing old issue. If anyone still has this question please open a new one.

Awesome!

xAxis.setLabelCount(lineData.entryCount, true);

2nd param must be true

Was this page helpful?
0 / 5 - 0 ratings