|
@@
-1,151
+1,159
|
|
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.0
|
|
22
|
import QtCommercial.Chart 1.0
|
|
23
|
|
|
23
|
|
|
24
|
Rectangle {
|
|
24
|
Rectangle {
|
|
25
|
width: 360
|
|
25
|
width: 360
|
|
26
|
height: 360
|
|
26
|
height: 360
|
|
27
|
|
|
27
|
|
|
|
|
|
28
|
//![1]
|
|
28
|
ChartView {
|
|
29
|
ChartView {
|
|
29
|
id: chartView
|
|
30
|
id: chartView
|
|
30
|
title: "Weather forecast"
|
|
31
|
title: "Weather forecast"
|
|
|
|
|
32
|
//![1]
|
|
31
|
anchors.top: parent.top
|
|
33
|
anchors.top: parent.top
|
|
32
|
anchors.bottom: weatherImageRow.top
|
|
34
|
anchors.bottom: weatherImageRow.top
|
|
33
|
anchors.left: parent.left
|
|
35
|
anchors.left: parent.left
|
|
34
|
anchors.right: parent.right
|
|
36
|
anchors.right: parent.right
|
|
35
|
legend: ChartView.LegendTop
|
|
37
|
legend: ChartView.LegendTop
|
|
36
|
|
|
38
|
|
|
|
|
|
39
|
//![2]
|
|
|
|
|
40
|
// BarSeries {
|
|
37
|
GroupedBarSeries {
|
|
41
|
GroupedBarSeries {
|
|
38
|
barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
|
|
42
|
barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
|
|
39
|
BarSet {
|
|
43
|
BarSet {
|
|
40
|
id: rainfallSet
|
|
44
|
id: rainfallSet
|
|
41
|
name: "Rainfall"
|
|
45
|
name: "Rainfall"
|
|
42
|
}
|
|
46
|
}
|
|
43
|
}
|
|
47
|
}
|
|
44
|
// BarSeries {
|
|
|
|
|
45
|
// barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
|
|
|
|
|
46
|
// BarSet {
|
|
|
|
|
47
|
// id: rainfallSet
|
|
|
|
|
48
|
// name: "Rainfall"
|
|
|
|
|
49
|
// }
|
|
|
|
|
50
|
// }
|
|
|
|
|
51
|
|
|
48
|
|
|
52
|
ScatterSeries {
|
|
49
|
ScatterSeries {
|
|
53
|
id: maxTempSeries
|
|
50
|
id: maxTempSeries
|
|
54
|
name: "Max. temperature"
|
|
51
|
name: "Max. temperature"
|
|
55
|
}
|
|
52
|
}
|
|
56
|
|
|
53
|
|
|
57
|
ScatterSeries {
|
|
54
|
ScatterSeries {
|
|
58
|
id: minTempSeries
|
|
55
|
id: minTempSeries
|
|
59
|
name: "Min. temperature"
|
|
56
|
name: "Min. temperature"
|
|
60
|
}
|
|
57
|
}
|
|
|
|
|
58
|
//![2]
|
|
61
|
}
|
|
59
|
}
|
|
62
|
|
|
60
|
|
|
63
|
// A timer to refresh the forecast every 5 minutes
|
|
61
|
// A timer to refresh the forecast every 5 minutes
|
|
64
|
Timer {
|
|
62
|
Timer {
|
|
65
|
interval: 300000;
|
|
63
|
interval: 300000;
|
|
66
|
repeat: true
|
|
64
|
repeat: true
|
|
67
|
triggeredOnStart: true
|
|
65
|
triggeredOnStart: true
|
|
68
|
running: true
|
|
66
|
running: true
|
|
69
|
onTriggered: {
|
|
67
|
onTriggered: {
|
|
70
|
if (weatherAppKey != "") {
|
|
68
|
if (weatherAppKey != "") {
|
|
|
|
|
69
|
//![3]
|
|
|
|
|
70
|
// Make HTTP GET request and parse the result
|
|
71
|
var xhr = new XMLHttpRequest;
|
|
71
|
var xhr = new XMLHttpRequest;
|
|
72
|
xhr.open("GET", "http://free.worldweatheronline.com/feed/weather.ashx?q=Jyv%c3%a4skyl%c3%a4,Finland&format=json&num_of_days=5&key=" + weatherAppKey);
|
|
72
|
xhr.open("GET",
|
|
|
|
|
73
|
"http://free.worldweatheronline.com/feed/weather.ashx?q=Jyv%c3%a4skyl%c3%a4,Finland&format=json&num_of_days=5&key="
|
|
|
|
|
74
|
+ weatherAppKey);
|
|
73
|
xhr.onreadystatechange = function() {
|
|
75
|
xhr.onreadystatechange = function() {
|
|
74
|
if (xhr.readyState == XMLHttpRequest.DONE) {
|
|
76
|
if (xhr.readyState == XMLHttpRequest.DONE) {
|
|
75
|
var a = JSON.parse(xhr.responseText);
|
|
77
|
var a = JSON.parse(xhr.responseText);
|
|
76
|
parseWeatherData(a);
|
|
78
|
parseWeatherData(a);
|
|
77
|
}
|
|
79
|
}
|
|
78
|
}
|
|
80
|
}
|
|
79
|
xhr.send();
|
|
81
|
xhr.send();
|
|
|
|
|
82
|
//![3]
|
|
80
|
} else {
|
|
83
|
} else {
|
|
81
|
// No app key for worldweatheronline.com given by the user -> use static data
|
|
84
|
// No app key for worldweatheronline.com given by the user -> use static data
|
|
82
|
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\" } ] }}";
|
|
85
|
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\" } ] }}";
|
|
83
|
var a = JSON.parse(responseText);
|
|
86
|
var a = JSON.parse(responseText);
|
|
84
|
parseWeatherData(a);
|
|
87
|
parseWeatherData(a);
|
|
85
|
}
|
|
88
|
}
|
|
86
|
}
|
|
89
|
}
|
|
87
|
}
|
|
90
|
}
|
|
88
|
|
|
91
|
|
|
89
|
Row {
|
|
92
|
Row {
|
|
90
|
id: weatherImageRow
|
|
93
|
id: weatherImageRow
|
|
91
|
anchors.bottom: parent.bottom
|
|
94
|
anchors.bottom: parent.bottom
|
|
92
|
anchors.bottomMargin: 10
|
|
95
|
anchors.bottomMargin: 10
|
|
93
|
anchors.left: parent.left
|
|
96
|
anchors.left: parent.left
|
|
94
|
anchors.leftMargin: 25
|
|
97
|
anchors.leftMargin: 25
|
|
95
|
anchors.right: parent.right
|
|
98
|
anchors.right: parent.right
|
|
96
|
anchors.rightMargin: 25
|
|
99
|
anchors.rightMargin: 25
|
|
97
|
|
|
100
|
|
|
98
|
ListModel {
|
|
101
|
ListModel {
|
|
99
|
id: weatherImageModel
|
|
102
|
id: weatherImageModel
|
|
100
|
}
|
|
103
|
}
|
|
101
|
|
|
104
|
|
|
102
|
Repeater {
|
|
105
|
Repeater {
|
|
103
|
id: repeater
|
|
106
|
id: repeater
|
|
104
|
model: weatherImageModel
|
|
107
|
model: weatherImageModel
|
|
105
|
delegate: Image {
|
|
108
|
delegate: Image {
|
|
106
|
source: imageSource
|
|
109
|
source: imageSource
|
|
107
|
width: weatherImageRow.width / weatherImageModel.count
|
|
110
|
width: weatherImageRow.width / weatherImageModel.count
|
|
108
|
height: width
|
|
111
|
height: width
|
|
109
|
fillMode: Image.PreserveAspectCrop
|
|
112
|
fillMode: Image.PreserveAspectCrop
|
|
110
|
}
|
|
113
|
}
|
|
111
|
}
|
|
114
|
}
|
|
112
|
}
|
|
115
|
}
|
|
113
|
|
|
116
|
|
|
114
|
function parseWeatherData(weatherData) {
|
|
117
|
function parseWeatherData(weatherData) {
|
|
115
|
// Clear previous values
|
|
118
|
// Clear previous values
|
|
116
|
chartView.axisX.min = 0;
|
|
119
|
chartView.axisX.min = 0;
|
|
117
|
chartView.axisX.max = 5;
|
|
120
|
chartView.axisX.max = 5;
|
|
118
|
chartView.axisY.min = 0;
|
|
121
|
chartView.axisY.min = 0;
|
|
119
|
chartView.axisY.max = 5;
|
|
122
|
chartView.axisY.max = 5;
|
|
120
|
maxTempSeries.clear();
|
|
123
|
maxTempSeries.clear();
|
|
121
|
minTempSeries.clear();
|
|
124
|
minTempSeries.clear();
|
|
122
|
weatherImageModel.clear();
|
|
125
|
weatherImageModel.clear();
|
|
123
|
|
|
126
|
|
|
|
|
|
127
|
//![4]
|
|
124
|
// Loop through the parsed JSON
|
|
128
|
// Loop through the parsed JSON
|
|
125
|
for (var i in weatherData.data.weather) {
|
|
129
|
for (var i in weatherData.data.weather) {
|
|
126
|
var weatherObj = weatherData.data.weather[i];
|
|
130
|
var weatherObj = weatherData.data.weather[i];
|
|
|
|
|
131
|
//![4]
|
|
127
|
|
|
132
|
|
|
|
|
|
133
|
//![5]
|
|
128
|
// Store temperature values, rainfall and weather icon
|
|
134
|
// Store temperature values, rainfall and weather icon
|
|
129
|
maxTempSeries.append(Number(i), weatherObj.tempMaxC);
|
|
135
|
maxTempSeries.append(i, weatherObj.tempMaxC);
|
|
130
|
minTempSeries.append(Number(i), weatherObj.tempMinC);
|
|
136
|
minTempSeries.append(i, weatherObj.tempMinC);
|
|
131
|
rainfallSet.append(Number(i), weatherObj.precipMM);
|
|
137
|
rainfallSet.append(i, weatherObj.precipMM);
|
|
|
|
|
138
|
|
|
132
|
weatherImageModel.append({"imageSource":weatherObj.weatherIconUrl[0].value});
|
|
139
|
weatherImageModel.append({"imageSource":weatherObj.weatherIconUrl[0].value});
|
|
|
|
|
140
|
//![5]
|
|
133
|
|
|
141
|
|
|
134
|
// Update scale of the chart
|
|
142
|
// Update scale of the chart
|
|
135
|
chartView.axisX.min = 0;
|
|
143
|
chartView.axisX.min = 0;
|
|
136
|
chartView.axisX.max = i;
|
|
144
|
chartView.axisX.max = i;
|
|
137
|
while (chartView.axisY.min >= Number(weatherObj.tempMinC))
|
|
145
|
while (chartView.axisY.min >= Number(weatherObj.tempMinC))
|
|
138
|
chartView.axisY.min = chartView.axisY.min - 10;
|
|
146
|
chartView.axisY.min = chartView.axisY.min - 10;
|
|
139
|
while (chartView.axisY.max <= Number(weatherObj.tempMaxC))
|
|
147
|
while (chartView.axisY.max <= Number(weatherObj.tempMaxC))
|
|
140
|
chartView.axisY.max = chartView.axisY.max + 10;
|
|
148
|
chartView.axisY.max = chartView.axisY.max + 10;
|
|
141
|
|
|
149
|
|
|
142
|
// Set the x-axis labels to the dates of the forecast
|
|
150
|
// Set the x-axis labels to the dates of the forecast
|
|
143
|
// TODO: the API could probably be more intuitive..
|
|
151
|
// TODO: the API could probably be more intuitive..
|
|
144
|
// Now it takes an array of strings: chartView.axisXLabels = ["value1", "label1", "value2", "label2", ...]
|
|
152
|
// Now it takes an array of strings: chartView.axisXLabels = ["value1", "label1", "value2", "label2", ...]
|
|
145
|
var xLabels = chartView.axisXLabels;
|
|
153
|
var xLabels = chartView.axisXLabels;
|
|
146
|
xLabels[Number(i) * 2] = i;
|
|
154
|
xLabels[Number(i) * 2] = i;
|
|
147
|
xLabels[(Number(i) * 2) + 1] = weatherObj.date.substring(5, 10);
|
|
155
|
xLabels[(Number(i) * 2) + 1] = weatherObj.date.substring(5, 10);
|
|
148
|
chartView.axisXLabels = xLabels;
|
|
156
|
chartView.axisXLabels = xLabels;
|
|
149
|
}
|
|
157
|
}
|
|
150
|
}
|
|
158
|
}
|
|
151
|
}
|
|
159
|
}
|