PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.64k stars 9.02k forks source link

Custom labels over entries instead of their values #820

Closed goshki closed 9 years ago

goshki commented 9 years ago

Is it possible to attach custom text labels to each entry in a given dataset?

taltstidl commented 9 years ago

@goshki You can use a custom ValueFormatter to change the String displayed above the entry. Depending on what exactly you want to achieve you can either just format the value or provide another String. Unfortunately it doesn't provide the entry index though, so if you need that it'll get complex.

goshki commented 9 years ago

Good point. Unfortunately the label I need to apply is index-dependent.

taltstidl commented 9 years ago

@goshki Ok, then you could try this (you'll have to clone or fork the project):

  1. Clone or fork this project and include that instead of the Jitpack version
  2. Search for getFormattedValue (see Github search results): you can then change the occurrences in ValueFormatters to accept Entry instead of float value and the occurrences in Renderers to pass the Entry. (Only in MPChartLib folder)
  3. Then you can write your custom formatter that has access to the complete Entry

Normally I would consider doing it myself and opening a pull request, but I'm short in time. Passing the Entry definitely allows for more flexible formatting.

paamand commented 6 years ago

@goshki : I had the same issue. Solved it by defining a IValueFormatter:

myDataset.setValueFormatter(new IValueFormatter() {
    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        int idx = myData.getDataSetByIndex(dataSetIndex).getEntryIndex(entry);
        ...
    }
};