public class MainActivity extends AppCompatActivity {
Api_Client api = new Api_Client("connect Key",
"Secret Key");
List<Integer> price_list = new ArrayList<Integer>();
List<Integer> total_list = new ArrayList<Integer>();
]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GraphView graph=(GraphView)findViewById(R.id.graph);
NetworkThread thread=new NetworkThread();
thread.start();
}
public synchronized void drawLineGraph()throws Exception{
DataPoint[] pricePoints=new DataPoint[price_list.size()];
DataPoint[] totalPoints=new DataPoint[price_list.size()];
for (int i = 0; i < pricePoints.length; i++) {
pricePoints[i] = new DataPoint(i, price_list.get(i));
totalPoints[i] = new DataPoint(i, total_list.get(i));
}
LineGraphSeries series=new LineGraphSeries<>(pricePoints);
// 그래프에 데이터 점들 붙이기
graph.addSeries(series);
// x축의 연장 가능성 true
graph.getViewport().setXAxisBoundsManual(true);
// 데이터가 늘어날때 x축의 scroll 이 생기도록 설정
graph.getViewport().setScrollable(true);
// 데이터가 늘어날때 y축의 scroll 이 생겨지도록 설정
graph.getViewport().setScrollableY(true);
}
class NetworkThread extends Thread{
@Override
public void run() {
try {
HashMap<String, String> rgParams = new HashMap<String, String>();
rgParams = new HashMap<String, String>();
rgParams.put("order_currency", "BTC");
rgParams.put("payment_currency", "KRW");
//bithumb 거래소 거래 체결 완료 내역 요청하기
String result = api.callApi("/public/recent_transactions/BTC?offset=0&count=" + 20, rgParams);
JSONObject obj = new JSONObject(result);
String status = obj.getString("status");
JSONArray data_list = obj.getJSONArray("data");
for (int i = 0; i < data_list.length(); i++) {
JSONObject data_list_obj = data_list.getJSONObject(i);
String transaction_date = data_list_obj.getString("transaction_date");
final int price = Integer.parseInt(data_list_obj.getString("price"));
final int total = Integer.parseInt(data_list_obj.getString("total"));
price_list.add(price);
total_list.add(total);
}
drawLineGraph();
} catch (Exception e) {
e.printStackTrace();
}
}
}
이게 전체코드고
onCreate에 있는 graph를 drawLineGraph에 넘겨야하는데
어떻게 넘겨야할지 모르겠음
실시간 그래프 그리는건데
Thread에서 돌고 있는거 아님?
try { // 이 함수 이전에 안된다면 디버깅을 해봐야지 알듯... drawLineGraph(); } catch (Exception e) { e.printStackTrace(); }
drawLineGraph의 graph가 빨간글씨임 graph가 onCreate에만 선언되어있어서 일단 이 graph를 넘겨야 테스트가 가능한데
밑에 소스처럼 전역으로 빼면 안됨?
List total_list = new ArrayList(); public GraphView graph; // 전역변수 , 기억이 잘안나네 갑자기 멤버변수? public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); graph=(GraphView)findViewById(R.id.graph); NetworkThread thread=new NetworkThread();thread.start(); }
아 일단 그래프뷰는 해결한거같은데 그래프가 뻑난다 값 받아와서 그려야하는데
http://jjoe64.github.io/GraphView/javadoc/com/jjoe64/graphview/series/LineGraphSeries.html
Class
LineGraphSeries
LineGraphSeries series=new LineGraphSeries<>(pricePoints);
Type을 넣어주어야 하지 않겠냐만, 자바랑 c#이랑 햇깔려서 내가 옳은소리로 하는건지 모르겠네,
LineGraphSeries series=new LineGraphSeries(pricePoints);
LineGraphSeries series=new LineGraphSeries < DataPoint > (pricePoints);
그래프 뻑나는건 api 문제인듯 api좀 바꿔야겠다
과제야?
텀프로젝트