[英]How to retrieve JSON data and plot a LineChart using MPAndroidChart and retrofit on android
I am trying to retrieve data using retrofit and put them using MPAndroidChart.我正在尝试使用 retrofit 检索数据并使用 MPAndroidChart 放置它们。 I am able to retrieve data from JSON and I am also able to manually add a defined x, y on a line plot (such as ie. x=1, y=1).
我能够从 JSON 检索数据,并且我还能够在 plot 行上手动添加定义的 x、y(例如 x=1、y=1)。
However, I am still learning MPLineChart and (especially with LineChart) and am getting struck on combine the two concepts - retrieving the y1 and y2 data and use those the data to plot against with time for a line graph .但是,我仍在学习 MPLineChart 和(尤其是 LineChart),并且对结合这两个概念感到震惊 -检索 y1 和 y2 数据并将这些数据用于 plot 与时间线图。 Could anyone lighten me a bit on how to retrieve the JSON data and plot?
任何人都可以让我了解如何检索 JSON 数据和 plot 吗?
Here is what I have so far:这是我到目前为止所拥有的:
MainActivity:主要活动:
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawLineChart();
}
private void drawLineChart() {
LineChart lineChart = findViewById(R.id.chart);
List<Entry> lineEntries = getDataSet();
LineDataSet lineDataSet = new LineDataSet(lineEntries, getString(R.string.data1));
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setHighlightEnabled(true);
lineDataSet.setLineWidth(2);
lineDataSet.setColor(Color.RED);
lineDataSet.setCircleColor(Color.YELLOW);
lineDataSet.setCircleRadius(6);
lineDataSet.setCircleHoleRadius(3);
lineDataSet.setDrawHighlightIndicators(true);
lineDataSet.setHighLightColor(Color.RED);
lineDataSet.setValueTextSize(12);
lineDataSet.setValueTextColor(Color.DKGRAY);
LineData lineData = new LineData(lineDataSet);
lineChart.getDescription().setTextSize(12);
lineChart.setDrawMarkers(true);
lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTH_SIDED);
lineChart.animateY(1000);
lineChart.getXAxis().setGranularityEnabled(true);
lineChart.getXAxis().setGranularity(1.0f);
lineChart.getXAxis().setLabelCount(lineDataSet.getEntryCount());
lineChart.setData(lineData);
}
private List<Entry> getDataSet() {
List<Entry> lineEntries = new ArrayList<Entry>();
//lineEntries.add(new Entry(0, 1));
//lineEntries.add(new Entry(1, 2));
//lineEntries.add(new Entry(2, 3));
//lineEntries.add(new Entry(3, 4));
return lineEntries;
}
private void fetchData() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<List<Details>> call = api.getstatus();
call.enqueue(new Callback<List<Details>>() {
@Override
public void onResponse(Call<List<Details>> call, Response<List<Details>> response) {
List<Details> adslist = response.body();
}
@Override
public void onFailure(Call<List<Details>> call, Throwable t) {
Toast.makeText(MainActivity.this, ""+t.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
For gson:对于 gson:
public class Details {
@SerializedName("y1")
@Expose
private String y1;
@SerializedName("y2")
@Expose
private String y2;
@SerializedName("timestamps")
@Expose
private String timestamps;
public String getY1() {
return y1;
}
public void setY1(String y1) {
y1 = y1;
}
public String getY2() {
return y2;
}
public void setY2(String y2) {
y2 = y2;
}
public String getTimestamps() {
return timestamps;
}
public void setTimestamps(String timestamps) {
timestamps = timestamps;
}
}
For Api:对于 Api:
public interface Api {
String BASE_URL = "http://..";
@GET("Api.php")
Call<List<Details>> getstatus();
}
And data is:数据是:
[{"y1":"40","y2":"80","timestamps":"2020-1-10 18:00:34"},{"y1":"50","y2":"60","timestamps":"2020-1-10 17:40:20"},{"y1":"70","y2":"30","timestamps":"2020-1-10 17:20:34"}]
Change your getDataSet method like this像这样更改您的 getDataSet 方法
private void getDataSet(List<Details> list) {
List<Entry> lineEntries = new ArrayList<Entry>();
if(list!=null){
for(Details details : list){
lineEntries.add(new Entry(Float.parseFloar(details.getY1()),Float.parseFloat(details.getY2()));
}
}
LineDataSet lineDataSet = new LineDataSet(lineEntries, getString(R.string.data1));
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setHighlightEnabled(true);
lineDataSet.setLineWidth(2);
lineDataSet.setColor(Color.RED);
lineDataSet.setCircleColor(Color.YELLOW);
lineDataSet.setCircleRadius(6);
lineDataSet.setCircleHoleRadius(3);
lineDataSet.setDrawHighlightIndicators(true);
lineDataSet.setHighLightColor(Color.RED);
lineDataSet.setValueTextSize(12);
lineDataSet.setValueTextColor(Color.DKGRAY);
LineData lineData = new LineData(lineDataSet);
lineChart.getDescription().setTextSize(12);
lineChart.setDrawMarkers(true);
lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTH_SIDED);
lineChart.animateY(1000);
lineChart.getXAxis().setGranularityEnabled(true);
lineChart.getXAxis().setGranularity(1.0f);
lineChart.getXAxis().setLabelCount(lineDataSet.getEntryCount());
lineChart.setData(lineData);
}
decalre your line in oncreate method <br> LineChart lineChart = findViewById(R.id.chart);<br>
And call this getDataSet(List list) method from fetchData onResposne() method并从 fetchData onResposne() 方法调用此 getDataSet(List list) 方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.