##// END OF EJS Templates
Added QML api for bar series
Tero Ahola -
r646:a47b376c6f77
parent child
Show More
@@ -0,0 +1,50
1 #include "declarativebarseries.h"
2 #include "declarativechart.h"
3 #include "qchart.h"
4 #include "qbarseries.h"
5 #include "qbarset.h"
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) :
10 QDeclarativeItem(parent)
11 {
12 setFlag(QGraphicsItem::ItemHasNoContents, false);
13 connect(this, SIGNAL(parentChanged()),
14 this, SLOT(setParentForSeries()));
15 }
16
17 void DeclarativeBarSeries::setParentForSeries()
18 {
19 if (!m_series) {
20 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
21
22 if (declarativeChart) {
23 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
24 Q_ASSERT(chart);
25
26 QStringList categories;
27 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
28 m_series = new QBarSeries(categories);
29
30 // TODO: use data from model
31 QBarSet *set0 = new QBarSet("Bub");
32 QBarSet *set1 = new QBarSet("Bob");
33 QBarSet *set2 = new QBarSet("Guybrush");
34
35 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
36 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
37 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
38
39 m_series->addBarSet(set0);
40 m_series->addBarSet(set1);
41 m_series->addBarSet(set2);
42
43 chart->addSeries(m_series);
44 }
45 }
46 }
47
48 #include "moc_declarativebarseries.cpp"
49
50 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,34
1 #ifndef DECLARATIVEBARSERIES_H
2 #define DECLARATIVEBARSERIES_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 QBarSeries;
12
13 class DeclarativeBarSeries : public QDeclarativeItem
14 {
15 Q_OBJECT
16
17 public:
18 explicit DeclarativeBarSeries(QDeclarativeItem *parent = 0);
19
20 signals:
21
22 public slots:
23
24 private slots:
25 void setParentForSeries();
26
27 private:
28 QChart *m_chart;
29 QBarSeries *m_series;
30 };
31
32 QTCOMMERCIALCHART_END_NAMESPACE
33
34 #endif // DECLARATIVEBARSERIES_H
@@ -1,28 +1,40
1 #include "declarativechart.h"
1 #include "declarativechart.h"
2 #include <QPainter>s
2
3
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5
5 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
6 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
6 : QDeclarativeItem(parent),
7 : QDeclarativeItem(parent),
7 m_chart(new QChart(this))
8 m_chart(new QChart(this))
8 {
9 {
9 setFlag(QGraphicsItem::ItemHasNoContents, false);
10 setFlag(QGraphicsItem::ItemHasNoContents, false);
10 }
11 }
11
12
12 DeclarativeChart::ChartTheme DeclarativeChart::theme()
13 DeclarativeChart::ChartTheme DeclarativeChart::theme()
13 {
14 {
14 return (ChartTheme) m_chart->chartTheme();
15 return (ChartTheme) m_chart->chartTheme();
15 }
16 }
16
17
17 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
18 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
18 {
19 {
20 Q_UNUSED(oldGeometry)
21
19 if (newGeometry.isValid()) {
22 if (newGeometry.isValid()) {
20 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
23 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
21 m_chart->resize(newGeometry.width(), newGeometry.height());
24 m_chart->resize(newGeometry.width(), newGeometry.height());
22 }
25 }
23 }
26 }
24 }
27 }
25
28
29 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
30 {
31 Q_UNUSED(option)
32 Q_UNUSED(widget)
33
34 // TODO: optimized?
35 painter->setRenderHint(QPainter::Antialiasing, true);
36 }
37
26 #include "moc_declarativechart.cpp"
38 #include "moc_declarativechart.cpp"
27
39
28 QTCOMMERCIALCHART_END_NAMESPACE
40 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,44 +1,46
1 #ifndef DECLARATIVECHART_H
1 #ifndef DECLARATIVECHART_H
2 #define DECLARATIVECHART_H
2 #define DECLARATIVECHART_H
3
3
4 #include <QtCore/QtGlobal>
4 #include <QtCore/QtGlobal>
5 #include <QDeclarativeItem>
5 #include <QDeclarativeItem>
6 #include <qchart.h>
6 #include <qchart.h>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class DeclarativeChart : public QDeclarativeItem
10 class DeclarativeChart : public QDeclarativeItem
11 // TODO: for QTQUICK2: extend QQuickPainterItem instead
11 // TODO: for QTQUICK2: extend QQuickPainterItem instead
12 //class DeclarativeChart : public QQuickPaintedItem, public Chart
12 //class DeclarativeChart : public QQuickPaintedItem, public Chart
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 Q_ENUMS(ChartTheme)
15 Q_ENUMS(ChartTheme)
16 Q_PROPERTY(ChartTheme theme READ theme WRITE setTheme)
16 Q_PROPERTY(ChartTheme theme READ theme WRITE setTheme)
17
17
18 public:
18 public:
19 enum ChartTheme {
19 enum ChartTheme {
20 ThemeDefault,
20 ThemeDefault,
21 ThemeVanilla,
21 ThemeVanilla,
22 ThemeIcy,
22 ThemeIcy,
23 ThemeGrayscale,
23 ThemeGrayscale,
24 ThemeScientific,
24 ThemeScientific,
25 ThemeUnnamed1
25 ThemeBlueCerulean,
26 ThemeLight
26 };
27 };
27 DeclarativeChart(QDeclarativeItem *parent = 0);
28 DeclarativeChart(QDeclarativeItem *parent = 0);
28
29
29 public: // From QDeclarativeItem/QGraphicsItem
30 public: // From QDeclarativeItem/QGraphicsItem
30 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
31 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
32 void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
31
33
32 public:
34 public:
33 void setTheme(ChartTheme theme) {m_chart->setChartTheme((QChart::ChartTheme) theme);}
35 void setTheme(ChartTheme theme) {m_chart->setChartTheme((QChart::ChartTheme) theme);}
34 ChartTheme theme();
36 ChartTheme theme();
35
37
36 public:
38 public:
37 // Extending QChart with DeclarativeChart is not possible because QObject does not support
39 // Extending QChart with DeclarativeChart is not possible because QObject does not support
38 // multi inheritance, so we now have a QChart as a member instead
40 // multi inheritance, so we now have a QChart as a member instead
39 QChart *m_chart;
41 QChart *m_chart;
40 };
42 };
41
43
42 QTCOMMERCIALCHART_END_NAMESPACE
44 QTCOMMERCIALCHART_END_NAMESPACE
43
45
44 #endif // DECLARATIVECHART_H
46 #endif // DECLARATIVECHART_H
@@ -1,35 +1,37
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 "scatterelement.h"
4 #include "scatterelement.h"
5 #include "declarativescatterseries.h"
5 #include "declarativescatterseries.h"
6 #include "declarativelineseries.h"
6 #include "declarativelineseries.h"
7 #include "declarativebarseries.h"
7 #include "declarativepieseries.h"
8 #include "declarativepieseries.h"
8
9
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11
11 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
12 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
12 {
13 {
13 Q_OBJECT
14 Q_OBJECT
14 public:
15 public:
15 virtual void registerTypes(const char *uri)
16 virtual void registerTypes(const char *uri)
16 {
17 {
17 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
18 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
18
19
19 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "Chart");
20 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "Chart");
20 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
21 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
21 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
22 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
23 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
22 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
24 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
23 qmlRegisterType<QPieSlice>(uri, 1, 0, "ChartPieElement");
25 qmlRegisterType<QPieSlice>(uri, 1, 0, "ChartPieElement");
24 // TODO: rename ScatterElement class to something like "PointElement"
26 // TODO: rename ScatterElement class to something like "PointElement"
25 qmlRegisterType<ScatterElement>(uri, 1, 0, "ChartPointElement");
27 qmlRegisterType<ScatterElement>(uri, 1, 0, "ChartPointElement");
26 }
28 }
27 };
29 };
28
30
29 #include "plugin.moc"
31 #include "plugin.moc"
30
32
31 QTCOMMERCIALCHART_END_NAMESPACE
33 QTCOMMERCIALCHART_END_NAMESPACE
32
34
33 QTCOMMERCIALCHART_USE_NAMESPACE
35 QTCOMMERCIALCHART_USE_NAMESPACE
34
36
35 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
37 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
@@ -1,43 +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 scatterelement.cpp \
28 scatterelement.cpp \
29 declarativepieseries.cpp \
29 declarativepieseries.cpp \
30 declarativelineseries.cpp
30 declarativelineseries.cpp \
31 declarativebarseries.cpp
31 HEADERS += \
32 HEADERS += \
32 declarativechart.h \
33 declarativechart.h \
33 declarativescatterseries.h \
34 declarativescatterseries.h \
34 scatterelement.h \
35 scatterelement.h \
35 declarativepieseries.h \
36 declarativepieseries.h \
36 declarativelineseries.h
37 declarativelineseries.h \
38 declarativebarseries.h
37
39
38 TARGETPATH = QtCommercial/Chart
40 TARGETPATH = QtCommercial/Chart
39 target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
41 target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
40 qmldir.files += $$PWD/qmldir
42 qmldir.files += $$PWD/qmldir
41 qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
43 qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
42
44
43 INSTALLS += target qmldir
45 INSTALLS += target qmldir
@@ -1,112 +1,102
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("Component.onCompleted: " + ChartPointElement.x);
24 // console.log("Component.onCompleted: " + ChartPointElement.y);
25 // console.log("Component.onCompleted: " + ChartPointElement.dataX);
26 // console.log("Component.onCompleted: " + ChartPointElement.dataY);
27 //console.log("Component.onCompleted: " + chartModel.get(0).x);
28 //console.log("Component.onCompleted: " + chartModel.ChartPointElements);
29 // console.log("Component.onCompleted: " + elementt.dataX);
30 // console.log("Component.onCompleted: " + chartModel.get(0).dataX);
23 // console.log("Component.onCompleted: " + chartModel.get(0).dataX);
31 //ChartPointElement { x: 0.3; y: 0.3 }
32 }
24 }
33
25
26 // ChartModel {
27 // id: dynamicData
28 // }
29
34 Chart {
30 Chart {
35 id: chart1
31 id: chart1
36 anchors.top: parent.top
32 anchors.top: parent.top
37 anchors.left: parent.left
33 anchors.left: parent.left
38 anchors.right: parent.right
34 anchors.right: parent.right
39 height: parent.height / 2
35 height: parent.height / 2
40 theme: Chart.ThemeVanilla
36 theme: Chart.ThemeBlueCerulean
41
37
42 PieSeries {
38 BarSeries {
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 ]
53 }
39 }
54
40
55 // Series {
41 // PieSeries {
56 // seriesType: Series.SeriesTypeLine
42 // data: [
57 // }
43 // // TODO: "NnElement" matches the naming convention of for example ListModel...
58 // TODO:
44 // // But PieSlice would match the naming of QtCommercial Charts C++ api
59 // Series {
45 // ChartPieElement { label: "Volkswagen"; value: 13.5 },
60 // seriesType: Series.SeriesTypeBar
46 // ChartPieElement { label: "Toyota"; value: 10.9 },
47 // ChartPieElement { label: "Ford"; value: 8.6 },
48 // ChartPieElement { label: "Skoda"; value: 8.2 },
49 // ChartPieElement { label: "Volvo"; value: 6.8 },
50 // ChartPieElement { label: "Others"; value: 52.0 }
51 // ]
61 // }
52 // }
62 }
53 }
63
54
64
55
65 Chart {
56 Chart {
66 id: chart2
57 id: chart2
67 anchors.top: chart1.bottom
58 anchors.top: chart1.bottom
68 anchors.bottom: parent.bottom
59 anchors.bottom: parent.bottom
69 anchors.left: parent.left
60 anchors.left: parent.left
70 anchors.right: parent.right
61 anchors.right: parent.right
71 theme: Chart.ThemeScientific
62 theme: Chart.ThemeScientific
72
63
73 LineSeries {
64 LineSeries {
74 data: [
65 data: [
75 ChartPointElement { x: 0.0; y: 0.0 },
66 ChartPointElement { x: 0.0; y: 0.0 },
76 ChartPointElement { x: 1.1; y: 2.1 },
67 ChartPointElement { x: 1.1; y: 2.1 },
77 ChartPointElement { x: 2.9; y: 4.9 },
68 ChartPointElement { x: 2.9; y: 4.9 },
78 ChartPointElement { x: 3.2; y: 3.0 }
69 ChartPointElement { x: 3.2; y: 3.0 }
79 ]
70 ]
80 }
71 }
81
72
82 ScatterSeries {
73 ScatterSeries {
83 data: [
74 data: [
84 ChartPointElement { x: 1.1; y: 1.1 },
75 ChartPointElement { x: 1.1; y: 1.1 },
85 ChartPointElement { x: 1.1; y: 1.2 },
76 ChartPointElement { x: 1.1; y: 1.2 },
86 ChartPointElement { x: 1.17; y: 1.15 }
77 ChartPointElement { x: 1.17; y: 1.15 }
87 ]
78 ]
88 }
79 }
89 ScatterSeries {
80 ScatterSeries {
90 data: [
81 data: [
91 ChartPointElement { x: 1.5; y: 1.5 },
82 ChartPointElement { x: 1.5; y: 1.5 },
92 ChartPointElement { x: 1.5; y: 1.6 },
83 ChartPointElement { x: 1.5; y: 1.6 },
93 ChartPointElement { x: 1.57; y: 1.55 }
84 ChartPointElement { x: 1.57; y: 1.55 }
94 ]
85 ]
95 }
86 }
96 ScatterSeries {
87 ScatterSeries {
97 data: [
88 data: [
98 ChartPointElement { x: 2.0; y: 2.0 },
89 ChartPointElement { x: 2.0; y: 2.0 },
99 ChartPointElement { x: 2.0; y: 2.1 },
90 ChartPointElement { x: 2.0; y: 2.1 },
100 ChartPointElement { x: 2.07; y: 2.05 }
91 ChartPointElement { x: 2.07; y: 2.05 }
101 ]
92 ]
102 }
93 }
103 ScatterSeries {
94 ScatterSeries {
104 id: scatter4
105 data: [
95 data: [
106 ChartPointElement { x: 2.6; y: 2.6 },
96 ChartPointElement { x: 2.6; y: 2.6 },
107 ChartPointElement { x: 2.6; y: 2.7 },
97 ChartPointElement { x: 2.6; y: 2.7 },
108 ChartPointElement { x: 2.67; y: 2.65 }
98 ChartPointElement { x: 2.67; y: 2.65 }
109 ]
99 ]
110 }
100 }
111 }
101 }
112 }
102 }
General Comments 0
You need to be logged in to leave comments. Login now