2016-08-23 4 views
2

このコードでは、データはチャンネルIDを指定してthingspeakウェブサイトから取得されます。チャンネルは公開されています。グラフは、hellochartと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を打つことができ); - それは何ですか? –

+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

私は知らない自分のデータにアクセスするが、この特定のライブラリ試合あなたは単にあなたも(図表... `tsChart.loadChartDataのためにXとYの値を取得どこ私は見ていないよ

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(); 
     } 
    } 
} 
関連する問題