samples/barchart_weather.js

Fri, 05 Jun 2015 12:38:53 +0200

author
mhaupt
date
Fri, 05 Jun 2015 12:38:53 +0200
changeset 1398
2f1b9f4daec1
parent 962
ac62e33a99b0
permissions
-rw-r--r--

8080087: Nashorn $ENV.PWD is originally undefined
Summary: On Windows, the PWD environment variable does not exist and cannot be imported in scripting mode, so it is set explicitly.
Reviewed-by: lagergren, sundar

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

mercurial