##// END OF EJS Templates
Fancy background to QML Weather demo
Tero Ahola -
r1959:cd9047668198
parent child
Show More
@@ -1,182 +1,189
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 import QtQuick 1.1
21 import QtQuick 1.1
22 import QtCommercial.Chart 1.1
22 import QtCommercial.Chart 1.1
23
23
24 Rectangle {
24 Rectangle {
25 width: 360
25 width: 360
26 height: 360
26 height: 360
27 gradient: Gradient {
28 GradientStop { position: 0.0; color: "lightblue" }
29 GradientStop { position: 1.0; color: "white" }
30 }
27
31
28 //![1]
32 //![1]
29 ChartView {
33 ChartView {
30 id: chartView
34 id: chartView
31 title: "Weather forecast"
35 title: "Weather forecast"
32 //![1]
36 //![1]
33 anchors.top: parent.top
37 anchors.top: parent.top
34 anchors.bottom: weatherImageRow.top
38 anchors.bottom: weatherImageRow.top
35 anchors.left: parent.left
39 anchors.left: parent.left
36 anchors.right: parent.right
40 anchors.right: parent.right
37 legend.alignment: Qt.AlignTop
41 legend.alignment: Qt.AlignTop
38
42
39 //![2]
43 //![2]
40 BarCategoriesAxis {
44 BarCategoriesAxis {
41 id: barCategoriesAxis
45 id: barCategoriesAxis
42 }
46 }
43
47
44 BarSeries {
48 BarSeries {
45 id: myBarSeries
49 id: myBarSeries
46 axisX: barCategoriesAxis
50 axisX: barCategoriesAxis
47 axisY: valueAxisY
51 axisY: valueAxisY
48 BarSet {
52 BarSet {
49 id: rainfallSet
53 id: rainfallSet
50 label: "Rainfall"
54 label: "Rainfall"
51 }
55 }
52 }
56 }
53
57
54 ValueAxis {
58 ValueAxis {
55 id: valueAxisX
59 id: valueAxisX
56 min: 0
60 min: 0
57 max: 5
61 max: 5
58 }
62 }
59
63
60 ValueAxis{
64 ValueAxis{
61 id: valueAxisY
65 id: valueAxisY
62 min: 0
66 min: 0
63 max: 10
67 max: 10
64 }
68 }
65
69
66 LineSeries {
70 LineSeries {
67 id: maxTempSeries
71 id: maxTempSeries
68 axisX: valueAxisX
72 axisX: valueAxisX
69 axisY: valueAxisY
73 axisY: valueAxisY
70 name: "Max. temperature"
74 name: "Max. temperature"
71 }
75 }
72
76
73 LineSeries {
77 LineSeries {
74 id: minTempSeries
78 id: minTempSeries
75 axisX: valueAxisX
79 axisX: valueAxisX
76 axisY: valueAxisY
80 axisY: valueAxisY
77 name: "Min. temperature"
81 name: "Min. temperature"
78 }
82 }
79 //![2]
83 //![2]
80 }
84 }
81
85
82 // A timer to refresh the forecast every 5 minutes
86 // A timer to refresh the forecast every 5 minutes
83 Timer {
87 Timer {
84 interval: 300000
88 interval: 300000
85 repeat: true
89 repeat: true
86 triggeredOnStart: true
90 triggeredOnStart: true
87 running: true
91 running: true
88 onTriggered: {
92 onTriggered: {
89 if (weatherAppKey != "") {
93 if (weatherAppKey != "") {
90 //![3]
94 //![3]
91 // Make HTTP GET request and parse the result
95 // Make HTTP GET request and parse the result
92 var xhr = new XMLHttpRequest;
96 var xhr = new XMLHttpRequest;
93 xhr.open("GET",
97 xhr.open("GET",
94 "http://free.worldweatheronline.com/feed/weather.ashx?q=Jyv%c3%a4skyl%c3%a4,Finland&format=json&num_of_days=5&key="
98 "http://free.worldweatheronline.com/feed/weather.ashx?q=Jyv%c3%a4skyl%c3%a4,Finland&format=json&num_of_days=5&key="
95 + weatherAppKey);
99 + weatherAppKey);
96 xhr.onreadystatechange = function() {
100 xhr.onreadystatechange = function() {
97 if (xhr.readyState == XMLHttpRequest.DONE) {
101 if (xhr.readyState == XMLHttpRequest.DONE) {
98 var a = JSON.parse(xhr.responseText);
102 var a = JSON.parse(xhr.responseText);
99 parseWeatherData(a);
103 parseWeatherData(a);
100 }
104 }
101 }
105 }
102 xhr.send();
106 xhr.send();
103 //![3]
107 //![3]
104 } else {
108 } else {
105 // No app key for worldweatheronline.com given by the user -> use dummy static data
109 // No app key for worldweatheronline.com given by the user -> use dummy static data
106 var responseText = "{ \"data\": { \"current_condition\": [ {\"cloudcover\": \"10\", \"humidity\": \"61\", \"observation_time\": \"06:26 AM\", \"precipMM\": \"0.0\", \"pressure\": \"1022\", \"temp_C\": \"6\", \"temp_F\": \"43\", \"visibility\": \"10\", \"weatherCode\": \"113\", \"weatherDesc\": [ {\"value\": \"Sunny\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png\" } ], \"winddir16Point\": \"SE\", \"winddirDegree\": \"140\", \"windspeedKmph\": \"7\", \"windspeedMiles\": \"4\" } ], \"request\": [ {\"query\": \"Jyvaskyla, Finland\", \"type\": \"City\" } ], \"weather\": [ {\"date\": \"2012-05-09\", \"precipMM\": \"0.4\", \"tempMaxC\": \"14\", \"tempMaxF\": \"57\", \"tempMinC\": \"7\", \"tempMinF\": \"45\", \"weatherCode\": \"116\", \"weatherDesc\": [ {\"value\": \"Partly Cloudy\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png\" } ], \"winddir16Point\": \"S\", \"winddirDegree\": \"179\", \"winddirection\": \"S\", \"windspeedKmph\": \"20\", \"windspeedMiles\": \"12\" }, {\"date\": \"2012-05-10\", \"precipMM\": \"2.4\", \"tempMaxC\": \"13\", \"tempMaxF\": \"55\", \"tempMinC\": \"8\", \"tempMinF\": \"46\", \"weatherCode\": \"266\", \"weatherDesc\": [ {\"value\": \"Light drizzle\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0017_cloudy_with_light_rain.png\" } ], \"winddir16Point\": \"SW\", \"winddirDegree\": \"219\", \"winddirection\": \"SW\", \"windspeedKmph\": \"21\", \"windspeedMiles\": \"13\" }, {\"date\": \"2012-05-11\", \"precipMM\": \"11.1\", \"tempMaxC\": \"15\", \"tempMaxF\": \"59\", \"tempMinC\": \"7\", \"tempMinF\": \"44\", \"weatherCode\": \"266\", \"weatherDesc\": [ {\"value\": \"Light drizzle\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0017_cloudy_with_light_rain.png\" } ], \"winddir16Point\": \"SSW\", \"winddirDegree\": \"200\", \"winddirection\": \"SSW\", \"windspeedKmph\": \"20\", \"windspeedMiles\": \"12\" }, {\"date\": \"2012-05-12\", \"precipMM\": \"2.8\", \"tempMaxC\": \"7\", \"tempMaxF\": \"44\", \"tempMinC\": \"2\", \"tempMinF\": \"35\", \"weatherCode\": \"317\", \"weatherDesc\": [ {\"value\": \"Light sleet\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0021_cloudy_with_sleet.png\" } ], \"winddir16Point\": \"NW\", \"winddirDegree\": \"311\", \"winddirection\": \"NW\", \"windspeedKmph\": \"24\", \"windspeedMiles\": \"15\" }, {\"date\": \"2012-05-13\", \"precipMM\": \"0.4\", \"tempMaxC\": \"6\", \"tempMaxF\": \"42\", \"tempMinC\": \"2\", \"tempMinF\": \"35\", \"weatherCode\": \"116\", \"weatherDesc\": [ {\"value\": \"Partly Cloudy\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png\" } ], \"winddir16Point\": \"WNW\", \"winddirDegree\": \"281\", \"winddirection\": \"WNW\", \"windspeedKmph\": \"21\", \"windspeedMiles\": \"13\" } ] }}";
110 var responseText = "{ \"data\": { \"current_condition\": [ {\"cloudcover\": \"10\", \"humidity\": \"61\", \"observation_time\": \"06:26 AM\", \"precipMM\": \"0.0\", \"pressure\": \"1022\", \"temp_C\": \"6\", \"temp_F\": \"43\", \"visibility\": \"10\", \"weatherCode\": \"113\", \"weatherDesc\": [ {\"value\": \"Sunny\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png\" } ], \"winddir16Point\": \"SE\", \"winddirDegree\": \"140\", \"windspeedKmph\": \"7\", \"windspeedMiles\": \"4\" } ], \"request\": [ {\"query\": \"Jyvaskyla, Finland\", \"type\": \"City\" } ], \"weather\": [ {\"date\": \"2012-05-09\", \"precipMM\": \"0.4\", \"tempMaxC\": \"14\", \"tempMaxF\": \"57\", \"tempMinC\": \"7\", \"tempMinF\": \"45\", \"weatherCode\": \"116\", \"weatherDesc\": [ {\"value\": \"Partly Cloudy\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png\" } ], \"winddir16Point\": \"S\", \"winddirDegree\": \"179\", \"winddirection\": \"S\", \"windspeedKmph\": \"20\", \"windspeedMiles\": \"12\" }, {\"date\": \"2012-05-10\", \"precipMM\": \"2.4\", \"tempMaxC\": \"13\", \"tempMaxF\": \"55\", \"tempMinC\": \"8\", \"tempMinF\": \"46\", \"weatherCode\": \"266\", \"weatherDesc\": [ {\"value\": \"Light drizzle\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0017_cloudy_with_light_rain.png\" } ], \"winddir16Point\": \"SW\", \"winddirDegree\": \"219\", \"winddirection\": \"SW\", \"windspeedKmph\": \"21\", \"windspeedMiles\": \"13\" }, {\"date\": \"2012-05-11\", \"precipMM\": \"11.1\", \"tempMaxC\": \"15\", \"tempMaxF\": \"59\", \"tempMinC\": \"7\", \"tempMinF\": \"44\", \"weatherCode\": \"266\", \"weatherDesc\": [ {\"value\": \"Light drizzle\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0017_cloudy_with_light_rain.png\" } ], \"winddir16Point\": \"SSW\", \"winddirDegree\": \"200\", \"winddirection\": \"SSW\", \"windspeedKmph\": \"20\", \"windspeedMiles\": \"12\" }, {\"date\": \"2012-05-12\", \"precipMM\": \"2.8\", \"tempMaxC\": \"7\", \"tempMaxF\": \"44\", \"tempMinC\": \"2\", \"tempMinF\": \"35\", \"weatherCode\": \"317\", \"weatherDesc\": [ {\"value\": \"Light sleet\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0021_cloudy_with_sleet.png\" } ], \"winddir16Point\": \"NW\", \"winddirDegree\": \"311\", \"winddirection\": \"NW\", \"windspeedKmph\": \"24\", \"windspeedMiles\": \"15\" }, {\"date\": \"2012-05-13\", \"precipMM\": \"0.4\", \"tempMaxC\": \"6\", \"tempMaxF\": \"42\", \"tempMinC\": \"2\", \"tempMinF\": \"35\", \"weatherCode\": \"116\", \"weatherDesc\": [ {\"value\": \"Partly Cloudy\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png\" } ], \"winddir16Point\": \"WNW\", \"winddirDegree\": \"281\", \"winddirection\": \"WNW\", \"windspeedKmph\": \"21\", \"windspeedMiles\": \"13\" } ] }}";
107 var a = JSON.parse(responseText);
111 var a = JSON.parse(responseText);
108 parseWeatherData(a);
112 parseWeatherData(a);
109 }
113 }
110 }
114 }
111 }
115 }
112
116
113 Row {
117 Row {
114 id: weatherImageRow
118 id: weatherImageRow
115 anchors.bottom: poweredByText.top
119 anchors.bottom: poweredByText.top
116 anchors.bottomMargin: 10
120 anchors.bottomMargin: 10
117 anchors.left: parent.left
121 anchors.left: parent.left
118 anchors.leftMargin: 25
122 anchors.leftMargin: 25
119 anchors.right: parent.right
123 anchors.right: parent.right
120 anchors.rightMargin: 25
124 anchors.rightMargin: 25
121
125
122 ListModel {
126 ListModel {
123 id: weatherImageModel
127 id: weatherImageModel
124 }
128 }
125
129
126 Repeater {
130 Repeater {
127 id: repeater
131 id: repeater
128 model: weatherImageModel
132 model: weatherImageModel
129 delegate: Image {
133 delegate: Image {
130 source: imageSource
134 source: imageSource
131 width: weatherImageRow.width / weatherImageModel.count
135 width: weatherImageRow.width / weatherImageModel.count
132 height: width
136 height: width
133 fillMode: Image.PreserveAspectCrop
137 fillMode: Image.PreserveAspectCrop
134 }
138 }
135 }
139 }
136 }
140 }
137
141
138 Text {
142 Text {
139 id: poweredByText
143 id: poweredByText
140 anchors.bottom: parent.bottom
144 anchors.bottom: parent.bottom
141 anchors.bottomMargin: 10
145 anchors.bottomMargin: 10
142 anchors.left: parent.left
146 anchors.left: parent.left
143 anchors.leftMargin: 25
147 anchors.leftMargin: 25
144 text: "Powered by World Weather Online"
148 text: "Powered by World Weather Online"
145 }
149 }
146
150
147 function parseWeatherData(weatherData) {
151 function parseWeatherData(weatherData) {
148 // Clear previous values
152 // Clear previous values
149 maxTempSeries.clear();
153 maxTempSeries.clear();
150 minTempSeries.clear();
154 minTempSeries.clear();
151 weatherImageModel.clear();
155 weatherImageModel.clear();
152
156
153 //![4]
157 //![4]
154 // Loop through the parsed JSON
158 // Loop through the parsed JSON
155 for (var i in weatherData.data.weather) {
159 for (var i in weatherData.data.weather) {
156 var weatherObj = weatherData.data.weather[i];
160 var weatherObj = weatherData.data.weather[i];
157 //![4]
161 //![4]
158
162
159 //![5]
163 //![5]
160 // Store temperature values, rainfall and weather icon
164 // Store temperature values, rainfall and weather icon.
165 // The temperature values begin from 0.5 instead of 0.0 to make the start from the
166 // middle of the rainfall bars. This makes the temperature lines visually better
167 // synchronized with the rainfall bars.
161 maxTempSeries.append(Number(i) + 0.5, weatherObj.tempMaxC);
168 maxTempSeries.append(Number(i) + 0.5, weatherObj.tempMaxC);
162 minTempSeries.append(Number(i) + 0.5, weatherObj.tempMinC);
169 minTempSeries.append(Number(i) + 0.5, weatherObj.tempMinC);
163 rainfallSet.append(i, weatherObj.precipMM);
170 rainfallSet.append(i, weatherObj.precipMM);
164 weatherImageModel.append({"imageSource":weatherObj.weatherIconUrl[0].value});
171 weatherImageModel.append({"imageSource":weatherObj.weatherIconUrl[0].value});
165 //![5]
172 //![5]
166
173
167 // Update scale of the chart
174 // Update scale of the chart
168 valueAxisY.max = Math.max(chartView.axisY().max,weatherObj.tempMaxC);
175 valueAxisY.max = Math.max(chartView.axisY().max,weatherObj.tempMaxC);
169 valueAxisX.min = 0;
176 valueAxisX.min = 0;
170 valueAxisX.max = Number(i) + 1;
177 valueAxisX.max = Number(i) + 1;
171
178
172 // Set the x-axis labels to the dates of the forecast
179 // Set the x-axis labels to the dates of the forecast
173 var xLabels = barCategoriesAxis.categories;
180 var xLabels = barCategoriesAxis.categories;
174 xLabels[Number(i)] = weatherObj.date.substring(5, 10);
181 xLabels[Number(i)] = weatherObj.date.substring(5, 10);
175 barCategoriesAxis.categories = xLabels;
182 barCategoriesAxis.categories = xLabels;
176 barCategoriesAxis.visible = true;
183 barCategoriesAxis.visible = true;
177 barCategoriesAxis.min = 0;
184 barCategoriesAxis.min = 0;
178 barCategoriesAxis.max = xLabels.length - 1;
185 barCategoriesAxis.max = xLabels.length - 1;
179 }
186 }
180 }
187 }
181
188
182 }
189 }
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
General Comments 0
You need to be logged in to leave comments. Login now