从thingspeak网站检索数据到HelloCharts

时间:2016-08-23 15:07:55

标签: java android

在此代码中,数据来自具有通道ID的thingspeak网站。渠道是公开的。该图是使用hellocharts和chartview获得的。

问题: 我想打印在图表上绘制的所有数据值或在文本视图中添加的最后一个数据。哪个变量是存储的数据因为我想进一步处理数据。

For the output please see the link

在输出中我想知道值395存储在哪个变量中。

我在下面提供了java代码。

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;
import com.macroyau.thingspeakandroid.ThingSpeakChannel;
import com.macroyau.thingspeakandroid.ThingSpeakLineChart;
import com.macroyau.thingspeakandroid.model.ChannelFeed;
import java.util.Calendar;
import java.util.Date;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

public class DemoActivity extends ActionBarActivity {

private ThingSpeakChannel tsChannel;
private ThingSpeakLineChart tsChart;
private LineChartView chartView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Connect to ThinkSpeak Channel 9
    tsChannel = new ThingSpeakChannel(135855);
    // Set listener for Channel feed update events
    tsChannel.setChannelFeedUpdateListener(new ThingSpeakChannel.ChannelFeedUpdateListener() {
        @Override
        public void onChannelFeedUpdated(long channelId, String channelName, ChannelFeed channelFeed) {
            // Show Channel ID and name on the Action Bar
            getSupportActionBar().setTitle(channelName);
            getSupportActionBar().setSubtitle("Channel " + channelId);
            // Notify last update time of the Channel feed through a Toast message
            Date lastUpdate = channelFeed.getChannel().getUpdatedAt();
            Toast.makeText(DemoActivity.this, lastUpdate.toString(), Toast.LENGTH_LONG).show();
        }
    });
    // Fetch the specific Channel feed
    tsChannel.loadChannelFeed();

    // Create a Calendar object dated 5 minutes ago
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, -5);

    // Configure LineChartView
    chartView = (LineChartView) findViewById(R.id.chart);
    chartView.setZoomEnabled(false);
    chartView.setValueSelectionEnabled(true);

    // Create a line chart from Field1 of ThinkSpeak Channel 9
    tsChart = new ThingSpeakLineChart(135855, 2);
    // Get 200 entries at maximum
    tsChart.setNumberOfEntries(200);
    // Set value axis labels on 10-unit interval
    tsChart.setValueAxisLabelInterval(10);
    // Set date axis labels on 5-minute interval
    tsChart.setDateAxisLabelInterval(10);
    // Show the line as a cubic spline
    tsChart.useSpline(true);
    // Set the line color
    tsChart.setLineColor(Color.parseColor("#D32F2F"));
    // Set the axis color
    tsChart.setAxisColor(Color.parseColor("#455a64"));
    // Set the starting date (5 minutes ago) for the default viewport of the chart
   // tsChart.setChartStartDate(calendar.getTime());
    // Set listener for chart data update
    tsChart.setListener(new ThingSpeakLineChart.ChartDataUpdateListener() {
        @Override
        public void onChartDataUpdated(long channelId, int fieldId, String title, LineChartData lineChartData, Viewport maxViewport, Viewport initialViewport) {
            // Set chart data to the LineChartView
            chartView.setLineChartData(lineChartData);
            // Set scrolling bounds of the chart
            chartView.setMaximumViewport(maxViewport);
            // Set the initial chart bounds
            chartView.setCurrentViewport(initialViewport);
           /* LineChartData data = new LineChartData();
            float data1=data.getBaseValue();
            TextView tvName = (TextView)findViewById(R.id.textView);
            tvName.setText((int) data1);*/

        }
    });
    // Load chart data asynchronously
    tsChart.loadChartData();
    }

}

2 个答案:

答案 0 :(得分:0)

根据HelloCharts source-code,我相信您在异步加载图表数据后正在寻找this method

chartView.getLineChartData() 

从那时起,您应该能够找到Line个对象,而line.getValues()会将您想要的数据保存为List<PointValue>

总而言之,

List<PointValue> values = chartView.getLineChartData().getLines().getValues();
// TODO: for (PointValue p : values) { p.getX(); p.getY(); }

此列表包含PointValuegetXgetY方法

答案 1 :(得分:0)

我不知道这个特定的库,但是为了访问你的数据,你可以简单地点击给定频道的API,即

String lightApi = "https://api.thingspeak.com/channels/<your_channel_id>/fields/<field_number>.json?api_key=<read_API_key>&results=<number_of_readings>";

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,lightApi, null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        try {
            JSONArray feeds = response.getJSONArray("feeds");
            for(int i=0; i<feeds.length();i++){
                JSONObject jo = feeds.getJSONObject(i);
                String l=jo.getString("<your_field_number>"); //you may want to Integer.parseInt the returned value
                //String date=jo.getString("created_at"); -->to get the date and time
                // Do whatever you want to do with each of these values...
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}