##// END OF EJS Templates
QML bar series categories
Tero Ahola -
r726:5bffcd7b4ba5
parent child
Show More
@@ -10,11 +10,9 DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) :
10 QDeclarativeItem(parent)
10 QDeclarativeItem(parent)
11 {
11 {
12 setFlag(QGraphicsItem::ItemHasNoContents, false);
12 setFlag(QGraphicsItem::ItemHasNoContents, false);
13 connect(this, SIGNAL(parentChanged()),
14 this, SLOT(setParentForSeries()));
15 }
13 }
16
14
17 void DeclarativeBarSeries::setParentForSeries()
15 void DeclarativeBarSeries::componentComplete()
18 {
16 {
19 if (!m_series) {
17 if (!m_series) {
20 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
18 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
@@ -23,9 +21,11 void DeclarativeBarSeries::setParentForSeries()
23 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
21 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
24 Q_ASSERT(chart);
22 Q_ASSERT(chart);
25
23
26 QStringList categories;
24 // QStringList categories;
27 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
25 // categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
28 m_series = new QBarSeries(categories);
26 // m_series = new QBarSeries(categories);
27 // m_series = new QBarSeries(m_categories);
28 m_series = new QBarSeries(m_categories);
29
29
30 // TODO: use data from model
30 // TODO: use data from model
31 QBarSet *set0 = new QBarSet("Bub");
31 QBarSet *set0 = new QBarSet("Bub");
@@ -45,6 +45,25 void DeclarativeBarSeries::setParentForSeries()
45 }
45 }
46 }
46 }
47
47
48 void DeclarativeBarSeries::setBarCategories(QStringList categories)
49 {
50 m_categories = categories;
51 if (m_series) {
52 // Replace categories of the QBarSeries with the new categories
53 for (int i(0); i < m_categories.count(); i++) {
54 if (m_series->categories().at(i) != m_categories.at(i))
55 m_series->insertCategory(m_series->categoryCount(), m_categories.at(i));
56 }
57 while (m_series->categoryCount() > m_categories.count())
58 m_series->removeCategory(m_series->categoryCount() - 1);
59 }
60 }
61
62 QStringList DeclarativeBarSeries::barCategories()
63 {
64 return m_categories;
65 }
66
48 #include "moc_declarativebarseries.cpp"
67 #include "moc_declarativebarseries.cpp"
49
68
50 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
@@ -12,20 +12,26 class QBarSeries;
12 class DeclarativeBarSeries : public QDeclarativeItem
12 class DeclarativeBarSeries : public QDeclarativeItem
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 Q_PROPERTY(QStringList barCategories READ barCategories WRITE setBarCategories)
15
16
16 public:
17 public:
17 explicit DeclarativeBarSeries(QDeclarativeItem *parent = 0);
18 explicit DeclarativeBarSeries(QDeclarativeItem *parent = 0);
18
19
20 public: // from QDeclarativeParserStatus
21 void componentComplete();
22
23 public:
24 void setBarCategories(QStringList categories);
25 QStringList barCategories();
26
19 signals:
27 signals:
20
28
21 public slots:
29 public slots:
22
30
23 private slots:
31 public:
24 void setParentForSeries();
25
26 private:
27 QChart *m_chart;
32 QChart *m_chart;
28 QBarSeries *m_series;
33 QBarSeries *m_series;
34 QStringList m_categories;
29 };
35 };
30
36
31 QTCOMMERCIALCHART_END_NAMESPACE
37 QTCOMMERCIALCHART_END_NAMESPACE
@@ -28,31 +28,31 void DeclarativeScatterSeries::componentComplete()
28 Q_ASSERT(m_chart);
28 Q_ASSERT(m_chart);
29
29
30 m_series = new QScatterSeries();
30 m_series = new QScatterSeries();
31 for (int i(0); i < m_data.count(); i++) {
31 for (int i(0); i < m_points.count(); i++) {
32 DeclarativeXyPoint *element = m_data.at(i);
32 DeclarativeXyPoint *element = m_points.at(i);
33 *m_series << QPointF(element->x(), element->y());
33 *m_series << QPointF(element->x(), element->y());
34 }
34 }
35 m_chart->addSeries(m_series);
35 m_chart->addSeries(m_series);
36 }
36 }
37 }
37 }
38
38
39 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeScatterSeries::data()
39 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeScatterSeries::points()
40 {
40 {
41 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0,
41 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0,
42 &DeclarativeScatterSeries::appendData);
42 &DeclarativeScatterSeries::appendPoints);
43 }
43 }
44
44
45 void DeclarativeScatterSeries::appendData(QDeclarativeListProperty<DeclarativeXyPoint> *list,
45 void DeclarativeScatterSeries::appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
46 DeclarativeXyPoint *element)
46 DeclarativeXyPoint *element)
47 {
47 {
48 DeclarativeScatterSeries *series = qobject_cast<DeclarativeScatterSeries *>(list->object);
48 DeclarativeScatterSeries *series = qobject_cast<DeclarativeScatterSeries *>(list->object);
49 qDebug() << "appendData: " << series;
49 qDebug() << "appendPoints: " << series;
50 qDebug() << "appendData: " << element;
50 qDebug() << "appendPoints: " << element;
51 qDebug() << "appendData: " << element->x();
51 qDebug() << "appendPoints: " << element->x();
52 qDebug() << "appendData: " << element->y();
52 qDebug() << "appendPoints: " << element->y();
53 qDebug() << "appendData: " << series->m_series;
53 qDebug() << "appendPoints: " << series->m_series;
54 if (series) {
54 if (series) {
55 series->m_data.append(element);
55 series->m_points.append(element);
56 if (series->m_series)
56 if (series->m_series)
57 series->m_series->add(element->x(), element->y());
57 series->m_series->add(element->x(), element->y());
58 }
58 }
@@ -4,17 +4,16
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "declarativexypoint.h"
5 #include "declarativexypoint.h"
6 #include <QDeclarativeItem>
6 #include <QDeclarativeItem>
7 #include <QDeclarativeParserStatus>
8
7
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
9
11 class QChart;
10 class QChart;
12 class QScatterSeries;
11 class QScatterSeries;
13
12
14 class DeclarativeScatterSeries : public QDeclarativeItem//, public QDeclarativeParserStatus
13 class DeclarativeScatterSeries : public QDeclarativeItem
15 {
14 {
16 Q_OBJECT
15 Q_OBJECT
17 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> data READ data)
16 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> points READ points)
18
17
19 public:
18 public:
20 explicit DeclarativeScatterSeries(QDeclarativeItem *parent = 0);
19 explicit DeclarativeScatterSeries(QDeclarativeItem *parent = 0);
@@ -24,12 +23,12 public: // from QDeclarativeParserStatus
24 void componentComplete();
23 void componentComplete();
25
24
26 public:
25 public:
27 QDeclarativeListProperty<DeclarativeXyPoint> data();
26 QDeclarativeListProperty<DeclarativeXyPoint> points();
28
27
29 signals:
28 signals:
30
29
31 public slots:
30 public slots:
32 static void appendData(QDeclarativeListProperty<DeclarativeXyPoint> *list,
31 static void appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
33 DeclarativeXyPoint *element);
32 DeclarativeXyPoint *element);
34
33
35 private slots:
34 private slots:
@@ -37,7 +36,7 private slots:
37 public:
36 public:
38 QChart *m_chart; // not owned
37 QChart *m_chart; // not owned
39 QScatterSeries *m_series; // not owned
38 QScatterSeries *m_series; // not owned
40 QList<DeclarativeXyPoint *> m_data;
39 QList<DeclarativeXyPoint *> m_points; // not owned
41 };
40 };
42
41
43 QTCOMMERCIALCHART_END_NAMESPACE
42 QTCOMMERCIALCHART_END_NAMESPACE
@@ -35,6 +35,10 Rectangle {
35 theme: Chart.ThemeBlueCerulean
35 theme: Chart.ThemeBlueCerulean
36
36
37 BarSeries {
37 BarSeries {
38 barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
39 // data: [
40 // BarSet { }
41 // ]
38 }
42 }
39
43
40 // PieSeries {
44 // PieSeries {
@@ -56,7 +60,7 Rectangle {
56 anchors.bottom: parent.bottom
60 anchors.bottom: parent.bottom
57 anchors.left: parent.left
61 anchors.left: parent.left
58 anchors.right: parent.right
62 anchors.right: parent.right
59 theme: Chart.ThemeScientific
63 theme: Chart.ThemeBrownSand
60
64
61 LineSeries {
65 LineSeries {
62 data: [
66 data: [
@@ -67,34 +71,34 Rectangle {
67 ]
71 ]
68 }
72 }
69
73
70 // ScatterSeries {
74 ScatterSeries {
71 // id: scatter
75 id: scatter
72 // data: [
76 points: [
73 // XyPoint { x: 1.1; y: 1.1 },
77 XyPoint { x: 1.1; y: 1.1 },
74 // XyPoint { x: 1.1; y: 1.2 },
78 XyPoint { x: 1.1; y: 1.2 },
75 // XyPoint { x: 1.17; y: 1.15 }
79 XyPoint { x: 1.17; y: 1.15 }
76 // ]
80 ]
77 // }
81 }
78 // ScatterSeries {
82 ScatterSeries {
79 // data: [
83 points: [
80 // XyPoint { x: 1.5; y: 1.5 },
84 XyPoint { x: 1.5; y: 1.5 },
81 // XyPoint { x: 1.5; y: 1.6 },
85 XyPoint { x: 1.5; y: 1.6 },
82 // XyPoint { x: 1.57; y: 1.55 }
86 XyPoint { x: 1.57; y: 1.55 }
83 // ]
87 ]
84 // }
88 }
85 // ScatterSeries {
89 ScatterSeries {
86 // data: [
90 points: [
87 // XyPoint { x: 2.0; y: 2.0 },
91 XyPoint { x: 2.0; y: 2.0 },
88 // XyPoint { x: 2.0; y: 2.1 },
92 XyPoint { x: 2.0; y: 2.1 },
89 // XyPoint { x: 2.07; y: 2.05 }
93 XyPoint { x: 2.07; y: 2.05 }
90 // ]
94 ]
91 // }
95 }
92 // ScatterSeries {
96 ScatterSeries {
93 // data: [
97 points: [
94 // XyPoint { x: 2.6; y: 2.6 },
98 XyPoint { x: 2.6; y: 2.6 },
95 // XyPoint { x: 2.6; y: 2.7 },
99 XyPoint { x: 2.6; y: 2.7 },
96 // XyPoint { x: 2.67; y: 2.65 }
100 XyPoint { x: 2.67; y: 2.65 }
97 // ]
101 ]
98 // }
102 }
99 }
103 }
100 }
104 }
General Comments 0
You need to be logged in to leave comments. Login now