Reputation: 33
I'm having a problam with my chart. The chart itself is showing but the line that displays the values does not. I'm getting the values from a DB
. I'm adding some images below and the code for my Activity:
My code:
public class SensorDataActivity extends AppCompatActivity {
RelativeLayout mainLayout;
SensorDataRequest_Temperature sensorDataRequest_temperature;
ArrayList<Entry> temperature_data_entry;
ArrayList<String> temperature_dates_entry;
XAxis xAxis;
YAxis leftAxis;
LineChart mChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sensor_data_activity_layout);
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
mChart =(LineChart) findViewById(R.id.mChart);
if (mChart != null) {
mChart.setNoDataTextDescription("press anywhere to refresh or wait");
mChart.setHighlightPerTapEnabled(true);
mChart.setTouchEnabled(false);
mChart.setDrawGridBackground(false);
mChart.setDrawBorders(false);
mChart.setBackgroundColor(Color.WHITE);
mChart.setHighlightPerTapEnabled(true);
// disable the right axis's
mChart.getAxisRight().setEnabled(false);
}
addResponseListener_temperature();
queueInit();
}
/*
Response Listener for the Temperature table Request.
used in case "Month" or "Week" was clicked in spinner.
*/
private void addResponseListener_temperature() {
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Response",response);
JSONArray json;
try {
json = new JSONArray(response);
Log.i("log_json",json.get(1).toString());
temperature_data_entry = new ArrayList<>();
temperature_dates_entry = new ArrayList<>();
for (int i=0; i<json.length(); i++)
{
JSONArray tempJ = new JSONArray(json.get(i).toString());
//Log.i("tempJ",tempJ.toString());
// get the temperature values.
temperature_data_entry.add(new Entry( Integer.valueOf(tempJ.getString(1)) ,i));
// get the temperature timestamps.
temperature_dates_entry.add(tempJ.getString(0));
}
xAxis = mChart.getXAxis();
xAxis.setTextSize(12f);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setEnabled(true);
xAxis.setAxisMinValue(0);
xAxis.setValueFormatter(new AxisValueFormatter() {
private SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public String getFormattedValue(float value, AxisBase axis) {
String timeStamp = "none";
try {
Date date = mFormat.parse(temperature_dates_entry.get((int)value));
timeStamp = mFormat.format(new Date(date.getTime()));
} catch (ParseException e) {
e.printStackTrace();
}
return timeStamp;
}
@Override
public int getDecimalDigits() {
return 0;
}
});
leftAxis = mChart.getAxisLeft();
leftAxis.setAxisMaxValue(60f);
leftAxis.setTextSize(20f);
leftAxis.setDrawAxisLine(false);
leftAxis.setEnabled(true);
LineDataSet set1 = new LineDataSet(temperature_data_entry,"temperature data");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(Color.BLUE);
set1.setDrawCircles(false);
set1.setDrawValues(true);
set1.setLineWidth(3f);
set1.setValues(temperature_data_entry);
set1.setValueTextColor(Color.RED);
set1.setVisible(true);
LineData data = new LineData(set1);
mChart.setData(data); // set the data and list of lables into chart
data.setValueTextColor(Color.RED);
data.setDrawValues(true);
mChart.invalidate();
mChart.notifyDataSetChanged();
}catch (JSONException je)
{
Log.e("JSONException",je.getMessage());
}
}
};
// initiating the SensorDataRequest object with the appropriate parameters.
sensorDataRequest_temperature = new SensorDataRequest_Temperature(
//userDetails.get("raspberry_product_key"),
TEMP_RASP_ID,
responseListener);
}
/*
initiates the RequestQueue object with the activity context and
adds all the table's request's to the queue.
*/
private void queueInit() {
// creating a RequestQueue object and providing it with the context of this activity.
RequestQueue queue = Volley.newRequestQueue(SensorDataActivity.this);
// adding our SensorDataRequest object to our RequestQueue object.
queue.add(sensorDataRequest_temperature);
}
@Override
protected void onResume() {
mChart.invalidate();
mChart.notifyDataSetChanged();
super.onResume();
}
Upvotes: 1
Views: 4055
Reputation: 10929
I think the problem is because of this code:
@Override
protected void onResume() {
mChart.invalidate();
mChart.notifyDataSetChanged();
super.onResume();
}
You should do mChart.invalidate();
only after a dataset is changed/added. I had the same issue where I had used invalidate before adding the dataset. So I could see the data but it never got populated. When I added this line after adding data set, it worked perfectly.
Upvotes: 0
Reputation: 3246
Judging from the code your provided, you are using the latest 3.0.0-beta1
version of the MPAndroidChart library. A lot of breaking changes have been introduced there.
In your case the likely problem is that your entries aren't properly constructed. The entry constructor has changed and now is Entry(float x, float y)
, so you need to first provide the x-value and then the y-value. Changing the code to this should work (just switch the values):
// Get the temperature values.
temperature_data_entry.add(new Entry(i, Integer.valueOf(tempJ.getString(1))));
Upvotes: 1