Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/samples/barchart_weather.js
32278 views
#// Usage: jjs -fx barchart_weather.js12/*3* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* - Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* - Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* - Neither the name of Oracle nor the names of its17* contributors may be used to endorse or promote products derived18* from this software without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS21* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,22* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR23* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR24* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,25* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,26* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR27* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF28* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING29* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS30* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233// Example that retrieves weather data from a URL in JSON34// format and draws bar chart using JavaFX3536// -fx check37if (! $OPTIONS._fx) {38print("Usage: jjs -fx barchart_weather.js");39exit(1);40}4142// Java classes used43var URL = Java.type("java.net.URL");44var BufferedReader = Java.type("java.io.BufferedReader");45var InputStreamReader = Java.type("java.io.InputStreamReader");4647// function to retrieve text content of the given URL48function readTextFromURL(url) {49var str = '';50var u = new URL(url);51var reader = new BufferedReader(52new InputStreamReader(u.openStream()));53try {54reader.lines().forEach(function(x) str += x);55return str;56} finally {57reader.close();58}59}6061// change URL for your city here!62var url = "http://api.openweathermap.org/data/2.5/forecast?q=chennai,india&units=metric&mode=json";6364// download JSON document and parse65var json = readTextFromURL(url);66var weather = JSON.parse(json);6768// View JSON of this using site such as http://www.jsoneditoronline.org/ to know69// about the JSON data format used by this site7071// Extracted data from the json object72var temp = weather.list.map(function(x) x.main.temp);73var temp_min = weather.list.map(function(x) x.main.temp_min);74var temp_max = weather.list.map(function(x) x.main.temp_max);75var date = weather.list.map(function(x) x.dt_txt);7677// JavaFX classes used78var Scene = Java.type("javafx.scene.Scene");79var BarChart = Java.type("javafx.scene.chart.BarChart");80var CategoryAxis = Java.type("javafx.scene.chart.CategoryAxis");81var NumberAxis = Java.type("javafx.scene.chart.NumberAxis");82var XYChart = Java.type("javafx.scene.chart.XYChart");8384function start(stage) {85stage.title="Chennai Weather Bar Chart";86var xAxis = new CategoryAxis();87xAxis.label = "date/time";88var yAxis = new NumberAxis();89yAxis.label = "temp in C";90var bc = new BarChart(xAxis, yAxis);9192// 3 bars per datetime item - temp, min temp and max temp93var s1 = new XYChart.Series();94s1.name = "temp";95for (d in date) {96s1.data.add(new XYChart.Data(date[d], temp[d]));97}9899var s2 = new XYChart.Series();100s2.name = "min temp";101for (d in date) {102s2.data.add(new XYChart.Data(date[d], temp_min[d]));103}104105var s3 = new XYChart.Series();106s3.name = "max temp";107for (d in date) {108s3.data.add(new XYChart.Data(date[d], temp_max[d]));109}110111bc.data.addAll(s1, s2, s3);112113stage.scene = new Scene(bc, 800, 600);114stage.show();115}116117118