Search code examples
androidlogarithmgraphing

LogLinear and LogLog plots in android


I want my application to be able to display frequency response graphs but I cannot find any graphing library that has this functionality. I am currently using MPAndroidChart for some other charts (and it is great !) but sadly I could not find any way to use it to do log plots. I have also tried using numAndroidCharts (numcharts logplot example), but that library seems broken/outdated since i couldn't even get the example code to work properly. Is there anyway that you know of to achieve this ?


Solution

  • I use MPAndroidChart with a log scale with converted values.

    For example if your value is 1E-5:

    value = 1E-5;
    Entry entry = new Entry();
    entry.setX(Math.log10(value)); // entry.getX() will return -5
    

    Because your value now is -5 you need to create an AxisFormatter to show that it really represents an logarithmic value:

    public class Log10AxisValueFormatter implements IAxisValueFormatter {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return String.format(Locale.ENGLISH, "%.2E", Math.pow(10,value));
        }
    }
    

    You need to set an instance of this to your axis when you create your chart:

    XAxis xAxis = chart.getXAxis();
    xAxis.setValueFormatter(new Log10AxisValueFormatter());