2

I am using MPAndroidChart BubbleChart v3.0.2. In this version I am unable to set my custom labels on xAxis. how can I set xAxis label for each bar. i.e (label1, label2 etc)

if any one worked in it or know how to do kindly share.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
Mubashar
  • 333
  • 6
  • 15

1 Answers1

1

You can do this by extending IValueFormatter. If you wanted to create a simple mapping of Entry to String labels you could do it like this:

import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.Map;

public class MapValueFormatter implements IValueFormatter {

    private final Map<Entry, String> labels;

    public MapValueFormatter(Map<Entry, String> labels) {
        this.labels = labels;
    }

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        String label = labels.get(entry);
        if (label == null) {
            return "";
        }

        return label;
    }
}

Then you can create labels like this:

Map<Entry,String> labels = new HashMap<>();
for (BubbleEntry bubbleEntry : dataSet.getValues()) {
    labels.put(bubbleEntry, generateRandomString());
}
dataSet.setValueFormatter(new MapValueFormatter(labels));

The result looks like this:

a bubble chart with custom labels

I am sure you can do better though. Good luck!

David Rawson
  • 20,912
  • 7
  • 88
  • 124
  • one more question. how can I show text in bubbles (text+y-axis value)? – Mubashar May 10 '17 at 10:13
  • @Mubashar can you please accept the answer if it is helpful? (green tick next to answer) – David Rawson May 10 '17 at 10:14
  • accepted because it worked and I didn't find this solution anywhere before. – Mubashar May 10 '17 at 10:16
  • @Mubashar thanks for the accept. To show (text + y-axis value) just change the code to: `return label + value;` – David Rawson May 10 '17 at 10:38
  • can you send any helpful link? I have problems. when I hardcode entries it work fine. but when I get from an arrayList and add in bar entry. it doesn't appear properly and not aligned to x-axis labels. – Mubashar May 11 '17 at 12:34
  • @Mubashar Please look at the code for the sample project. It's in the GitHub repo for MPAndroidChart. If you still have doubts and are getting stuck, then create a question containing a [mcve] that we can easily copy paste into an IDE and debug. – David Rawson May 11 '17 at 22:11