A simple 2d plot class for Android

Download (The zip file contains plot2d.java in src\com\android\graphbutton)

Thomas Thron has modified my code to make it editable in an XML editor. He has also added some new functions to plot data more easily and to have it plotted logarithmically. His java file can be downloaded here.

My post about the files:


I have added an Android tab to the site. A dynamic 2-D plot class can also be downloaded from there.

Since I have this awesome piece of gadgetry in my phone I decided to learn a little bit of Android application development. I initially just wanted to access the various sensors in the phone but having figured that out, I decided that it would be cool to do something with all the sensor data that I now had access to. As a first step I wanted to make a dynamic plot for the sensor readings, bundle it all up and release it as a free app in the app-jungle that is the Android Market. Having very minimal java experience I tried see if I can find a simple 2d plotting utility. I found a small number of solutions but some were too involved and the ones which were simple were just too ugly to look at. I thought that even with my beginner's knowledge of Android programming, I would still be able to make something functional and not too bad a looker. So I made plot2d. It's a simple Android java class which uses the Canvas class to display a 2-D plot between two float arrays on your Android phone. It automatically scales the plot for different screens and decides reasonable locations for the axes. It can automatically add the axes markers too. You can use it in your own code/app if you want to show a quick plot. Here are some results which were taken using the screenshots of the emulator:


Improvements can obviously be done but this serves my purpose for now. The code which generates the above examples can be downloaded here.

21 observations on “A simple 2d plot class for Android
  1. Ashish

    Hay ankit, you are working well but you should update your code , This code is not good as i accessed dude... No Zoom in Zoom out functions. Hay dude do something...

  2. Troy C.

    Nice example... I've been looking for an example that reads in data. I'm actually reading from a CSV file, but this gives me a good idea of how to do it. Thank You!

  3. Troy C.

    Nice example... I've been looking for an example that reads in data. I'm actually reading from a CSV file, but this gives me some idea of how to do it. Thank You!

  4. TimT

    Thanks, just what I needed to get a better understanding of plotting in Android.
    Keep up the good work!

  5. Jonathan

    This is an excellent example to drop in, start from, and modify on my first app, thanks so much!

  6. Jhon A

    Excellent work, congratulations.
    I have a doubt:
    if i want to paint one or more points in a specific part of the graphic, ¿ What i do have to add in the code? and in what part?
    thanks a lot.

  7. Sharif

    Thank you soooo much man ,
    i have been looking for simple example from a long time . thank you .
    Awesome example.

  8. Sharif

    HI Ankit,
    now i am trying to draw graphs using Date on X Axis and data on Y Axis . Can you please help me regarding

  9. Gerv

    Nice Work. But why you insert two button with no action? And how is possible to change plot at onClickListner?


Leave Your Observation

Your email address will not be published. Required fields are marked *