Trying to pull data from mysql into android using Retrofit and output a line graph. The first part (pulling data) works fine. Filtering the data into the MPALinechart in android is proving difficult.
I would like the line graph to show the user's mood over time. So the Bottom Axis would be Jan, Feb, March etc and the Left Axis would be their mood.
Moods are ranked from 0-4 where 0 is a negative mood like depressed and 4 is a positive mood like exited. The idea is that the line will show the person their mood over time and they can see which weeks/months their moods dipped.
My Code so far:
// Retrofit call to the MYSQL server.
Call<List<MoodLogLineGraph>> call = RetrofitClient.getInstance().getApi().moodLogLineGraph(userId);
call.enqueue(new Callback<List<MoodLogLineGraph>>() {
@Override
public void onResponse(Call<List<MoodLogLineGraph>> call, Response<List<MoodLogLineGraph>> response) {
// All JSON entries form the MYSQL server are parsed into an ArrayList.
ArrayList<Entry> dataVals = new ArrayList<Entry>();
// For loop which dynamically adds all entries to the ArrayList
for (MoodLogLineGraph moodLogList : response.body()) {
moodLogList.getMoodName();
moodLogList.getPosted();
if (moodLogList.getMoodName().equals("Excited")) {
dataVals.add(new Entry(1, 3));
}
if (moodLogList.getMoodName().equals("Happy")) {
dataVals.add(new Entry(1, 1));
}
if (moodLogList.getMoodName().equals("Fine")) {
dataVals.add(new Entry(1, 0));
}
if (moodLogList.getMoodName().equals("Calm")) {
dataVals.add(new Entry(1, 1));
}
if (moodLogList.getMoodName().equals("Upset")) {
dataVals.add(new Entry(1, 1));
}
if (moodLogList.getMoodName().equals("Stressed")) {
dataVals.add(new Entry(1, 0));
}
if (moodLogList.getMoodName().equals("Anxious")) {
dataVals.add(new Entry(1, 1));
}
if (moodLogList.getMoodName().equals("Confused")) {
dataVals.add(new Entry(1, 1));
}
if (moodLogList.getMoodName().equals("Guilty")) {
dataVals.add(new Entry(1, 1));
}
if (moodLogList.getMoodName().equals("sad")) {
dataVals.add(new Entry(1, 1));
}
if (moodLogList.getMoodName().equals("Depressed")) {
dataVals.add(new Entry(1, 0));
}
}
//Initialising the PieChart variables which control the presentation.
LineDataSet lineDataSet1 = new LineDataSet(dataVals,"");
lineDataSet1.setColors(color);
lineDataSet1.setValueTextSize(14);
LineData lineDataSet = new LineData(lineDataSet1);
mLineChart.setData(lineDataSet);
mLineChart.invalidate();
mLineChart.setDrawGridBackground(false);
mLineChart.setTouchEnabled(true);
mLineChart.setDragEnabled(true);
mLineChart.setScaleEnabled(true);
mLineChart.setPinchZoom(true);
XAxis xl = mLineChart.getXAxis();
xl.setAvoidFirstLastClipping(true);
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
final String[] months = new String[]{"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Sep", "oct", "Nov", "Dec"};
IAxisValueFormatter formatter = new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return months[(int) value];
}
};
xl.setGranularity(1f); // minimum axis-step (interval) is 1
xl.setValueFormatter(formatter);
YAxis leftAxis = mLineChart.getAxisLeft();
leftAxis.setInverted(true);
YAxis rightAxis = mLineChart.getAxisRight();
rightAxis.setEnabled(false);
I don't know how to use each row of data and plot it on the graph over time. This needs to be done dynamically from the database and not hard coded. If anyone could help me get this code up and running I would be very grateful.