Visualizing Flu Activity With Javafx

Salah satu fitur baru (kelas) di javafx 1.2 adalah chart, sebagai latihan saya membuat sebuah chart untuk aktivitas flu (influenza) di Australia, yang datanya disediakan oleh Google.org. Berdasakan penelitian dari google.org, jumlah query tertentu yang berkaitan dengan flu dari suatu wilayah tertentu dengan jumlah persebaran penyakit flu di daerah tersebut ternyata ada kesesuaian, oleh karena itu, jumlah kueri ini digunakan untuk memprediksi persebaran virus flu di suatu wilayah/negara tertentu.

ini screenshotnya:
flutrend

ada dua bagian utama dari source code ini, yang pertama adalah pengambilan data dari server google

var fludataRequest = HttpRequest {
location: “http://www.google.org/flutrends/intl/en_au/data.txt”;
onInput: function(stream: InputStream) {
try{
var buff = new BufferedReader(new InputStreamReader(stream));
var i:Integer;
while (++i<11) {buff.readLine();} i=1; while((line = buff.readLine())!= null){ //insert line into ldata; } buff.close(); }finally{ stream.close(); } } onDone: insertData } [/sourcecode] Saya menggunakan HttpRequest untuk menerima dari server. disini tidak dilakukan proses parsing dengan PullParser karena data yang disediakan oleh google.org berupa file text dalam format csv (comma separated value). data yang berupa csv ini kemudian diolah langsung dan di tampilkan ke dalam sebuah chart seperti di atas. kode selengkapnya adalah seperti ini (kelihatan panjang, karena memang setiap variabel di deskripsikan sendiri, biar lebih clean code-nya) [sourcecode language='java'] /* * AustraliaFluActivity.fx * * Created on 05 Jun 09, 18:58:54 */ package testareafx; import javafx.io.http.HttpRequest; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.chart.*; import javafx.scene.chart.part.*; import javafx.scene.text.Text; import javafx.scene.text.Font; import javafx.ext.swing.SwingComboBox; import javafx.ext.swing.SwingComboBoxItem; /** * @author Muhammad Hakim A */ def info = Text { font : Font { size: 12 } x: 20, y: 430 content: "Date: Percentage: " } var idx:Integer = 1; var nationaldata:LineChart.Data[]; var australianCapitaldata:LineChart.Data[]; var newSouthWalesdata:LineChart.Data[]; var queenslanddata:LineChart.Data[]; var southAustraliadata:LineChart.Data[]; var victoriadata:LineChart.Data[]; var westernAustraliadata:LineChart.Data[]; var line:String = "" on replace{ if (line != ""){ var data:String[] =line.split(","); //get date var date = data[0]; // national var nationalData = LineChart.Data{ xValue: idx; yValue: Float.parseFloat(data[1])*100; action: function(){ info.content = "Date: {data[0]} : ({data[1]})"; } } insert nationalData into nationaldata; // capital var australianCapitalData = LineChart.Data{ xValue: idx; yValue: Float.parseFloat(data[2])*100; action: function(){ info.content = "Date: {data[0]} : ({data[2]})"; } } insert australianCapitalData into australianCapitaldata; // new south wales var newSouthWalesData = LineChart.Data{ xValue: idx; yValue: Float.parseFloat(data[3])*100; action: function(){ info.content = "Date: {data[0]} : ({data[3]})"; } } insert newSouthWalesData into newSouthWalesdata; // queensland var queenslandData = LineChart.Data{ xValue: idx; yValue: Float.parseFloat(data[4])*100; action: function(){ info.content = "Date: {data[0]} : ({data[4]})"; } } insert queenslandData into queenslanddata; // south australia var southAustraliaData = LineChart.Data{ xValue: idx; yValue: Float.parseFloat(data[5])*100; action: function(){ info.content = "Date: {data[0]} : ({data[5]})"; } } insert southAustraliaData into southAustraliadata; // victoria var victoriaData = LineChart.Data{ xValue: idx; yValue: Float.parseFloat(data[6])*100; action: function(){ info.content = "Date: {data[0]} : ({data[6]})"; } } insert victoriaData into victoriadata; // western australie var westernAustraliaData = LineChart.Data{ xValue: idx; yValue: Float.parseFloat(data[7])*100; action: function(){ info.content = "Date: {data[0]} : ({data[7]})"; } } insert westernAustraliaData into westernAustraliadata; idx++; } }; var fludataRequest = HttpRequest { location: "http://www.google.org/flutrends/intl/en_au/data.txt"; onInput: function(stream: InputStream) { try{ var buff = new BufferedReader(new InputStreamReader(stream)); var i:Integer; while (++i<11) {buff.readLine();} i=1; while((line = buff.readLine())!= null){ //insert line into ldata; } buff.close(); }finally{ stream.close(); } } onDone: insertData } function insertData():Void{ // insert data into series insert nationaldata into national.data; insert australianCapitaldata into australianCapital.data; insert newSouthWalesdata into newSouthWales.data; insert queenslanddata into queensland.data; insert southAustraliadata into southAustralia.data; insert victoriadata into victoria.data; insert westernAustraliadata into westernAustralia.data; } def xAxis = NumberAxis{ lowerBound: 0 upperBound : 48 label: "Week" visible: true axisStrokeWidth: 1 tickUnit : 4 tickLabelsVisible: true minorTickVisible: false } def yAxis = NumberAxis{ lowerBound: 0 upperBound : 100 label: "Percent (0.01)" visible: false axisStrokeWidth: 1 tickUnit : 10 tickLabelsVisible: false minorTickVisible: false tickMarkVisible: false } var national:LineChart.Series = LineChart.Series{ name: "Australia" }; var australianCapital:LineChart.Series = LineChart.Series{ name: "Australian Capital Territory" }; var newSouthWales:LineChart.Series = LineChart.Series{ name: "New South Wales" }; var queensland:LineChart.Series = LineChart.Series{ name: "Queensland" }; var southAustralia:LineChart.Series = LineChart.Series{ name: "South Australia" }; var victoria:LineChart.Series = LineChart.Series{ name: "Victoria" }; var westernAustralia:LineChart.Series = LineChart.Series{ name: "Western Australia" }; def chart = LineChart{ title: "Australia Flu Activity 2009"; cache: true xAxis: xAxis yAxis: yAxis data: [national/*, newSouthWales, queensland, southAustralia, victoria, westernAustralia, australianCapital*/]; } var selectedIndex:Integer = 0 on replace{ delete chart.data; if (selectedIndex == 0){ chart.data = national; }else if (selectedIndex == 1){ chart.data = australianCapital; }else if (selectedIndex == 2){ chart.data = newSouthWales; }else if (selectedIndex == 3){ chart.data = queensland; }else if (selectedIndex == 4){ chart.data = southAustralia; }else if (selectedIndex == 5){ chart.data = victoria; }else if (selectedIndex == 6){ chart.data = westernAustralia; }else{ chart.data = national; } } def cmbx:SwingComboBox = SwingComboBox { translateX: 350 translateY: 40 editable: false; selectedIndex: bind selectedIndex with inverse; items: [ SwingComboBoxItem { text: "Australia" selected: true } SwingComboBoxItem { text: "Australian Capital Territory" selected: true } SwingComboBoxItem { text: "New South Wales" selected: true }, SwingComboBoxItem { text: "Queensland" selected: true }, SwingComboBoxItem { text: "Southern Australia" selected: true }, SwingComboBoxItem { text: "Victoria" selected: true }, SwingComboBoxItem { text: "Western Australia" selected: true } ] } def stage:Stage = Stage { title : "Google Flu Trend" scene: Scene { width: 800 height: 500 content: [cmbx,chart,info] } } public function run(){ fludataRequest.start(); stage.visible = true; } [/sourcecode]

One thought on “Visualizing Flu Activity With Javafx

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s