##// END OF EJS Templates
Dynamic data for QML pie and line series
Tero Ahola -
r215:4c8c373c63a9
parent child
Show More
@@ -0,0 +1,55
1 #include "declarativelineseries.h"
2 #include "declarativechart.h"
3 #include "qchart.h"
4 #include "qlinechartseries.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 DeclarativeLineSeries::DeclarativeLineSeries(QDeclarativeItem *parent) :
9 QDeclarativeItem(parent)
10 {
11 setFlag(QGraphicsItem::ItemHasNoContents, false);
12 connect(this, SIGNAL(parentChanged()),
13 this, SLOT(setParentForSeries()));
14 }
15
16 void DeclarativeLineSeries::setParentForSeries()
17 {
18 if (!m_series) {
19 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
20
21 if (declarativeChart) {
22 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
23 qDebug() << "creating line series for chart: " << chart;
24 Q_ASSERT(chart);
25
26 m_series = new QLineChartSeries();
27 Q_ASSERT(m_series);
28 for (int i(0); i < m_data.count(); i++) {
29 ScatterElement *element = m_data.at(i);
30 m_series->add(element->x(), element->y());
31 }
32 chart->addSeries(m_series);
33 }
34 }
35 }
36
37 QDeclarativeListProperty<ScatterElement> DeclarativeLineSeries::data()
38 {
39 return QDeclarativeListProperty<ScatterElement>(this, 0, &DeclarativeLineSeries::appendData);
40 }
41
42 void DeclarativeLineSeries::appendData(QDeclarativeListProperty<ScatterElement> *list,
43 ScatterElement *element)
44 {
45 DeclarativeLineSeries *series = qobject_cast<DeclarativeLineSeries *>(list->object);
46 if (series) {
47 series->m_data.append(element);
48 if (series->m_series)
49 series->m_series->add(element->x(), element->y());
50 }
51 }
52
53 #include "moc_declarativelineseries.cpp"
54
55 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,39
1 #ifndef DECLARATIVELINESERIES_H
2 #define DECLARATIVELINESERIES_H
3
4 #include "qchartglobal.h"
5 #include "scatterelement.h" // TODO: rename header
6 #include <QDeclarativeItem>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class QChart;
11 class QLineChartSeries;
12
13 class DeclarativeLineSeries : public QDeclarativeItem
14 {
15 Q_OBJECT
16 Q_PROPERTY(QDeclarativeListProperty<ScatterElement> data READ data)
17
18 public:
19 explicit DeclarativeLineSeries(QDeclarativeItem *parent = 0);
20 QDeclarativeListProperty<ScatterElement> data();
21
22 signals:
23
24 public slots:
25 static void appendData(QDeclarativeListProperty<ScatterElement> *list,
26 ScatterElement *element);
27
28 private slots:
29 void setParentForSeries();
30
31 private:
32 QChart *m_chart;
33 QLineChartSeries *m_series;
34 QList<ScatterElement *> m_data;
35 };
36
37 QTCOMMERCIALCHART_END_NAMESPACE
38
39 #endif // DECLARATIVELINESERIES_H
@@ -0,0 +1,58
1 #include "declarativepieseries.h"
2 #include "declarativechart.h"
3 #include "qchart.h"
4 #include "qpieseries.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 DeclarativePieSeries::DeclarativePieSeries(QDeclarativeItem *parent) :
9 QDeclarativeItem(parent),
10 m_chart(0),
11 m_series(0)
12 {
13 setFlag(QGraphicsItem::ItemHasNoContents, false);
14 connect(this, SIGNAL(parentChanged()),
15 this, SLOT(setParentForSeries()));
16 }
17
18 void DeclarativePieSeries::setParentForSeries()
19 {
20 if (!m_series) {
21 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
22 if (declarativeChart) {
23 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
24 qDebug() << "creating pie series for chart: " << chart;
25 Q_ASSERT(chart);
26
27 m_series = new QPieSeries();
28 Q_ASSERT(m_series);
29 foreach (QPieSlice* slice, m_data) {
30 // Have to duplicate the data
31 m_series->add(slice->value(), slice->label());
32 }
33 chart->addSeries(m_series);
34 //chart->axisY();
35 }
36 }
37 }
38
39 QDeclarativeListProperty<QPieSlice> DeclarativePieSeries::data()
40 {
41 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieSeries::appendData);
42 }
43
44 void DeclarativePieSeries::appendData(QDeclarativeListProperty<QPieSlice> *list,
45 QPieSlice *slice)
46 {
47 DeclarativePieSeries *series = qobject_cast<DeclarativePieSeries *>(list->object);
48 if (series) {
49 series->m_data.append(slice);
50 // Have to duplicate the data
51 if (series->m_series)
52 series->m_series->add(slice->value(), slice->label());
53 }
54 }
55
56 #include "moc_declarativepieseries.cpp"
57
58 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,39
1 #ifndef DECLARATIVEPIESERIES_H
2 #define DECLARATIVEPIESERIES_H
3
4 #include "qchartglobal.h"
5 #include "qpieslice.h"
6 #include <QDeclarativeItem>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class QPieSeries;
11 class QChart;
12
13 class DeclarativePieSeries : public QDeclarativeItem
14 {
15 Q_OBJECT
16 Q_PROPERTY(QDeclarativeListProperty<QPieSlice> data READ data)
17
18 public:
19 explicit DeclarativePieSeries(QDeclarativeItem *parent = 0);
20 QDeclarativeListProperty<QPieSlice> data();
21
22 signals:
23
24 public slots:
25 static void appendData(QDeclarativeListProperty<QPieSlice> *list,
26 QPieSlice *element);
27
28 private slots:
29 void setParentForSeries();
30
31 private:
32 QChart *m_chart;
33 QPieSeries *m_series;
34 QList<QPieSlice *> m_data;
35 };
36
37 QTCOMMERCIALCHART_END_NAMESPACE
38
39 #endif // DECLARATIVEPIESERIES_H
@@ -2,7 +2,6
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 #include <cmath>
6
5
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
7
@@ -24,7 +24,6 signals:
24 public slots:
24 public slots:
25 static void appendData(QDeclarativeListProperty<ScatterElement> *list,
25 static void appendData(QDeclarativeListProperty<ScatterElement> *list,
26 ScatterElement *element);
26 ScatterElement *element);
27 // void append(ScatterElement element);
28
27
29 private slots:
28 private slots:
30 void setParentForSeries();
29 void setParentForSeries();
@@ -4,6 +4,8
4 #include "declarativeseries.h"
4 #include "declarativeseries.h"
5 #include "scatterelement.h"
5 #include "scatterelement.h"
6 #include "declarativescatterseries.h"
6 #include "declarativescatterseries.h"
7 #include "declarativelineseries.h"
8 #include "declarativepieseries.h"
7
9
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
11
@@ -14,10 +16,15 public:
14 virtual void registerTypes(const char *uri)
16 virtual void registerTypes(const char *uri)
15 {
17 {
16 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
18 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
19
17 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "Chart");
20 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "Chart");
18 qmlRegisterType<DeclarativeSeries>(uri, 1, 0, "Series");
21 qmlRegisterType<DeclarativeSeries>(uri, 1, 0, "Series");
19 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
22 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
20 qmlRegisterType<ScatterElement>(uri, 1, 0, "ScatterElement");
23 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
24 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
25 qmlRegisterType<QPieSlice>(uri, 1, 0, "ChartPieElement");
26 // TODO: rename ScatterElement class to something like "PointElement"
27 qmlRegisterType<ScatterElement>(uri, 1, 0, "ChartPointElement");
21 }
28 }
22 };
29 };
23
30
@@ -26,12 +26,16 SOURCES += \
26 declarativechart.cpp \
26 declarativechart.cpp \
27 declarativeseries.cpp \
27 declarativeseries.cpp \
28 declarativescatterseries.cpp \
28 declarativescatterseries.cpp \
29 scatterelement.cpp
29 scatterelement.cpp \
30 declarativepieseries.cpp \
31 declarativelineseries.cpp
30 HEADERS += \
32 HEADERS += \
31 declarativechart.h \
33 declarativechart.h \
32 declarativeseries.h \
34 declarativeseries.h \
33 declarativescatterseries.h \
35 declarativescatterseries.h \
34 scatterelement.h
36 scatterelement.h \
37 declarativepieseries.h \
38 declarativelineseries.h
35
39
36 TARGETPATH = QtCommercial/Chart
40 TARGETPATH = QtCommercial/Chart
37 target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
41 target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
@@ -55,3 +59,7 INSTALLS += target qmldir
55
59
56
60
57
61
62
63
64
65
@@ -12,6 +12,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
12 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 Q_PROPERTY(QString label READ label WRITE setLabel /*NOTIFY dataYChanged*/)
16 Q_PROPERTY(qreal value READ value WRITE setValue /*NOTIFY dataXChanged*/)
15
17
16 public:
18 public:
17 QPieSlice(QObject *parent = 0);
19 QPieSlice(QObject *parent = 0);
@@ -20,14 +20,15 Rectangle {
20 // }
20 // }
21
21
22 Component.onCompleted: {
22 Component.onCompleted: {
23 // console.log("Component.onCompleted: " + scatterElement.x);
23 // console.log("Component.onCompleted: " + ChartPointElement.x);
24 // console.log("Component.onCompleted: " + scatterElement.y);
24 // console.log("Component.onCompleted: " + ChartPointElement.y);
25 // console.log("Component.onCompleted: " + scatterElement.dataX);
25 // console.log("Component.onCompleted: " + ChartPointElement.dataX);
26 // console.log("Component.onCompleted: " + scatterElement.dataY);
26 // console.log("Component.onCompleted: " + ChartPointElement.dataY);
27 //console.log("Component.onCompleted: " + chartModel.get(0).x);
27 //console.log("Component.onCompleted: " + chartModel.get(0).x);
28 //console.log("Component.onCompleted: " + chartModel.scatterElements);
28 //console.log("Component.onCompleted: " + chartModel.ChartPointElements);
29 // console.log("Component.onCompleted: " + elementt.dataX);
29 // console.log("Component.onCompleted: " + elementt.dataX);
30 // console.log("Component.onCompleted: " + chartModel.get(0).dataX);
30 // console.log("Component.onCompleted: " + chartModel.get(0).dataX);
31 //ChartPointElement { x: 0.3; y: 0.3 }
31 }
32 }
32
33
33 Chart {
34 Chart {
@@ -36,16 +37,24 Rectangle {
36 anchors.left: parent.left
37 anchors.left: parent.left
37 anchors.right: parent.right
38 anchors.right: parent.right
38 height: parent.height / 2
39 height: parent.height / 2
39 theme: Chart.ThemeIcy
40 theme: Chart.ThemeVanilla
40 // opacity: 0.3
41
41
42 Series {
42 PieSeries {
43 seriesType: Series.SeriesTypePie
43 data: [
44 // TODO: "NnElement" matches the naming convention of for example ListModel...
45 // But PieSlice would match the naming of QtCommercial Charts C++ api
46 ChartPieElement { label: "Volkswagen"; value: 13.5 },
47 ChartPieElement { label: "Toyota"; value: 10.9 },
48 ChartPieElement { label: "Ford"; value: 8.6 },
49 ChartPieElement { label: "Skoda"; value: 8.2 },
50 ChartPieElement { label: "Volvo"; value: 6.8 },
51 ChartPieElement { label: "Others"; value: 52.0 }
52 ]
44 }
53 }
45
54
46 Series {
55 // Series {
47 seriesType: Series.SeriesTypeLine
56 // seriesType: Series.SeriesTypeLine
48 }
57 // }
49 // TODO:
58 // TODO:
50 // Series {
59 // Series {
51 // seriesType: Series.SeriesTypeBar
60 // seriesType: Series.SeriesTypeBar
@@ -61,32 +70,42 Rectangle {
61 anchors.right: parent.right
70 anchors.right: parent.right
62 theme: Chart.ThemeScientific
71 theme: Chart.ThemeScientific
63
72
73 LineSeries {
74 data: [
75 ChartPointElement { x: 0.0; y: 0.0 },
76 ChartPointElement { x: 1.1; y: 2.1 },
77 ChartPointElement { x: 2.9; y: 4.9 },
78 ChartPointElement { x: 3.2; y: 3.0 }
79 ]
80 }
81
64 ScatterSeries {
82 ScatterSeries {
65 data: [
83 data: [
66 ScatterElement { x: 1.1; y: 2.1 },
84 ChartPointElement { x: 1.1; y: 1.1 },
67 ScatterElement { x: 1.2; y: 2.0 },
85 ChartPointElement { x: 1.1; y: 1.2 },
68 ScatterElement { x: 1.4; y: 2.3 }
86 ChartPointElement { x: 1.17; y: 1.15 }
69 ]
87 ]
70 }
88 }
71 ScatterSeries {
89 ScatterSeries {
72 data: [
90 data: [
73 ScatterElement { x: 1.2; y: 2.2 },
91 ChartPointElement { x: 1.5; y: 1.5 },
74 ScatterElement { x: 1.3; y: 2.2 },
92 ChartPointElement { x: 1.5; y: 1.6 },
75 ScatterElement { x: 1.7; y: 2.6 }
93 ChartPointElement { x: 1.57; y: 1.55 }
76 ]
94 ]
77 }
95 }
78 ScatterSeries {
96 ScatterSeries {
79 data: [
97 data: [
80 ScatterElement { x: 1.3; y: 2.3 },
98 ChartPointElement { x: 2.0; y: 2.0 },
81 ScatterElement { x: 1.5; y: 2.4 },
99 ChartPointElement { x: 2.0; y: 2.1 },
82 ScatterElement { x: 2.0; y: 2.9 }
100 ChartPointElement { x: 2.07; y: 2.05 }
83 ]
101 ]
84 }
102 }
85 ScatterSeries {
103 ScatterSeries {
104 id: scatter4
86 data: [
105 data: [
87 ScatterElement { x: 1.4; y: 2.4 },
106 ChartPointElement { x: 2.6; y: 2.6 },
88 ScatterElement { x: 1.8; y: 2.7 },
107 ChartPointElement { x: 2.6; y: 2.7 },
89 ScatterElement { x: 2.5; y: 3.2 }
108 ChartPointElement { x: 2.67; y: 2.65 }
90 ]
109 ]
91 }
110 }
92 }
111 }
General Comments 0
You need to be logged in to leave comments. Login now