From 5bffcd7b4ba5ad9cb6cf8704540db86cd57738a0 2012-03-26 11:27:34 From: Tero Ahola Date: 2012-03-26 11:27:34 Subject: [PATCH] QML bar series categories --- diff --git a/qmlplugin/declarativebarseries.cpp b/qmlplugin/declarativebarseries.cpp index bdae134..e0147ea 100644 --- a/qmlplugin/declarativebarseries.cpp +++ b/qmlplugin/declarativebarseries.cpp @@ -10,11 +10,9 @@ DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) : QDeclarativeItem(parent) { setFlag(QGraphicsItem::ItemHasNoContents, false); - connect(this, SIGNAL(parentChanged()), - this, SLOT(setParentForSeries())); } -void DeclarativeBarSeries::setParentForSeries() +void DeclarativeBarSeries::componentComplete() { if (!m_series) { DeclarativeChart *declarativeChart = qobject_cast(parent()); @@ -23,9 +21,11 @@ void DeclarativeBarSeries::setParentForSeries() QChart *chart = qobject_cast(declarativeChart->m_chart); Q_ASSERT(chart); - QStringList categories; - categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; - m_series = new QBarSeries(categories); +// QStringList categories; +// categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; +// m_series = new QBarSeries(categories); +// m_series = new QBarSeries(m_categories); + m_series = new QBarSeries(m_categories); // TODO: use data from model QBarSet *set0 = new QBarSet("Bub"); @@ -45,6 +45,25 @@ void DeclarativeBarSeries::setParentForSeries() } } +void DeclarativeBarSeries::setBarCategories(QStringList categories) +{ + m_categories = categories; + if (m_series) { + // Replace categories of the QBarSeries with the new categories + for (int i(0); i < m_categories.count(); i++) { + if (m_series->categories().at(i) != m_categories.at(i)) + m_series->insertCategory(m_series->categoryCount(), m_categories.at(i)); + } + while (m_series->categoryCount() > m_categories.count()) + m_series->removeCategory(m_series->categoryCount() - 1); + } +} + +QStringList DeclarativeBarSeries::barCategories() +{ + return m_categories; +} + #include "moc_declarativebarseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativebarseries.h b/qmlplugin/declarativebarseries.h index 50fbc55..9091e4e 100644 --- a/qmlplugin/declarativebarseries.h +++ b/qmlplugin/declarativebarseries.h @@ -12,20 +12,26 @@ class QBarSeries; class DeclarativeBarSeries : public QDeclarativeItem { Q_OBJECT + Q_PROPERTY(QStringList barCategories READ barCategories WRITE setBarCategories) public: explicit DeclarativeBarSeries(QDeclarativeItem *parent = 0); +public: // from QDeclarativeParserStatus + void componentComplete(); + +public: + void setBarCategories(QStringList categories); + QStringList barCategories(); + signals: public slots: -private slots: - void setParentForSeries(); - -private: +public: QChart *m_chart; QBarSeries *m_series; + QStringList m_categories; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativescatterseries.cpp b/qmlplugin/declarativescatterseries.cpp index 2266bd6..9dbd0f9 100644 --- a/qmlplugin/declarativescatterseries.cpp +++ b/qmlplugin/declarativescatterseries.cpp @@ -28,31 +28,31 @@ void DeclarativeScatterSeries::componentComplete() Q_ASSERT(m_chart); m_series = new QScatterSeries(); - for (int i(0); i < m_data.count(); i++) { - DeclarativeXyPoint *element = m_data.at(i); + for (int i(0); i < m_points.count(); i++) { + DeclarativeXyPoint *element = m_points.at(i); *m_series << QPointF(element->x(), element->y()); } m_chart->addSeries(m_series); } } -QDeclarativeListProperty DeclarativeScatterSeries::data() +QDeclarativeListProperty DeclarativeScatterSeries::points() { return QDeclarativeListProperty(this, 0, - &DeclarativeScatterSeries::appendData); + &DeclarativeScatterSeries::appendPoints); } -void DeclarativeScatterSeries::appendData(QDeclarativeListProperty *list, +void DeclarativeScatterSeries::appendPoints(QDeclarativeListProperty *list, DeclarativeXyPoint *element) { DeclarativeScatterSeries *series = qobject_cast(list->object); - qDebug() << "appendData: " << series; - qDebug() << "appendData: " << element; - qDebug() << "appendData: " << element->x(); - qDebug() << "appendData: " << element->y(); - qDebug() << "appendData: " << series->m_series; + qDebug() << "appendPoints: " << series; + qDebug() << "appendPoints: " << element; + qDebug() << "appendPoints: " << element->x(); + qDebug() << "appendPoints: " << element->y(); + qDebug() << "appendPoints: " << series->m_series; if (series) { - series->m_data.append(element); + series->m_points.append(element); if (series->m_series) series->m_series->add(element->x(), element->y()); } diff --git a/qmlplugin/declarativescatterseries.h b/qmlplugin/declarativescatterseries.h index 6d892e7..5d9511d 100644 --- a/qmlplugin/declarativescatterseries.h +++ b/qmlplugin/declarativescatterseries.h @@ -4,17 +4,16 @@ #include "qchartglobal.h" #include "declarativexypoint.h" #include -#include QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChart; class QScatterSeries; -class DeclarativeScatterSeries : public QDeclarativeItem//, public QDeclarativeParserStatus +class DeclarativeScatterSeries : public QDeclarativeItem { Q_OBJECT - Q_PROPERTY(QDeclarativeListProperty data READ data) + Q_PROPERTY(QDeclarativeListProperty points READ points) public: explicit DeclarativeScatterSeries(QDeclarativeItem *parent = 0); @@ -24,12 +23,12 @@ public: // from QDeclarativeParserStatus void componentComplete(); public: - QDeclarativeListProperty data(); + QDeclarativeListProperty points(); signals: public slots: - static void appendData(QDeclarativeListProperty *list, + static void appendPoints(QDeclarativeListProperty *list, DeclarativeXyPoint *element); private slots: @@ -37,7 +36,7 @@ private slots: public: QChart *m_chart; // not owned QScatterSeries *m_series; // not owned - QList m_data; + QList m_points; // not owned }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/test/qmlchart/qml/qmlchart/main.qml b/test/qmlchart/qml/qmlchart/main.qml index 5eadba6..ab5d515 100644 --- a/test/qmlchart/qml/qmlchart/main.qml +++ b/test/qmlchart/qml/qmlchart/main.qml @@ -35,6 +35,10 @@ Rectangle { theme: Chart.ThemeBlueCerulean BarSeries { + barCategories: [ "2008", "2009", "2010", "2011", "2012" ] +// data: [ +// BarSet { } +// ] } // PieSeries { @@ -56,7 +60,7 @@ Rectangle { anchors.bottom: parent.bottom anchors.left: parent.left anchors.right: parent.right - theme: Chart.ThemeScientific + theme: Chart.ThemeBrownSand LineSeries { data: [ @@ -67,34 +71,34 @@ Rectangle { ] } -// ScatterSeries { -// id: scatter -// data: [ -// XyPoint { x: 1.1; y: 1.1 }, -// XyPoint { x: 1.1; y: 1.2 }, -// XyPoint { x: 1.17; y: 1.15 } -// ] -// } -// ScatterSeries { -// data: [ -// XyPoint { x: 1.5; y: 1.5 }, -// XyPoint { x: 1.5; y: 1.6 }, -// XyPoint { x: 1.57; y: 1.55 } -// ] -// } -// ScatterSeries { -// data: [ -// XyPoint { x: 2.0; y: 2.0 }, -// XyPoint { x: 2.0; y: 2.1 }, -// XyPoint { x: 2.07; y: 2.05 } -// ] -// } -// ScatterSeries { -// data: [ -// XyPoint { x: 2.6; y: 2.6 }, -// XyPoint { x: 2.6; y: 2.7 }, -// XyPoint { x: 2.67; y: 2.65 } -// ] -// } + ScatterSeries { + id: scatter + points: [ + XyPoint { x: 1.1; y: 1.1 }, + XyPoint { x: 1.1; y: 1.2 }, + XyPoint { x: 1.17; y: 1.15 } + ] + } + ScatterSeries { + points: [ + XyPoint { x: 1.5; y: 1.5 }, + XyPoint { x: 1.5; y: 1.6 }, + XyPoint { x: 1.57; y: 1.55 } + ] + } + ScatterSeries { + points: [ + XyPoint { x: 2.0; y: 2.0 }, + XyPoint { x: 2.0; y: 2.1 }, + XyPoint { x: 2.07; y: 2.05 } + ] + } + ScatterSeries { + points: [ + XyPoint { x: 2.6; y: 2.6 }, + XyPoint { x: 2.6; y: 2.7 }, + XyPoint { x: 2.67; y: 2.65 } + ] + } } }