我有传感器值和日期时间格式的数据,格式为(Year-month-dateThour:min:secZ)格式,例如“ 2019-05-29T12:48:18Z”值。我想打印所取的值从传感器数据中提取数据,并在xaxis中将图形绘制为日期时间,在yaxis中将其绘制为值。我知道如何绘制yaxis值以及如何添加xaxis标签,请帮忙
这是我的mobilefragment类,它开始执行后台进程,以便从服务器获取数据并将其绘制在mobilefragmentclass中(其中linechart在mobilefragmentactivity中)
public class mobilefragment extends Fragment {
public static TextView data;
public static LineChart mchart;
public static Button click;
List<Entry> x;
ArrayList<String> y;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.mobile, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mchart=view.findViewById(R.id.linechart);
click=view.findViewById(R.id.button);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fetchdata process=new fetchdata();
process.execute();
}
});
//data=view.findViewById(R.id.fetcheddata);
}
}
这是后台进程类,在执行后执行后从服务器获取数据时,将数据绘制在移动碎片活动中,并且我也希望在xaxis中使用日期时间格式,并将其存储在名为“ y”的列表中
public class fetchdata extends AsyncTask<Void,Void,Void> {
String data="";
String dataparsed="";
String singleparsed="";
List<Entry> x;
List<String> y;
boolean flag=false;
/*Context ctx;
fetchdata(Context ctx) {
this.ctx = ctx;
}*/
@Override
protected Void doInBackground(Void... voids) {
x = new ArrayList<Entry>();
y = new ArrayList<String>();
try {
URL url=new URL("https://io.adafruit.com/api/v2/Yarev/feeds/pir-sensor/data");
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String line="";
while (line!=null)
{
line=bufferedReader.readLine();
data=data+line;
}
JSONArray JA=new JSONArray(data);
for(int i=0;i<JA.length();i++)
{
JSONObject JO= (JSONObject) JA.get(i);
singleparsed="Value:"+JO.get("value")+"\n"+
"Feed key:"+JO.get("feed_key")+"\n"+
"Created:"+JO.get("created_at")+"\n";
int value=JO.getInt("value");
float v1=value;
String time=JO.getString("created_at");
x.add(new Entry(i,v1));
y.add(time); //time string
dataparsed=dataparsed+singleparsed;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//mobilefragment.data.setText(this.dataparsed);
mobilefragment.mchart.setDragEnabled(true);
mobilefragment.mchart.setScaleEnabled(true);
mobilefragment.mchart.setDrawGridBackground(false);
mobilefragment.mchart.setTouchEnabled(true);
mobilefragment.mchart.setPinchZoom(false);
XAxis xl = mobilefragment.mchart.getXAxis();
xl.setAvoidFirstLastClipping(true);
YAxis leftAxis = mobilefragment.mchart.getAxisLeft();
leftAxis.setInverted(false);
YAxis rightAxis = mobilefragment.mchart.getAxisRight();
rightAxis.setEnabled(false);
Legend l = mobilefragment.mchart.getLegend();
l.setForm(Legend.LegendForm.LINE);
LineDataSet set1=new LineDataSet(x,"Data set 1");
set1.setFillAlpha(110);
set1.setValueTextColor(Color.GREEN);
List<ILineDataSet> datasets= new ArrayList<>();
datasets.add(set1);
LineData data=new LineData(datasets);
mobilefragment.mchart.setData(data);
mobilefragment.mchart.invalidate();
mobilefragment.click.setVisibility(View.INVISIBLE);
//activity.startActivity(new Intent(activity, Main2Activity.class));
}
public List<Entry> getList() {
return x;
}
}
答案 0 :(得分:0)
https://github.com/jjoe64/GraphView
使用此u可以满足您的要求
答案 1 :(得分:0)
我假设您具有在图形中设置数据的列表。如果是,那么此代码将为您提供帮助。让我知道您是否需要任何东西...
ArrayList<ILineDataSet> lines = getLineDataSetAsPerGraphType(bookingLineList);
XAxis x = lineChart.getXAxis();
x.setDrawGridLines(false);
x.setPosition(XAxis.XAxisPosition.BOTTOM);
x.setTextColor(getResources().getColor(R.color.color_aeaeae));
x.setTextSize(9);
x.setLabelRotationAngle(-30f);
x.setLabelsToSkip(0);
YAxis yLabels1 = lineChart.getAxisRight();
yLabels1.setEnabled(false);
yLabels1.setDrawGridLines(false);
String[] xAxis = new String[bookingLineList.size()];
for (int i = 0; i < bookingLineList.size(); i++) {
xAxis[i] = DateUtils.changeDateFormat("yyyy-MM-dd", "MMM dd", bookingLineList.get(i).getBooking_date());
}
lineChart.setData(new LineData(xAxis, lines));
lineChart.setDragEnabled(true);
lineChart.animateX(1000, Easing.EasingOption.EaseInOutQuart);
// Scroll
lineChart.canScrollHorizontally(5); // Scroll = true
lineChart.setVisibleXRangeMaximum(10f); // Min Gap between two values
lineChart.moveViewToX(0f); // Show from