java - 如何在 x 轴作为日期时间和 y 轴作为值之间绘制图表
问题描述
我有传感器值和日期时间格式的数据,格式为(Year-month-dateThour:min:secZ),例如“2019-05-29T12:48:18Z”值。我想打印取自传感器数据的值并将 xaxis 中的图形绘制为日期时间,将 yaxis 绘制为值。我知道如何绘制 yaxis 值以及如何添加 xaxis 标签也请帮助
这是我的 mobilefragment 类,它开始执行后台进程,以便从服务器获取数据并将其绘制在 mobile fragmentclass 中(其中 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);
}
}
这是后台进程类,当在 onpostexecution 后从服务器获取数据时,数据被绘制在 mobilefragment 活动中,而且我希望 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;
}
}
解决方案
我假设您拥有将在图表中设置数据的列表。如果是,那么此代码将对您有所帮助。如果你需要帮助,请告诉我...
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
推荐阅读
- pandas - 将 pandas 数据帧逻辑转换为基于 pyspark 数据帧的逻辑
- swift - 如何停止自动重新加载firebase
- django - 如何在模板表单循环中排除特定字段类型
- mysql - 在特定字符之后清除左边的 14 个字符和右边的“x”
- kotlin - Dynamic/Sectioned RecyclerView, Room 和 Single Data Class
- grafana - 我应该如何让 grafana 知道安装在不同主机上的 telegraf 和 influxdb?
- linux - 在 bash 中 2 个日期之间的 bash 差异 by yyyy-mm-dd 在 N 天内
- reactjs - 我想将复选框数据存储到功能状态挂钩中但无法执行
- python - 如何调试无响应、模糊不清的 Werkzeug 404 错误?
- memory-leaks - 如何检查之前是否运行过子程序?