##// END OF EJS Templates
Initial support for x-axis labels in QML api
Tero Ahola -
r1157:bad1d525be80
parent child
Show More
@@ -1,129 +1,131
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.0
21 import QtQuick 1.0
22 import QtCommercial.Chart 1.0
22 import QtCommercial.Chart 1.0
23
23
24 Rectangle {
24 Rectangle {
25 anchors.fill: parent
25 anchors.fill: parent
26
26
27 ChartView {
27 ChartView {
28 title: "NHL All-Star Team Players"
28 title: "NHL All-Star Team Players"
29 anchors.fill: parent
29 anchors.fill: parent
30 theme: ChartView.ChartThemeHighContrast
30 theme: ChartView.ChartThemeHighContrast
31 legend: ChartView.LegendTop
31 legend: ChartView.LegendTop
32 axisXLabels: ["0", "2000", "1", "2001", "2", "2002", "3", "2003", "4", "2004", "5", "2005",
33 "6", "2006", "7", "2007", "8", "2008", "9", "2009", "10", "2010", "11", "2011"]
32
34
33 AreaSeries {
35 AreaSeries {
34 name: "Swedish"
36 name: "Swedish"
35 points: [
37 points: [
36 XyPoint { x: 0; y: 1 },
38 XyPoint { x: 0; y: 1 },
37 XyPoint { x: 1; y: 1 },
39 XyPoint { x: 1; y: 1 },
38 XyPoint { x: 2; y: 3 },
40 XyPoint { x: 2; y: 3 },
39 XyPoint { x: 3; y: 3 },
41 XyPoint { x: 3; y: 3 },
40 XyPoint { x: 4; y: 2 },
42 XyPoint { x: 4; y: 2 },
41 XyPoint { x: 5; y: 0 },
43 XyPoint { x: 5; y: 0 },
42 XyPoint { x: 6; y: 2 },
44 XyPoint { x: 6; y: 2 },
43 XyPoint { x: 7; y: 1 },
45 XyPoint { x: 7; y: 1 },
44 XyPoint { x: 8; y: 2 },
46 XyPoint { x: 8; y: 2 },
45 XyPoint { x: 9; y: 1 },
47 XyPoint { x: 9; y: 1 },
46 XyPoint { x: 10; y: 3 },
48 XyPoint { x: 10; y: 3 },
47 XyPoint { x: 11; y: 3 }
49 XyPoint { x: 11; y: 3 }
48 ]
50 ]
49 lowerPoints: [
51 lowerPoints: [
50 XyPoint { x: 0; y: 0 },
52 XyPoint { x: 0; y: 0 },
51 XyPoint { x: 1; y: 0 },
53 XyPoint { x: 1; y: 0 },
52 XyPoint { x: 2; y: 0 },
54 XyPoint { x: 2; y: 0 },
53 XyPoint { x: 3; y: 0 },
55 XyPoint { x: 3; y: 0 },
54 XyPoint { x: 4; y: 0 },
56 XyPoint { x: 4; y: 0 },
55 XyPoint { x: 5; y: 0 },
57 XyPoint { x: 5; y: 0 },
56 XyPoint { x: 6; y: 0 },
58 XyPoint { x: 6; y: 0 },
57 XyPoint { x: 7; y: 0 },
59 XyPoint { x: 7; y: 0 },
58 XyPoint { x: 8; y: 0 },
60 XyPoint { x: 8; y: 0 },
59 XyPoint { x: 9; y: 0 },
61 XyPoint { x: 9; y: 0 },
60 XyPoint { x: 10; y: 0 },
62 XyPoint { x: 10; y: 0 },
61 XyPoint { x: 11; y: 0 }
63 XyPoint { x: 11; y: 0 }
62 ]
64 ]
63 }
65 }
64
66
65 AreaSeries {
67 AreaSeries {
66 name: "Russian"
68 name: "Russian"
67 points: [
69 points: [
68 XyPoint { x: 0; y: 1 },
70 XyPoint { x: 0; y: 1 },
69 XyPoint { x: 1; y: 1 },
71 XyPoint { x: 1; y: 1 },
70 XyPoint { x: 2; y: 1 },
72 XyPoint { x: 2; y: 1 },
71 XyPoint { x: 3; y: 1 },
73 XyPoint { x: 3; y: 1 },
72 XyPoint { x: 4; y: 1 },
74 XyPoint { x: 4; y: 1 },
73 XyPoint { x: 5; y: 0 },
75 XyPoint { x: 5; y: 0 },
74 XyPoint { x: 6; y: 1 },
76 XyPoint { x: 6; y: 1 },
75 XyPoint { x: 7; y: 1 },
77 XyPoint { x: 7; y: 1 },
76 XyPoint { x: 8; y: 4 },
78 XyPoint { x: 8; y: 4 },
77 XyPoint { x: 9; y: 3 },
79 XyPoint { x: 9; y: 3 },
78 XyPoint { x: 10; y: 2 },
80 XyPoint { x: 10; y: 2 },
79 XyPoint { x: 11; y: 1 }
81 XyPoint { x: 11; y: 1 }
80 ]
82 ]
81 lowerPoints: [
83 lowerPoints: [
82 XyPoint { x: 0; y: 0 },
84 XyPoint { x: 0; y: 0 },
83 XyPoint { x: 1; y: 0 },
85 XyPoint { x: 1; y: 0 },
84 XyPoint { x: 2; y: 0 },
86 XyPoint { x: 2; y: 0 },
85 XyPoint { x: 3; y: 0 },
87 XyPoint { x: 3; y: 0 },
86 XyPoint { x: 4; y: 0 },
88 XyPoint { x: 4; y: 0 },
87 XyPoint { x: 5; y: 0 },
89 XyPoint { x: 5; y: 0 },
88 XyPoint { x: 6; y: 0 },
90 XyPoint { x: 6; y: 0 },
89 XyPoint { x: 7; y: 0 },
91 XyPoint { x: 7; y: 0 },
90 XyPoint { x: 8; y: 0 },
92 XyPoint { x: 8; y: 0 },
91 XyPoint { x: 9; y: 0 },
93 XyPoint { x: 9; y: 0 },
92 XyPoint { x: 10; y: 0 },
94 XyPoint { x: 10; y: 0 },
93 XyPoint { x: 11; y: 0 }
95 XyPoint { x: 11; y: 0 }
94 ]
96 ]
95 }
97 }
96
98
97 AreaSeries {
99 AreaSeries {
98 name: "Finnish"
100 name: "Finnish"
99 points: [
101 points: [
100 XyPoint { x: 0; y: 0 },
102 XyPoint { x: 0; y: 0 },
101 XyPoint { x: 1; y: 0 },
103 XyPoint { x: 1; y: 0 },
102 XyPoint { x: 2; y: 0 },
104 XyPoint { x: 2; y: 0 },
103 XyPoint { x: 3; y: 0 },
105 XyPoint { x: 3; y: 0 },
104 XyPoint { x: 4; y: 0 },
106 XyPoint { x: 4; y: 0 },
105 XyPoint { x: 5; y: 0 },
107 XyPoint { x: 5; y: 0 },
106 XyPoint { x: 6; y: 1 },
108 XyPoint { x: 6; y: 1 },
107 XyPoint { x: 7; y: 0 },
109 XyPoint { x: 7; y: 0 },
108 XyPoint { x: 8; y: 0 },
110 XyPoint { x: 8; y: 0 },
109 XyPoint { x: 9; y: 0 },
111 XyPoint { x: 9; y: 0 },
110 XyPoint { x: 10; y: 0 },
112 XyPoint { x: 10; y: 0 },
111 XyPoint { x: 11; y: 1 }
113 XyPoint { x: 11; y: 1 }
112 ]
114 ]
113 lowerPoints: [
115 lowerPoints: [
114 XyPoint { x: 0; y: 0 },
116 XyPoint { x: 0; y: 0 },
115 XyPoint { x: 1; y: 0 },
117 XyPoint { x: 1; y: 0 },
116 XyPoint { x: 2; y: 0 },
118 XyPoint { x: 2; y: 0 },
117 XyPoint { x: 3; y: 0 },
119 XyPoint { x: 3; y: 0 },
118 XyPoint { x: 4; y: 0 },
120 XyPoint { x: 4; y: 0 },
119 XyPoint { x: 5; y: 0 },
121 XyPoint { x: 5; y: 0 },
120 XyPoint { x: 6; y: 0 },
122 XyPoint { x: 6; y: 0 },
121 XyPoint { x: 7; y: 0 },
123 XyPoint { x: 7; y: 0 },
122 XyPoint { x: 8; y: 0 },
124 XyPoint { x: 8; y: 0 },
123 XyPoint { x: 9; y: 0 },
125 XyPoint { x: 9; y: 0 },
124 XyPoint { x: 10; y: 0 },
126 XyPoint { x: 10; y: 0 },
125 XyPoint { x: 11; y: 0 }
127 XyPoint { x: 11; y: 0 }
126 ]
128 ]
127 }
129 }
128 }
130 }
129 }
131 }
@@ -1,34 +1,43
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 #include <QApplication>
21 #include <QApplication>
22 #include <QDeclarativeContext>
23 #include <QDebug>
22 #include "qmlapplicationviewer.h"
24 #include "qmlapplicationviewer.h"
23
25
24 Q_DECL_EXPORT int main(int argc, char *argv[])
26 Q_DECL_EXPORT int main(int argc, char *argv[])
25 {
27 {
26 QScopedPointer<QApplication> app(createApplication(argc, argv));
28 QScopedPointer<QApplication> app(createApplication(argc, argv));
27
29
28 QmlApplicationViewer viewer;
30 QmlApplicationViewer viewer;
29 viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
31 viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
32 QString appKey;
33 if (argc > 1) {
34 appKey = argv[1];
35 qDebug() << "App key for worldweatheronline.com:" << appKey;
36 } else {
37 qWarning() << "No app key for worldweatheronline.com given. Using static test data instead of live data.";
38 }
39 viewer.rootContext()->setContextProperty("weatherAppKey", appKey);
30 viewer.setSource(QUrl("qrc:/qml/qmlweather/main.qml"));
40 viewer.setSource(QUrl("qrc:/qml/qmlweather/main.qml"));
31 viewer.showExpanded();
41 viewer.showExpanded();
32
33 return app->exec();
42 return app->exec();
34 }
43 }
@@ -1,136 +1,147
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 ChartView {
28 ChartView {
29 id: chartView
29 id: chartView
30 anchors.top: parent.top
30 anchors.top: parent.top
31 anchors.bottom: weatherImageRow.top
31 anchors.bottom: weatherImageRow.top
32 anchors.left: parent.left
32 anchors.left: parent.left
33 anchors.right: parent.right
33 anchors.right: parent.right
34 title: "Weather forecast"
34 axisX.min: 0
35 axisX.min: 0
35 axisX.max: 6
36 axisX.max: 4
36 axisY.min: 0
37 axisY.min: 0
37 axisY.max: 50
38 axisY.max: 0
38
39 legend: ChartView.LegendTop
39 // TODO: implement categories
40 // AxisCategory { axis: AxisCategory.AxisX; value: 0.0; label: "" }
41 // AxisCategory { axis: AxisCategory.AxisX; value: 1.0; label: "Today" }
42 // AxisCategory { axis: AxisCategory.AxisX; value: 2.0; label: "Tomorrow" }
43 // AxisCategory { axis: AxisCategory.AxisX; value: 3.0; label: "Day after tomorrow" }
44
40
45 LineSeries {
41 LineSeries {
46 model: maxModel
42 model: maxModel
43 name: "Max. temperature"
47 }
44 }
48
45
49 LineSeries {
46 LineSeries {
50 model: minModel
47 model: minModel
48 name: "Min. temperature"
51 }
49 }
52
50
53 // TODO: use a single base model with mappings instead of two separate xy models
51 // TODO: use a single base model with mappings instead of two separate xy models
54 // LineSeries {
52 // LineSeries {
55 // model: chartModel
53 // model: chartModel
56 // modelMapping: XyModelMapping {
54 // modelMapping: XyModelMapping {
57 // xColumn: 0
55 // xColumn: 0
58 // yColumn: 1
56 // yColumn: 1
59 // }
57 // }
60 // }
58 // }
61 // LineSeries {
59 // LineSeries {
62 // model: chartModel
60 // model: chartModel
63 // modelMapping: XyModelMapping {
61 // modelMapping: XyModelMapping {
64 // xColumn: 0
62 // xColumn: 0
65 // yColumn: 2
63 // yColumn: 2
66 // }
64 // }
67 // }
65 // }
68 }
66 }
69
67
70 // ChartModel {
68 // ChartModel {
71 // id: chartModel
69 // id: chartModel
72 // }
70 // }
73
71
74 XYModel {
72 XYModel {
75 id: maxModel
73 id: maxModel
76 }
74 }
77
75
78 XYModel {
76 XYModel {
79 id: minModel
77 id: minModel
80 }
78 }
81
79
82 Component.onCompleted: {
80 Component.onCompleted: {
83 // TODO: use live data instead of hard coded example data
81 if (weatherAppKey != "") {
84 // in case an application key was defined for this demo app
82 var xhr = new XMLHttpRequest;
85 // var xhr = new XMLHttpRequest;
83 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);
86 // var appKey = "";
84 xhr.onreadystatechange = function() {
87 // xhr.open("GET", "http://free.worldweatheronline.com/feed/weather.ashx?q=Jyv%c3%a4skyl%c3%a4,Finland&format=json&num_of_days=5&key=" + appKey");
85 if (xhr.readyState == XMLHttpRequest.DONE) {
88 // xhr.onreadystatechange = function() {
86 var a = JSON.parse(xhr.responseText);
89 // if (xhr.readyState == XMLHttpRequest.DONE) {
87 parseWeatherData(a);
90 // var a = JSON.parse(xhr.responseText);
88 }
91 // console.log("a: " + a);
89 }
92 // console.log("response: " + xhr.responseText);
90 xhr.send();
93 // for (var b in a) {
91 } else {
94 // var o = a[b];
92 // No app key for worldweatheronline.com given by the user -> use static data
95 // console.log("o: " + o);
93 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\" } ] }}";
96 //// model.append({id: o.id, name: o.name, duration: o.duration});
94 var a = JSON.parse(responseText);
97 // }
95 parseWeatherData(a);
98 // }
96 }
99 // }
97 }
100 // xhr.send();
98
101
99 function parseWeatherData(weatherData) {
102 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\" } ] }}";
100 for (var i in weatherData.data.weather) {
103 var a = JSON.parse(responseText);
101 var weatherObj = weatherData.data.weather[i];
104
102
105 for (var i in a.data.weather) {
103 // Add min and max temperature values into models used by series
106 var weatherObj = a.data.weather[i];
104 maxModel.append([Number(i), weatherObj.tempMaxC]);
107 maxModel.append([Number(i) + 1, weatherObj.tempMaxC]);
105 minModel.append([Number(i), weatherObj.tempMinC]);
108 minModel.append([Number(i) + 1, weatherObj.tempMinC]);
109 weatherImageModel.append({"imageSource":weatherObj.weatherIconUrl[0].value});
106 weatherImageModel.append({"imageSource":weatherObj.weatherIconUrl[0].value});
107
108 // Update scale of the chart
109 while (chartView.axisY.min >= Number(weatherObj.tempMinC))
110 chartView.axisY.min = chartView.axisY.min - 10;
111 while (chartView.axisY.max <= Number(weatherObj.tempMaxC))
112 chartView.axisY.max = chartView.axisY.max + 10;
113
114 // Set the x-axis labels to the dates of the forecast
115 // TODO: the API could probably be more intuitive..
116 // Now it takes an array of strings: chartView.axisXLabels = ["value1", "label1", "value2", "label2", ...]
117 var xLabels = chartView.axisXLabels;
118 xLabels[Number(i) * 2] = i;
119 xLabels[(Number(i) * 2) + 1] = weatherObj.date.substring(5, 10);
120 chartView.axisXLabels = xLabels;
110 }
121 }
111 }
122 }
112
123
113 ListModel {
124 ListModel {
114 id: weatherImageModel
125 id: weatherImageModel
115 }
126 }
116
127
117 Row {
128 Row {
118 id: weatherImageRow
129 id: weatherImageRow
119 anchors.bottom: parent.bottom
130 anchors.bottom: parent.bottom
120 anchors.bottomMargin: 10
131 anchors.bottomMargin: 10
121 anchors.left: parent.left
132 anchors.left: parent.left
122 anchors.leftMargin: 25
133 anchors.leftMargin: 25
123 anchors.right: parent.right
134 anchors.right: parent.right
124 anchors.rightMargin: 25
135 anchors.rightMargin: 25
125 Repeater {
136 Repeater {
126 id: repeater
137 id: repeater
127 model: weatherImageModel
138 model: weatherImageModel
128 delegate: Image {
139 delegate: Image {
129 source: imageSource
140 source: imageSource
130 width: weatherImageRow.width / weatherImageModel.count
141 width: weatherImageRow.width / weatherImageModel.count
131 height: width
142 height: width
132 fillMode: Image.PreserveAspectCrop
143 fillMode: Image.PreserveAspectCrop
133 }
144 }
134 }
145 }
135 }
146 }
136 }
147 }
@@ -1,148 +1,173
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 #include "declarativechart.h"
21 #include "declarativechart.h"
22 #include <QPainter>
22 #include <QPainter>
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
26 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
27 : QDeclarativeItem(parent),
27 : QDeclarativeItem(parent),
28 m_chart(new QChart(this)),
28 m_chart(new QChart(this)),
29 m_legend(LegendDisabled)
29 m_legend(LegendDisabled)
30 {
30 {
31 setFlag(QGraphicsItem::ItemHasNoContents, false);
31 setFlag(QGraphicsItem::ItemHasNoContents, false);
32 m_chart->axisX()->setNiceNumbersEnabled(false);
32 m_chart->axisX()->setNiceNumbersEnabled(false);
33 }
33 }
34
34
35 DeclarativeChart::~DeclarativeChart()
35 DeclarativeChart::~DeclarativeChart()
36 {
36 {
37 delete m_chart;
37 delete m_chart;
38 }
38 }
39
39
40 void DeclarativeChart::childEvent(QChildEvent *event)
40 void DeclarativeChart::childEvent(QChildEvent *event)
41 {
41 {
42 if (event->type() == QEvent::ChildAdded) {
42 if (event->type() == QEvent::ChildAdded) {
43 if (qobject_cast<QAbstractSeries *>(event->child())) {
43 if (qobject_cast<QAbstractSeries *>(event->child())) {
44 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
44 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
45 }
45 }
46 }
46 }
47 }
47 }
48
48
49 void DeclarativeChart::componentComplete()
49 void DeclarativeChart::componentComplete()
50 {
50 {
51 // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max();
51 // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max();
52 foreach(QObject *child, children()) {
52 foreach(QObject *child, children()) {
53 if (qobject_cast<QAbstractSeries *>(child)) {
53 if (qobject_cast<QAbstractSeries *>(child)) {
54 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
54 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
55 }
55 }
56 }
56 }
57 // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max();
57 // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max();
58
58
59 QDeclarativeItem::componentComplete();
59 QDeclarativeItem::componentComplete();
60 }
60 }
61
61
62 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
62 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
63 {
63 {
64 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
64 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
65 Q_UNUSED(oldGeometry)
65 Q_UNUSED(oldGeometry)
66
66
67 if (newGeometry.isValid()) {
67 if (newGeometry.isValid()) {
68 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
68 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
69 m_chart->resize(newGeometry.width(), newGeometry.height());
69 m_chart->resize(newGeometry.width(), newGeometry.height());
70 }
70 }
71 }
71 }
72 }
72 }
73
73
74 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
74 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
75 {
75 {
76 Q_UNUSED(option)
76 Q_UNUSED(option)
77 Q_UNUSED(widget)
77 Q_UNUSED(widget)
78
78
79 // TODO: optimized?
79 // TODO: optimized?
80 painter->setRenderHint(QPainter::Antialiasing, true);
80 painter->setRenderHint(QPainter::Antialiasing, true);
81 }
81 }
82
82
83 void DeclarativeChart::setAnimationOptions(QChart::AnimationOption animations)
83 void DeclarativeChart::setAnimationOptions(QChart::AnimationOption animations)
84 {
84 {
85 m_chart->setAnimationOptions(animations);
85 m_chart->setAnimationOptions(animations);
86 }
86 }
87
87
88 QChart::AnimationOption DeclarativeChart::animationOptions()
88 QChart::AnimationOption DeclarativeChart::animationOptions()
89 {
89 {
90 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
90 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
91 return QChart::AllAnimations;
91 return QChart::AllAnimations;
92 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
92 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
93 return QChart::GridAxisAnimations;
93 return QChart::GridAxisAnimations;
94 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
94 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
95 return QChart::SeriesAnimations;
95 return QChart::SeriesAnimations;
96 else
96 else
97 return QChart::NoAnimation;
97 return QChart::NoAnimation;
98 }
98 }
99
99
100 void DeclarativeChart::setLegend(ChartLegend legend)
100 void DeclarativeChart::setLegend(ChartLegend legend)
101 {
101 {
102 if (legend != m_legend) {
102 if (legend != m_legend) {
103 m_legend = legend;
103 m_legend = legend;
104 switch (m_legend) {
104 switch (m_legend) {
105 case LegendDisabled:
105 case LegendDisabled:
106 m_chart->legend()->setVisible(false);
106 m_chart->legend()->setVisible(false);
107 break;
107 break;
108 case LegendTop:
108 case LegendTop:
109 m_chart->legend()->setVisible(true);
109 m_chart->legend()->setVisible(true);
110 m_chart->legend()->setAlignment(QLegend::AlignmentTop);
110 m_chart->legend()->setAlignment(QLegend::AlignmentTop);
111 break;
111 break;
112 case LegendBottom:
112 case LegendBottom:
113 m_chart->legend()->setVisible(true);
113 m_chart->legend()->setVisible(true);
114 m_chart->legend()->setAlignment(QLegend::AlignmentBottom);
114 m_chart->legend()->setAlignment(QLegend::AlignmentBottom);
115 break;
115 break;
116 case LegendLeft:
116 case LegendLeft:
117 m_chart->legend()->setVisible(true);
117 m_chart->legend()->setVisible(true);
118 m_chart->legend()->setAlignment(QLegend::AlignmentLeft);
118 m_chart->legend()->setAlignment(QLegend::AlignmentLeft);
119 break;
119 break;
120 case LegendRight:
120 case LegendRight:
121 m_chart->legend()->setVisible(true);
121 m_chart->legend()->setVisible(true);
122 m_chart->legend()->setAlignment(QLegend::AlignmentRight);
122 m_chart->legend()->setAlignment(QLegend::AlignmentRight);
123 break;
123 break;
124 default:
124 default:
125 m_chart->legend()->setVisible(false);
125 m_chart->legend()->setVisible(false);
126 break;
126 break;
127 }
127 }
128 }
128 }
129 }
129 }
130
130
131 DeclarativeChart::ChartLegend DeclarativeChart::legend()
131 DeclarativeChart::ChartLegend DeclarativeChart::legend()
132 {
132 {
133 return m_legend;
133 return m_legend;
134 }
134 }
135
135
136 QAxis *DeclarativeChart::axisX()
136 QAxis *DeclarativeChart::axisX()
137 {
137 {
138 return m_chart->axisX();
138 return m_chart->axisX();
139 }
139 }
140
140
141 QAxis *DeclarativeChart::axisY()
141 QAxis *DeclarativeChart::axisY()
142 {
142 {
143 return m_chart->axisY();
143 return m_chart->axisY();
144 }
144 }
145
145
146 QVariantList DeclarativeChart::axisXLabels()
147 {
148 QVariantList labels;
149 foreach (qreal value, m_chart->axisX()->categories()->values()) {
150 // qDebug() << "Label for" << value << "is" << m_chart->axisX()->categories()->label(value);
151 labels.append(value);
152 labels.append(m_chart->axisX()->categories()->label(value));
153 }
154 return labels;
155 }
156
157 void DeclarativeChart::setAxisXLabels(QVariantList list)
158 {
159 QVariant value(QVariant::Invalid);
160 foreach (QVariant element, list) {
161 if (value.isValid() && element.type() == QVariant::String) {
162 m_chart->axisX()->categories()->insert(value.toDouble(), element.toString());
163 value = QVariant(QVariant::Invalid);
164 } else {
165 if (element.canConvert(QVariant::Double))
166 value = element;
167 }
168 }
169 }
170
146 #include "moc_declarativechart.cpp"
171 #include "moc_declarativechart.cpp"
147
172
148 QTCOMMERCIALCHART_END_NAMESPACE
173 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,85 +1,89
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 #ifndef DECLARATIVECHART_H
21 #ifndef DECLARATIVECHART_H
22 #define DECLARATIVECHART_H
22 #define DECLARATIVECHART_H
23
23
24 #include <QtCore/QtGlobal>
24 #include <QtCore/QtGlobal>
25 #include <QDeclarativeItem>
25 #include <QDeclarativeItem>
26 #include <qchart.h>
26 #include <qchart.h>
27 #include <QAxis>
27 #include <QAxis>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 // TODO: Derive from QChart for easier definition of properties?
31 // TODO: Derive from QChart for easier definition of properties?
32 class DeclarativeChart : public QDeclarativeItem
32 class DeclarativeChart : public QDeclarativeItem
33 // TODO: for QTQUICK2: extend QQuickPainterItem instead
33 // TODO: for QTQUICK2: extend QQuickPainterItem instead
34 //class DeclarativeChart : public QQuickPaintedItem, public Chart
34 //class DeclarativeChart : public QQuickPaintedItem, public Chart
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37 Q_PROPERTY(QChart::ChartTheme theme READ theme WRITE setTheme)
37 Q_PROPERTY(QChart::ChartTheme theme READ theme WRITE setTheme)
38 Q_PROPERTY(QChart::AnimationOption animationOptions READ animationOptions WRITE setAnimationOptions)
38 Q_PROPERTY(QChart::AnimationOption animationOptions READ animationOptions WRITE setAnimationOptions)
39 Q_PROPERTY(QString title READ title WRITE setTitle)
39 Q_PROPERTY(QString title READ title WRITE setTitle)
40 Q_PROPERTY(ChartLegend legend READ legend WRITE setLegend)
40 Q_PROPERTY(ChartLegend legend READ legend WRITE setLegend)
41 Q_PROPERTY(QAxis *axisX READ axisX)
41 Q_PROPERTY(QAxis *axisX READ axisX)
42 Q_PROPERTY(QAxis *axisY READ axisY)
42 Q_PROPERTY(QAxis *axisY READ axisY)
43 // TODO: how to define axis labels? This is not very convenient
44 Q_PROPERTY(QVariantList axisXLabels READ axisXLabels WRITE setAxisXLabels)
43 Q_ENUMS(ChartLegend)
45 Q_ENUMS(ChartLegend)
44
46
45 public:
47 public:
46 enum ChartLegend {
48 enum ChartLegend {
47 LegendDisabled = 0,
49 LegendDisabled = 0,
48 LegendTop,
50 LegendTop,
49 LegendBottom,
51 LegendBottom,
50 LegendLeft,
52 LegendLeft,
51 LegendRight
53 LegendRight
52 };
54 };
53
55
54 public:
56 public:
55 DeclarativeChart(QDeclarativeItem *parent = 0);
57 DeclarativeChart(QDeclarativeItem *parent = 0);
56 ~DeclarativeChart();
58 ~DeclarativeChart();
57
59
58 public: // From QDeclarativeItem/QGraphicsItem
60 public: // From QDeclarativeItem/QGraphicsItem
59 void childEvent(QChildEvent *event);
61 void childEvent(QChildEvent *event);
60 void componentComplete();
62 void componentComplete();
61 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
63 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
62 void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
64 void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
63
65
64 public:
66 public:
65 void setTheme(QChart::ChartTheme theme) {m_chart->setTheme(theme);}
67 void setTheme(QChart::ChartTheme theme) {m_chart->setTheme(theme);}
66 QChart::ChartTheme theme() {return m_chart->theme();}
68 QChart::ChartTheme theme() {return m_chart->theme();}
67 void setAnimationOptions(QChart::AnimationOption animations);
69 void setAnimationOptions(QChart::AnimationOption animations);
68 QChart::AnimationOption animationOptions();
70 QChart::AnimationOption animationOptions();
69 void setTitle(QString title) {m_chart->setTitle(title);}
71 void setTitle(QString title) {m_chart->setTitle(title);}
70 QString title() { return m_chart->title();}
72 QString title() { return m_chart->title();}
71 void setLegend(ChartLegend legend);
73 void setLegend(ChartLegend legend);
72 ChartLegend legend();
74 ChartLegend legend();
73 QAxis *axisX();
75 QAxis *axisX();
74 QAxis *axisY();
76 QAxis *axisY();
77 QVariantList axisXLabels();
78 void setAxisXLabels(QVariantList list);
75
79
76 public:
80 public:
77 // Extending QChart with DeclarativeChart is not possible because QObject does not support
81 // Extending QChart with DeclarativeChart is not possible because QObject does not support
78 // multi inheritance, so we now have a QChart as a member instead
82 // multi inheritance, so we now have a QChart as a member instead
79 QChart *m_chart;
83 QChart *m_chart;
80 ChartLegend m_legend;
84 ChartLegend m_legend;
81 };
85 };
82
86
83 QTCOMMERCIALCHART_END_NAMESPACE
87 QTCOMMERCIALCHART_END_NAMESPACE
84
88
85 #endif // DECLARATIVECHART_H
89 #endif // DECLARATIVECHART_H
General Comments 0
You need to be logged in to leave comments. Login now