aoqi@0: #// Usage: jjs -fx barchart_weather.js aoqi@0: aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. attila@962: * aoqi@0: * Redistribution and use in source and binary forms, with or without aoqi@0: * modification, are permitted provided that the following conditions aoqi@0: * are met: attila@962: * aoqi@0: * - Redistributions of source code must retain the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer. attila@962: * aoqi@0: * - Redistributions in binary form must reproduce the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer in the aoqi@0: * documentation and/or other materials provided with the distribution. attila@962: * aoqi@0: * - Neither the name of Oracle nor the names of its aoqi@0: * contributors may be used to endorse or promote products derived aoqi@0: * from this software without specific prior written permission. attila@962: * aoqi@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS aoqi@0: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, aoqi@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR aoqi@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR aoqi@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, aoqi@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, aoqi@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR aoqi@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF aoqi@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING aoqi@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS aoqi@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. aoqi@0: */ aoqi@0: aoqi@0: // Example that retrieves weather data from a URL in JSON aoqi@0: // format and draws bar chart using JavaFX aoqi@0: aoqi@0: // -fx check aoqi@0: if (! $OPTIONS._fx) { aoqi@0: print("Usage: jjs -fx barchart_weather.js"); aoqi@0: exit(1); aoqi@0: } aoqi@0: aoqi@0: // Java classes used aoqi@0: var URL = Java.type("java.net.URL"); aoqi@0: var BufferedReader = Java.type("java.io.BufferedReader"); aoqi@0: var InputStreamReader = Java.type("java.io.InputStreamReader"); aoqi@0: aoqi@0: // function to retrieve text content of the given URL aoqi@0: function readTextFromURL(url) { aoqi@0: var str = ''; aoqi@0: var u = new URL(url); aoqi@0: var reader = new BufferedReader( aoqi@0: new InputStreamReader(u.openStream())); aoqi@0: try { aoqi@0: reader.lines().forEach(function(x) str += x); aoqi@0: return str; aoqi@0: } finally { aoqi@0: reader.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // change URL for your city here! aoqi@0: var url = "http://api.openweathermap.org/data/2.5/forecast?q=chennai,india&units=metric&mode=json"; aoqi@0: aoqi@0: // download JSON document and parse aoqi@0: var json = readTextFromURL(url); aoqi@0: var weather = JSON.parse(json); aoqi@0: aoqi@0: // View JSON of this using site such as http://www.jsoneditoronline.org/ to know aoqi@0: // about the JSON data format used by this site aoqi@0: aoqi@0: // Extracted data from the json object aoqi@0: var temp = weather.list.map(function(x) x.main.temp); aoqi@0: var temp_min = weather.list.map(function(x) x.main.temp_min); aoqi@0: var temp_max = weather.list.map(function(x) x.main.temp_max); aoqi@0: var date = weather.list.map(function(x) x.dt_txt); aoqi@0: aoqi@0: // JavaFX classes used aoqi@0: var Scene = Java.type("javafx.scene.Scene"); aoqi@0: var BarChart = Java.type("javafx.scene.chart.BarChart"); aoqi@0: var CategoryAxis = Java.type("javafx.scene.chart.CategoryAxis"); aoqi@0: var NumberAxis = Java.type("javafx.scene.chart.NumberAxis"); aoqi@0: var XYChart = Java.type("javafx.scene.chart.XYChart"); aoqi@0: aoqi@0: function start(stage) { aoqi@0: stage.title="Chennai Weather Bar Chart"; aoqi@0: var xAxis = new CategoryAxis(); aoqi@0: xAxis.label = "date/time"; aoqi@0: var yAxis = new NumberAxis(); aoqi@0: yAxis.label = "temp in C"; aoqi@0: var bc = new BarChart(xAxis, yAxis); aoqi@0: aoqi@0: // 3 bars per datetime item - temp, min temp and max temp aoqi@0: var s1 = new XYChart.Series(); aoqi@0: s1.name = "temp"; aoqi@0: for (d in date) { aoqi@0: s1.data.add(new XYChart.Data(date[d], temp[d])); aoqi@0: } aoqi@0: aoqi@0: var s2 = new XYChart.Series(); aoqi@0: s2.name = "min temp"; aoqi@0: for (d in date) { aoqi@0: s2.data.add(new XYChart.Data(date[d], temp_min[d])); aoqi@0: } aoqi@0: aoqi@0: var s3 = new XYChart.Series(); aoqi@0: s3.name = "max temp"; aoqi@0: for (d in date) { aoqi@0: s3.data.add(new XYChart.Data(date[d], temp_max[d])); aoqi@0: } aoqi@0: aoqi@0: bc.data.addAll(s1, s2, s3); aoqi@0: aoqi@0: stage.scene = new Scene(bc, 800, 600); aoqi@0: stage.show(); aoqi@0: }