If you want to save a complex dataset to SharedPreferences, you can either save the attributes individually, or serialize the data (e.g. convert it to a JSON string) and save that.
There are a lot of possible sets of data in a CombineData
class, but here is an example of how to save just the LineData sets from it using the JSON approach.
For example, say you made a CombinedData
of just lines, using the following code
LineData ld = new LineData();
{
List<Entry> line_entries = new ArrayList<>();
for (int i = 0; i < 4; ++i) {
line_entries.add(new Entry(i, i + 1));
}
LineDataSet ldata = new LineDataSet(line_entries, "Line");
ld.addDataSet(ldata);
}
{
List<Entry> line_entries = new ArrayList<>();
for (int i = 0; i < 4; ++i) {
line_entries.add(new Entry(i+2, i + 4));
}
LineDataSet ldata = new LineDataSet(line_entries, "Base");
ld.addDataSet(ldata);
}
CombinedData data = new CombinedData();
data.setData(ld);
You could save this data as a JSON string using the following method.
private void saveData(CombinedData data) throws JSONException {
JSONObject json_data = new JSONObject();
// Lines
JSONArray lines = new JSONArray();
LineData ld = data.getLineData();
for(int i = 0; i < ld.getDataSetCount(); ++i) {
ILineDataSet d = ld.getDataSets().get(i);
JSONObject lineI = new JSONObject();
JSONArray lineEntries = new JSONArray();
int num_entries = d.getEntryCount();
for(int j = 0; j < num_entries; ++j) {
Entry e = d.getEntryForIndex(j);
JSONObject entry = new JSONObject();
entry.put("x", e.getX());
entry.put("y", e.getY());
// Add other entry attributes you care about here
lineEntries.put(entry);
}
lineI.put("entries", lineEntries);
lineI.put("label", d.getLabel());
// Add other data set attributes you care about here
lines.put(lineI);
}
json_data.put("lines", lines);
SharedPreferences prefs = getSharedPreferences("MY_PREFS", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = prefs.edit();
ed.putString("CombinedData", json_data.toString());
ed.apply();
}
This will save a string like the following
{
"lines":[
{
"entries":[{"x":0,"y":1},{"x":1,"y":2},{"x":2,"y":3},{"x":3,"y":4}],
"label":"Line"
},
{
"entries":[{"x":2,"y":4},{"x":3,"y":5},{"x":4,"y":6},{"x":5,"y":7}],
"label":"Base"
}
]
}
And you can load it and rebuild the CombinedData like this
private CombinedData loadData() throws JSONException {
SharedPreferences prefs = getSharedPreferences("MY_PREFS", Context.MODE_PRIVATE);
String json_string = prefs.getString("CombinedData", "{}");
JSONObject json_data = new JSONObject(json_string);
LineData ld = new LineData();
JSONArray lines = json_data.getJSONArray("lines");
for(int i = 0; i < lines.length(); ++i) {
JSONObject lineI = lines.getJSONObject(i);
JSONArray entries = lineI.getJSONArray("entries");
List<Entry> line_entries = new ArrayList<>();
for(int j = 0; j < entries.length(); ++j) {
JSONObject objJ = entries.getJSONObject(j);
line_entries.add(new Entry((float)objJ.getDouble("x"), (float)objJ.getDouble("y")));
}
String label = lineI.getString("label");
LineDataSet ldata = new LineDataSet(line_entries, label);
ld.addDataSet(ldata);
}
CombinedData data = new CombinedData();
data.setData(ld);
return data;
}
Since you are building the CombinedData
in your code, you could also just build the JSON string using the raw data you used to build the CombinedData
in the first place. If you store the data you need in your own class, you can use something like GSON to serialize and deserialize your class more easily than the manual approach above too.
CombinedData can contain a lot of different data types, so you will probably want to tailor this to the types you care about (for example, no need to serialize BarData if you never plan to have BarData in your plot).