diff --git a/example/scatter/main.cpp b/example/scatter/main.cpp index 7d0b6f0..a3749d0 100644 --- a/example/scatter/main.cpp +++ b/example/scatter/main.cpp @@ -13,14 +13,15 @@ int main(int argc, char *argv[]) // Create widget and scatter series QChartView *chartWidget = new QChartView(); - QScatterSeries *scatter = - qobject_cast(chartWidget->createSeries(QChartSeries::SeriesTypeScatter)); - Q_ASSERT(scatter); + QScatterSeries *scatter = new QScatterSeries(); // Add test data to the series for (qreal i(0.0); i < 20; i += 0.5) - scatter->addData(QPointF(i + ((qreal)(rand() % 100)) / 100, - i + ((qreal)(rand() % 100)) / 100 )); + (*scatter) << QPointF(i + (qreal)(rand() % 100) / 100.0, + i + (qreal)(rand() % 100) / 100.0); + + // Add series to the chart widget + chartWidget->addSeries(scatter); // Use the chart widget as the central widget QMainWindow w; diff --git a/qmlplugin/declarativeseries.cpp b/qmlplugin/declarativeseries.cpp index 6925ab2..7dc9dfc 100644 --- a/qmlplugin/declarativeseries.cpp +++ b/qmlplugin/declarativeseries.cpp @@ -23,12 +23,17 @@ void DeclarativeSeries::setSeriesType(SeriesType type) if (!m_series || type != m_seriesType) { m_seriesType = type; initSeries(); + } else { + m_seriesType = type; } } void DeclarativeSeries::setParentForSeries() { - initSeries(); + if (!m_series) + initSeries(); + else if (m_series->type() != m_seriesType) + initSeries(); } void DeclarativeSeries::initSeries() diff --git a/src/qscatterseries.cpp b/src/qscatterseries.cpp index 4d527c2..592782b 100644 --- a/src/qscatterseries.cpp +++ b/src/qscatterseries.cpp @@ -20,17 +20,22 @@ QScatterSeries::~QScatterSeries() delete d; } -// TODO: change to list of QPointFs? -//bool QScatterSeries::setData(QList xlist, QList ylist) -void QScatterSeries::setData(QList data) +void QScatterSeries::addData(QPointF value) { - d->m_data = data; + d->m_data.append(value); + emit changed(); +} + +QScatterSeries& QScatterSeries::operator << (const QPointF &value) +{ + d->m_data.append(value); emit changed(); + return *this; } -void QScatterSeries::addData(QPointF data) +void QScatterSeries::setData(QList data) { - d->m_data.append(data); + d->m_data = data; emit changed(); } diff --git a/src/qscatterseries.h b/src/qscatterseries.h index 89a4411..b29b32b 100644 --- a/src/qscatterseries.h +++ b/src/qscatterseries.h @@ -20,8 +20,9 @@ public: // from QChartSeries QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; } public: - // TODO: the name of the function? addPoint? addData? addX? - void addData(QPointF data); + // TODO: the name of the function? addPoint? addData? addValue? + void addData(QPointF value); + QScatterSeries& operator << (const QPointF &value); void setData(QList data); diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index ab17603..e5fbeee 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -227,11 +227,10 @@ void MainWidget::addSeries(QString series, QString data) // TODO: color of the series QChartSeries *newSeries = 0; if (series == "Scatter") { - newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter); - QScatterSeries *scatterSeries = qobject_cast(newSeries); - Q_ASSERT(scatterSeries); + QScatterSeries *scatter = new QScatterSeries(); for (int i(0); i < x.count() && i < y.count(); i++) - scatterSeries->addData(QPointF(x.at(i), y.at(i))); + (*scatter) << QPointF(x.at(i), y.at(i)); + m_chartWidget->addSeries(scatter); } else if (series == "Pie") { newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie); Q_ASSERT(newSeries->setData(y)); @@ -306,26 +305,28 @@ void MainWidget::addSeries(QString series, QString data) void MainWidget::setCurrentSeries(QChartSeries *series) { - m_currentSeries = series; - switch (m_currentSeries->type()) { - case QChartSeries::SeriesTypeLine: - break; - case QChartSeries::SeriesTypeScatter: - break; - case QChartSeries::SeriesTypePie: - break; - case QChartSeries::SeriesTypeBar: - qDebug() << "setCurrentSeries (bar)"; - break; - case QChartSeries::SeriesTypeStackedBar: - qDebug() << "setCurrentSeries (Stackedbar)"; - break; - case QChartSeries::SeriesTypePercentBar: - qDebug() << "setCurrentSeries (Percentbar)"; - break; - default: - Q_ASSERT(false); - break; + if (series) { + m_currentSeries = series; + switch (m_currentSeries->type()) { + case QChartSeries::SeriesTypeLine: + break; + case QChartSeries::SeriesTypeScatter: + break; + case QChartSeries::SeriesTypePie: + break; + case QChartSeries::SeriesTypeBar: + qDebug() << "setCurrentSeries (bar)"; + break; + case QChartSeries::SeriesTypeStackedBar: + qDebug() << "setCurrentSeries (Stackedbar)"; + break; + case QChartSeries::SeriesTypePercentBar: + qDebug() << "setCurrentSeries (Percentbar)"; + break; + default: + Q_ASSERT(false); + break; + } } }