我创建了一个函数来显示我的android应用程序使用图表的数据。从我的服务器使用API检索数据。
我的API结果
{
"items": [
{
"idarchive": "33",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-10",
"date2": "2016-07-10",
"period": "1",
"ts_archived": "2016-07-10 17:32:55",
"value": "1217"
},
{
"idarchive": "74",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-11",
"date2": "2016-07-11",
"period": "1",
"ts_archived": "2016-07-11 17:02:51",
"value": "1781"
},
{
"idarchive": "90",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-12",
"date2": "2016-07-12",
"period": "1",
"ts_archived": "2016-07-13 09:18:54",
"value": "1809"
}
]
}
从API函数中获取的数据正常,直到客户端(android)以ArrayList的形式获取。
我正在尝试显示数据,以便使用我的API从服务器动态检索数据。但是如何更改原始示例代码
int numSubcolumns = 1;
int numColumns = 3;
List<Column> columns = new ArrayList<Column>();
List<SubcolumnValue> values;
for (int i = 0; i < numColumns; ++i) {
values = new ArrayList<SubcolumnValue>();
for (int j = 0; j < numSubcolumns; ++j) {
int sign = getSign();
values.add(new SubcolumnValue((float) Math.random() * 20f * sign + 5 * sign, ChartUtils.pickColor()));
}
Column column = new Column(values);
column.setHasLabels(hasLabels);
column.setHasLabelsOnlyForSelected(hasLabelForSelected);
columns.add(column);
}
data = new ColumnChartData(columns);
// Set stacked flag.
data.setStacked(true);
if (hasAxes) {
Axis axisX = new Axis();
Axis axisY = new Axis().setHasLines(true);
if (hasAxesNames) {
axisX.setName("Axis X");
axisY.setName("Axis Y");
}
data.setAxisXBottom(axisX);
data.setAxisYLeft(axisY);
} else {
data.setAxisXBottom(null);
data.setAxisYLeft(null);
}
chart.setColumnChartData(data);
成为我的API的结果?
我想替换此部分
List<Column> columns = new ArrayList<Column>();
List<SubcolumnValue> values;
使用来自服务器的循环数据
for (int x=0; x<listVisitor.size(); x++) {
}
当我尝试加载数据和合并与示例图表代码
时,这是我的完整代码private void load() {
progress = ProgressDialog.show(getActivity(), "Retrieving Data", "Please wait...", true);
progress.setIndeterminate(true);
ApiInterface apiService = ApiClient.getVisitor().create(ApiInterface.class);
Call<NewVisitorResponse> call = apiService.getVisitor();
call.enqueue(new Callback<NewVisitorResponse>() {
@Override
public void onResponse(Call<NewVisitorResponse> call, Response<NewVisitorResponse> response) {
listVisitor = response.body().getVisitor(); //Dont worry, the data successfully loaded here
int numSubcolumns = 1;
int numColumns = listVisitor.size();
List<Column> columns = new ArrayList<Column>();
List<SubcolumnValue> values;
for (int i = 0; i < numColumns; ++i) {
values = new ArrayList<SubcolumnValue>();
for (int j = 0; j < numSubcolumns; ++j) {
values.add(new SubcolumnValue((listVisitor.get(i).getValue(), ChartUtils.pickColor()));
}
for (int x=0; x<listVisitor.size(); x++){
Column column = new Column(values);
column.setHasLabels(hasLabels);
column.setHasLabelsOnlyForSelected(hasLabelForSelected);
columns.add(listVisitor.get(i).getDate1());
}
}
data = new ColumnChartData(columns);
if (hasAxes) {
Axis axisX = new Axis();
Axis axisY = new Axis().setHasLines(true);
if (hasAxesNames) {
axisX.setName("Date");
axisY.setName("Total Visitor");
}
data.setAxisXBottom(axisX);
data.setAxisYLeft(axisY);
} else {
data.setAxisXBottom(null);
data.setAxisYLeft(null);
}
chart.setColumnChartData(data);
progress.dismiss();
}
@Override
public void onFailure(Call<NewVisitorResponse> call, Throwable t) {
Log.d(TAG, "STATUS CODE : " + t.toString());
Snackbar.make(getView(), "CONNECTION ERROR. PLEASE TRY AGAIN", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
});
}
数据检索是成功的,这是图表中数据逻辑放置的唯一问题。
我想在图表中添加date1
和value
字段
也许你可以帮助我。感谢