##// END OF EJS Templates
Removed usage of model in QML pie example
Tero Ahola -
r1189:88d7235253d9
parent child
Show More
@@ -1,136 +1,142
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 property int __explodedIndex: -1
27 27
28 28 ChartView {
29 29 id: chart
30 30 title: "Top-5 car brand shares in Finland"
31 31 anchors.top: parent.top
32 32 anchors.bottom: button.top
33 33 anchors.left: parent.left
34 34 anchors.right: parent.right
35 35 theme: ChartView.ChartThemeLight
36 36 legend: ChartView.LegendBottom
37 37 animationOptions: ChartView.SeriesAnimations
38 38
39 39 // If you have static data, you can simply use the PieSlice API
40 40 PieSeries {
41 41 id: pieSeries
42 PieSlice { label: "Volkswagen"; value: 13.5 }
42 PieSlice { id: volkswagenSlice; label: "Volkswagen"; value: 13.5 }
43 43 PieSlice { label: "Toyota"; value: 10.9 }
44 44 PieSlice { label: "Ford"; value: 8.6 }
45 45 PieSlice { label: "Skoda"; value: 8.2 }
46 46 PieSlice { label: "Volvo"; value: 6.8 }
47 47 }
48 48
49 // For dynamic data you can use the ChartModel API.
49 // TODO: move ChartModel API into a demo application instead of making it a public API
50 // // For dynamic data you can use the ChartModel API.
50 51 // ChartModel {
51 52 // id: chartModel
52 53 // ChartModelRow { values: ["Volkswagen", 13.5] }
53 54 // ChartModelRow { values: ["Toyota", 10.9] }
54 55 // ChartModelRow { values: ["Ford", 8.6] }
55 56 // ChartModelRow { values: ["Skoda", 8.2] }
56 57 // ChartModelRow { values: ["Volvo", 6.8] }
57 58 // }
58
59 // In this case you need to define how the data maps to pie slices with the ModelMapper API of the pie series.
59 // // In this case you need to define how the data maps to pie slices with the ModelMapper API of the pie series.
60 60 // PieSeries {
61 61 // id: pieSeries
62 62 // model: chartModel
63 63 // modelMapper.mapLabels: 0
64 64 // modelMapper.mapValues: 1
65 65 // modelMapper.first: 0
66 66 // modelMapper.count: -1 // "Undefined" = -1 by default
67 67 // modelMapper.orientation: PieModelMapper.Vertical
68 68 // }
69
70 // TODO: you could also append to your model, for example:
71 // pieSeries.model.append(["Others", 52.0]);
69 72 }
70 73
71 74 Component.onCompleted: {
72 // You can also add data dynamically
73 pieSeries.model.append(["Others", 52.0]);
75 volkswagenSlice.exploded = true;
76 // You can also add slices dynamically
77 var newSlice = pieSeries.append("Others", 52.0);
74 78 }
75 79
76 80 Timer {
77 81 repeat: true
78 82 interval: 2000
79 83 running: true
80 84 onTriggered: {
81 85 // Set all slices as not exploded
82 86 for (var i = 0; i < pieSeries.count; i++)
83 pieSeries.slice(i).exploded = false;
87 pieSeries.at(i).exploded = false;
84 88
85 89 // Explode one of the slices
86 90 __explodedIndex = (__explodedIndex + 1) % pieSeries.count;
87 pieSeries.slice(__explodedIndex).exploded = true;
91 pieSeries.at(__explodedIndex).exploded = true;
92
93 // TODO: implement for convenience
94 // pieSeries.find("Ford").exploded = true;
95 // pieSeries.removeAll("Ford")
88 96 }
89 97 }
90 98
91 99 Rectangle {
92 100 id: button
93 101 anchors.bottom: parent.bottom
94 102 anchors.bottomMargin: 10
95 103 anchors.horizontalCenter: parent.horizontalCenter
96 104 height: 40
97 105 width: 100
98 106 color: "orange"
99 107 radius: 5
100 108 Text {
101 109 id: buttonText
102 110 anchors.centerIn: parent
103 111 text: "Hide others"
104 112 }
105 113 MouseArea {
106 114 anchors.fill: parent
107 115 onClicked: {
108 116 if (buttonText.text == "Show others") {
109 117 pieSeries.modelMapper.count = -1;
110 118 buttonText.text = "Hide others";
111 119 } else {
112 120 pieSeries.modelMapper.count = 5;
113 121 buttonText.text = "Show others";
114 //pieModel.removeRow(pieModel.count - 1);
115 // TODO: removeAll("label") ?
116 122 }
117 123 }
118 124 }
119 125 }
120 126
121 127 // TODO: show how to use data from a list model in a chart view
122 128 // i.e. copy the data into a chart model
123 129 // ListModel {
124 130 // id: listModel
125 131 // ListElement {
126 132 // label: "Volkswagen"
127 133 // value: 13.5
128 134 // }
129 135 // ListElement {
130 136 // label: "Toyota"
131 137 // value: 10.9
132 138 // }
133 139 // // and so on...
134 140 // }
135 141
136 142 }
@@ -1,79 +1,78
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 legend: ChartView.LegendTop
32 31 axisXLabels: ["0", "2000", "1", "2001", "2", "2002", "3", "2003", "4", "2004", "5", "2005",
33 32 "6", "2006", "7", "2007", "8", "2008", "9", "2009", "10", "2010", "11", "2011"]
34 33
35 34 AreaSeries {
36 35 name: "Russian"
37 36 upperModel: chartModel
38 37 upperModelMapper.mapX: 0
39 38 upperModelMapper.mapY: 2
40 39 lowerModel: chartModel
41 40 lowerModelMapper.mapX: 0
42 41 lowerModelMapper.mapY: 1
43 42 }
44 43 AreaSeries {
45 44 name: "Swedish"
46 45 upperModel: chartModel
47 46 upperModelMapper.mapX: 0
48 47 upperModelMapper.mapY: 3
49 48 lowerModel: chartModel
50 49 lowerModelMapper.mapX: 0
51 50 lowerModelMapper.mapY: 1
52 51 }
53 52 AreaSeries {
54 53 name: "Finnish"
55 54 upperModel: chartModel
56 55 upperModelMapper.mapX: 0
57 56 upperModelMapper.mapY: 4
58 57 lowerModel: chartModel
59 58 lowerModelMapper.mapX: 0
60 59 lowerModelMapper.mapY: 1
61 60 }
62 61 }
63 62
64 63 ChartModel {
65 64 id: chartModel
66 65 ChartModelRow { values: [0, 0, 1, 1, 0] }
67 66 ChartModelRow { values: [1, 0, 1, 1, 0] }
68 67 ChartModelRow { values: [2, 0, 1, 3, 0] }
69 68 ChartModelRow { values: [3, 0, 1, 3, 0] }
70 69 ChartModelRow { values: [4, 0, 1, 2, 0] }
71 70 ChartModelRow { values: [5, 0, 0, 0, 0] }
72 71 ChartModelRow { values: [6, 0, 1, 2, 1] }
73 72 ChartModelRow { values: [7, 0, 1, 1, 0] }
74 73 ChartModelRow { values: [8, 0, 4, 2, 0] }
75 74 ChartModelRow { values: [9, 0, 3, 1, 0] }
76 75 ChartModelRow { values: [10, 0, 2, 3, 0] }
77 76 ChartModelRow { values: [11, 0, 1, 3, 1] }
78 77 }
79 78 }
@@ -1,173 +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 m_legend(LegendDisabled)
29 m_legend(LegendTop)
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 146 QVariantList DeclarativeChart::axisXLabels()
147 147 {
148 148 QVariantList labels;
149 149 foreach (qreal value, m_chart->axisX()->categories()->values()) {
150 150 // qDebug() << "Label for" << value << "is" << m_chart->axisX()->categories()->label(value);
151 151 labels.append(value);
152 152 labels.append(m_chart->axisX()->categories()->label(value));
153 153 }
154 154 return labels;
155 155 }
156 156
157 157 void DeclarativeChart::setAxisXLabels(QVariantList list)
158 158 {
159 159 QVariant value(QVariant::Invalid);
160 160 foreach (QVariant element, list) {
161 161 if (value.isValid() && element.type() == QVariant::String) {
162 162 m_chart->axisX()->categories()->insert(value.toDouble(), element.toString());
163 163 value = QVariant(QVariant::Invalid);
164 164 } else {
165 165 if (element.canConvert(QVariant::Double))
166 166 value = element;
167 167 }
168 168 }
169 169 }
170 170
171 171 #include "moc_declarativechart.cpp"
172 172
173 173 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,96 +1,95
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 "declarativepieseries.h"
22 22 #include "declarativechart.h"
23 23 #include "qchart.h"
24 24 #include <qdeclarativelist.h>
25 25 #include "qpiemodelmapper.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
30 30 QPieSeries(parent)
31 31 {
32 32 // TODO: set default model on init?
33 setModel(new DeclarativeTableModel());
33 // setModel(new DeclarativeTableModel());
34 34
35 // Set default mapper parameters to allow easy to use PieSeries api
35 // TODO: Set default mapper parameters to allow easy to use PieSeries api?
36 36 QPieModelMapper *mapper = new QPieModelMapper();
37 37 mapper->setMapLabels(0);
38 38 mapper->setMapValues(1);
39 39 mapper->setOrientation(Qt::Vertical);
40 40 mapper->setFirst(0);
41 41 mapper->setCount(-1);
42 42 setModelMapper(mapper);
43 43 }
44 44
45 45 void DeclarativePieSeries::classBegin()
46 46 {
47 47 }
48 48
49 49 void DeclarativePieSeries::componentComplete()
50 50 {
51 51 foreach(QObject *child, children()) {
52 qDebug() << "pie child: " << child;
53 52 if (qobject_cast<QPieSlice *>(child)) {
54 QPieSlice *slice = qobject_cast<QPieSlice *>(child);
55 QVariantList values;
56 values.insert(modelMapper()->mapLabels(), slice->label());
57 values.insert(modelMapper()->mapValues(), slice->value());
58 DeclarativeTableModel *m = qobject_cast<DeclarativeTableModel *>(model());
59 Q_ASSERT(m);
60 m->append(values);
53 QPieSeries::append(qobject_cast<QPieSlice *>(child));
61 54 }
62 55 }
63 56 }
64 57
65 58 QDeclarativeListProperty<QPieSlice> DeclarativePieSeries::initialSlices()
66 59 {
67 60 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieSeries::appendInitialSlices);
68 61 }
69 62
70 QPieSlice *DeclarativePieSeries::slice(int index)
63 QPieSlice *DeclarativePieSeries::at(int index)
71 64 {
72 65 QList<QPieSlice*> sliceList = slices();
73 66 if (index < sliceList.count())
74 67 return sliceList[index];
75 68
76 69 return 0;
77 70 }
78 71
72 QPieSlice* DeclarativePieSeries::append(QString name, qreal value)
73 {
74 // TODO: parameter order is wrong, switch it:
75 return QPieSeries::append(value, name);
76 }
77
79 78 void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model)
80 79 {
81 80 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
82 81 if (m) {
83 82 QPieSeries::setModel(m);
84 83 } else {
85 84 qWarning("DeclarativePieSeries: Illegal model");
86 85 }
87 86 }
88 87
89 88 DeclarativeTableModel *DeclarativePieSeries::pieModel()
90 89 {
91 90 return qobject_cast<DeclarativeTableModel *>(model());
92 91 }
93 92
94 93 #include "moc_declarativepieseries.cpp"
95 94
96 95 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,62 +1,63
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 DECLARATIVEPIESERIES_H
22 22 #define DECLARATIVEPIESERIES_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include <QPieSlice>
26 26 #include <QPieSeries>
27 27 #include <QDeclarativeParserStatus>
28 28 #include <QDeclarativeListProperty>
29 29 #include <QAbstractItemModel>
30 30 #include <QVariant>
31 31 #include "declarativemodel.h"
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 class QChart;
36 36
37 37 class DeclarativePieSeries : public QPieSeries, public QDeclarativeParserStatus
38 38 {
39 39 Q_OBJECT
40 40 Q_INTERFACES(QDeclarativeParserStatus)
41 41 Q_PROPERTY(DeclarativeTableModel *model READ pieModel WRITE setPieModel)
42 42 Q_PROPERTY(QDeclarativeListProperty<QPieSlice> initialSlices READ initialSlices)
43 43 Q_CLASSINFO("DefaultProperty", "initialSlices")
44 44
45 45 public:
46 46 explicit DeclarativePieSeries(QObject *parent = 0);
47 47 QDeclarativeListProperty<QPieSlice> initialSlices();
48 48 DeclarativeTableModel *pieModel();
49 49 void setPieModel(DeclarativeTableModel *model);
50 Q_INVOKABLE QPieSlice *slice(int index);
50 Q_INVOKABLE QPieSlice *at(int index);
51 Q_INVOKABLE QPieSlice* append(QString name, qreal value);
51 52
52 53 public:
53 54 void classBegin();
54 55 void componentComplete();
55 56
56 57 public Q_SLOTS:
57 58 static void appendInitialSlices(QDeclarativeListProperty<QPieSlice> */*list*/, QPieSlice */*element*/) {}
58 59 };
59 60
60 61 QTCOMMERCIALCHART_END_NAMESPACE
61 62
62 63 #endif // DECLARATIVEPIESERIES_H
General Comments 0
You need to be logged in to leave comments. Login now