1

I am making an android application where I need to draw a graph from data entered by the user at runtime and with this data I will draw a simple line graph, but the line will be a line of best fit. The user will mostly only enter data at most 3-4 times a day.

I know there are a few libraries out there like androidplot and MPandroidchart. I download the demos for both of these from the appstore and MPandroidchart looks more appealing.

However, none of these libraries have a chart in the demos where they show a graph with a line of best fit.

So I was wondering if either of this libraries allows you to draw a line of best fit or would you have to manually calculate the slope of the line from the data points and then use the library to draw the line on the chart?

Also can you guys recommend which library I could use to do what I need to do? I am new to android so I would prefer a library which is easy to use and has good tutorials/support or is popular so I could ask questions from people and get an answer in a timely manner.

simcity
  • 57
  • 1
  • 7

1 Answers1

3

You can draw a line of best fit with any of these libraries, but as you suspect you'll need to do the linear regression yourself. There may be libraries that focus on visualizing mathematical functions which include these calculations, but in general plotting libraries will only offer a way to implement either in the form of f(x) or as a raw series of points.

The good news is it's quite easy to do the math part yourself, and is pretty much what everyone else is doing:

  • Write a class or method to calculate the line of best fit given a series of points. This example seems like a reasonable starting point.
  • Pass your data through your implementation and get the slope.
  • Using the slope, generate the dataset you want to plot. If you're using Androidplot you can alternatively just implement the XYSeries interface and do f(x) using the above calculated slope in your getY(i) method.
Nick
  • 8,181
  • 4
  • 38
  • 63