##// 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 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.0
22 22 import QtCommercial.Chart 1.0
23 23
24 24 Rectangle {
25 25 anchors.fill: parent
26 26
27 27 ChartView {
28 28 title: "NHL All-Star Team Players"
29 29 anchors.fill: parent
30 30 theme: ChartView.ChartThemeHighContrast
31 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 35 AreaSeries {
34 36 name: "Swedish"
35 37 points: [
36 38 XyPoint { x: 0; y: 1 },
37 39 XyPoint { x: 1; y: 1 },
38 40 XyPoint { x: 2; y: 3 },
39 41 XyPoint { x: 3; y: 3 },
40 42 XyPoint { x: 4; y: 2 },
41 43 XyPoint { x: 5; y: 0 },
42 44 XyPoint { x: 6; y: 2 },
43 45 XyPoint { x: 7; y: 1 },
44 46 XyPoint { x: 8; y: 2 },
45 47 XyPoint { x: 9; y: 1 },
46 48 XyPoint { x: 10; y: 3 },
47 49 XyPoint { x: 11; y: 3 }
48 50 ]
49 51 lowerPoints: [
50 52 XyPoint { x: 0; y: 0 },
51 53 XyPoint { x: 1; y: 0 },
52 54 XyPoint { x: 2; y: 0 },
53 55 XyPoint { x: 3; y: 0 },
54 56 XyPoint { x: 4; y: 0 },
55 57 XyPoint { x: 5; y: 0 },
56 58 XyPoint { x: 6; y: 0 },
57 59 XyPoint { x: 7; y: 0 },
58 60 XyPoint { x: 8; y: 0 },
59 61 XyPoint { x: 9; y: 0 },
60 62 XyPoint { x: 10; y: 0 },
61 63 XyPoint { x: 11; y: 0 }
62 64 ]
63 65 }
64 66
65 67 AreaSeries {
66 68 name: "Russian"
67 69 points: [
68 70 XyPoint { x: 0; y: 1 },
69 71 XyPoint { x: 1; y: 1 },
70 72 XyPoint { x: 2; y: 1 },
71 73 XyPoint { x: 3; y: 1 },
72 74 XyPoint { x: 4; y: 1 },
73 75 XyPoint { x: 5; y: 0 },
74 76 XyPoint { x: 6; y: 1 },
75 77 XyPoint { x: 7; y: 1 },
76 78 XyPoint { x: 8; y: 4 },
77 79 XyPoint { x: 9; y: 3 },
78 80 XyPoint { x: 10; y: 2 },
79 81 XyPoint { x: 11; y: 1 }
80 82 ]
81 83 lowerPoints: [
82 84 XyPoint { x: 0; y: 0 },
83 85 XyPoint { x: 1; y: 0 },
84 86 XyPoint { x: 2; y: 0 },
85 87 XyPoint { x: 3; y: 0 },
86 88 XyPoint { x: 4; y: 0 },
87 89 XyPoint { x: 5; y: 0 },
88 90 XyPoint { x: 6; y: 0 },
89 91 XyPoint { x: 7; y: 0 },
90 92 XyPoint { x: 8; y: 0 },
91 93 XyPoint { x: 9; y: 0 },
92 94 XyPoint { x: 10; y: 0 },
93 95 XyPoint { x: 11; y: 0 }
94 96 ]
95 97 }
96 98
97 99 AreaSeries {
98 100 name: "Finnish"
99 101 points: [
100 102 XyPoint { x: 0; y: 0 },
101 103 XyPoint { x: 1; y: 0 },
102 104 XyPoint { x: 2; y: 0 },
103 105 XyPoint { x: 3; y: 0 },
104 106 XyPoint { x: 4; y: 0 },
105 107 XyPoint { x: 5; y: 0 },
106 108 XyPoint { x: 6; y: 1 },
107 109 XyPoint { x: 7; y: 0 },
108 110 XyPoint { x: 8; y: 0 },
109 111 XyPoint { x: 9; y: 0 },
110 112 XyPoint { x: 10; y: 0 },
111 113 XyPoint { x: 11; y: 1 }
112 114 ]
113 115 lowerPoints: [
114 116 XyPoint { x: 0; y: 0 },
115 117 XyPoint { x: 1; y: 0 },
116 118 XyPoint { x: 2; y: 0 },
117 119 XyPoint { x: 3; y: 0 },
118 120 XyPoint { x: 4; y: 0 },
119 121 XyPoint { x: 5; y: 0 },
120 122 XyPoint { x: 6; y: 0 },
121 123 XyPoint { x: 7; y: 0 },
122 124 XyPoint { x: 8; y: 0 },
123 125 XyPoint { x: 9; y: 0 },
124 126 XyPoint { x: 10; y: 0 },
125 127 XyPoint { x: 11; y: 0 }
126 128 ]
127 129 }
128 130 }
129 131 }
@@ -1,34 +1,43
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 #include <QApplication>
22 #include <QDeclarativeContext>
23 #include <QDebug>
22 24 #include "qmlapplicationviewer.h"
23 25
24 26 Q_DECL_EXPORT int main(int argc, char *argv[])
25 27 {
26 28 QScopedPointer<QApplication> app(createApplication(argc, argv));
27 29
28 30 QmlApplicationViewer viewer;
29 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 40 viewer.setSource(QUrl("qrc:/qml/qmlweather/main.qml"));
31 41 viewer.showExpanded();
32
33 42 return app->exec();
34 43 }
@@ -1,136 +1,147
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.0
23 23
24 24 Rectangle {
25 25 width: 360
26 26 height: 360
27 27
28 28 ChartView {
29 29 id: chartView
30 30 anchors.top: parent.top
31 31 anchors.bottom: weatherImageRow.top
32 32 anchors.left: parent.left
33 33 anchors.right: parent.right
34 title: "Weather forecast"
34 35 axisX.min: 0
35 axisX.max: 6
36 axisX.max: 4
36 37 axisY.min: 0
37 axisY.max: 50
38
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" }
38 axisY.max: 0
39 legend: ChartView.LegendTop
44 40
45 41 LineSeries {
46 42 model: maxModel
43 name: "Max. temperature"
47 44 }
48 45
49 46 LineSeries {
50 47 model: minModel
48 name: "Min. temperature"
51 49 }
52 50
53 51 // TODO: use a single base model with mappings instead of two separate xy models
54 52 // LineSeries {
55 53 // model: chartModel
56 54 // modelMapping: XyModelMapping {
57 55 // xColumn: 0
58 56 // yColumn: 1
59 57 // }
60 58 // }
61 59 // LineSeries {
62 60 // model: chartModel
63 61 // modelMapping: XyModelMapping {
64 62 // xColumn: 0
65 63 // yColumn: 2
66 64 // }
67 65 // }
68 66 }
69 67
70 68 // ChartModel {
71 69 // id: chartModel
72 70 // }
73 71
74 72 XYModel {
75 73 id: maxModel
76 74 }
77 75
78 76 XYModel {
79 77 id: minModel
80 78 }
81 79
82 80 Component.onCompleted: {
83 // TODO: use live data instead of hard coded example data
84 // in case an application key was defined for this demo app
85 // var xhr = new XMLHttpRequest;
86 // var appKey = "";
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");
88 // xhr.onreadystatechange = function() {
89 // if (xhr.readyState == XMLHttpRequest.DONE) {
90 // var a = JSON.parse(xhr.responseText);
91 // console.log("a: " + a);
92 // console.log("response: " + xhr.responseText);
93 // for (var b in a) {
94 // var o = a[b];
95 // console.log("o: " + o);
96 //// model.append({id: o.id, name: o.name, duration: o.duration});
97 // }
98 // }
99 // }
100 // xhr.send();
101
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\" } ] }}";
103 var a = JSON.parse(responseText);
104
105 for (var i in a.data.weather) {
106 var weatherObj = a.data.weather[i];
107 maxModel.append([Number(i) + 1, weatherObj.tempMaxC]);
108 minModel.append([Number(i) + 1, weatherObj.tempMinC]);
81 if (weatherAppKey != "") {
82 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);
84 xhr.onreadystatechange = function() {
85 if (xhr.readyState == XMLHttpRequest.DONE) {
86 var a = JSON.parse(xhr.responseText);
87 parseWeatherData(a);
88 }
89 }
90 xhr.send();
91 } else {
92 // No app key for worldweatheronline.com given by the user -> use static data
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\" } ] }}";
94 var a = JSON.parse(responseText);
95 parseWeatherData(a);
96 }
97 }
98
99 function parseWeatherData(weatherData) {
100 for (var i in weatherData.data.weather) {
101 var weatherObj = weatherData.data.weather[i];
102
103 // Add min and max temperature values into models used by series
104 maxModel.append([Number(i), weatherObj.tempMaxC]);
105 minModel.append([Number(i), weatherObj.tempMinC]);
109 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 124 ListModel {
114 125 id: weatherImageModel
115 126 }
116 127
117 128 Row {
118 129 id: weatherImageRow
119 130 anchors.bottom: parent.bottom
120 131 anchors.bottomMargin: 10
121 132 anchors.left: parent.left
122 133 anchors.leftMargin: 25
123 134 anchors.right: parent.right
124 135 anchors.rightMargin: 25
125 136 Repeater {
126 137 id: repeater
127 138 model: weatherImageModel
128 139 delegate: Image {
129 140 source: imageSource
130 141 width: weatherImageRow.width / weatherImageModel.count
131 142 height: width
132 143 fillMode: Image.PreserveAspectCrop
133 144 }
134 145 }
135 146 }
136 147 }
@@ -1,148 +1,173
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 #include "declarativechart.h"
22 22 #include <QPainter>
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
27 27 : QDeclarativeItem(parent),
28 28 m_chart(new QChart(this)),
29 29 m_legend(LegendDisabled)
30 30 {
31 31 setFlag(QGraphicsItem::ItemHasNoContents, false);
32 32 m_chart->axisX()->setNiceNumbersEnabled(false);
33 33 }
34 34
35 35 DeclarativeChart::~DeclarativeChart()
36 36 {
37 37 delete m_chart;
38 38 }
39 39
40 40 void DeclarativeChart::childEvent(QChildEvent *event)
41 41 {
42 42 if (event->type() == QEvent::ChildAdded) {
43 43 if (qobject_cast<QAbstractSeries *>(event->child())) {
44 44 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
45 45 }
46 46 }
47 47 }
48 48
49 49 void DeclarativeChart::componentComplete()
50 50 {
51 51 // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max();
52 52 foreach(QObject *child, children()) {
53 53 if (qobject_cast<QAbstractSeries *>(child)) {
54 54 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
55 55 }
56 56 }
57 57 // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max();
58 58
59 59 QDeclarativeItem::componentComplete();
60 60 }
61 61
62 62 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
63 63 {
64 64 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
65 65 Q_UNUSED(oldGeometry)
66 66
67 67 if (newGeometry.isValid()) {
68 68 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
69 69 m_chart->resize(newGeometry.width(), newGeometry.height());
70 70 }
71 71 }
72 72 }
73 73
74 74 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
75 75 {
76 76 Q_UNUSED(option)
77 77 Q_UNUSED(widget)
78 78
79 79 // TODO: optimized?
80 80 painter->setRenderHint(QPainter::Antialiasing, true);
81 81 }
82 82
83 83 void DeclarativeChart::setAnimationOptions(QChart::AnimationOption animations)
84 84 {
85 85 m_chart->setAnimationOptions(animations);
86 86 }
87 87
88 88 QChart::AnimationOption DeclarativeChart::animationOptions()
89 89 {
90 90 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
91 91 return QChart::AllAnimations;
92 92 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
93 93 return QChart::GridAxisAnimations;
94 94 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
95 95 return QChart::SeriesAnimations;
96 96 else
97 97 return QChart::NoAnimation;
98 98 }
99 99
100 100 void DeclarativeChart::setLegend(ChartLegend legend)
101 101 {
102 102 if (legend != m_legend) {
103 103 m_legend = legend;
104 104 switch (m_legend) {
105 105 case LegendDisabled:
106 106 m_chart->legend()->setVisible(false);
107 107 break;
108 108 case LegendTop:
109 109 m_chart->legend()->setVisible(true);
110 110 m_chart->legend()->setAlignment(QLegend::AlignmentTop);
111 111 break;
112 112 case LegendBottom:
113 113 m_chart->legend()->setVisible(true);
114 114 m_chart->legend()->setAlignment(QLegend::AlignmentBottom);
115 115 break;
116 116 case LegendLeft:
117 117 m_chart->legend()->setVisible(true);
118 118 m_chart->legend()->setAlignment(QLegend::AlignmentLeft);
119 119 break;
120 120 case LegendRight:
121 121 m_chart->legend()->setVisible(true);
122 122 m_chart->legend()->setAlignment(QLegend::AlignmentRight);
123 123 break;
124 124 default:
125 125 m_chart->legend()->setVisible(false);
126 126 break;
127 127 }
128 128 }
129 129 }
130 130
131 131 DeclarativeChart::ChartLegend DeclarativeChart::legend()
132 132 {
133 133 return m_legend;
134 134 }
135 135
136 136 QAxis *DeclarativeChart::axisX()
137 137 {
138 138 return m_chart->axisX();
139 139 }
140 140
141 141 QAxis *DeclarativeChart::axisY()
142 142 {
143 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 171 #include "moc_declarativechart.cpp"
147 172
148 173 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,85 +1,89
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 #ifndef DECLARATIVECHART_H
22 22 #define DECLARATIVECHART_H
23 23
24 24 #include <QtCore/QtGlobal>
25 25 #include <QDeclarativeItem>
26 26 #include <qchart.h>
27 27 #include <QAxis>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 // TODO: Derive from QChart for easier definition of properties?
32 32 class DeclarativeChart : public QDeclarativeItem
33 33 // TODO: for QTQUICK2: extend QQuickPainterItem instead
34 34 //class DeclarativeChart : public QQuickPaintedItem, public Chart
35 35 {
36 36 Q_OBJECT
37 37 Q_PROPERTY(QChart::ChartTheme theme READ theme WRITE setTheme)
38 38 Q_PROPERTY(QChart::AnimationOption animationOptions READ animationOptions WRITE setAnimationOptions)
39 39 Q_PROPERTY(QString title READ title WRITE setTitle)
40 40 Q_PROPERTY(ChartLegend legend READ legend WRITE setLegend)
41 41 Q_PROPERTY(QAxis *axisX READ axisX)
42 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 45 Q_ENUMS(ChartLegend)
44 46
45 47 public:
46 48 enum ChartLegend {
47 49 LegendDisabled = 0,
48 50 LegendTop,
49 51 LegendBottom,
50 52 LegendLeft,
51 53 LegendRight
52 54 };
53 55
54 56 public:
55 57 DeclarativeChart(QDeclarativeItem *parent = 0);
56 58 ~DeclarativeChart();
57 59
58 60 public: // From QDeclarativeItem/QGraphicsItem
59 61 void childEvent(QChildEvent *event);
60 62 void componentComplete();
61 63 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
62 64 void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
63 65
64 66 public:
65 67 void setTheme(QChart::ChartTheme theme) {m_chart->setTheme(theme);}
66 68 QChart::ChartTheme theme() {return m_chart->theme();}
67 69 void setAnimationOptions(QChart::AnimationOption animations);
68 70 QChart::AnimationOption animationOptions();
69 71 void setTitle(QString title) {m_chart->setTitle(title);}
70 72 QString title() { return m_chart->title();}
71 73 void setLegend(ChartLegend legend);
72 74 ChartLegend legend();
73 75 QAxis *axisX();
74 76 QAxis *axisY();
77 QVariantList axisXLabels();
78 void setAxisXLabels(QVariantList list);
75 79
76 80 public:
77 81 // Extending QChart with DeclarativeChart is not possible because QObject does not support
78 82 // multi inheritance, so we now have a QChart as a member instead
79 83 QChart *m_chart;
80 84 ChartLegend m_legend;
81 85 };
82 86
83 87 QTCOMMERCIALCHART_END_NAMESPACE
84 88
85 89 #endif // DECLARATIVECHART_H
General Comments 0
You need to be logged in to leave comments. Login now