繁体   English   中英

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

[英]How to plot graph between x axis as date time and y axis as value

我有传感器值和日期时间格式的数据,格式为(Year-month-dateThour:min:secZ),例如“2019-05-29T12:48:18Z”值。我想打印从传感器数据中获取的值并将xaxis中的图形绘制为datetime,将yaxis绘制为值。 我知道如何绘制yaxis值以及如何添加xaxis lable也请帮忙

这是我的mobilefragment类,它开始执行后台进程,以便从服务器获取数据并将其绘制在移动片段类中(其中线图在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'的List中

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

使用它你可以达到你的要求

我假设您有列表将在图表中设置数据。 如果是,那么此代码将帮助您。 你有任何需要都请告诉我...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM