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