gpt4 book ai didi

java - Android - MPAndroidChart - 折线图不显示线

转载 作者:行者123 更新时间:2023-11-30 01:05:56 30 4
gpt4 key购买 nike

我的图表有问题。图表本身正在显示,但显示值的线没有显示。我从 DB 获取值。我在下面添加了一些图片和我的 Activity 代码:

我第一眼看到的 enter image description here

几秒钟后... enter image description here

我的代码:

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();
}

最佳答案

从您提供的代码来看,您使用的是最新的 3.0.0-beta1 版本的 MPAndroidChart 库。那里引入了许多重大变化。

在您的情况下,可能的问题是您的条目构造不正确。入口构造函数已更改,现在是 Entry(float x, float y),因此您需要先提供 x 值,然后再提供 y 值。将代码更改为此应该可以工作(只需切换值):

// Get the temperature values.
temperature_data_entry.add(new Entry(i, Integer.valueOf(tempJ.getString(1))));

关于java - Android - MPAndroidChart - 折线图不显示线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38879152/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com