Rahul Giradkar
Rahul Giradkar

Reputation: 1818

How to remove Legend of BarChart

How to remove Legend from BarChat. I have some code.

float[] yData = { (float)Math.abs(item._Lent),(float) Math.abs( item._Barrow )};
    String[] xData = { "salary", "Spends" };
    ArrayList<BarEntry> entries = new ArrayList<>();
    for (int i = 0; i < yData.length; i++)
        entries.add(new BarEntry(yData[i], i));
    BarDataSet dataset = new BarDataSet(entries, "");
    ArrayList<String> labels = new ArrayList<String>();
    for (int i = 0; i < xData.length; i++)
        labels.add(xData[i]);

    BarData data = new BarData(labels, dataset);
    mChart.setData(data); // set the data and list of lables into chart
    BarChart barchart = mChart;
    Legend legend = barchart.getLegend();
    legend.setEnabled(false);

    YAxis topAxis = barchart.getAxisLeft();
    topAxis.setDrawLabels(false);

    YAxis bottomAxis = barchart.getAxisRight();
    bottomAxis.setDrawLabels(false);

    XAxis rightAxis = barchart.getXAxis();
    rightAxis.setDrawLabels(false);
    bottomAxis.setDrawLabels(false);

`

This code not working properly. I gives me following out put.

output

Upvotes: 2

Views: 1079

Answers (2)

Bertrand Martel
Bertrand Martel

Reputation: 45382

Hide the description with setDescription("") :

barchart.setDescription("");

Upvotes: 3

ishmaelMakitla
ishmaelMakitla

Reputation: 3812

You need to set legend visible to false, like this: barchart.setLegendVisible(false); - so your code will look something like this:

//rest of your code
    ....
BarData data = new BarData(labels, dataset);
mChart.setData(data); // set the data and list of lables into chart
BarChart barchart = mChart;
barchart.setLegendVisible(false);
    ...
//more of your code...

Give it a try and let me know if it helps. You can also look at the selected answer to this closely related question here.

Upvotes: 2

Related Questions