MPAndroidChart 折线图添加数据时的回调
MPAndroidChart Line Chart Callback when data added
我正在尝试用来自 Http 调用的数据填充折线图,一切正常,只是有时数据似乎尚未完全添加到图表中。有没有办法在数据添加到图表时获得回调?
我的代码:
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject jsonObj = new JSONObject(response.body().string());
JSONArray price = jsonObj.getJSONArray("price");
for(int I = 0; I < price.length(); I++)
{
JSONArray jArr = price.getJSONArray(I);
values.add(new Entry((float) jArr.getDouble(0), (float) jArr.getDouble(1)));
// create a dataset and give it a type
LineDataSet set1 = new LineDataSet(values, "DataSet 1");
set1.setAxisDependency(AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
set1.setDrawFilled(true);
// create a data object with the datasets
final LineData data = new LineData(set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
mChart.setData(data);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
问题似乎是您不断地用一个点覆盖您的数据集。您需要将 LineDataSet 和 LineData 创建移到 for 循环之外。
for(int I = 0; I < price.length(); I++)
{
JSONArray jArr = price.getJSONArray(I);
values.add(new Entry((float) jArr.getDouble(0), (float) jArr.getDouble(1)));
}
// create a dataset and give it a type
LineDataSet set1 = new LineDataSet(values, "DataSet 1");
set1.setAxisDependency(AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
set1.setDrawFilled(true);
// create a data object with the datasets
final LineData data = new LineData(set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
mChart.setData(data);
我正在尝试用来自 Http 调用的数据填充折线图,一切正常,只是有时数据似乎尚未完全添加到图表中。有没有办法在数据添加到图表时获得回调?
我的代码:
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject jsonObj = new JSONObject(response.body().string());
JSONArray price = jsonObj.getJSONArray("price");
for(int I = 0; I < price.length(); I++)
{
JSONArray jArr = price.getJSONArray(I);
values.add(new Entry((float) jArr.getDouble(0), (float) jArr.getDouble(1)));
// create a dataset and give it a type
LineDataSet set1 = new LineDataSet(values, "DataSet 1");
set1.setAxisDependency(AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
set1.setDrawFilled(true);
// create a data object with the datasets
final LineData data = new LineData(set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
mChart.setData(data);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
问题似乎是您不断地用一个点覆盖您的数据集。您需要将 LineDataSet 和 LineData 创建移到 for 循环之外。
for(int I = 0; I < price.length(); I++)
{
JSONArray jArr = price.getJSONArray(I);
values.add(new Entry((float) jArr.getDouble(0), (float) jArr.getDouble(1)));
}
// create a dataset and give it a type
LineDataSet set1 = new LineDataSet(values, "DataSet 1");
set1.setAxisDependency(AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
set1.setDrawFilled(true);
// create a data object with the datasets
final LineData data = new LineData(set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
mChart.setData(data);