2016-08-23 111 views
2

在這段代碼中,數據來自thingspeak網站給出的頻道ID。該頻道是公開的。該圖是使用hellocharts和chartview獲得的。從thingspeak網站檢索數據到HelloCharts

問題: 我想打印所有在圖形上繪製的數據值或在文本視圖中添加的最後一個數據。在哪個變量中存儲數據正如我想進一步處理數據一樣。

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(); 
    } 

} 
+0

的API,我沒有看到,你甚至獲得X和Y值圖表...'tsChart.loadChartData( );' - 那是幹什麼的? –

+0

看「ThingSpeakLineChart」的源代碼,它不會公開該信息 –

回答

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(); } 

此列表包含PointValue類與getXgetY方法

+0

您能否提供代碼來聲明線對象?和line.getvalues()將返回一個字符串? –

+0

我沒有使用過這個庫。我純粹閱讀Github上的源代碼。 Android Studio甚至應該自動完成這些方法,以便您可以看到返回的內容 –

+0

您可以提供您引用源代碼的github鏈接 –

0

我不知道布特這個特定的庫,但訪問您的數據,你可以簡單地打你定通道即

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(); 
     } 
    } 
}