##// END OF EJS Templates
Added declarative model for bar series
Tero Ahola -
r1162:e5feb9c12a84
parent child
Show More
@@ -23,6 +23,7 import QtCommercial.Chart 1.0
23 23
24 24 Rectangle {
25 25 anchors.fill: parent
26 property int __explodedIndex: -1
26 27
27 28 ChartView {
28 29 id: chart
@@ -36,9 +37,12 Rectangle {
36 37 animationOptions: ChartView.SeriesAnimations
37 38
38 39 PieSeries {
40 id: pieSeries
39 41 model: PieModel {
40 42 id: pieModel
41 PieSlice { label: "Volkswagen"; value: 13.5 }
43 // TODO: initializing properties does not work at the moment, see DeclarativePieModel::append
44 // TODO: explode range, color, border color, border thickness, font, ..
45 PieSlice { exploded: true; label: "Volkswagen"; value: 13.5 }
42 46 PieSlice { label: "Toyota"; value: 10.9 }
43 47 PieSlice { label: "Ford"; value: 8.6 }
44 48 PieSlice { label: "Skoda"; value: 8.2 }
@@ -47,6 +51,23 Rectangle {
47 51 }
48 52 }
49 53
54 Timer {
55 repeat: true
56 interval: 2000
57 running: true
58 onTriggered: {
59 changeSliceExploded(__explodedIndex);
60 __explodedIndex = (__explodedIndex + 1) % pieModel.count;
61 changeSliceExploded(__explodedIndex);
62 }
63 }
64
65 function changeSliceExploded(index) {
66 if (index >= 0 && index < pieModel.count) {
67 pieSeries.slice(index).exploded = !pieSeries.slice(index).exploded;
68 }
69 }
70
50 71 Rectangle {
51 72 id: button
52 73 anchors.bottom: parent.bottom
@@ -74,6 +95,7 Rectangle {
74 95 // TODO: this should also be doable by redefining the range inside the model
75 96 button.state = "";
76 97 pieModel.removeRow(pieModel.count - 1);
98 // TODO: removeAll("label") ?
77 99 }
78 100 }
79 101 }
@@ -89,9 +111,14 Rectangle {
89 111 // // column3 not used by pie series
90 112 // PieSeries {
91 113 // model: chartModel
92 // mappings: [ {"column1":"label"}, {"column2":"value"} ]
114 // modelMapping: PieMapping {
115 // labels: 0 // undefined by default
116 // values: 1 // undefined by default
117 // first: 0 // 0 by default
118 // count: 10 // "Undefined" by default
119 // orientation: PieMapping.Vertical // Vertical by default
120 // }
93 121 // }
94
95 122
96 123 // TODO: show how to use data from a list model in a chart view
97 124 // i.e. copy the data into a chart model
@@ -49,6 +49,19 Rectangle {
49 49 }
50 50 }
51 51
52 // TODO: optional implementation with generic ChartModel
53 // AreaSeries {
54 // model: chartModel
55 // modelMapping: XyMapping {
56 // xValues: 0 // undefined by default
57 // yValues: 1 // undefined by default
58 // first: 0 // 0 by default
59 // count: 10 // "Undefined" by default
60 // orientation: XyMapping.Vertical // Vertical by default
61 // }
62 // }
63
64
52 65 XYModel {
53 66 id: zerosModel
54 67 XyPoint { x: 0; y: 0 }
@@ -29,10 +29,50 Rectangle {
29 29 anchors.fill: parent
30 30 theme: ChartView.ChartThemeLight
31 31 legend: ChartView.LegendBottom
32 // axisXLabels: ["0", "2008", "1", "2009", "2", "2010", "3", "2012"]
33 axisX.max: 10
32 34
33 35 BarSeries {
34 36 barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
35 // data implementation missing
37 model: barModel
36 38 }
39
40
41 // // TODO: optional syntax with ChartModel base model API
42 // BarSeries {
43 // model: chartModel
44 // modelMapping: BarSeriesMapping {
45 // // Giving "undefined" x mapping value means that the indexes are used as x-values
46 // setIndexes: [BarSeriesMapping.Undefined, 0,
47 // BarSeriesMapping.Undefined, 1,
48 // BarSeriesMapping.Undefined, 2] // defaults to []
49 //// setValues: [
50 //// BarSetMapping {x: BarSetMapping.Undefined; y: 0},
51 //// BarSetMapping {x: BarSetMapping.Undefined; y: 1},
52 //// BarSetMapping {x: BarSetMapping.Undefined; y: 2}
53 //// ]
54 // orientation: BarSeriesMapping.Vertical // defaults to Vertical
55 // startIndex: 0 // defaults to 0
56 // count: BarSeriesMapping.Undefined // defaults to "Undefined"
57 // }
58 // }
59 }
60
61 // ChartModel {
62 // id: chartModel
63 // }
64
65 BarModel {
66 id: barModel
67 BarSet { name: "Bob"; values: [2, 2, 3, 4, 5, 6] }
68 BarSet { name: "Bub"; values: [5, 1, 2, 4, 1, 7] }
69 BarSet { name: "Bib"; values: [3, 5, 8, 13, 5, 8] }
37 70 }
71
72 // TODO
73 // Component.onCompleted: {
74 // bobBars.append(1.2);
75 // bobBars.append(1.5);
76 // bobBars.append([1.5, 1.4, 1.9]);
77 // }
38 78 }
@@ -20,60 +20,76
20 20
21 21 #include "declarativebarseries.h"
22 22 #include "declarativechart.h"
23 #include "qchart.h"
24 #include "qbarseries.h"
25 #include "qbarset.h"
23 #include <QBarSet>
26 24
27 25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 26
29 DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) :
30 QDeclarativeItem(parent)
27 DeclarativeBarSet::DeclarativeBarSet(QObject *parent) :
28 QBarSet("", parent)
31 29 {
32 setFlag(QGraphicsItem::ItemHasNoContents, false);
33 30 }
34 31
35 void DeclarativeBarSeries::componentComplete()
32 QVariantList DeclarativeBarSet::values()
36 33 {
34 QVariantList values;
35 for (int i(0); i < count(); i++)
36 values.append(QVariant(at(i)));
37 return values;
37 38 }
38 39
39 void DeclarativeBarSeries::setBarCategories(QStringList categories)
40 void DeclarativeBarSet::setValues(QVariantList values)
40 41 {
41 m_categories = categories;
42 while (count())
43 remove(count() - 1);
42 44
43 if (m_series) {
44 delete m_series;
45 m_series = 0;
45 for (int i(0); i < values.count(); i++) {
46 if (values.at(i).canConvert(QVariant::Double))
47 append(values[i].toDouble());
48 }
46 49 }
47 50
48 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
49 if (declarativeChart) {
50 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
51 Q_ASSERT(chart);
52
53 // m_series = new QBarSeries(m_categories);
54 m_series = new QBarSeries();
55 m_series->setCategories(m_categories);
51 DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) :
52 QBarSeries(parent)
53 {
54 }
56 55
57 // TODO: use data from model
58 QBarSet *set0 = new QBarSet("Bub");
59 QBarSet *set1 = new QBarSet("Bob");
60 QBarSet *set2 = new QBarSet("Guybrush");
56 void DeclarativeBarSeries::classBegin()
57 {
58 }
61 59
62 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
63 *set1 << 5 << 1 << 2 << 4 << 1 << 7;
64 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
60 void DeclarativeBarSeries::componentComplete()
61 {
62 if (model())
63 setModelMapping(0, 1, 1, Qt::Vertical);
64 }
65 65
66 m_series->appendBarSet(set0);
67 m_series->appendBarSet(set1);
68 m_series->appendBarSet(set2);
66 bool DeclarativeBarSeries::setDeclarativeModel(DeclarativeBarModel *model)
67 {
68 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
69 bool value(false);
70 if (m) {
71 value = setModel(m);
72 //setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
73 setModelMapping(0, 1, 1, Qt::Vertical);
74 } else {
75 qWarning("DeclarativeBarSeries: Illegal model");
76 }
77 return value;
78 }
69 79
70 chart->addSeries(m_series);
80 DeclarativeBarModel *DeclarativeBarSeries::declarativeModel()
81 {
82 return qobject_cast<DeclarativeBarModel *>(model());
71 83 }
84
85 void DeclarativeBarSeries::setBarCategories(QStringList categories)
86 {
87 setCategories(categories);
72 88 }
73 89
74 90 QStringList DeclarativeBarSeries::barCategories()
75 91 {
76 return m_categories;
92 return categories();
77 93 }
78 94
79 95 #include "moc_declarativebarseries.cpp"
@@ -22,25 +22,45
22 22 #define DECLARATIVEBARSERIES_H
23 23
24 24 #include "qchartglobal.h"
25 #include "declarativemodel.h"
25 26 #include <QDeclarativeItem>
27 #include <QDeclarativeParserStatus>
28 #include <QBarSeries>
26 29
27 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 31
29 32 class QChart;
30 33 class QBarSeries;
31 34
32 class DeclarativeBarSeries : public QDeclarativeItem
35 class DeclarativeBarSet : public QBarSet
33 36 {
34 37 Q_OBJECT
38 Q_PROPERTY(QVariantList values READ values WRITE setValues)
39 Q_PROPERTY(QString name READ name WRITE setName)
40
41 public:
42 explicit DeclarativeBarSet(QObject *parent = 0);
43 QVariantList values();
44 void setValues(QVariantList values);
45 };
46
47 class DeclarativeBarSeries : public QBarSeries, public QDeclarativeParserStatus
48 {
49 Q_OBJECT
50 Q_INTERFACES(QDeclarativeParserStatus)
51 Q_PROPERTY(DeclarativeBarModel *model READ declarativeModel WRITE setDeclarativeModel)
35 52 Q_PROPERTY(QStringList barCategories READ barCategories WRITE setBarCategories)
36 53
37 54 public:
38 55 explicit DeclarativeBarSeries(QDeclarativeItem *parent = 0);
39 56
40 57 public: // from QDeclarativeParserStatus
58 void classBegin();
41 59 void componentComplete();
42 60
43 61 public:
62 bool setDeclarativeModel(DeclarativeBarModel *model);
63 DeclarativeBarModel *declarativeModel();
44 64 void setBarCategories(QStringList categories);
45 65 QStringList barCategories();
46 66
@@ -49,9 +69,6 Q_SIGNALS:
49 69 public Q_SLOTS:
50 70
51 71 public:
52 QChart *m_chart;
53 QBarSeries *m_series;
54 QStringList m_categories;
55 72 };
56 73
57 74 QTCOMMERCIALCHART_END_NAMESPACE
@@ -24,10 +24,54
24 24
25 25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 26
27 ////////////// Table model (base) ///////////////////
28
29 DeclarativeTableModel::DeclarativeTableModel(QObject *parent) :
30 ChartTableModel(parent)
31 {
32 }
33
34 void DeclarativeTableModel::classBegin()
35 {
36 }
37
38 void DeclarativeTableModel::componentComplete()
39 {
40 foreach (QObject *child, children())
41 appendToModel(child);
42 }
43
44 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
45 {
46 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeTableModel::appendModelChild);
47 }
48
49 void DeclarativeTableModel::appendModelChild(QDeclarativeListProperty<QObject> *list,
50 QObject *child)
51 {
52 // childs are added in componentComplete instead
53 Q_UNUSED(list)
54 Q_UNUSED(child)
55 }
56
57 void DeclarativeTableModel::appendToModel(QObject *object)
58 {
59 if (qobject_cast<DeclarativeBarModel *>(this)) {
60 DeclarativeBarModel *model = qobject_cast<DeclarativeBarModel *>(this);
61 model->append(qobject_cast<QBarSet *>(object));
62 } else if (qobject_cast<DeclarativePieModel *>(this)) {
63 DeclarativePieModel *model = qobject_cast<DeclarativePieModel *>(this);
64 model->append(qobject_cast<QPieSlice *>(object));
65 } else if (qobject_cast<DeclarativeXyModel *>(this)) {
66 DeclarativeXyModel *model = qobject_cast<DeclarativeXyModel *>(this);
67 model->append(qobject_cast<DeclarativeXyPoint *>(object));
68 }
69 }
70
27 71 ////////////// XY model ///////////////////////
28 72
29 73 DeclarativeXyModel::DeclarativeXyModel(QObject *parent) :
30 ChartTableModel(parent)
74 DeclarativeTableModel(parent)
31 75 {
32 76 }
33 77
@@ -67,25 +111,10 void DeclarativeXyModel::append(QVariantList points)
67 111 }
68 112 }
69 113
70 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeXyModel::points()
71 {
72 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0, &DeclarativeXyModel::appendPoint);
73 }
74
75 void DeclarativeXyModel::appendPoint(QDeclarativeListProperty<DeclarativeXyPoint> *list,
76 DeclarativeXyPoint *point)
77 {
78 DeclarativeXyModel *model = qobject_cast<DeclarativeXyModel *>(list->object);
79 if (model)
80 model->append(point);
81 else
82 qWarning() << "Illegal point item";
83 }
84
85 114 ////////////// Pie model ///////////////////////
86 115
87 116 DeclarativePieModel::DeclarativePieModel(QObject *parent) :
88 ChartTableModel(parent)
117 DeclarativeTableModel(parent)
89 118 {
90 119 }
91 120
@@ -109,6 +138,10 void DeclarativePieModel::append(QVariantList slices)
109 138 if (ok) {
110 139 QPieSlice *slice = new QPieSlice(value, label);
111 140 append(slice);
141 // TODO: how to copy the properties to the newly added slice?
142 // (DeclarativePieModel::append only copies the label and value to the model)
143 // QPieSlice *addedSlice = append(slice);
144 // addedSlice->setExploded(slice->isExploded());
112 145 } else {
113 146 qWarning() << "Illegal slice item";
114 147 }
@@ -118,19 +151,25 void DeclarativePieModel::append(QVariantList slices)
118 151 }
119 152 }
120 153
121 QDeclarativeListProperty<QPieSlice> DeclarativePieModel::slices()
154 ////////////// Bar model ///////////////////////
155
156 DeclarativeBarModel::DeclarativeBarModel(QObject *parent) :
157 DeclarativeTableModel(parent)
122 158 {
123 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieModel::appendSlice);
124 159 }
125 160
126 void DeclarativePieModel::appendSlice(QDeclarativeListProperty<QPieSlice> *list,
127 QPieSlice *slice)
161 void DeclarativeBarModel::append(QBarSet* barSet)
128 162 {
129 DeclarativePieModel *pieModel = qobject_cast<DeclarativePieModel *>(list->object);
130 if (pieModel)
131 pieModel->append(slice);
132 else
133 qWarning() << "Illegal slice item";
163 insertColumn(columnCount());
164 for (int i(0); i < barSet->count(); i++) {
165 if (rowCount() < (i + 1))
166 insertRow(rowCount());
167 setData(createIndex(i, columnCount() - 1), barSet->at(i));
168 // insertRow(rowCount());
169 // setData(createIndex(rowCount() - 1, 0), );
170 // setData(createIndex(rowCount() - 1, 1), barSet->at(i));
171 }
172 // TODO: setModelMapping(0, 1, columnCount(), Qt::Vertical);
134 173 }
135 174
136 175 #include "moc_declarativemodel.cpp"
@@ -23,46 +23,70
23 23
24 24 #include "qchartglobal.h"
25 25 #include "declarativexypoint.h"
26 #include "qpieslice.h"
26 #include <QPieSlice>
27 27 #include "../src/charttablemodel.h" // TODO
28 #include <QBarSet>
28 29 #include <QDeclarativeListProperty>
29 30 #include <QVariant>
31 #include <QDeclarativeParserStatus>
30 32
31 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 34
33 class DeclarativeXyModel : public ChartTableModel
35 class DeclarativeTableModel : public ChartTableModel, public QDeclarativeParserStatus
36 {
37 Q_OBJECT
38 Q_INTERFACES(QDeclarativeParserStatus)
39 Q_PROPERTY(QDeclarativeListProperty<QObject> modelChildren READ modelChildren)
40 Q_CLASSINFO("DefaultProperty", "modelChildren")
41
42 public:
43 explicit DeclarativeTableModel(QObject *parent = 0);
44 QDeclarativeListProperty<QObject> modelChildren();
45
46 public: // from QDeclarativeParserStatus
47 void classBegin();
48 void componentComplete();
49
50 public Q_SLOTS:
51 static void appendModelChild(QDeclarativeListProperty<QObject> *list,
52 QObject *element);
53 private:
54 void appendToModel(QObject *object);
55 };
56
57 class DeclarativeXyModel : public DeclarativeTableModel
34 58 {
35 59 Q_OBJECT
36 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> points READ points)
37 Q_CLASSINFO("DefaultProperty", "points")
38 60
39 61 public:
40 62 explicit DeclarativeXyModel(QObject *parent = 0);
41 QDeclarativeListProperty<DeclarativeXyPoint> points();
42 63
43 64 public Q_SLOTS:
44 65 void append(DeclarativeXyPoint* point);
45 66 void append(QVariantList points);
46 static void appendPoint(QDeclarativeListProperty<DeclarativeXyPoint> *list,
47 DeclarativeXyPoint *element);
48 67 };
49 68
50
51 class DeclarativePieModel : public ChartTableModel
69 class DeclarativePieModel : public DeclarativeTableModel
52 70 {
53 71 Q_OBJECT
54 Q_PROPERTY(QDeclarativeListProperty<QPieSlice> slices READ slices)
55 Q_CLASSINFO("DefaultProperty", "slices")
56 72
57 73 public:
58 74 explicit DeclarativePieModel(QObject *parent = 0);
59 QDeclarativeListProperty<QPieSlice> slices();
60 75
61 76 public Q_SLOTS:
62 77 void append(QPieSlice* slice);
63 78 void append(QVariantList slices);
64 static void appendSlice(QDeclarativeListProperty<QPieSlice> *list,
65 QPieSlice *element);
79 };
80
81 class DeclarativeBarModel : public DeclarativeTableModel
82 {
83 Q_OBJECT
84
85 public:
86 explicit DeclarativeBarModel(QObject *parent = 0);
87
88 public Q_SLOTS:
89 void append(QBarSet* barSet);
66 90 };
67 91
68 92 QTCOMMERCIALCHART_END_NAMESPACE
@@ -30,6 +30,15 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
30 30 {
31 31 }
32 32
33 QPieSlice *DeclarativePieSeries::slice(int index)
34 {
35 QList<QPieSlice*> sliceList = slices();
36 if (index < sliceList.count())
37 return sliceList[index];
38
39 return 0;
40 }
41
33 42 bool DeclarativePieSeries::setPieModel(DeclarativePieModel *model)
34 43 {
35 44 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
@@ -41,6 +41,9 class DeclarativePieSeries : public QPieSeries
41 41 public:
42 42 explicit DeclarativePieSeries(QObject *parent = 0);
43 43
44 public:
45 Q_INVOKABLE QPieSlice *slice(int index);
46
44 47 public Q_SLOTS:
45 48
46 49 public:
@@ -40,7 +40,7 bool DeclarativeXySeries::setDeclarativeModel(DeclarativeXyModel *model)
40 40 if (m) {
41 41 // All the inherited objects must be of type QXYSeries, so it is safe to cast
42 42 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
43 series->setModel(m);
43 value = series->setModel(m);
44 44 series->setModelMapping(0, 1, Qt::Vertical);
45 45 } else {
46 46 qWarning("DeclarativeXySeries: Illegal model");
@@ -30,7 +30,6
30 30 #include "declarativescatterseries.h"
31 31 #include "declarativebarseries.h"
32 32 #include "declarativepieseries.h"
33 //#include "declarativepiemodel.h"
34 33
35 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 35
@@ -54,9 +53,10 public:
54 53 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
55 54 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
56 55 qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice");
57 // TODO: a declarative model for each type
58 56 qmlRegisterType<DeclarativePieModel>(uri, 1, 0, "PieModel");
59 57 qmlRegisterType<DeclarativeXyModel>(uri, 1, 0, "XYModel");
58 qmlRegisterType<DeclarativeBarModel>(uri, 1, 0, "BarModel");
59 qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet");
60 60 }
61 61 };
62 62
@@ -137,6 +137,9 void QBarSet::replace(const int index, const qreal value)
137 137 */
138 138 qreal QBarSet::at(const int index) const
139 139 {
140 if (index < 0 || index >= d_ptr->m_values.count())
141 return 0.0;
142
140 143 return d_ptr->m_values.at(index);
141 144 }
142 145
@@ -32,6 +32,7 class QBarSetPrivate;
32 32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
33 33 {
34 34 Q_OBJECT
35 Q_PROPERTY(QString name READ name WRITE setName)
35 36
36 37 public:
37 38 explicit QBarSet(const QString name, QObject *parent = 0);
@@ -64,16 +64,15 QVariant ChartTableModel::headerData (int section, Qt::Orientation orientation,
64 64 if (role != Qt::DisplayRole)
65 65 return QVariant();
66 66
67 if (orientation == Qt::Horizontal)
68 {
67 if (orientation == Qt::Horizontal) {
69 68 if (section%2 == 0)
70 69 return "x";
71 70 else
72 71 return "y";
73 }
74 else
72 } else {
75 73 return QString("%1").arg(section + 1);
76 74 }
75 }
77 76
78 77 QVariant ChartTableModel::data(const QModelIndex &index, int role) const
79 78 {
@@ -33,8 +33,9 class PieSliceData;
33 33 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
34 34 {
35 35 Q_OBJECT
36 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY changed)
37 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY changed)
36 Q_PROPERTY(QString label READ label WRITE setLabel)
37 Q_PROPERTY(qreal value READ value WRITE setValue)
38 Q_PROPERTY(bool exploded READ isExploded WRITE setExploded)
38 39
39 40 public:
40 41 explicit QPieSlice(QObject *parent = 0);
General Comments 0
You need to be logged in to leave comments. Login now