diff --git a/qmlplugin/declarativelineseries.cpp b/qmlplugin/declarativelineseries.cpp new file mode 100644 index 0000000..627c3c4 --- /dev/null +++ b/qmlplugin/declarativelineseries.cpp @@ -0,0 +1,55 @@ +#include "declarativelineseries.h" +#include "declarativechart.h" +#include "qchart.h" +#include "qlinechartseries.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +DeclarativeLineSeries::DeclarativeLineSeries(QDeclarativeItem *parent) : + QDeclarativeItem(parent) +{ + setFlag(QGraphicsItem::ItemHasNoContents, false); + connect(this, SIGNAL(parentChanged()), + this, SLOT(setParentForSeries())); +} + +void DeclarativeLineSeries::setParentForSeries() +{ + if (!m_series) { + DeclarativeChart *declarativeChart = qobject_cast(parent()); + + if (declarativeChart) { + QChart *chart = qobject_cast(declarativeChart->m_chart); + qDebug() << "creating line series for chart: " << chart; + Q_ASSERT(chart); + + m_series = new QLineChartSeries(); + Q_ASSERT(m_series); + for (int i(0); i < m_data.count(); i++) { + ScatterElement *element = m_data.at(i); + m_series->add(element->x(), element->y()); + } + chart->addSeries(m_series); + } + } +} + +QDeclarativeListProperty DeclarativeLineSeries::data() +{ + return QDeclarativeListProperty(this, 0, &DeclarativeLineSeries::appendData); +} + +void DeclarativeLineSeries::appendData(QDeclarativeListProperty *list, + ScatterElement *element) +{ + DeclarativeLineSeries *series = qobject_cast(list->object); + if (series) { + series->m_data.append(element); + if (series->m_series) + series->m_series->add(element->x(), element->y()); + } +} + +#include "moc_declarativelineseries.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativelineseries.h b/qmlplugin/declarativelineseries.h new file mode 100644 index 0000000..9819453 --- /dev/null +++ b/qmlplugin/declarativelineseries.h @@ -0,0 +1,39 @@ +#ifndef DECLARATIVELINESERIES_H +#define DECLARATIVELINESERIES_H + +#include "qchartglobal.h" +#include "scatterelement.h" // TODO: rename header +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QChart; +class QLineChartSeries; + +class DeclarativeLineSeries : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(QDeclarativeListProperty data READ data) + +public: + explicit DeclarativeLineSeries(QDeclarativeItem *parent = 0); + QDeclarativeListProperty data(); + +signals: + +public slots: + static void appendData(QDeclarativeListProperty *list, + ScatterElement *element); + +private slots: + void setParentForSeries(); + +private: + QChart *m_chart; + QLineChartSeries *m_series; + QList m_data; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // DECLARATIVELINESERIES_H diff --git a/qmlplugin/declarativepieseries.cpp b/qmlplugin/declarativepieseries.cpp new file mode 100644 index 0000000..3270387 --- /dev/null +++ b/qmlplugin/declarativepieseries.cpp @@ -0,0 +1,58 @@ +#include "declarativepieseries.h" +#include "declarativechart.h" +#include "qchart.h" +#include "qpieseries.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +DeclarativePieSeries::DeclarativePieSeries(QDeclarativeItem *parent) : + QDeclarativeItem(parent), + m_chart(0), + m_series(0) +{ + setFlag(QGraphicsItem::ItemHasNoContents, false); + connect(this, SIGNAL(parentChanged()), + this, SLOT(setParentForSeries())); +} + +void DeclarativePieSeries::setParentForSeries() +{ + if (!m_series) { + DeclarativeChart *declarativeChart = qobject_cast(parent()); + if (declarativeChart) { + QChart *chart = qobject_cast(declarativeChart->m_chart); + qDebug() << "creating pie series for chart: " << chart; + Q_ASSERT(chart); + + m_series = new QPieSeries(); + Q_ASSERT(m_series); + foreach (QPieSlice* slice, m_data) { + // Have to duplicate the data + m_series->add(slice->value(), slice->label()); + } + chart->addSeries(m_series); + //chart->axisY(); + } + } +} + +QDeclarativeListProperty DeclarativePieSeries::data() +{ + return QDeclarativeListProperty(this, 0, &DeclarativePieSeries::appendData); +} + +void DeclarativePieSeries::appendData(QDeclarativeListProperty *list, + QPieSlice *slice) +{ + DeclarativePieSeries *series = qobject_cast(list->object); + if (series) { + series->m_data.append(slice); + // Have to duplicate the data + if (series->m_series) + series->m_series->add(slice->value(), slice->label()); + } +} + +#include "moc_declarativepieseries.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativepieseries.h b/qmlplugin/declarativepieseries.h new file mode 100644 index 0000000..5e27423 --- /dev/null +++ b/qmlplugin/declarativepieseries.h @@ -0,0 +1,39 @@ +#ifndef DECLARATIVEPIESERIES_H +#define DECLARATIVEPIESERIES_H + +#include "qchartglobal.h" +#include "qpieslice.h" +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QPieSeries; +class QChart; + +class DeclarativePieSeries : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(QDeclarativeListProperty data READ data) + +public: + explicit DeclarativePieSeries(QDeclarativeItem *parent = 0); + QDeclarativeListProperty data(); + +signals: + +public slots: + static void appendData(QDeclarativeListProperty *list, + QPieSlice *element); + +private slots: + void setParentForSeries(); + +private: + QChart *m_chart; + QPieSeries *m_series; + QList m_data; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // DECLARATIVEPIESERIES_H diff --git a/qmlplugin/declarativescatterseries.cpp b/qmlplugin/declarativescatterseries.cpp index 50925ff..6100df3 100644 --- a/qmlplugin/declarativescatterseries.cpp +++ b/qmlplugin/declarativescatterseries.cpp @@ -2,7 +2,6 @@ #include "declarativechart.h" #include "qchart.h" #include "qscatterseries.h" -#include QTCOMMERCIALCHART_BEGIN_NAMESPACE diff --git a/qmlplugin/declarativescatterseries.h b/qmlplugin/declarativescatterseries.h index fda58c0..1ae0af2 100644 --- a/qmlplugin/declarativescatterseries.h +++ b/qmlplugin/declarativescatterseries.h @@ -24,7 +24,6 @@ signals: public slots: static void appendData(QDeclarativeListProperty *list, ScatterElement *element); -// void append(ScatterElement element); private slots: void setParentForSeries(); diff --git a/qmlplugin/plugin.cpp b/qmlplugin/plugin.cpp index 545952c..697b982 100644 --- a/qmlplugin/plugin.cpp +++ b/qmlplugin/plugin.cpp @@ -4,6 +4,8 @@ #include "declarativeseries.h" #include "scatterelement.h" #include "declarativescatterseries.h" +#include "declarativelineseries.h" +#include "declarativepieseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -14,10 +16,15 @@ public: virtual void registerTypes(const char *uri) { Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart")); + qmlRegisterType(uri, 1, 0, "Chart"); qmlRegisterType(uri, 1, 0, "Series"); qmlRegisterType(uri, 1, 0, "ScatterSeries"); - qmlRegisterType(uri, 1, 0, "ScatterElement"); + qmlRegisterType(uri, 1, 0, "LineSeries"); + qmlRegisterType(uri, 1, 0, "PieSeries"); + qmlRegisterType(uri, 1, 0, "ChartPieElement"); + // TODO: rename ScatterElement class to something like "PointElement" + qmlRegisterType(uri, 1, 0, "ChartPointElement"); } }; diff --git a/qmlplugin/qmlplugin.pro b/qmlplugin/qmlplugin.pro index 6a452a8..3416541 100644 --- a/qmlplugin/qmlplugin.pro +++ b/qmlplugin/qmlplugin.pro @@ -26,12 +26,16 @@ SOURCES += \ declarativechart.cpp \ declarativeseries.cpp \ declarativescatterseries.cpp \ - scatterelement.cpp + scatterelement.cpp \ + declarativepieseries.cpp \ + declarativelineseries.cpp HEADERS += \ declarativechart.h \ declarativeseries.h \ declarativescatterseries.h \ - scatterelement.h + scatterelement.h \ + declarativepieseries.h \ + declarativelineseries.h TARGETPATH = QtCommercial/Chart target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH @@ -55,3 +59,7 @@ INSTALLS += target qmldir + + + + diff --git a/src/piechart/qpieslice.h b/src/piechart/qpieslice.h index 4a3bddf..1d42328 100644 --- a/src/piechart/qpieslice.h +++ b/src/piechart/qpieslice.h @@ -12,6 +12,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject { Q_OBJECT + Q_PROPERTY(QString label READ label WRITE setLabel /*NOTIFY dataYChanged*/) + Q_PROPERTY(qreal value READ value WRITE setValue /*NOTIFY dataXChanged*/) public: QPieSlice(QObject *parent = 0); diff --git a/test/qmlchart/qml/qmlchart/main.qml b/test/qmlchart/qml/qmlchart/main.qml index 87ba5e2..7988a5c 100644 --- a/test/qmlchart/qml/qmlchart/main.qml +++ b/test/qmlchart/qml/qmlchart/main.qml @@ -20,14 +20,15 @@ Rectangle { // } Component.onCompleted: { -// console.log("Component.onCompleted: " + scatterElement.x); -// console.log("Component.onCompleted: " + scatterElement.y); -// console.log("Component.onCompleted: " + scatterElement.dataX); -// console.log("Component.onCompleted: " + scatterElement.dataY); +// console.log("Component.onCompleted: " + ChartPointElement.x); +// console.log("Component.onCompleted: " + ChartPointElement.y); +// console.log("Component.onCompleted: " + ChartPointElement.dataX); +// console.log("Component.onCompleted: " + ChartPointElement.dataY); //console.log("Component.onCompleted: " + chartModel.get(0).x); - //console.log("Component.onCompleted: " + chartModel.scatterElements); + //console.log("Component.onCompleted: " + chartModel.ChartPointElements); // console.log("Component.onCompleted: " + elementt.dataX); // console.log("Component.onCompleted: " + chartModel.get(0).dataX); + //ChartPointElement { x: 0.3; y: 0.3 } } Chart { @@ -36,16 +37,24 @@ Rectangle { anchors.left: parent.left anchors.right: parent.right height: parent.height / 2 - theme: Chart.ThemeIcy -// opacity: 0.3 + theme: Chart.ThemeVanilla - Series { - seriesType: Series.SeriesTypePie + PieSeries { + data: [ + // TODO: "NnElement" matches the naming convention of for example ListModel... + // But PieSlice would match the naming of QtCommercial Charts C++ api + ChartPieElement { label: "Volkswagen"; value: 13.5 }, + ChartPieElement { label: "Toyota"; value: 10.9 }, + ChartPieElement { label: "Ford"; value: 8.6 }, + ChartPieElement { label: "Skoda"; value: 8.2 }, + ChartPieElement { label: "Volvo"; value: 6.8 }, + ChartPieElement { label: "Others"; value: 52.0 } + ] } - Series { - seriesType: Series.SeriesTypeLine - } +// Series { +// seriesType: Series.SeriesTypeLine +// } // TODO: // Series { // seriesType: Series.SeriesTypeBar @@ -61,32 +70,42 @@ Rectangle { anchors.right: parent.right theme: Chart.ThemeScientific + LineSeries { + data: [ + ChartPointElement { x: 0.0; y: 0.0 }, + ChartPointElement { x: 1.1; y: 2.1 }, + ChartPointElement { x: 2.9; y: 4.9 }, + ChartPointElement { x: 3.2; y: 3.0 } + ] + } + ScatterSeries { data: [ - ScatterElement { x: 1.1; y: 2.1 }, - ScatterElement { x: 1.2; y: 2.0 }, - ScatterElement { x: 1.4; y: 2.3 } + ChartPointElement { x: 1.1; y: 1.1 }, + ChartPointElement { x: 1.1; y: 1.2 }, + ChartPointElement { x: 1.17; y: 1.15 } ] } ScatterSeries { data: [ - ScatterElement { x: 1.2; y: 2.2 }, - ScatterElement { x: 1.3; y: 2.2 }, - ScatterElement { x: 1.7; y: 2.6 } + ChartPointElement { x: 1.5; y: 1.5 }, + ChartPointElement { x: 1.5; y: 1.6 }, + ChartPointElement { x: 1.57; y: 1.55 } ] } ScatterSeries { data: [ - ScatterElement { x: 1.3; y: 2.3 }, - ScatterElement { x: 1.5; y: 2.4 }, - ScatterElement { x: 2.0; y: 2.9 } + ChartPointElement { x: 2.0; y: 2.0 }, + ChartPointElement { x: 2.0; y: 2.1 }, + ChartPointElement { x: 2.07; y: 2.05 } ] } ScatterSeries { + id: scatter4 data: [ - ScatterElement { x: 1.4; y: 2.4 }, - ScatterElement { x: 1.8; y: 2.7 }, - ScatterElement { x: 2.5; y: 3.2 } + ChartPointElement { x: 2.6; y: 2.6 }, + ChartPointElement { x: 2.6; y: 2.7 }, + ChartPointElement { x: 2.67; y: 2.65 } ] } }