Wed, 07 Oct 2015 10:01:40 -0700
Merge
aoqi@0 | 1 | #// Usage: jjs -fx barchart_weather.js |
aoqi@0 | 2 | |
aoqi@0 | 3 | /* |
aoqi@0 | 4 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
attila@962 | 5 | * |
aoqi@0 | 6 | * Redistribution and use in source and binary forms, with or without |
aoqi@0 | 7 | * modification, are permitted provided that the following conditions |
aoqi@0 | 8 | * are met: |
attila@962 | 9 | * |
aoqi@0 | 10 | * - Redistributions of source code must retain the above copyright |
aoqi@0 | 11 | * notice, this list of conditions and the following disclaimer. |
attila@962 | 12 | * |
aoqi@0 | 13 | * - Redistributions in binary form must reproduce the above copyright |
aoqi@0 | 14 | * notice, this list of conditions and the following disclaimer in the |
aoqi@0 | 15 | * documentation and/or other materials provided with the distribution. |
attila@962 | 16 | * |
aoqi@0 | 17 | * - Neither the name of Oracle nor the names of its |
aoqi@0 | 18 | * contributors may be used to endorse or promote products derived |
aoqi@0 | 19 | * from this software without specific prior written permission. |
attila@962 | 20 | * |
aoqi@0 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
aoqi@0 | 22 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 23 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
aoqi@0 | 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
aoqi@0 | 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
aoqi@0 | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
aoqi@0 | 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
aoqi@0 | 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
aoqi@0 | 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
aoqi@0 | 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
aoqi@0 | 32 | */ |
aoqi@0 | 33 | |
aoqi@0 | 34 | // Example that retrieves weather data from a URL in JSON |
aoqi@0 | 35 | // format and draws bar chart using JavaFX |
aoqi@0 | 36 | |
aoqi@0 | 37 | // -fx check |
aoqi@0 | 38 | if (! $OPTIONS._fx) { |
aoqi@0 | 39 | print("Usage: jjs -fx barchart_weather.js"); |
aoqi@0 | 40 | exit(1); |
aoqi@0 | 41 | } |
aoqi@0 | 42 | |
aoqi@0 | 43 | // Java classes used |
aoqi@0 | 44 | var URL = Java.type("java.net.URL"); |
aoqi@0 | 45 | var BufferedReader = Java.type("java.io.BufferedReader"); |
aoqi@0 | 46 | var InputStreamReader = Java.type("java.io.InputStreamReader"); |
aoqi@0 | 47 | |
aoqi@0 | 48 | // function to retrieve text content of the given URL |
aoqi@0 | 49 | function readTextFromURL(url) { |
aoqi@0 | 50 | var str = ''; |
aoqi@0 | 51 | var u = new URL(url); |
aoqi@0 | 52 | var reader = new BufferedReader( |
aoqi@0 | 53 | new InputStreamReader(u.openStream())); |
aoqi@0 | 54 | try { |
aoqi@0 | 55 | reader.lines().forEach(function(x) str += x); |
aoqi@0 | 56 | return str; |
aoqi@0 | 57 | } finally { |
aoqi@0 | 58 | reader.close(); |
aoqi@0 | 59 | } |
aoqi@0 | 60 | } |
aoqi@0 | 61 | |
aoqi@0 | 62 | // change URL for your city here! |
aoqi@0 | 63 | var url = "http://api.openweathermap.org/data/2.5/forecast?q=chennai,india&units=metric&mode=json"; |
aoqi@0 | 64 | |
aoqi@0 | 65 | // download JSON document and parse |
aoqi@0 | 66 | var json = readTextFromURL(url); |
aoqi@0 | 67 | var weather = JSON.parse(json); |
aoqi@0 | 68 | |
aoqi@0 | 69 | // View JSON of this using site such as http://www.jsoneditoronline.org/ to know |
aoqi@0 | 70 | // about the JSON data format used by this site |
aoqi@0 | 71 | |
aoqi@0 | 72 | // Extracted data from the json object |
aoqi@0 | 73 | var temp = weather.list.map(function(x) x.main.temp); |
aoqi@0 | 74 | var temp_min = weather.list.map(function(x) x.main.temp_min); |
aoqi@0 | 75 | var temp_max = weather.list.map(function(x) x.main.temp_max); |
aoqi@0 | 76 | var date = weather.list.map(function(x) x.dt_txt); |
aoqi@0 | 77 | |
aoqi@0 | 78 | // JavaFX classes used |
aoqi@0 | 79 | var Scene = Java.type("javafx.scene.Scene"); |
aoqi@0 | 80 | var BarChart = Java.type("javafx.scene.chart.BarChart"); |
aoqi@0 | 81 | var CategoryAxis = Java.type("javafx.scene.chart.CategoryAxis"); |
aoqi@0 | 82 | var NumberAxis = Java.type("javafx.scene.chart.NumberAxis"); |
aoqi@0 | 83 | var XYChart = Java.type("javafx.scene.chart.XYChart"); |
aoqi@0 | 84 | |
aoqi@0 | 85 | function start(stage) { |
aoqi@0 | 86 | stage.title="Chennai Weather Bar Chart"; |
aoqi@0 | 87 | var xAxis = new CategoryAxis(); |
aoqi@0 | 88 | xAxis.label = "date/time"; |
aoqi@0 | 89 | var yAxis = new NumberAxis(); |
aoqi@0 | 90 | yAxis.label = "temp in C"; |
aoqi@0 | 91 | var bc = new BarChart(xAxis, yAxis); |
aoqi@0 | 92 | |
aoqi@0 | 93 | // 3 bars per datetime item - temp, min temp and max temp |
aoqi@0 | 94 | var s1 = new XYChart.Series(); |
aoqi@0 | 95 | s1.name = "temp"; |
aoqi@0 | 96 | for (d in date) { |
aoqi@0 | 97 | s1.data.add(new XYChart.Data(date[d], temp[d])); |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | var s2 = new XYChart.Series(); |
aoqi@0 | 101 | s2.name = "min temp"; |
aoqi@0 | 102 | for (d in date) { |
aoqi@0 | 103 | s2.data.add(new XYChart.Data(date[d], temp_min[d])); |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | var s3 = new XYChart.Series(); |
aoqi@0 | 107 | s3.name = "max temp"; |
aoqi@0 | 108 | for (d in date) { |
aoqi@0 | 109 | s3.data.add(new XYChart.Data(date[d], temp_max[d])); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | bc.data.addAll(s1, s2, s3); |
aoqi@0 | 113 | |
aoqi@0 | 114 | stage.scene = new Scene(bc, 800, 600); |
aoqi@0 | 115 | stage.show(); |
aoqi@0 | 116 | } |