##// END OF EJS Templates
Spline to QML API
Tero Ahola -
r732:4aded284df80
parent child
Show More
@@ -0,0 +1,23
1 #ifndef DECLARATIVESPLINESERIES_H
2 #define DECLARATIVESPLINESERIES_H
3
4 #include "qchartglobal.h"
5 #include "declarativexyseries.h"
6 #include <QDeclarativeItem>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class DeclarativeSplineSeries : public DeclarativeXySeries
11 {
12 Q_OBJECT
13
14 public:
15 explicit DeclarativeSplineSeries(QDeclarativeItem *parent = 0) :
16 DeclarativeXySeries(QSeries::SeriesTypeSpline, parent) {}
17 };
18
19 #include "moc_declarativesplineseries.cpp"
20
21 QTCOMMERCIALCHART_END_NAMESPACE
22
23 #endif // DECLARATIVESPLINESERIES_H
@@ -0,0 +1,85
1 #include "DeclarativeXySeries.h"
2 #include "declarativechart.h"
3 #include "qchart.h"
4 #include "qxyseries.h"
5 #include "qareaseries.h"
6 #include "qsplineseries.h"
7 #include "qscatterseries.h"
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11 DeclarativeXySeries::DeclarativeXySeries(QSeries::QSeriesType type, QDeclarativeItem *parent) :
12 QDeclarativeItem(parent),
13 m_seriesType(type),
14 m_chart(0),
15 m_series(0)
16 {
17 setFlag(QGraphicsItem::ItemHasNoContents, false);
18 }
19
20 DeclarativeXySeries::~DeclarativeXySeries()
21 {
22 }
23
24 void DeclarativeXySeries::componentComplete()
25 {
26 Q_ASSERT(!m_series);
27 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
28
29 if (declarativeChart) {
30 m_chart = qobject_cast<QChart *>(declarativeChart->m_chart);
31 Q_ASSERT(m_chart);
32
33 switch (m_seriesType) {
34 case QSeries::SeriesTypeLine:
35 qDebug() << "creating line series for chart: " << m_chart;
36 m_series = new QLineSeries();
37 break;
38 case QSeries::SeriesTypeArea:
39 qDebug() << "TODO: creating area series for chart: " << m_chart;
40 // m_series = new QAreaSeries();
41 break;
42 case QSeries::SeriesTypeSpline:
43 qDebug() << "creating spline series for chart: " << m_chart;
44 m_series = new QSplineSeries();
45 break;
46 case QSeries::SeriesTypeScatter:
47 qDebug() << "creating scatter series for chart: " << m_chart;
48 m_series = new QScatterSeries();
49 break;
50 default:
51 Q_ASSERT(false);
52 }
53
54 for (int i(0); i < m_points.count(); i++) {
55 DeclarativeXyPoint *element = m_points.at(i);
56 m_series->add(element->x(), element->y());
57 }
58 m_chart->addSeries(m_series);
59 }
60 }
61
62 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeXySeries::points()
63 {
64 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0, &DeclarativeXySeries::appendPoints);
65 }
66
67 void DeclarativeXySeries::appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
68 DeclarativeXyPoint *element)
69 {
70 DeclarativeXySeries *series = qobject_cast<DeclarativeXySeries *>(list->object);
71 qDebug() << "appendPoints: " << series;
72 qDebug() << "appendPoints: " << element;
73 qDebug() << "appendPoints: " << element->x();
74 qDebug() << "appendPoints: " << element->y();
75 qDebug() << "appendPoints: " << series->m_series;
76 if (series) {
77 series->m_points.append(element);
78 if (series->m_series)
79 series->m_series->add(element->x(), element->y());
80 }
81 }
82
83 #include "moc_declarativexyseries.cpp"
84
85 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,45
1 #ifndef DECLARATIVE_XY_SERIES_H
2 #define DECLARATIVE_XY_SERIES_H
3
4 #include "qchartglobal.h"
5 #include "declarativexypoint.h"
6 #include "qxyseries.h"
7 #include <QDeclarativeItem>
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11 class QChart;
12
13 class DeclarativeXySeries : public QDeclarativeItem
14 {
15 Q_OBJECT
16 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> points READ points)
17
18 public:
19 explicit DeclarativeXySeries(QSeries::QSeriesType type, QDeclarativeItem *parent = 0);
20 ~DeclarativeXySeries();
21
22 public: // from QDeclarativeParserStatus
23 void componentComplete();
24
25 public:
26 QDeclarativeListProperty<DeclarativeXyPoint> points();
27
28 signals:
29
30 public slots:
31 static void appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
32 DeclarativeXyPoint *element);
33
34 private slots:
35
36 public:
37 QSeries::QSeriesType m_seriesType;
38 QChart *m_chart;
39 QXYSeries *m_series;
40 QList<DeclarativeXyPoint *> m_points;
41 };
42
43 QTCOMMERCIALCHART_END_NAMESPACE
44
45 #endif // DECLARATIVE_XY_SERIES_H
@@ -1,55 +1,15
1 1 #include "declarativelineseries.h"
2 2 #include "declarativechart.h"
3 3 #include "qchart.h"
4 4 #include "qlineseries.h"
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 DeclarativeLineSeries::DeclarativeLineSeries(QDeclarativeItem *parent) :
9 QDeclarativeItem(parent)
9 DeclarativeXySeries(QSeries::SeriesTypeLine, parent)
10 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 QLineSeries();
27 Q_ASSERT(m_series);
28 for (int i(0); i < m_data.count(); i++) {
29 DeclarativeXyPoint *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<DeclarativeXyPoint> DeclarativeLineSeries::data()
38 {
39 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0, &DeclarativeLineSeries::appendData);
40 }
41
42 void DeclarativeLineSeries::appendData(QDeclarativeListProperty<DeclarativeXyPoint> *list,
43 DeclarativeXyPoint *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 11 }
52 12
53 13 #include "moc_declarativelineseries.cpp"
54 14
55 15 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,39 +1,20
1 1 #ifndef DECLARATIVELINESERIES_H
2 2 #define DECLARATIVELINESERIES_H
3 3
4 4 #include "qchartglobal.h"
5 #include "declarativexypoint.h"
5 #include "declarativexyseries.h"
6 6 #include <QDeclarativeItem>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 class QChart;
11 class QLineSeries;
12
13 class DeclarativeLineSeries : public QDeclarativeItem
10 class DeclarativeLineSeries : public DeclarativeXySeries
14 11 {
15 12 Q_OBJECT
16 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> data READ data)
17 13
18 14 public:
19 15 explicit DeclarativeLineSeries(QDeclarativeItem *parent = 0);
20 QDeclarativeListProperty<DeclarativeXyPoint> data();
21
22 signals:
23
24 public slots:
25 static void appendData(QDeclarativeListProperty<DeclarativeXyPoint> *list,
26 DeclarativeXyPoint *element);
27
28 private slots:
29 void setParentForSeries();
30
31 private:
32 QChart *m_chart;
33 QLineSeries *m_series;
34 QList<DeclarativeXyPoint *> m_data;
35 16 };
36 17
37 18 QTCOMMERCIALCHART_END_NAMESPACE
38 19
39 20 #endif // DECLARATIVELINESERIES_H
@@ -1,63 +1,16
1 1 #include "declarativescatterseries.h"
2 2 #include "declarativechart.h"
3 3 #include "qchart.h"
4 4 #include "qscatterseries.h"
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) :
9 QDeclarativeItem(parent),
10 m_chart(0),
11 m_series(0)
9 DeclarativeXySeries(QSeries::SeriesTypeScatter, parent)
12 10 {
13 11 setFlag(QGraphicsItem::ItemHasNoContents, false);
14 12 }
15 13
16 DeclarativeScatterSeries::~DeclarativeScatterSeries()
17 {
18 }
19
20 void DeclarativeScatterSeries::componentComplete()
21 {
22 Q_ASSERT(!m_series);
23 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
24
25 if (declarativeChart) {
26 m_chart = qobject_cast<QChart *>(declarativeChart->m_chart);
27 qDebug() << "creating scatter series for chart: " << m_chart;
28 Q_ASSERT(m_chart);
29
30 m_series = new QScatterSeries();
31 for (int i(0); i < m_points.count(); i++) {
32 DeclarativeXyPoint *element = m_points.at(i);
33 *m_series << QPointF(element->x(), element->y());
34 }
35 m_chart->addSeries(m_series);
36 }
37 }
38
39 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeScatterSeries::points()
40 {
41 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0,
42 &DeclarativeScatterSeries::appendPoints);
43 }
44
45 void DeclarativeScatterSeries::appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
46 DeclarativeXyPoint *element)
47 {
48 DeclarativeScatterSeries *series = qobject_cast<DeclarativeScatterSeries *>(list->object);
49 qDebug() << "appendPoints: " << series;
50 qDebug() << "appendPoints: " << element;
51 qDebug() << "appendPoints: " << element->x();
52 qDebug() << "appendPoints: " << element->y();
53 qDebug() << "appendPoints: " << series->m_series;
54 if (series) {
55 series->m_points.append(element);
56 if (series->m_series)
57 series->m_series->add(element->x(), element->y());
58 }
59 }
60
61 14 #include "moc_declarativescatterseries.cpp"
62 15
63 16 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,44 +1,20
1 1 #ifndef DECLARATIVESCATTERSERIES_H
2 2 #define DECLARATIVESCATTERSERIES_H
3 3
4 4 #include "qchartglobal.h"
5 #include "declarativexypoint.h"
5 #include "declarativexyseries.h"
6 6 #include <QDeclarativeItem>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 class QChart;
11 class QScatterSeries;
12
13 class DeclarativeScatterSeries : public QDeclarativeItem
10 class DeclarativeScatterSeries : public DeclarativeXySeries
14 11 {
15 12 Q_OBJECT
16 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> points READ points)
17 13
18 14 public:
19 15 explicit DeclarativeScatterSeries(QDeclarativeItem *parent = 0);
20 ~DeclarativeScatterSeries();
21
22 public: // from QDeclarativeParserStatus
23 void componentComplete();
24
25 public:
26 QDeclarativeListProperty<DeclarativeXyPoint> points();
27
28 signals:
29
30 public slots:
31 static void appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
32 DeclarativeXyPoint *element);
33
34 private slots:
35
36 public:
37 QChart *m_chart; // not owned
38 QScatterSeries *m_series; // not owned
39 QList<DeclarativeXyPoint *> m_points; // not owned
40 16 };
41 17
42 18 QTCOMMERCIALCHART_END_NAMESPACE
43 19
44 20 #endif // DECLARATIVESCATTERSERIES_H
@@ -1,36 +1,38
1 1 #include <QtDeclarative/qdeclarativeextensionplugin.h>
2 2 #include <QtDeclarative/qdeclarative.h>
3 3 #include "declarativechart.h"
4 4 #include "declarativexypoint.h"
5 #include "declarativescatterseries.h"
6 5 #include "declarativelineseries.h"
6 #include "declarativesplineseries.h"
7 #include "declarativescatterseries.h"
7 8 #include "declarativebarseries.h"
8 9 #include "declarativepieseries.h"
9 10
10 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 12
12 13 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
13 14 {
14 15 Q_OBJECT
15 16 public:
16 17 virtual void registerTypes(const char *uri)
17 18 {
18 19 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
19 20
20 21 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "Chart");
21 22 qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint");
22 23 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
23 24 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
25 qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries");
24 26 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
25 27 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
26 28 qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice");
27 29 }
28 30 };
29 31
30 32 #include "plugin.moc"
31 33
32 34 QTCOMMERCIALCHART_END_NAMESPACE
33 35
34 36 QTCOMMERCIALCHART_USE_NAMESPACE
35 37
36 38 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
@@ -1,45 +1,48
1 1 TEMPLATE = lib
2 2 TARGET = qtcommercialchartqml
3 3 CONFIG += qt plugin
4 4 QT += declarative
5 5
6 6 !include( ../common.pri ) {
7 7 error( "Couldn't find the common.pri file!" )
8 8 }
9 9 !include( ../integrated.pri ) {
10 10 error( "Couldn't find the integrated.pri file !")
11 11 }
12 12
13 13 DESTDIR = $$CHART_BUILD_PLUGIN_DIR
14 14 contains(QT_MAJOR_VERSION, 5) {
15 15 # TODO: QtQuick2 not supported by the implementation currently
16 16 DEFINES += QTQUICK2
17 17 }
18 18
19 19 OBJECTS_DIR = $$CHART_BUILD_DIR/plugin
20 20 MOC_DIR = $$CHART_BUILD_DIR/plugin
21 21 UI_DIR = $$CHART_BUILD_DIR/plugin
22 22 RCC_DIR = $$CHART_BUILD_DIR/plugin
23 23
24 24 SOURCES += \
25 25 plugin.cpp \
26 26 declarativechart.cpp \
27 declarativescatterseries.cpp \
28 27 declarativexypoint.cpp \
29 declarativepieseries.cpp \
28 declarativexyseries.cpp \
30 29 declarativelineseries.cpp \
30 declarativescatterseries.cpp \
31 declarativepieseries.cpp \
31 32 declarativebarseries.cpp
32 33 HEADERS += \
33 34 declarativechart.h \
34 declarativescatterseries.h \
35 35 declarativexypoint.h \
36 declarativepieseries.h \
36 declarativexyseries.h \
37 37 declarativelineseries.h \
38 declarativesplineseries.h \
39 declarativescatterseries.h \
40 declarativepieseries.h \
38 41 declarativebarseries.h
39 42
40 43 TARGETPATH = QtCommercial/Chart
41 44 target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
42 45 qmldir.files += $$PWD/qmldir
43 46 qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
44 47
45 48 INSTALLS += target qmldir
@@ -1,104 +1,104
1 1 import QtQuick 1.0
2 2 import QtCommercial.Chart 1.0
3 3
4 4 Rectangle {
5 5 width: parent.width
6 6 height: parent.height
7 7
8 8 // Another option for QML data api:
9 9 // ListModel {
10 10 // id: listModelForPie
11 11 // // PieDataElement
12 12 // ListElement {
13 13 // label: "Apple"
14 14 // value: 4.3
15 15 // }
16 16 // ListElement {
17 17 // label: "Blackberry"
18 18 // value: 15.1
19 19 // }
20 20 // }
21 21
22 22 Component.onCompleted: {
23 23 // console.log("model:" + myModel.item(0));
24 24 // myModel.insert(1, {"time":1.4; "speed":41.1 });
25 25 // scatter.appendData();
26 26 }
27 27
28 28
29 29 Chart {
30 30 id: chart1
31 31 anchors.top: parent.top
32 32 anchors.left: parent.left
33 33 anchors.right: parent.right
34 34 height: parent.height / 2
35 35 theme: Chart.ThemeBlueCerulean
36 36
37 37 BarSeries {
38 38 barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
39 39 // data: [
40 40 // BarSet { }
41 41 // ]
42 42 }
43 43
44 44 // PieSeries {
45 45 // data: [
46 46 // PieSlice { label: "Volkswagen"; value: 13.5 },
47 47 // PieSlice { label: "Toyota"; value: 10.9 },
48 48 // PieSlice { label: "Ford"; value: 8.6 },
49 49 // PieSlice { label: "Skoda"; value: 8.2 },
50 50 // PieSlice { label: "Volvo"; value: 6.8 },
51 51 // PieSlice { label: "Others"; value: 52.0 }
52 52 // ]
53 53 // }
54 54 }
55 55
56 56
57 57 Chart {
58 58 id: chart2
59 59 anchors.top: chart1.bottom
60 60 anchors.bottom: parent.bottom
61 61 anchors.left: parent.left
62 62 anchors.right: parent.right
63 63 theme: Chart.ThemeBrownSand
64 64
65 65 LineSeries {
66 data: [
66 points: [
67 67 XyPoint { x: 0.0; y: 0.0 },
68 68 XyPoint { x: 1.1; y: 2.1 },
69 69 XyPoint { x: 2.9; y: 4.9 },
70 70 XyPoint { x: 3.2; y: 3.0 }
71 71 ]
72 72 }
73 73
74 ScatterSeries {
74 SplineSeries {
75 75 id: scatter
76 76 points: [
77 XyPoint { x: 1.1; y: 1.1 },
78 XyPoint { x: 1.1; y: 1.2 },
79 XyPoint { x: 1.17; y: 1.15 }
77 XyPoint { x: 0.0; y: 0.3 },
78 XyPoint { x: 1.1; y: 3.2 },
79 XyPoint { x: 4.17; y: 3.15 }
80 80 ]
81 81 }
82 82 ScatterSeries {
83 83 points: [
84 84 XyPoint { x: 1.5; y: 1.5 },
85 85 XyPoint { x: 1.5; y: 1.6 },
86 86 XyPoint { x: 1.57; y: 1.55 }
87 87 ]
88 88 }
89 89 ScatterSeries {
90 90 points: [
91 91 XyPoint { x: 2.0; y: 2.0 },
92 92 XyPoint { x: 2.0; y: 2.1 },
93 93 XyPoint { x: 2.07; y: 2.05 }
94 94 ]
95 95 }
96 96 ScatterSeries {
97 97 points: [
98 98 XyPoint { x: 2.6; y: 2.6 },
99 99 XyPoint { x: 2.6; y: 2.7 },
100 100 XyPoint { x: 2.67; y: 2.65 }
101 101 ]
102 102 }
103 103 }
104 104 }
General Comments 0
You need to be logged in to leave comments. Login now