我正在创建一个图表,该图表从数据库中获取数据并每 10 秒更新一次。图表y-axis将显示float值,而x-axis标签将位于 中string timestamp。我使用 php 代码返回一个 json 对象,我可以知道要添加哪些值。下面是调用的方法/函数,loadDB()它获取数据:
void loadDB() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATABASE,
response -> {
try {
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
product = array.getJSONObject(i);
addEntry(Float.parseFloat(product.getString("value1")), product.getInt("id"));
addEntryPower(Float.parseFloat(product.getString("value2")), product.getInt("id"));
latestID = product.getInt("id") + 1;
}
} catch (JSONException e) {
e.printStackTrace();
}
},
error -> {});
Volley.newRequestQueue(this).add(stringRequest);
}
获取数据后,我开始使用方法/函数将其添加到图表中addEntry(float, int)
void addEntry(float current, int id) {
LineData data = lineChart.getData();
if(data != null) {
ILineDataSet set = data.getDataSetByIndex(0);
if(set == null) {
set = createSet();
data.addDataSet(set);
}
data.addEntry(new Entry((float) id, current), 0);
lineChart.notifyDataSetChanged();
lineChart.setVisibleXRangeMaximum(6);
lineChart.moveViewToX(data.getEntryCount());
}
}
我不太确定我是否做对了,但在LineChart声明和设置中,我使用了这段代码来修改X-Axis标签。
lineChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter() {
@Override
public String getAxisLabel(float value, AxisBase axisBase) {
try {
return product.getString("reading_time");
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}
});
data.addEntry(new Entry(x-axis, y-axis))它基本上忽略了方法/函数中的 x 轴输入void addEntry(float, float),并使用reading_timefromjsonObject作为标签。
我不太确定我在哪里做错了,它导致图形输出的所有x-axis标签都相同。当它更新时(图表每 10 秒更新一次),所有x-axis标签都会更改为最新的reading_time。
下面是图表的截图。(您可以看到 x 轴是相同的)而且我的setupGraph()方法/功能只是以防万一问题来自那里。
void setupGraph() {
lineChart.getDescription().setEnabled(true);
lineChart.getDescription().setText("Real Time Current Graph");
lineChart.getDescription().setTextSize(10f);
lineChart.setTouchEnabled(true);
lineChart.setDragEnabled(true);
lineChart.setScaleEnabled(true);
lineChart.setDrawGridBackground(false);
lineChart.setPinchZoom(true);
lineChart.setExtraLeftOffset(15);
lineChart.setExtraRightOffset(15);
lineChart.setBackgroundColor(Color.LTGRAY);
XAxis xAxis = lineChart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setEnabled(true);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawGridLines(false);
xAxis.setAxisLineColor(Color.BLACK);
xAxis.setAxisLineWidth(1.5f);
xAxis.setTextColor(Color.BLACK);
xAxis.setGranularity(1);
xAxis.setLabelRotationAngle(-45);
LineData data = new LineData();
data.setValueTextColor(Color.WHITE);
lineChart.setData(data);
Legend l = lineChart.getLegend();
l.setForm(Legend.LegendForm.LINE);
l.setTextColor(Color.WHITE);
YAxis y1 = lineChart.getAxisLeft();
y1.setTextColor(Color.BLACK);
y1.setAxisLineColor(Color.BLACK);
y1.setAxisLineWidth(1.5f);
y1.setAxisMaximum(80f);
y1.setAxisMinimum(-10f);
y1.setDrawGridLines(true);
YAxis y12 = lineChart.getAxisRight();
y12.setEnabled(false);
lineChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter() {
@Override
public String getAxisLabel(float value, AxisBase axisBase) {
try {
return product.getString("reading_time");
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}
});
}
将不胜感激一些帮助,谢谢!
