From 51e529f06d1a143ad5a530945ae49f90ff05454c 2012-01-25 10:10:51 From: Tero Ahola Date: 2012-01-25 10:10:51 Subject: [PATCH] Fixed line series usage in the test app --- diff --git a/src/qchart.cpp b/src/qchart.cpp index 2d50797..4ba4f6f 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -49,6 +49,9 @@ void QChart::addSeries(QChartSeries* series) case QChartSeries::SeriesTypeLine: { QXYChartSeries* xyseries = static_cast(series); + // Use color defined by theme in case the series does not define a custom color + if (!xyseries->color().isValid() && m_themeColors.count()) + xyseries->setColor(m_themeColors.takeFirst()); XYPlotDomain domain; //TODO "nice numbers algorithm" @@ -71,16 +74,6 @@ void QChart::addSeries(QChartSeries* series) m_xyLineChartItems<(series); -// if (scatter) { -// scatter->d->setParentItem(this); -// scene()->addItem(scatter->d); -// } -// break; -// } - case QChartSeries::SeriesTypeBar: { qDebug() << "barSeries added"; @@ -92,39 +85,56 @@ void QChart::addSeries(QChartSeries* series) m_BarGroupItems.append(group); // If we need to access group later break; } - } -} - -QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) -{ - // TODO: support also other types; not only scatter and pie - - switch (type) { case QChartSeries::SeriesTypeScatter: { - QScatterSeries *scatterSeries = new QScatterSeries(this); + QScatterSeries *scatterSeries = qobject_cast(series); connect(this, SIGNAL(sizeChanged(QRectF)), scatterSeries, SLOT(chartSizeChanged(QRectF))); scatterSeries->d->setParentItem(this); QColor nextColor = m_themeColors.takeFirst(); nextColor.setAlpha(150); // TODO: default opacity? scatterSeries->setMarkerColor(nextColor); - return scatterSeries; - } + } case QChartSeries::SeriesTypePie: { // TODO: we now have also a list of y values as a parameter, it is ignored // we should use a generic data class instead of list of x and y values - QPieSeries *pieSeries = new QPieSeries(this); + QPieSeries *pieSeries = qobject_cast(series); connect(this, SIGNAL(sizeChanged(QRectF)), pieSeries, SLOT(chartSizeChanged(QRectF))); // TODO: how to define the color for all the slices of a pie? - return pieSeries; + } + } +} + +QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) +{ + // TODO: support also other types; not only scatter and pie + + QChartSeries *series(0); + + switch (type) { + case QChartSeries::SeriesTypeLine: { + series = QXYChartSeries::create(); + break; + } + case QChartSeries::SeriesTypeBar: { + series = new BarChartSeries(this); + break; + } + case QChartSeries::SeriesTypeScatter: { + series = new QScatterSeries(this); + break; + } + case QChartSeries::SeriesTypePie: { + series = new QPieSeries(this); + break; } default: Q_ASSERT(false); break; } - return 0; + addSeries(series); + return series; } void QChart::setSize(const QSizeF& size) diff --git a/src/xylinechart/qxychartseries.cpp b/src/xylinechart/qxychartseries.cpp index 21ba0d9..8811aa4 100644 --- a/src/xylinechart/qxychartseries.cpp +++ b/src/xylinechart/qxychartseries.cpp @@ -3,7 +3,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QXYChartSeries::QXYChartSeries(QObject* parent):QChartSeries(parent), - m_color(Qt::black) + m_color() { } diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index 80aae62..0718a3c 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -170,8 +170,19 @@ void MainWidget::addSeries(QString series, QString data) newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie); Q_ASSERT(newSeries->setData(y)); } else if (series == "Line") { - newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie); - Q_ASSERT(newSeries->setData(x, y)); + // TODO: adding data to an existing line series does not give any visuals for some reason +// newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine); +// QXYChartSeries *lineSeries = static_cast(newSeries); +// lineSeries->setColor(Qt::blue); +// for (int i(0); i < x.count() && i < y.count(); i++) { +// lineSeries->add(x.at(i), y.at(i)); +// } + //Q_ASSERT(newSeries->setData(x, y)); + QXYChartSeries* series0 = QXYChartSeries::create(); + for (int i(0); i < x.count() && i < y.count(); i++) + series0->add(x.at(i), y.at(i)); + m_chartWidget->addSeries(series0); + newSeries = series0; } else { // TODO }