##// END OF EJS Templates
QML and static data in pie and xy series
Tero Ahola -
r1186:0385556f52b3
parent child
Show More
@@ -25,15 +25,6 Rectangle {
25 anchors.fill: parent
25 anchors.fill: parent
26 property int __explodedIndex: -1
26 property int __explodedIndex: -1
27
27
28 ChartModel {
29 id: chartModel
30 ChartModelRow { values: ["Volkswagen", 13.5] }
31 ChartModelRow { values: ["Toyota", 10.9] }
32 ChartModelRow { values: ["Ford", 8.6] }
33 ChartModelRow { values: ["Skoda", 8.2] }
34 ChartModelRow { values: ["Volvo", 6.8] }
35 }
36
37 ChartView {
28 ChartView {
38 id: chart
29 id: chart
39 title: "Top-5 car brand shares in Finland"
30 title: "Top-5 car brand shares in Finland"
@@ -45,22 +36,41 Rectangle {
45 legend: ChartView.LegendBottom
36 legend: ChartView.LegendBottom
46 animationOptions: ChartView.SeriesAnimations
37 animationOptions: ChartView.SeriesAnimations
47
38
39 // If you have static data, you can simply use the PieSlice API
48 PieSeries {
40 PieSeries {
49 id: pieSeries
41 id: pieSeries
50 model: chartModel
42 PieSlice { label: "Volkswagen"; value: 13.5 }
51 modelMapper.mapLabels: 0
43 PieSlice { label: "Toyota"; value: 10.9 }
52 modelMapper.mapValues: 1
44 PieSlice { label: "Ford"; value: 8.6 }
53 modelMapper.first: 0
45 PieSlice { label: "Skoda"; value: 8.2 }
54 modelMapper.count: -1 // "Undefined" = -1 by default
46 PieSlice { label: "Volvo"; value: 6.8 }
55 modelMapper.orientation: PieModelMapper.Vertical
56
57 // TODO: PieSlice to append the data directly into the mapped columns
58 //PieSlice { label: "Toyota"; value: 10.9 }
59 }
47 }
48
49 // For dynamic data you can use the ChartModel API.
50 // ChartModel {
51 // id: chartModel
52 // ChartModelRow { values: ["Volkswagen", 13.5] }
53 // ChartModelRow { values: ["Toyota", 10.9] }
54 // ChartModelRow { values: ["Ford", 8.6] }
55 // ChartModelRow { values: ["Skoda", 8.2] }
56 // ChartModelRow { values: ["Volvo", 6.8] }
57 // }
58
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 {
61 // id: pieSeries
62 // model: chartModel
63 // modelMapper.mapLabels: 0
64 // modelMapper.mapValues: 1
65 // modelMapper.first: 0
66 // modelMapper.count: -1 // "Undefined" = -1 by default
67 // modelMapper.orientation: PieModelMapper.Vertical
68 // }
60 }
69 }
61
70
62 Component.onCompleted: {
71 Component.onCompleted: {
63 chartModel.append(["Others", 52.0]);
72 // You can also add data dynamically
73 pieSeries.model.append(["Others", 52.0]);
64 }
74 }
65
75
66 Timer {
76 Timer {
@@ -32,26 +32,22 Rectangle {
32 ScatterSeries {
32 ScatterSeries {
33 id: scatter1
33 id: scatter1
34 name: "Scatter1"
34 name: "Scatter1"
35 model: ChartModel {
35 XyPoint { x: 1.5; y: 1.5 }
36 XyPoint { x: 1.5; y: 1.5 }
36 XyPoint { x: 1.5; y: 1.6 }
37 XyPoint { x: 1.5; y: 1.6 }
37 XyPoint { x: 1.57; y: 1.55 }
38 XyPoint { x: 1.57; y: 1.55 }
38 XyPoint { x: 1.8; y: 1.8 }
39 XyPoint { x: 1.8; y: 1.8 }
39 XyPoint { x: 1.9; y: 1.6 }
40 XyPoint { x: 1.9; y: 1.6 }
40 XyPoint { x: 2.1; y: 1.3 }
41 XyPoint { x: 2.1; y: 1.3 }
41 XyPoint { x: 2.5; y: 2.1 }
42 XyPoint { x: 2.5; y: 2.1 }
43 }
44 }
42 }
45 ScatterSeries {
43 ScatterSeries {
46 name: "Scatter2"
44 name: "Scatter2"
47 model: ChartModel {
45 XyPoint { x: 2.0; y: 2.0 }
48 XyPoint { x: 2.0; y: 2.0 }
46 XyPoint { x: 2.0; y: 2.1 }
49 XyPoint { x: 2.0; y: 2.1 }
47 XyPoint { x: 2.07; y: 2.05 }
50 XyPoint { x: 2.07; y: 2.05 }
48 XyPoint { x: 2.2; y: 2.9 }
51 XyPoint { x: 2.2; y: 2.9 }
49 XyPoint { x: 2.4; y: 2.7 }
52 XyPoint { x: 2.4; y: 2.7 }
50 XyPoint { x: 2.67; y: 2.65 }
53 XyPoint { x: 2.67; y: 2.65 }
54 }
55 }
51 }
56 }
52 }
57 }
53 }
@@ -55,8 +55,11 void DeclarativeTableModel::classBegin()
55
55
56 void DeclarativeTableModel::componentComplete()
56 void DeclarativeTableModel::componentComplete()
57 {
57 {
58 foreach (QObject *child, children())
58 foreach (QObject *child, children()) {
59 appendToModel(child);
59 if (qobject_cast<DeclarativeTableModelRow *>(child)) {
60 append(qobject_cast<DeclarativeTableModelRow *>(child)->values());
61 }
62 }
60 }
63 }
61
64
62 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
65 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
@@ -94,57 +97,50 void DeclarativeTableModel::append(QVariantList values)
94 dataChanged(beginIndex, endIndex);
97 dataChanged(beginIndex, endIndex);
95 }
98 }
96
99
97 void DeclarativeTableModel::appendToModel(QObject *object)
100 void DeclarativeTableModel::appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point)
98 {
101 {
99 if (qobject_cast<QBarSet *>(object)) {
102 qDebug() << "DeclarativeTableModel::appendPoint:" << point;
100 DeclarativeBarModel *model = qobject_cast<DeclarativeBarModel *>(this);
103 QVariantList values;
101 Q_ASSERT(model);
104 values.insert(mapper->mapX(), point->x());
102 model->append(qobject_cast<QBarSet *>(object));
105 values.insert(mapper->mapY(), point->y());
103 } else if (qobject_cast<QPieSlice *>(object)) {
106 append(values);
104 // TODO
105 } else if (qobject_cast<DeclarativeXyPoint *>(object)) {
106 // TODO
107 appendPoint(qobject_cast<DeclarativeXyPoint *>(object));
108 } else if (qobject_cast<DeclarativeTableModel *>(this)) {
109 append(qobject_cast<DeclarativeTableModelRow *>(object)->values());
110 }
111 }
107 }
112
108
113 void DeclarativeTableModel::appendPoints(QVariantList points)
109 //void DeclarativeTableModel::appendPoints(QVariantList points)
114 {
110 //{
115 qreal x = 0.0;
111 // qreal x = 0.0;
116 for (int i(0); i < points.count(); i++) {
112 // for (int i(0); i < points.count(); i++) {
117 if (i % 2) {
113 // if (i % 2) {
118 bool ok(false);
114 // bool ok(false);
119 qreal y = points.at(i).toReal(&ok);
115 // qreal y = points.at(i).toReal(&ok);
120 if (ok) {
116 // if (ok) {
121 DeclarativeXyPoint *point= new DeclarativeXyPoint();
117 // DeclarativeXyPoint *point= new DeclarativeXyPoint();
122 point->setX(x);
118 // point->setX(x);
123 point->setY(y);
119 // point->setY(y);
124 appendPoint(point);
120 // appendPoint(point);
125 } else {
121 // } else {
126 qWarning() << "Illegal y value";
122 // qWarning() << "Illegal y value";
127 }
123 // }
128 } else {
124 // } else {
129 bool ok(false);
125 // bool ok(false);
130 x = points.at(i).toReal(&ok);
126 // x = points.at(i).toReal(&ok);
131 if (!ok) {
127 // if (!ok) {
132 qWarning() << "Illegal x value";
128 // qWarning() << "Illegal x value";
133 }
129 // }
134 }
130 // }
135 }
131 // }
136 }
132 //}
137
133
138 void DeclarativeTableModel::appendPoint(DeclarativeXyPoint* point)
134 //void DeclarativeTableModel::appendPoint(DeclarativeXyPoint* point)
139 {
135 //{
140 // qDebug() << "DeclarativeTableModel::append:" << point->x() << " " << point->y();
136 //// qDebug() << "DeclarativeTableModel::append:" << point->x() << " " << point->y();
141 insertRow(rowCount());
137 // insertRow(rowCount());
142 QModelIndex xModelIndex = createIndex(rowCount() - 1, 0);
138 // QModelIndex xModelIndex = createIndex(rowCount() - 1, 0);
143 QModelIndex yModelIndex = createIndex(rowCount() - 1, 1);
139 // QModelIndex yModelIndex = createIndex(rowCount() - 1, 1);
144 setData(xModelIndex, point->x());
140 // setData(xModelIndex, point->x());
145 setData(yModelIndex, point->y());
141 // setData(yModelIndex, point->y());
146 dataChanged(xModelIndex, yModelIndex);
142 // dataChanged(xModelIndex, yModelIndex);
147 }
143 //}
148
144
149 ////////////// Bar model ///////////////////////
145 ////////////// Bar model ///////////////////////
150
146
@@ -26,6 +26,7
26 #include <QPieSlice>
26 #include <QPieSlice>
27 #include "../src/charttablemodel.h" // TODO
27 #include "../src/charttablemodel.h" // TODO
28 #include <QBarSet>
28 #include <QBarSet>
29 #include <QXYModelMapper>
29 #include <QDeclarativeListProperty>
30 #include <QDeclarativeListProperty>
30 #include <QVariant>
31 #include <QVariant>
31 #include <QDeclarativeParserStatus>
32 #include <QDeclarativeParserStatus>
@@ -55,6 +56,7 class DeclarativeTableModel : public ChartTableModel, public QDeclarativeParserS
55 public:
56 public:
56 explicit DeclarativeTableModel(QObject *parent = 0);
57 explicit DeclarativeTableModel(QObject *parent = 0);
57 QDeclarativeListProperty<QObject> modelChildren();
58 QDeclarativeListProperty<QObject> modelChildren();
59 void appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point);
58
60
59 public: // from QDeclarativeParserStatus
61 public: // from QDeclarativeParserStatus
60 void classBegin();
62 void classBegin();
@@ -62,12 +64,8 public: // from QDeclarativeParserStatus
62
64
63 public Q_SLOTS:
65 public Q_SLOTS:
64 void append(QVariantList slices);
66 void append(QVariantList slices);
65 void appendPoints(QVariantList points);
66 void appendPoint(DeclarativeXyPoint* point);
67 static void appendModelChild(QDeclarativeListProperty<QObject> *list,
67 static void appendModelChild(QDeclarativeListProperty<QObject> *list,
68 QObject *element);
68 QObject *element);
69 private:
70 void appendToModel(QObject *object);
71 };
69 };
72
70
73 class DeclarativeBarModel : public DeclarativeTableModel
71 class DeclarativeBarModel : public DeclarativeTableModel
@@ -30,7 +30,7 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 // Set default mapper parameters to allow easy to use PieSeries api
36 QPieModelMapper *mapper = new QPieModelMapper();
36 QPieModelMapper *mapper = new QPieModelMapper();
@@ -42,6 +42,31 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
42 setModelMapper(mapper);
42 setModelMapper(mapper);
43 }
43 }
44
44
45 void DeclarativePieSeries::classBegin()
46 {
47 }
48
49 void DeclarativePieSeries::componentComplete()
50 {
51 foreach(QObject *child, children()) {
52 qDebug() << "pie child: " << child;
53 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);
61 }
62 }
63 }
64
65 QDeclarativeListProperty<QPieSlice> DeclarativePieSeries::initialSlices()
66 {
67 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieSeries::appendInitialSlices);
68 }
69
45 QPieSlice *DeclarativePieSeries::slice(int index)
70 QPieSlice *DeclarativePieSeries::slice(int index)
46 {
71 {
47 QList<QPieSlice*> sliceList = slices();
72 QList<QPieSlice*> sliceList = slices();
@@ -22,8 +22,9
22 #define DECLARATIVEPIESERIES_H
22 #define DECLARATIVEPIESERIES_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "qpieslice.h"
25 #include <QPieSlice>
26 #include "qpieseries.h"
26 #include <QPieSeries>
27 #include <QDeclarativeParserStatus>
27 #include <QDeclarativeListProperty>
28 #include <QDeclarativeListProperty>
28 #include <QAbstractItemModel>
29 #include <QAbstractItemModel>
29 #include <QVariant>
30 #include <QVariant>
@@ -33,20 +34,27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
34
34 class QChart;
35 class QChart;
35
36
36 class DeclarativePieSeries : public QPieSeries
37 class DeclarativePieSeries : public QPieSeries, public QDeclarativeParserStatus
37 {
38 {
38 Q_OBJECT
39 Q_OBJECT
40 Q_INTERFACES(QDeclarativeParserStatus)
39 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)
43 Q_CLASSINFO("DefaultProperty", "initialSlices")
40
44
41 public:
45 public:
42 explicit DeclarativePieSeries(QObject *parent = 0);
46 explicit DeclarativePieSeries(QObject *parent = 0);
43
47 QDeclarativeListProperty<QPieSlice> initialSlices();
44 public:
48 DeclarativeTableModel *pieModel();
49 void setPieModel(DeclarativeTableModel *model);
45 Q_INVOKABLE QPieSlice *slice(int index);
50 Q_INVOKABLE QPieSlice *slice(int index);
46
51
47 public:
52 public:
48 void setPieModel(DeclarativeTableModel *model);
53 void classBegin();
49 DeclarativeTableModel *pieModel();
54 void componentComplete();
55
56 public Q_SLOTS:
57 static void appendInitialSlices(QDeclarativeListProperty<QPieSlice> */*list*/, QPieSlice */*element*/) {}
50 };
58 };
51
59
52 QTCOMMERCIALCHART_END_NAMESPACE
60 QTCOMMERCIALCHART_END_NAMESPACE
@@ -30,6 +30,18 DeclarativeScatterSeries::DeclarativeScatterSeries(QObject *parent) :
30 {
30 {
31 }
31 }
32
32
33 QDeclarativeListProperty<QObject> DeclarativeScatterSeries::declarativeChildren()
34 {
35 return QDeclarativeListProperty<QObject>(this, 0, &appendDeclarativeChildren);
36 }
37
38 void DeclarativeScatterSeries::appendDeclarativeChildren(QDeclarativeListProperty<QObject> *list, QObject *element)
39 {
40 DeclarativeScatterSeries *series = qobject_cast<DeclarativeScatterSeries *>(list->object);
41 if (series && qobject_cast<DeclarativeXyPoint *>(element))
42 series->declarativeModel()->appendPoint(series->modelMapper(), qobject_cast<DeclarativeXyPoint *>(element));
43 }
44
33 #include "moc_declarativescatterseries.cpp"
45 #include "moc_declarativescatterseries.cpp"
34
46
35 QTCOMMERCIALCHART_END_NAMESPACE
47 QTCOMMERCIALCHART_END_NAMESPACE
@@ -24,7 +24,6
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "qscatterseries.h"
25 #include "qscatterseries.h"
26 #include "declarativexyseries.h"
26 #include "declarativexyseries.h"
27 #include <QDeclarativeParserStatus>
28
27
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
29
@@ -32,9 +31,15 class DeclarativeScatterSeries : public QScatterSeries, public DeclarativeXySeri
32 {
31 {
33 Q_OBJECT
32 Q_OBJECT
34 Q_PROPERTY(DeclarativeTableModel *model READ declarativeModel WRITE setDeclarativeModel)
33 Q_PROPERTY(DeclarativeTableModel *model READ declarativeModel WRITE setDeclarativeModel)
34 Q_PROPERTY(QDeclarativeListProperty<QObject> declarativeChildren READ declarativeChildren)
35 Q_CLASSINFO("DefaultProperty", "declarativeChildren")
35
36
36 public:
37 public:
37 explicit DeclarativeScatterSeries(QObject *parent = 0);
38 explicit DeclarativeScatterSeries(QObject *parent = 0);
39 QDeclarativeListProperty<QObject> declarativeChildren();
40
41 public Q_SLOTS:
42 static void appendDeclarativeChildren(QDeclarativeListProperty<QObject> *list, QObject *element);
38 };
43 };
39
44
40 QTCOMMERCIALCHART_END_NAMESPACE
45 QTCOMMERCIALCHART_END_NAMESPACE
@@ -20,9 +20,10
20
20
21 //#include "DeclarativeXySeries.h"
21 //#include "DeclarativeXySeries.h"
22 #include "declarativexyseries.h"
22 #include "declarativexyseries.h"
23 #include "declarativechart.h"
23 #include <QXYSeries>
24 #include <QXYSeries>
24 #include <QXYModelMapper>
25 #include <QXYModelMapper>
25 #include "declarativechart.h"
26 #include <QDeclarativeListProperty>
26
27
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
29
@@ -30,6 +31,8 DeclarativeXySeries::DeclarativeXySeries()
30 {
31 {
31 // All the inherited objects must be of type QXYSeries, so it is safe to cast
32 // All the inherited objects must be of type QXYSeries, so it is safe to cast
32 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
33 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
34 // TODO: set default model on init?
35 series->setModel(new DeclarativeTableModel());
33 QXYModelMapper *mapper = new QXYModelMapper(series);
36 QXYModelMapper *mapper = new QXYModelMapper(series);
34 mapper->setMapX(0);
37 mapper->setMapX(0);
35 mapper->setMapY(1);
38 mapper->setMapY(1);
@@ -51,10 +54,6 bool DeclarativeXySeries::setDeclarativeModel(DeclarativeTableModel *model)
51 // All the inherited objects must be of type QXYSeries, so it is safe to cast
54 // All the inherited objects must be of type QXYSeries, so it is safe to cast
52 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
55 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
53 series->setModel(m);
56 series->setModel(m);
54 // QXYModelMapper *mapper = new QXYModelMapper;
55 // mapper->setMapX(0);
56 // mapper->setMapY(1);
57 // series->setModelMapper(mapper);
58 } else {
57 } else {
59 qWarning("DeclarativeXySeries: Illegal model");
58 qWarning("DeclarativeXySeries: Illegal model");
60 }
59 }
@@ -32,8 +32,6 class QAbstractSeries;
32
32
33 class DeclarativeXySeries
33 class DeclarativeXySeries
34 {
34 {
35 Q_INTERFACES(QDeclarativeParserStatus)
36
37 public:
35 public:
38 explicit DeclarativeXySeries();
36 explicit DeclarativeXySeries();
39 ~DeclarativeXySeries();
37 ~DeclarativeXySeries();
General Comments 0
You need to be logged in to leave comments. Login now