Comparing Different Graph View Libraries and Integrating Them in PSLab Android Application
There is a significant role of graphs in PSLab, they’re used for the following purpose:
- To plot voltage signals from analog inputs in Oscilloscope Activity.
- To plot digital signals in Logical Analyzer Activity.
- To plot data points from various sensors.
For this, we need to implement real time graphs that stimulate real time data from the PSLab device efficiently. It is necessary to analyze each and every Graph View Library, compare them and integrate the best one in PSLab Android app.
Available Graph Libraries
The available Graph View libraries of Android are:
Which one is the best with respect to the PSLab project?
MPAndroidChart
Line Graph plotted using MPAndroidChart (image source)
It is an open source graph view library by Philipp Jahoda. The following are the features of MPAndroidChart
- There are 8 different chart types
- Scaling on both axes. Scaling can be done using pinch zoom gesture.
- Dual Axes, we can have 2 Y-axis.
- Real time support
- Customizable axis ie we can define different labels to the axis
- Save chart to SD-Card
- Predefined color templates
- Legends which are used to define which line depicts what.
- Animations
- Fully customizable, from background color to color of the lines and grids.
On trying MPAndroidChart, I found it to be a slightly difficult to implement.
Graph-View
Line Graph plotted using GraphView Library (image source)
It is also an open source graph view library by Jonas Gehring. The following are features of the Graph-View
- Supports Line Chart, Bar Chart and Points.
- Scrolling vertical and horizontal
- Scaling on both axes.
- Realtime Graph support
- Draw multiple series of data. Let the diagram show more that one series in a graph. You can set a color and a description for every series.
- Legends (as discussed in MPAndroidChart)
- Custom labels
- Manual Y axis limits can be set.
SciChart
It is rich APIs for Axis Ranging, Label Formatting, Chart Modifiers (interaction) and Renderable Series. It is packed with features but unfortunately, it is not open sourced.
The Verdict
Both MPAndroidChart and Graph-View are good libraries, packed with a lot of features. GraphView is easier to implement as compared to MPAndroidChat (not that difficult either). Both of them have the features like pinch zoom. MPAndroidChart had the feature of scale adjustment even when the graph is being plotted. The rate of plotting was comparable in both but it was slightly faster in MPAndroidChart. So, finally GraphView is easier to implement but MPAndroidChart has slightly better performance. So, we integrated MPAndroidChart in PSLab Android application.
Integrating MPAndroidChart in PSLab Android App
In order to integrate MPAndroidChart in the Android project add the following code in the build.gradle of your project.
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
Creating Oscilloscope like graph
If we observe an Oscilloscope, it has a black/blue screen with grid lines. An oscilloscope is a voltage vs time graph hence the x axis represents the time elapsed and y axis the voltage of the signal at the instant of time. There are left and right y axis for different channels.
In order to implement a graph similar to that of Oscilloscope in PSLab Android App using MPAndroidChart library, the graph needed to be customized.
The following step was taken to customized the graph in Oscilloscope Activity.
Background Color
mChart.setBackgroundColor(Color.BLACK);
This sets the background color of the graph as black. mChart is an object of the Line graph.
Legend
Legend l = mChart.getLegend(); l.setForm(Legend.LegendForm.LINE); l.setTextColor(Color.WHITE);
Here we are setting the Legend form. There are many options available for the same like SQUARE, CIRCLE, and LINE. We are using LINE Legend form. Also, we set the white color for the legend text.
X Axis Customization
x = mChart.getXAxis(); x.setTextColor(Color.WHITE);
First, we create an object of XAxis and set the textcolor as white.
x.setDrawGridLines(true);
The above method draws the grid lines along the x axis.
x.setAxisMinimum(0f); x.setAxisMaximum(875f);
Now we will set the range of x axis by setting minimum value as 0 and the maximum value is 875.
Y Axis Customization
y1 = mChart.getAxisLeft(); y1.setTextColor(Color.WHITE); y1.setAxisMaximum(16f); y1.setAxisMinimum(-16f); y1.setDrawGridLines(true);
This is similar to what we did in x axis formatting.
After performing the above steps we got the following results.
To follow the entire code for graph customization refer chartinit method in Oscilloscope Activity, PSLab Android repository.
Resources