From 0dff3ce362756e16ec5770bdf131b6cfcc9d04c6 2012-02-15 09:43:37 From: Tero Ahola Date: 2012-02-15 09:43:37 Subject: [PATCH] Modifying QScatterSeries API --- diff --git a/example/scatter/main.cpp b/example/scatter/main.cpp index c5260d9..7d0b6f0 100644 --- a/example/scatter/main.cpp +++ b/example/scatter/main.cpp @@ -18,14 +18,9 @@ int main(int argc, char *argv[]) Q_ASSERT(scatter); // Add test data to the series - QList x; - QList y; - for (qreal i(0.0); i < 20; i += 0.5) { - // Linear data with random component - x.append(i + ((qreal)(rand() % 100)) / 100 ); - y.append(i + ((qreal)(rand() % 100)) / 100 ); - } - scatter->setData(x, y); + for (qreal i(0.0); i < 20; i += 0.5) + scatter->addData(QPointF(i + ((qreal)(rand() % 100)) / 100, + i + ((qreal)(rand() % 100)) / 100 )); // Use the chart widget as the central widget QMainWindow w; diff --git a/qmlplugin/declarativeseries.cpp b/qmlplugin/declarativeseries.cpp index db763f8..6925ab2 100644 --- a/qmlplugin/declarativeseries.cpp +++ b/qmlplugin/declarativeseries.cpp @@ -1,5 +1,6 @@ #include "declarativeseries.h" #include "declarativechart.h" +#include #include #include #include @@ -54,13 +55,11 @@ void DeclarativeSeries::initSeries() // fallthrough; bar and scatter use the same test data case SeriesTypeScatter: { m_series = chart->createSeries((QChartSeries::QChartSeriesType) m_seriesType); - QList datax; - QList datay; - for (qreal i = 0; i < 100; i += 0.1) { - datax.append(i + (rand() % 5)); - datay.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); - } - Q_ASSERT(m_series->setData(datax, datay)); + QScatterSeries *scatter = qobject_cast(m_series); + Q_ASSERT(scatter); + for (qreal i = 0; i < 100; i += 0.1) + scatter->addData(QPointF(i + (rand() % 5), + abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5))); break; } case SeriesTypeStackedBar: diff --git a/src/qscatterseries.cpp b/src/qscatterseries.cpp index 92a5ef2..4d527c2 100644 --- a/src/qscatterseries.cpp +++ b/src/qscatterseries.cpp @@ -21,30 +21,17 @@ QScatterSeries::~QScatterSeries() } // TODO: change to list of QPointFs? -bool QScatterSeries::setData(QList xlist, QList ylist) +//bool QScatterSeries::setData(QList xlist, QList ylist) +void QScatterSeries::setData(QList data) { - d->m_data.clear(); - // TODO: validate data - for (int i(0); i < xlist.count() && i < ylist.count(); i++) { - d->m_data.append(QPointF(xlist[i], ylist[i])); - } - - - // TODO: the following updates the visible chart area setting of the series, we would instead - // need to update the _chart's_ visible area... this would require a callback or - // similar to the parenting QChart object... -// foreach (qreal x, d->m_x) { -// d->m_visibleChartArea.m_minX = qMin(d->m_visibleChartArea.m_minX, x); -// d->m_visibleChartArea.m_maxX = qMax(d->m_visibleChartArea.m_maxX, x); -// } -// foreach (qreal y, d->m_y) { -// d->m_visibleChartArea.m_minY = qMin(d->m_visibleChartArea.m_minY, y); -// d->m_visibleChartArea.m_maxY = qMax(d->m_visibleChartArea.m_maxY, y); -// } -// d->changeGeometry(); + d->m_data = data; + emit changed(); +} +void QScatterSeries::addData(QPointF data) +{ + d->m_data.append(data); emit changed(); - return true; } QList QScatterSeries::data() @@ -52,17 +39,14 @@ QList QScatterSeries::data() return d->m_data; } -void QScatterSeries::setMarkerColor(QColor color) +void QScatterSeries::setMarkerPen(QPen pen) { - // TODO: -// d->m_markerColor = color; + d->m_markerPen = pen; } -QColor QScatterSeries::markerColor() +QPen QScatterSeries::markerPen() { - // TODO: -// return d->m_markerColor; - return QColor(); + return d->m_markerPen; } #include "moc_qscatterseries.cpp" diff --git a/src/qscatterseries.h b/src/qscatterseries.h index 7c72359..89a4411 100644 --- a/src/qscatterseries.h +++ b/src/qscatterseries.h @@ -18,21 +18,28 @@ public: public: // from QChartSeries QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; } - bool setData(QList x, QList y); public: + // TODO: the name of the function? addPoint? addData? addX? + void addData(QPointF data); + + void setData(QList data); + QList data(); + + //TODO? void insertData(int index, QPointF data); + + void setMarkerPen(QPen pen); + QPen markerPen(); + // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot + //void setMarkerShape(MarkerShape shape); + Q_SIGNALS: // TODO: move to PIMPL? - // TODO: more finegrained signaling + // TODO: more finegrained signaling for performance reasons void changed(); -public Q_SLOTS: - // TODO: also affects opacity of the marker...? To be documented - void setMarkerColor(QColor color); - QColor markerColor(); - // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot - //void setMarkerShape(QChartSeries::MarkerShape/QScatterSeries::MarkerShape shape); +//public Q_SLOTS: private: Q_DECLARE_PRIVATE(QScatterSeries) Q_DISABLE_COPY(QScatterSeries) diff --git a/src/qscatterseries_p.h b/src/qscatterseries_p.h index 2893c64..cbe256f 100644 --- a/src/qscatterseries_p.h +++ b/src/qscatterseries_p.h @@ -3,6 +3,7 @@ #include "qchartglobal.h" #include "qchartseries.h" +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -15,9 +16,8 @@ public: QScatterSeriesPrivate(); public: - - // TODO: use the chart data class instead of list of x and y values? QList m_data; + QPen m_markerPen; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/scatterpresenter.cpp b/src/scatterpresenter.cpp index 434cd21..2959349 100644 --- a/src/scatterpresenter.cpp +++ b/src/scatterpresenter.cpp @@ -60,10 +60,7 @@ void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem * // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize // event right after construction or maybe given a size during initialization - qDebug() << "scene w: "<< scene()->width() << " h: " << scene()->height(); for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) { - qDebug() << "scene w: "<< scene()->width() << " h: " << scene()->height(); - qDebug() << "x: "<< m_scenex.at(i) << " y: " << m_sceney.at(i); if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i)) //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760); painter->drawPoint(m_scenex.at(i), m_sceney.at(i)); diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index 8bfdfaa..ab17603 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -2,6 +2,7 @@ #include "dataseriedialog.h" #include "qchartseries.h" #include "qpieseries.h" +#include "qscatterseries.h" #include #include #include @@ -227,7 +228,10 @@ void MainWidget::addSeries(QString series, QString data) QChartSeries *newSeries = 0; if (series == "Scatter") { newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter); - Q_ASSERT(newSeries->setData(x, y)); + QScatterSeries *scatterSeries = qobject_cast(newSeries); + Q_ASSERT(scatterSeries); + for (int i(0); i < x.count() && i < y.count(); i++) + scatterSeries->addData(QPointF(x.at(i), y.at(i))); } else if (series == "Pie") { newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie); Q_ASSERT(newSeries->setData(y)); diff --git a/test/chartwidgettest/qscatterseries.cpp b/test/chartwidgettest/qscatterseries.cpp deleted file mode 100644 index 0d88a4d..0000000 --- a/test/chartwidgettest/qscatterseries.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "qscatterseries.h" -#include "qseriespointgraphicsitem.h" -#include -#include - -QScatterSeries::QScatterSeries(QGraphicsItem *parent) : - QGraphicsItem(parent) -{ -} - -QRectF QScatterSeries::boundingRect() const -{ - return QRectF(0, 50, 100, 100); -} - -void QScatterSeries::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) -{ -// painter->drawRect(boundingRect()); -} - -void QScatterSeries::setData(QList data) -//void QScatterSeries::setData(QList data) -{ - foreach(QChartDataPoint point, data) { - // TODO: ownership? - // TODO: position - QSeriesPointGraphicsItem *item = new QSeriesPointGraphicsItem( - point[0].toReal(), 100-point[1].toReal(), this); - } -} diff --git a/test/chartwidgettest/qscatterseries.h b/test/chartwidgettest/qscatterseries.h deleted file mode 100644 index e6e1b3b..0000000 --- a/test/chartwidgettest/qscatterseries.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef QSCATTERSERIES_H -#define QSCATTERSERIES_H - -#include - -// TODO: -#define QChartDataPoint QList - -class QSeriesPointGraphicsItem; - -class QScatterSeries : public QGraphicsItem -{ -public: - QScatterSeries(QGraphicsItem *parent = 0); - void setData(QList data); -// void setData(QList data); - -signals: - -public slots: - -public: - QRectF boundingRect() const; - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); -}; - -#endif // QSCATTERSERIES_H diff --git a/test/chartwidgettest/qseriespointgraphicsitem.cpp b/test/chartwidgettest/qseriespointgraphicsitem.cpp deleted file mode 100644 index 9c6558c..0000000 --- a/test/chartwidgettest/qseriespointgraphicsitem.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "qseriespointgraphicsitem.h" -#include -#include -#include -#include - -QSeriesPointGraphicsItem::QSeriesPointGraphicsItem(qreal dx, qreal dy, QGraphicsItem *parent) - : QGraphicsItem(parent), - m_dx(dx), - m_dy(dy) -{ -} - -QRectF QSeriesPointGraphicsItem::boundingRect() const -{ -// return parentItem()->boundingRect(); - return QRectF(0, 0, 10, 10); -} - -void QSeriesPointGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) -{ - QTransform transform = painter->transform(); - transform.translate(m_dx, m_dy); - painter->setTransform(transform); - - QPen pen = painter->pen(); - QBrush brush = pen.brush(); - // TODO: The opacity should be user definable... - brush.setColor(QColor(255, 82, 0, 50)); - pen.setBrush(brush); - pen.setWidth(4); - painter->setPen(pen); - painter->drawArc(0, 0, 4, 4, 0, 5760); - - // TODO: how about using a bitmap? -// QPixmap pixmap; -// pixmap.load("scatter.png"); -// painter->drawPixmap(boundingRect().toRect(), pixmap); - -// painter->drawRect(boundingRect()); -} diff --git a/test/chartwidgettest/qseriespointgraphicsitem.h b/test/chartwidgettest/qseriespointgraphicsitem.h deleted file mode 100644 index 29f94ef..0000000 --- a/test/chartwidgettest/qseriespointgraphicsitem.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef QSERIESPOINTGRAPHICSITEM_H -#define QSERIESPOINTGRAPHICSITEM_H - -#include - -class QSeriesPointGraphicsItem : public QGraphicsItem -{ -public: - QSeriesPointGraphicsItem(qreal dx, qreal dy, QGraphicsItem *parent = 0); - -private: - QRectF boundingRect() const; - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); - qreal m_dx; - qreal m_dy; -}; - -#endif // QSERIESPOINTGRAPHICSITEM_H diff --git a/test/qmlchart/qml/qmlchart/main.qml b/test/qmlchart/qml/qmlchart/main.qml index 4b43b8a..74d4e33 100644 --- a/test/qmlchart/qml/qmlchart/main.qml +++ b/test/qmlchart/qml/qmlchart/main.qml @@ -9,6 +9,69 @@ Rectangle { anchors.centerIn: parent } +// ListModel { +// id: listModelForPie +// // PieDataElement +// ListElement { +// label: "Apple" +// value: 40.3 +// } +// ListElement { +// label: "Pumpkin" +// value: 10.1 +// } +// ListElement { +// label: "Raspberry" +// value: 15.1 +// } +// ListElement { +// label: "Strawberry" +// value: 29.9 +// } +// } + +// ChartModel { +// id: chartModel +// ListElement { +// label: "dada" +// x: 1.1 +// y: 3.2 +// } +// } + +// ChartModel { +// ScatterElement {x: 1.1; y: 1.2} +// ScatterElement {x: 1.3; y: 1.9} +// ScatterElement {x: 1.1; y: 1.2} +// } + + ListModel { + id: listModelScatter + ListElement { + height: 154 + weight: 54 + } + ListElement { + height: 166 + weight: 64 + } + ListElement { + height: 199 + weight: 97 + } + } + +// Chart { +// anchors.fill: parent +// theme: Chart.ThemeIcy +// ScatterSeries { +// model: listModelScatter +// name: "scatter" +// xValue: x +// yValue: y +// } +// } + Chart { anchors.fill: parent theme: Chart.ThemeIcy @@ -33,6 +96,14 @@ Rectangle { // seriesType: Series.SeriesTypePie // } Series { + seriesType: Series.SeriesTypePie + //model: listModelForPie + //seriesData: {11.0, 6.4, 12.6, 22.4} + //seriesLabels: {"Strawberry", "Blackberry", "Apple", "Pumpkin"} + } + + Series { +// data: {[1.2], "y":2.2 } seriesType: Series.SeriesTypeScatter } Series {