-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Realm.io database integration
Since v2.2.0 MPAndroidChart supports plotting data coming directly from the Realm.io mobile database.
This short tutorial will show how to directly plot data stored in realm.io mobile database with MPAndroidChart. For general documentation regarding realm.io, how to use the database, how to read or write objects, please visit their official documentation.
In order to plot data with Realm, you need to add the following dependencies to your project:
The actual tutorial scenario looks as follows:
We have a data class Score
, extending RealmObject
(required) which stores the totalscore of an imaginary game as well as a scoreNr that will represent a simple incrementing counter and a playername.
public class Score extends RealmObject {
private float totalScore;
private int scoreNr;
private String playerName;
public Score() { } // no arguments constructor required for realm
public Score(float totalScore, int scoreNr, String playerName) {
this.scoreNr = scoreNr;
this.playerName = playerName;
this.totalScore = totalScore;
}
// all getters and setters (required for realm) ...
}
Make sure your data class provides a no arguments constructor (in addition to any other constructors you might have) as well as all required getter and setter methods. This is required for Realm to work.
In order to be able to access the data stored in realm database, we need a realm database instance:
// get realm instance
Realm realm = Realm.getDefaultInstance();
For detailed information on how to set up your realm instance (with RealmConfiguration
), please have a look here. In this tutorial, we assume our realm database-data looks like this:
class | playerName | totalScore (y-value) | scoreNr (x-index) |
---|---|---|---|
Score | Peter | 100 | 0 |
Score | Lisa | 110 | 1 |
Score | Dennis | 130 | 2 |
Score | Luke | 70 | 3 |
Score | Sarah | 80 | 4 |
The code to store the previously mentioned Score
objects in the local realm-database can for example look like this:
realm.beginTransaction();
Score score1 = new Score(100f, 0, "Peter");
realm.copyToRealm(score1);
Score score2 = new Score(110f, 1, "Lisa");
realm.copyToRealm(score2);
Score score3 = new Score(130f, 2, "Dennis");
realm.copyToRealm(score3);
Score score4 = new Score(70f, 3, "Luke");
realm.copyToRealm(score4);
Score score5 = new Score(80f, 4, "Sarah");
realm.copyToRealm(score5);
realm.commitTransaction();
Now, what we want to do is plot all totalscore and playername values shown above in a BarChart
directly from realm. The scoreNr will be used as an xIndex. In order to do all that, we need our realm-object
as well as a RealmResults
List
of our data object Score
.
// get the data from realm (of course more complex queries are possible here)
RealmResults<Score> results = realm.allObjects(Score.class);
After the query completed, we need to setup our RealmBarDataSet
. The constructor (one of the constructors) looks as follows:
public RealmBarDataSet(RealmResults<T> results, String yValuesField, String xIndexField) { ...
As the parameter results
we will provide our results
list we just queried from realm. The String
yValuesField
will be the name of the member variable of the Score
class that should represent the y-value plotted in the chart. In our case, we want to plot the totalscore, so we will provide "totalScore"
as the parameter. For the xIndexField
parameter we want to provide the name of the member variable that should be used as an xIndex. In this case, we will provide "scoreNr"
as a parameter. Please be aware that these parameters are case sensitive. The final DataSet
should look like this:
RealmBarDataSet<Score> dataSet = new RealmBarDataSet<Score>(results, "totalScore", "scoreNr");
// apply additional styling...
After creating the DataSet
, we need to add it to a RealmBarData
object. The class RealmBarData
also offers a specific constructor for realm related data that looks like this:
public RealmBarData(RealmResults<T> results, String xValuesField, List<IBarDataSet> dataSets) { ...
Again, as the parameter results
we will provide our results
list we just queried from realm. The parameter xValuesField
should be the name of the member variable of the Score
class that should be used as the x-value (value plotted alongside the x-axis). In our case, we want the name of the player plotted below each score, so we choose "playerName"
as the parameter. The result should look something like this:
ArrayList<IBarDataSet> dataSetList = new ArrayList<IBarDataSet>();
dataSetList.add(dataSet); // add the dataset
// create a data object with the dataset list
RealmBarData data = new RealmBarData(results, "playerName", dataSetList);
// additional data styling...
Last but not least, we add the RealmBarData
object to our BarChart
and refresh it:
// set data
barChart.setData(data);
barChart.invalidate(); // refresh
The plotted result should look somewhat like this:
Of course chart appearance & data styling depends on your individual settings. All realm-created charts can be styled just the same way as charts fed with data from other data sources.
Potting data with all other chart types works quite similar. An exception are stacked-bars, which require a special object (RealmList
) to represent the float[]
array in which the individual stack-values are stored.
Content yet to come.
- Code related to this wiki entry: RealmUncycloExample
- All realm related example code
- Realm.io website
- Realm.io Java documentation
- Realm.io Swift documentation