##// END OF EJS Templates
Added bar series draft to QML API
Tero Ahola -
r723:37842f579a1d
parent child
Show More
@@ -1,108 +1,63
1 #include "declarativescatterseries.h"
1 #include "declarativescatterseries.h"
2 #include "declarativechart.h"
2 #include "declarativechart.h"
3 #include "declarativetablemodel.h"
4 #include "qchart.h"
3 #include "qchart.h"
5 #include "qscatterseries.h"
4 #include "qscatterseries.h"
6
5
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
7
9 DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) :
8 DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) :
10 QDeclarativeItem(parent),
9 QDeclarativeItem(parent),
11 m_chart(0),
10 m_chart(0),
12 m_series(0),
11 m_series(0)
13 m_model(0),
14 m_xColumn(0),
15 m_yColumn(1)
16 {
12 {
17 setFlag(QGraphicsItem::ItemHasNoContents, false);
13 setFlag(QGraphicsItem::ItemHasNoContents, false);
18 }
14 }
19
15
20 DeclarativeScatterSeries::~DeclarativeScatterSeries()
16 DeclarativeScatterSeries::~DeclarativeScatterSeries()
21 {
17 {
22 }
18 }
23
19
24 void DeclarativeScatterSeries::componentComplete()
20 void DeclarativeScatterSeries::componentComplete()
25 {
21 {
26 Q_ASSERT(!m_series);
22 Q_ASSERT(!m_series);
27 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
23 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
28
24
29 if (declarativeChart) {
25 if (declarativeChart) {
30 m_chart = qobject_cast<QChart *>(declarativeChart->m_chart);
26 m_chart = qobject_cast<QChart *>(declarativeChart->m_chart);
31 qDebug() << "creating scatter series for chart: " << m_chart;
27 qDebug() << "creating scatter series for chart: " << m_chart;
32 Q_ASSERT(m_chart);
28 Q_ASSERT(m_chart);
33
29
34 m_series = new QScatterSeries();
30 m_series = new QScatterSeries();
35 // if (!m_model)
36 // m_model = new DeclarativeTableModel();
37 if (m_model) {
38 m_series->setModel(m_model);
39 m_series->setModelMapping(m_xColumn, m_yColumn);
40 }
41 for (int i(0); i < m_data.count(); i++) {
31 for (int i(0); i < m_data.count(); i++) {
42 DeclarativeXyPoint *element = m_data.at(i);
32 DeclarativeXyPoint *element = m_data.at(i);
43 *m_series << QPointF(element->x(), element->y());
33 *m_series << QPointF(element->x(), element->y());
44 }
34 }
45 m_chart->addSeries(m_series);
35 m_chart->addSeries(m_series);
46 }
36 }
47 }
37 }
48
38
49 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeScatterSeries::data()
39 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeScatterSeries::data()
50 {
40 {
51 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0,
41 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0,
52 &DeclarativeScatterSeries::appendData);
42 &DeclarativeScatterSeries::appendData);
53 }
43 }
54
44
55 void DeclarativeScatterSeries::appendData(QDeclarativeListProperty<DeclarativeXyPoint> *list,
45 void DeclarativeScatterSeries::appendData(QDeclarativeListProperty<DeclarativeXyPoint> *list,
56 DeclarativeXyPoint *element)
46 DeclarativeXyPoint *element)
57 {
47 {
58 DeclarativeScatterSeries *series = qobject_cast<DeclarativeScatterSeries *>(list->object);
48 DeclarativeScatterSeries *series = qobject_cast<DeclarativeScatterSeries *>(list->object);
59 qDebug() << "appendData: " << series;
49 qDebug() << "appendData: " << series;
60 qDebug() << "appendData: " << element;
50 qDebug() << "appendData: " << element;
61 qDebug() << "appendData: " << element->x();
51 qDebug() << "appendData: " << element->x();
62 qDebug() << "appendData: " << element->y();
52 qDebug() << "appendData: " << element->y();
63 qDebug() << "appendData: " << series->m_series;
53 qDebug() << "appendData: " << series->m_series;
64 if (series) {
54 if (series) {
65 series->m_data.append(element);
55 series->m_data.append(element);
66 if (series->m_series)
56 if (series->m_series)
67 series->m_series->add(element->x(), element->y());
57 series->m_series->add(element->x(), element->y());
68 }
58 }
69 }
59 }
70
60
71 DeclarativeTableModel *DeclarativeScatterSeries::model()
72 {
73 if (m_series)
74 return (DeclarativeTableModel *) m_series->model();
75 else
76 return m_model;
77 }
78
79 void DeclarativeScatterSeries::setModel(DeclarativeTableModel *model)
80 {
81 m_model = model;
82 if (m_chart && m_series) {
83 // Hack: remove and add the series to force an update for the chart range
84 m_chart->removeSeries(m_series);
85 m_series = new QScatterSeries();
86 m_series->setModel(m_model);
87 m_series->setModelMapping(m_xColumn, m_yColumn);
88 m_chart->addSeries(m_series);
89 }
90 }
91
92 void DeclarativeScatterSeries::setXColumn(int xColumn)
93 {
94 m_xColumn = xColumn;
95 if (m_series && m_series->model())
96 m_series->setModelMapping(m_xColumn, m_yColumn);
97 }
98
99 void DeclarativeScatterSeries::setYColumn(int yColumn)
100 {
101 m_yColumn = yColumn;
102 if (m_series && m_series->model())
103 m_series->setModelMapping(m_xColumn, m_yColumn);
104 }
105
106 #include "moc_declarativescatterseries.cpp"
61 #include "moc_declarativescatterseries.cpp"
107
62
108 QTCOMMERCIALCHART_END_NAMESPACE
63 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,61 +1,45
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 "declarativetablemodel.h"
6 #include "declarativexypoint.h"
5 #include "declarativexypoint.h"
7 #include <QDeclarativeItem>
6 #include <QDeclarativeItem>
8 #include <QDeclarativeParserStatus>
7 #include <QDeclarativeParserStatus>
9
8
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
10
12 class QChart;
11 class QChart;
13 class QScatterSeries;
12 class QScatterSeries;
14
13
15 class DeclarativeScatterSeries : public QDeclarativeItem//, public QDeclarativeParserStatus
14 class DeclarativeScatterSeries : public QDeclarativeItem//, public QDeclarativeParserStatus
16 {
15 {
17 Q_OBJECT
16 Q_OBJECT
18 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> data READ data)
17 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> data READ data)
19 Q_PROPERTY(DeclarativeTableModel *model READ model WRITE setModel)
20 // Q_PROPERTY(QObject *listModel READ listModel WRITE setListModel)
21 Q_PROPERTY(int xColumn READ xColumn WRITE setXColumn)
22 Q_PROPERTY(int yColumn READ yColumn WRITE setYColumn)
23
18
24 public:
19 public:
25 explicit DeclarativeScatterSeries(QDeclarativeItem *parent = 0);
20 explicit DeclarativeScatterSeries(QDeclarativeItem *parent = 0);
26 ~DeclarativeScatterSeries();
21 ~DeclarativeScatterSeries();
27
22
28 public: // from QDeclarativeParserStatus
23 public: // from QDeclarativeParserStatus
29 void componentComplete();
24 void componentComplete();
30
25
31 public:
26 public:
32 QDeclarativeListProperty<DeclarativeXyPoint> data();
27 QDeclarativeListProperty<DeclarativeXyPoint> data();
33 DeclarativeTableModel *model();
34 void setModel(DeclarativeTableModel *model);
35 //QObject *listModel();
36 //void setListModel(QObject *model);
37 int xColumn() { return m_xColumn; }
38 void setXColumn(int xColumn);
39 int yColumn() { return m_yColumn; }
40 void setYColumn(int yColumn);
41
28
42 signals:
29 signals:
43
30
44 public slots:
31 public slots:
45 static void appendData(QDeclarativeListProperty<DeclarativeXyPoint> *list,
32 static void appendData(QDeclarativeListProperty<DeclarativeXyPoint> *list,
46 DeclarativeXyPoint *element);
33 DeclarativeXyPoint *element);
47
34
48 private slots:
35 private slots:
49
36
50 public:
37 public:
51 QChart *m_chart; // not owned
38 QChart *m_chart; // not owned
52 QScatterSeries *m_series; // not owned
39 QScatterSeries *m_series; // not owned
53 DeclarativeTableModel *m_model; // not owned
54 QList<DeclarativeXyPoint *> m_data;
40 QList<DeclarativeXyPoint *> m_data;
55 int m_xColumn;
56 int m_yColumn;
57 };
41 };
58
42
59 QTCOMMERCIALCHART_END_NAMESPACE
43 QTCOMMERCIALCHART_END_NAMESPACE
60
44
61 #endif // DECLARATIVESCATTERSERIES_H
45 #endif // DECLARATIVESCATTERSERIES_H
@@ -1,38 +1,36
1 #include <QtDeclarative/qdeclarativeextensionplugin.h>
1 #include <QtDeclarative/qdeclarativeextensionplugin.h>
2 #include <QtDeclarative/qdeclarative.h>
2 #include <QtDeclarative/qdeclarative.h>
3 #include "declarativechart.h"
3 #include "declarativechart.h"
4 #include "declarativetablemodel.h"
5 #include "declarativexypoint.h"
4 #include "declarativexypoint.h"
6 #include "declarativescatterseries.h"
5 #include "declarativescatterseries.h"
7 #include "declarativelineseries.h"
6 #include "declarativelineseries.h"
8 #include "declarativebarseries.h"
7 #include "declarativebarseries.h"
9 #include "declarativepieseries.h"
8 #include "declarativepieseries.h"
10
9
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
11
13 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
12 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
14 {
13 {
15 Q_OBJECT
14 Q_OBJECT
16 public:
15 public:
17 virtual void registerTypes(const char *uri)
16 virtual void registerTypes(const char *uri)
18 {
17 {
19 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
18 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
20
19
21 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "Chart");
20 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "Chart");
22 qmlRegisterType<DeclarativeTableModel>(uri, 1, 0, "ChartTableModel");
23 qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint");
21 qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint");
24 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
22 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
25 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
23 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
26 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
24 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
27 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
25 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
28 qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice");
26 qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice");
29 }
27 }
30 };
28 };
31
29
32 #include "plugin.moc"
30 #include "plugin.moc"
33
31
34 QTCOMMERCIALCHART_END_NAMESPACE
32 QTCOMMERCIALCHART_END_NAMESPACE
35
33
36 QTCOMMERCIALCHART_USE_NAMESPACE
34 QTCOMMERCIALCHART_USE_NAMESPACE
37
35
38 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
36 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
@@ -1,47 +1,45
1 TEMPLATE = lib
1 TEMPLATE = lib
2 TARGET = qtcommercialchartqml
2 TARGET = qtcommercialchartqml
3 CONFIG += qt plugin
3 CONFIG += qt plugin
4 QT += declarative
4 QT += declarative
5
5
6 !include( ../common.pri ) {
6 !include( ../common.pri ) {
7 error( "Couldn't find the common.pri file!" )
7 error( "Couldn't find the common.pri file!" )
8 }
8 }
9 !include( ../integrated.pri ) {
9 !include( ../integrated.pri ) {
10 error( "Couldn't find the integrated.pri file !")
10 error( "Couldn't find the integrated.pri file !")
11 }
11 }
12
12
13 DESTDIR = $$CHART_BUILD_PLUGIN_DIR
13 DESTDIR = $$CHART_BUILD_PLUGIN_DIR
14 contains(QT_MAJOR_VERSION, 5) {
14 contains(QT_MAJOR_VERSION, 5) {
15 # TODO: QtQuick2 not supported by the implementation currently
15 # TODO: QtQuick2 not supported by the implementation currently
16 DEFINES += QTQUICK2
16 DEFINES += QTQUICK2
17 }
17 }
18
18
19 OBJECTS_DIR = $$CHART_BUILD_DIR/plugin
19 OBJECTS_DIR = $$CHART_BUILD_DIR/plugin
20 MOC_DIR = $$CHART_BUILD_DIR/plugin
20 MOC_DIR = $$CHART_BUILD_DIR/plugin
21 UI_DIR = $$CHART_BUILD_DIR/plugin
21 UI_DIR = $$CHART_BUILD_DIR/plugin
22 RCC_DIR = $$CHART_BUILD_DIR/plugin
22 RCC_DIR = $$CHART_BUILD_DIR/plugin
23
23
24 SOURCES += \
24 SOURCES += \
25 plugin.cpp \
25 plugin.cpp \
26 declarativechart.cpp \
26 declarativechart.cpp \
27 declarativescatterseries.cpp \
27 declarativescatterseries.cpp \
28 declarativexypoint.cpp \
28 declarativexypoint.cpp \
29 declarativepieseries.cpp \
29 declarativepieseries.cpp \
30 declarativelineseries.cpp \
30 declarativelineseries.cpp \
31 declarativebarseries.cpp \
31 declarativebarseries.cpp
32 declarativetablemodel.cpp
33 HEADERS += \
32 HEADERS += \
34 declarativechart.h \
33 declarativechart.h \
35 declarativescatterseries.h \
34 declarativescatterseries.h \
36 declarativexypoint.h \
35 declarativexypoint.h \
37 declarativepieseries.h \
36 declarativepieseries.h \
38 declarativelineseries.h \
37 declarativelineseries.h \
39 declarativebarseries.h \
38 declarativebarseries.h
40 declarativetablemodel.h
41
39
42 TARGETPATH = QtCommercial/Chart
40 TARGETPATH = QtCommercial/Chart
43 target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
41 target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
44 qmldir.files += $$PWD/qmldir
42 qmldir.files += $$PWD/qmldir
45 qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
43 qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
46
44
47 INSTALLS += target qmldir
45 INSTALLS += target qmldir
@@ -1,142 +1,100
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);
24 // console.log("model:" + myModel.item(0));
23 // console.log("model:" + myModel.item(0));
25 // myModel.insert(1, {"time":1.4; "speed":41.1 });
24 // myModel.insert(1, {"time":1.4; "speed":41.1 });
26 // scatter.appendData();
25 // scatter.appendData();
27 }
26 }
28
27
29 ListModel {
30 ListElement {
31 time: 0.0
32 speed: 45.2
33 }
34 }
35 ChartTableModel {
36 id: myModel
37
38 // ListElement {
39 // time: 0.0
40 // speed: 45.2
41 // }
42 // ListElement {
43 // time: 0.5
44 // speed: 48.9
45 // }
46 // ListElement {
47 // time: 1.1
48 // speed: 42.6
49 // }
50
51 // ChartTableElement {
52 // time: 0.0
53 // speed: 45.2
54 // }
55 // ChartTableElement {
56 // time: 0.5
57 // speed: 48.9
58 // }
59 // ChartTableElement {
60 // time: 1.1
61 // speed: 42.6
62 // }
63 }
64
28
65 Chart {
29 Chart {
66 id: chart1
30 id: chart1
67 anchors.top: parent.top
31 anchors.top: parent.top
68 anchors.left: parent.left
32 anchors.left: parent.left
69 anchors.right: parent.right
33 anchors.right: parent.right
70 height: parent.height / 2
34 height: parent.height / 2
71 theme: Chart.ThemeBlueCerulean
35 theme: Chart.ThemeBlueCerulean
72
36
73 // BarSeries {
37 BarSeries {
74 // }
75
76 ScatterSeries {
77 model: myModel
78 // xColumn: time
79 // yColumn: speed
80 }
38 }
81
39
82 // PieSeries {
40 // PieSeries {
83 // data: [
41 // data: [
84 // PieSlice { label: "Volkswagen"; value: 13.5 },
42 // PieSlice { label: "Volkswagen"; value: 13.5 },
85 // PieSlice { label: "Toyota"; value: 10.9 },
43 // PieSlice { label: "Toyota"; value: 10.9 },
86 // PieSlice { label: "Ford"; value: 8.6 },
44 // PieSlice { label: "Ford"; value: 8.6 },
87 // PieSlice { label: "Skoda"; value: 8.2 },
45 // PieSlice { label: "Skoda"; value: 8.2 },
88 // PieSlice { label: "Volvo"; value: 6.8 },
46 // PieSlice { label: "Volvo"; value: 6.8 },
89 // PieSlice { label: "Others"; value: 52.0 }
47 // PieSlice { label: "Others"; value: 52.0 }
90 // ]
48 // ]
91 // }
49 // }
92 }
50 }
93
51
94
52
95 Chart {
53 Chart {
96 id: chart2
54 id: chart2
97 anchors.top: chart1.bottom
55 anchors.top: chart1.bottom
98 anchors.bottom: parent.bottom
56 anchors.bottom: parent.bottom
99 anchors.left: parent.left
57 anchors.left: parent.left
100 anchors.right: parent.right
58 anchors.right: parent.right
101 theme: Chart.ThemeScientific
59 theme: Chart.ThemeScientific
102
60
103 LineSeries {
61 LineSeries {
104 data: [
62 data: [
105 XyPoint { x: 0.0; y: 0.0 },
63 XyPoint { x: 0.0; y: 0.0 },
106 XyPoint { x: 1.1; y: 2.1 },
64 XyPoint { x: 1.1; y: 2.1 },
107 XyPoint { x: 2.9; y: 4.9 },
65 XyPoint { x: 2.9; y: 4.9 },
108 XyPoint { x: 3.2; y: 3.0 }
66 XyPoint { x: 3.2; y: 3.0 }
109 ]
67 ]
110 }
68 }
111
69
112 // ScatterSeries {
70 // ScatterSeries {
113 // id: scatter
71 // id: scatter
114 // data: [
72 // data: [
115 // XyPoint { x: 1.1; y: 1.1 },
73 // XyPoint { x: 1.1; y: 1.1 },
116 // XyPoint { x: 1.1; y: 1.2 },
74 // XyPoint { x: 1.1; y: 1.2 },
117 // XyPoint { x: 1.17; y: 1.15 }
75 // XyPoint { x: 1.17; y: 1.15 }
118 // ]
76 // ]
119 // }
77 // }
120 // ScatterSeries {
78 // ScatterSeries {
121 // data: [
79 // data: [
122 // XyPoint { x: 1.5; y: 1.5 },
80 // XyPoint { x: 1.5; y: 1.5 },
123 // XyPoint { x: 1.5; y: 1.6 },
81 // XyPoint { x: 1.5; y: 1.6 },
124 // XyPoint { x: 1.57; y: 1.55 }
82 // XyPoint { x: 1.57; y: 1.55 }
125 // ]
83 // ]
126 // }
84 // }
127 // ScatterSeries {
85 // ScatterSeries {
128 // data: [
86 // data: [
129 // XyPoint { x: 2.0; y: 2.0 },
87 // XyPoint { x: 2.0; y: 2.0 },
130 // XyPoint { x: 2.0; y: 2.1 },
88 // XyPoint { x: 2.0; y: 2.1 },
131 // XyPoint { x: 2.07; y: 2.05 }
89 // XyPoint { x: 2.07; y: 2.05 }
132 // ]
90 // ]
133 // }
91 // }
134 // ScatterSeries {
92 // ScatterSeries {
135 // data: [
93 // data: [
136 // XyPoint { x: 2.6; y: 2.6 },
94 // XyPoint { x: 2.6; y: 2.6 },
137 // XyPoint { x: 2.6; y: 2.7 },
95 // XyPoint { x: 2.6; y: 2.7 },
138 // XyPoint { x: 2.67; y: 2.65 }
96 // XyPoint { x: 2.67; y: 2.65 }
139 // ]
97 // ]
140 // }
98 // }
141 }
99 }
142 }
100 }
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now