java - 如何在 x 轴作为日期时间和 y 轴作为值之间绘制图表

标签 java android graph mpandroidchart

我有传感器值和日期时间格式的数据,其格式为(年-月-日期Thour:min:secZ),例如“2019-05-29T12:48:18Z”值。我想打印所取的值来自传感器数据并在 x 轴中绘制图表作为日期时间,在 y 轴中绘制图表作为值。我知道如何绘制 yaxis 值以及如何添加 xaxis 标签也请帮助

这是我的 mobilefragment 类,它开始执行后台进程,以便从服务器获取数据并将其绘制在移动 fragment 类中(其中折线图位于 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 Activity 中绘制,并且我还希望 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;
    }

}

最佳答案

https://github.com/jjoe64/GraphView

使用这个你可以达到你的要求

关于java - 如何在 x 轴作为日期时间和 y 轴作为值之间绘制图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56421999/

相关文章:

java - 如何在 selenium webdriver 中使用 CSS 选择器 `contains` 获取元素

java - Android 和 GSON 抛出 No-Args Constructor not found 异常

java - 填充图 - java

android - 使用 SpanSizeLookup 为 GridLayoutManager 中的项目设置跨度

android - FragmentManager 和 FragmentTransaction 到底是做什么的?

android - 如何在屏幕旋转时使用 ProgressBar 处理 Android LiveData(ViewModel)?

java - 在 Android 中摆脱半个字符串

graph - 如何在 gnuplot 上绘制树/图/网络数据?

python - 如何按时间间隔调整 Bokeh x 轴标记

python - Python 中平滑图形的问题