Shubham Tunwal
Shubham Tunwal

Reputation: 237

How to set X axis labels in MP Android Chart (Bar Graph)?

This is with reference to the article : https://github.com/PhilJay/MPAndroidChart/wiki/Setting-Data under the heading Bar Graph.In the given code below:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_graph_test, container, false);

    BarChart chart = view.findViewById(R.id.bar_Chart_test);

    List<BarEntry> entries = new ArrayList<>();
    entries.add(new BarEntry(0f, 30f));
    entries.add(new BarEntry(1f, 80f));
    entries.add(new BarEntry(2f, 60f));
    entries.add(new BarEntry(3f, 50f));
    // gap of 2f
    entries.add(new BarEntry(5f, 70f));
    entries.add(new BarEntry(6f, 60f));

    BarDataSet set = new BarDataSet(entries, "BarDataSet");
    BarData data = new BarData(set);
    data.setBarWidth(0.9f); // set custom bar width
    chart.setData(data);
    chart.setFitBars(true); // make the x-axis fit exactly all bars
    chart.invalidate(); // refresh

    return view;
}

the output was:

Here the X values are not displayed.Click here for the ScreenShot

how to set the X-axis values as months(1st Jan,2nd Jan,3rd Jan.....) as displayed in the article.

Upvotes: 16

Views: 46844

Answers (7)

HASSNA AIT-ALI
HASSNA AIT-ALI

Reputation: 1

ArrayList<String> xMonth= new ArrayList<>();
            xMonth.add("January");
            xMonth.add("February");
            xMonth.add("March");
            xMonth.add("April");
            xMonth.add("May");
            xMonth.add("June");
XAxis xAxis = chartMth.getXAxis();
xAxis.setValueFormatter(new ValueFormatter() {
   
                @Override
                public String getAxisLabel(float value, AxisBase axis) {
                    return xMonth.get((int) value);
                }
            });

Upvotes: 0

Sarthak Gandhi
Sarthak Gandhi

Reputation: 2170

You just make a simple list of string like this :

final ArrayList<String> xAxisLabel = new ArrayList<>();
    xAxisLabel.add("Mon");
    xAxisLabel.add("Tue");
    xAxisLabel.add("Wed");
    xAxisLabel.add("Thu");
    xAxisLabel.add("Fri");
    xAxisLabel.add("Sat");
    xAxisLabel.add("Sun");

Then you do this :

    XAxis xAxis = chart.getXAxis();
    xAxis.setValueFormatter(new ValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return xAxisLabel.get((int) value);

        }
    });

Hope this helps.

Upvotes: 20

Amir Raza
Amir Raza

Reputation: 2860

Kotlin:

val xAxisLabels = listOf("1", "2", "3", "4", "5", "6" ...)      
barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(xAxisLabels)

Java:

ArrayList<String> xAxisLables = new ArrayList();
xAxisLables.add("1");
xAxisLables.add("2");
xAxisLables.add("3");
xAxisLables.add("4"); ...

OR

String[] xAxisLables = new String[]{"1","2", "3", "4" ...};

barChartView.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisLables));

You can prepare any data you want in xAxisLabels to display

Upvotes: 18

JCarlosR
JCarlosR

Reputation: 1664

In case anyone needs a Kotlin version example, I customized it a bit:

val labels = arrayListOf(
    "Ene", "Feb", "Mar",
    "Abr", "May", "Jun",
    "Jul", "Ago", "Set",
    "Oct", "Nov", "Dic"
)

barChart.xAxis.valueFormatter = IndexAxisValueFormatter(labels)
barChart.xAxis.position = XAxis.XAxisPosition.BOTTOM

barChart.setDrawGridBackground(false)
barChart.axisLeft.isEnabled = false
barChart.axisRight.isEnabled = false
barChart.description.isEnabled = false

val entries = arrayListOf(
    BarEntry(0f, 10f),
    BarEntry(1f, 20f),
    BarEntry(2f, 30f),
    BarEntry(3f, 40f),
    BarEntry(4f, 50f),
    BarEntry(5f, 60f),
    BarEntry(6f, 70f),
    BarEntry(7f, 60f),
    BarEntry(8f, 50f),
    BarEntry(9f, 40f),
    BarEntry(10f, 30f),
    BarEntry(11f, 20f)
)    
val set = BarDataSet(entries, "BarDataSet")
set.valueTextSize = 12f

barChart.data = BarData(set)    
barChart.invalidate()
  • By default the xAxis labels are shown at the top. This example show them at the bottom.
  • I also disabled the text description and the left and right axis (yes, by default we have 2 Y-axis at both sides).
  • I decided to disable the grid and to increase the text size as well (it's small by default).

Upvotes: 3

snehal agrawal
snehal agrawal

Reputation: 301

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

Upvotes: 4

Gayathri
Gayathri

Reputation: 249

In dynamically set X axis label,

XAxis xAxis = chart.getXAxis();

xAxis.setValueFormatter(new IndexAxisValueFormatter(getDate));

public ArrayList<String> getDate() {

        ArrayList<String> label = new ArrayList<>();
        for (int i = 0; i < yourList.size(); i++)
            label.add(yourList.get(i).getDateValue());
        return label;
    }

Upvotes: 3

Vinil Chandran
Vinil Chandran

Reputation: 1561

        HorizontalBarChart chart = findViewById(R.id.hbc_graph);   

        ArrayList<BarEntry> entries = new ArrayList<>();
        entries.add(new BarEntry(0, 3f));
        entries.add(new BarEntry(1, 8f));
        entries.add(new BarEntry(2, 6f));
        entries.add(new BarEntry(3, 11f));
        entries.add(new BarEntry(4, 5f));
        entries.add(new BarEntry(5, 14f));

        BarDataSet dataSet = new BarDataSet(entries,"Horizontal Bar");

        BarData data = new BarData(dataSet);
        chart.setData(data);
        chart.animateXY(2000, 2000);
        chart.invalidate();


        ArrayList<String> xLabels = new ArrayList<>();
        xLabels.add("January");
        xLabels.add("February");
        xLabels.add("March");
        xLabels.add("April");
        xLabels.add("May");
        xLabels.add("June");

        XAxis xAxis = chart.getXAxis();
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                Print.e(value);
                return xLabels.get((int) value);
            }

        });

Upvotes: 0

Related Questions