##// END OF EJS Templates
QML bar series categories
Tero Ahola -
r726:5bffcd7b4ba5
parent child
Show More
@@ -1,50 +1,69
1 #include "declarativebarseries.h"
1 #include "declarativebarseries.h"
2 #include "declarativechart.h"
2 #include "declarativechart.h"
3 #include "qchart.h"
3 #include "qchart.h"
4 #include "qbarseries.h"
4 #include "qbarseries.h"
5 #include "qbarset.h"
5 #include "qbarset.h"
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) :
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());
21
19
22 if (declarativeChart) {
20 if (declarativeChart) {
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");
32 QBarSet *set1 = new QBarSet("Bob");
32 QBarSet *set1 = new QBarSet("Bob");
33 QBarSet *set2 = new QBarSet("Guybrush");
33 QBarSet *set2 = new QBarSet("Guybrush");
34
34
35 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
35 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
36 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
36 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
37 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
37 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
38
38
39 m_series->addBarSet(set0);
39 m_series->addBarSet(set0);
40 m_series->addBarSet(set1);
40 m_series->addBarSet(set1);
41 m_series->addBarSet(set2);
41 m_series->addBarSet(set2);
42
42
43 chart->addSeries(m_series);
43 chart->addSeries(m_series);
44 }
44 }
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
@@ -1,33 +1,39
1 #ifndef DECLARATIVEBARSERIES_H
1 #ifndef DECLARATIVEBARSERIES_H
2 #define DECLARATIVEBARSERIES_H
2 #define DECLARATIVEBARSERIES_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QDeclarativeItem>
5 #include <QDeclarativeItem>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 class QChart;
9 class QChart;
10 class QBarSeries;
10 class QBarSeries;
11
11
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
32
38
33 #endif // DECLARATIVEBARSERIES_H
39 #endif // DECLARATIVEBARSERIES_H
@@ -1,63 +1,63
1 #include "declarativescatterseries.h"
1 #include "declarativescatterseries.h"
2 #include "declarativechart.h"
2 #include "declarativechart.h"
3 #include "qchart.h"
3 #include "qchart.h"
4 #include "qscatterseries.h"
4 #include "qscatterseries.h"
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) :
8 DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) :
9 QDeclarativeItem(parent),
9 QDeclarativeItem(parent),
10 m_chart(0),
10 m_chart(0),
11 m_series(0)
11 m_series(0)
12 {
12 {
13 setFlag(QGraphicsItem::ItemHasNoContents, false);
13 setFlag(QGraphicsItem::ItemHasNoContents, false);
14 }
14 }
15
15
16 DeclarativeScatterSeries::~DeclarativeScatterSeries()
16 DeclarativeScatterSeries::~DeclarativeScatterSeries()
17 {
17 {
18 }
18 }
19
19
20 void DeclarativeScatterSeries::componentComplete()
20 void DeclarativeScatterSeries::componentComplete()
21 {
21 {
22 Q_ASSERT(!m_series);
22 Q_ASSERT(!m_series);
23 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
23 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
24
24
25 if (declarativeChart) {
25 if (declarativeChart) {
26 m_chart = qobject_cast<QChart *>(declarativeChart->m_chart);
26 m_chart = qobject_cast<QChart *>(declarativeChart->m_chart);
27 qDebug() << "creating scatter series for chart: " << m_chart;
27 qDebug() << "creating scatter series for chart: " << m_chart;
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 }
59 }
59 }
60
60
61 #include "moc_declarativescatterseries.cpp"
61 #include "moc_declarativescatterseries.cpp"
62
62
63 QTCOMMERCIALCHART_END_NAMESPACE
63 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,45 +1,44
1 #ifndef DECLARATIVESCATTERSERIES_H
1 #ifndef DECLARATIVESCATTERSERIES_H
2 #define DECLARATIVESCATTERSERIES_H
2 #define DECLARATIVESCATTERSERIES_H
3
3
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);
21 ~DeclarativeScatterSeries();
20 ~DeclarativeScatterSeries();
22
21
23 public: // from QDeclarativeParserStatus
22 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:
36
35
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
44
43
45 #endif // DECLARATIVESCATTERSERIES_H
44 #endif // DECLARATIVESCATTERSERIES_H
@@ -1,100 +1,104
1 import QtQuick 1.0
1 import QtQuick 1.0
2 import QtCommercial.Chart 1.0
2 import QtCommercial.Chart 1.0
3
3
4 Rectangle {
4 Rectangle {
5 width: parent.width
5 width: parent.width
6 height: parent.height
6 height: parent.height
7
7
8 // Another option for QML data api:
8 // Another option for QML data api:
9 // ListModel {
9 // ListModel {
10 // id: listModelForPie
10 // id: listModelForPie
11 // // PieDataElement
11 // // PieDataElement
12 // ListElement {
12 // ListElement {
13 // label: "Apple"
13 // label: "Apple"
14 // value: 4.3
14 // value: 4.3
15 // }
15 // }
16 // ListElement {
16 // ListElement {
17 // label: "Blackberry"
17 // label: "Blackberry"
18 // value: 15.1
18 // value: 15.1
19 // }
19 // }
20 // }
20 // }
21
21
22 Component.onCompleted: {
22 Component.onCompleted: {
23 // console.log("model:" + myModel.item(0));
23 // console.log("model:" + myModel.item(0));
24 // myModel.insert(1, {"time":1.4; "speed":41.1 });
24 // myModel.insert(1, {"time":1.4; "speed":41.1 });
25 // scatter.appendData();
25 // scatter.appendData();
26 }
26 }
27
27
28
28
29 Chart {
29 Chart {
30 id: chart1
30 id: chart1
31 anchors.top: parent.top
31 anchors.top: parent.top
32 anchors.left: parent.left
32 anchors.left: parent.left
33 anchors.right: parent.right
33 anchors.right: parent.right
34 height: parent.height / 2
34 height: parent.height / 2
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 {
41 // data: [
45 // data: [
42 // PieSlice { label: "Volkswagen"; value: 13.5 },
46 // PieSlice { label: "Volkswagen"; value: 13.5 },
43 // PieSlice { label: "Toyota"; value: 10.9 },
47 // PieSlice { label: "Toyota"; value: 10.9 },
44 // PieSlice { label: "Ford"; value: 8.6 },
48 // PieSlice { label: "Ford"; value: 8.6 },
45 // PieSlice { label: "Skoda"; value: 8.2 },
49 // PieSlice { label: "Skoda"; value: 8.2 },
46 // PieSlice { label: "Volvo"; value: 6.8 },
50 // PieSlice { label: "Volvo"; value: 6.8 },
47 // PieSlice { label: "Others"; value: 52.0 }
51 // PieSlice { label: "Others"; value: 52.0 }
48 // ]
52 // ]
49 // }
53 // }
50 }
54 }
51
55
52
56
53 Chart {
57 Chart {
54 id: chart2
58 id: chart2
55 anchors.top: chart1.bottom
59 anchors.top: chart1.bottom
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: [
63 XyPoint { x: 0.0; y: 0.0 },
67 XyPoint { x: 0.0; y: 0.0 },
64 XyPoint { x: 1.1; y: 2.1 },
68 XyPoint { x: 1.1; y: 2.1 },
65 XyPoint { x: 2.9; y: 4.9 },
69 XyPoint { x: 2.9; y: 4.9 },
66 XyPoint { x: 3.2; y: 3.0 }
70 XyPoint { x: 3.2; y: 3.0 }
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