author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 45898 | nashorn/samples/barchart_weather.js@f66344672e6c |
permissions | -rw-r--r-- |
24283 | 1 |
#// Usage: jjs -fx barchart_weather.js |
2 |
||
3 |
/* |
|
4 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
5 |
* |
24283 | 6 |
* Redistribution and use in source and binary forms, with or without |
7 |
* modification, are permitted provided that the following conditions |
|
8 |
* are met: |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
9 |
* |
24283 | 10 |
* - Redistributions of source code must retain the above copyright |
11 |
* notice, this list of conditions and the following disclaimer. |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
12 |
* |
24283 | 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. |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
16 |
* |
24283 | 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. |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
20 |
* |
24283 | 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 |
*/ |
|
33 |
||
34 |
// Example that retrieves weather data from a URL in JSON |
|
35 |
// format and draws bar chart using JavaFX |
|
36 |
||
37 |
// -fx check |
|
38 |
if (! $OPTIONS._fx) { |
|
39 |
print("Usage: jjs -fx barchart_weather.js"); |
|
40 |
exit(1); |
|
41 |
} |
|
42 |
||
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"); |
|
47 |
||
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 |
} |
|
61 |
||
62 |
// change URL for your city here! |
|
45898 | 63 |
var url = "http://api.openweathermap.org/data/2.5/forecast?q=chennai,india&units=metric&mode=json&appid=9b2982987c080ae88d81f081dcb129e8"; |
24283 | 64 |
|
65 |
// download JSON document and parse |
|
66 |
var json = readTextFromURL(url); |
|
67 |
var weather = JSON.parse(json); |
|
68 |
||
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 |
|
71 |
||
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); |
|
77 |
||
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"); |
|
84 |
||
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); |
|
92 |
||
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 |
} |
|
99 |
||
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 |
} |
|
105 |
||
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 |
} |
|
111 |
||
112 |
bc.data.addAll(s1, s2, s3); |
|
113 |
||
114 |
stage.scene = new Scene(bc, 800, 600); |
|
115 |
stage.show(); |
|
116 |
} |