SolvedMPAndroidChart setting labels on x axis in line chart

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.

18 Answers

✔️Accepted Answer

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

Other Answers:

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

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 "";
 }
});

More Issues: