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