From b3ab09e857356fd069c6af43d44111f8748dd426 2012-03-01 07:09:56 From: Marek Rosa Date: 2012-03-01 07:09:56 Subject: [PATCH] Merge branch 'master' of https://git.it.local/repos/QtCommercialDevel-13049/charts --- diff --git a/example/example.pro b/example/example.pro index b7e00c7..f56f194 100644 --- a/example/example.pro +++ b/example/example.pro @@ -7,6 +7,7 @@ SUBDIRS += linechart \ percentbarchart \ scatter \ piechart \ + piechartdrilldown \ dynamiclinechart \ axischart \ multichart \ diff --git a/example/piechartdrilldown/main.cpp b/example/piechartdrilldown/main.cpp new file mode 100644 index 0000000..7a2fcb4 --- /dev/null +++ b/example/piechartdrilldown/main.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class DrilldownSlice : public QPieSlice +{ + Q_OBJECT + +public: + DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries) + :m_drilldownSeries(drilldownSeries), + m_prefix(prefix) + { + setValue(value); + setLabelVisible(true); + updateLabel(); + connect(this, SIGNAL(changed()), this, SLOT(updateLabel())); + } + + QSeries* drilldownSeries() const { return m_drilldownSeries; } + +public Q_SLOTS: + void updateLabel() + { + QString label = m_prefix; + label += " " + QString::number(this->value())+ "e ("; + label += QString::number(this->percentage()*100, 'f', 1) + "%)"; + setLabel(label); + } + +private: + QSeries* m_drilldownSeries; + QString m_prefix; +}; + +class DrilldownChart : public QChartView +{ + Q_OBJECT +public: + explicit DrilldownChart(QWidget *parent = 0):QChartView(parent), m_currentSeries(0) {} + + void changeSeries(QSeries* series) + { + if (m_currentSeries) + removeSeries(m_currentSeries); + m_currentSeries = series; + addSeries(series); + setChartTitle(series->title()); + } + +public Q_SLOTS: + void handleSliceClicked(QPieSlice* slice) + { + DrilldownSlice* drilldownSlice = static_cast(slice); + changeSeries(drilldownSlice->drilldownSeries()); + } + +private: + QSeries* m_currentSeries; +}; + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + + QMainWindow window; + + DrilldownChart* drilldownChart = new DrilldownChart(&window); + drilldownChart->setRenderHint(QPainter::Antialiasing); + drilldownChart->setChartTheme(QChart::ChartThemeVanilla); + + QPieSeries* yearSeries = new QPieSeries(drilldownChart); + yearSeries->setTitle("Sales by year - All"); + yearSeries->setHoverHighlighting(); + + QList months; + months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; + QList names; + names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob"; + + foreach (QString name, names) { + QPieSeries* series = new QPieSeries(drilldownChart); + series->setTitle("Sales by month - " + name); + series->setHoverHighlighting(); + + foreach (QString month, months) + *series << new DrilldownSlice(qrand() % 1000, month, yearSeries); + + QObject::connect(series, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); + + *yearSeries << new DrilldownSlice(series->total(), name, series); + } + + QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); + + drilldownChart->changeSeries(yearSeries); + + window.setCentralWidget(drilldownChart); + window.resize(600, 600); + window.show(); + + return a.exec(); +} + +#include "main.moc" diff --git a/example/piechartdrilldown/piechartdrilldown.pro b/example/piechartdrilldown/piechartdrilldown.pro new file mode 100644 index 0000000..df16e45 --- /dev/null +++ b/example/piechartdrilldown/piechartdrilldown.pro @@ -0,0 +1,9 @@ +!include( ../example.pri ) { + error( "Couldn't find the example.pri file!" ) +} +TARGET = piechartdrilldown +SOURCES += main.cpp +HEADERS += + + +MOC_DIR = $$PWD/moc diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index a26a10c..13fec18 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -8,7 +8,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Bar::Bar(QGraphicsItem *parent) : QGraphicsObject(parent) { - setAcceptedMouseButtons(Qt::LeftButton); + setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); setAcceptHoverEvents(true); } @@ -64,9 +64,13 @@ QRectF Bar::boundingRect() const return r; } -void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) +void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event) { - emit clicked(); + if (event->button() == Qt::LeftButton) { + emit clicked(); + } else if (event->button() == Qt::RightButton) { + emit rightClicked(); + } } void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event) diff --git a/src/barchart/bar_p.h b/src/barchart/bar_p.h index 12465a2..704f44b 100644 --- a/src/barchart/bar_p.h +++ b/src/barchart/bar_p.h @@ -34,6 +34,7 @@ public: Q_SIGNALS: void clicked(); + void rightClicked(); void hoverEntered(QPoint pos); void hoverLeaved(); @@ -43,7 +44,6 @@ private: qreal mWidth; qreal mXpos; qreal mYpos; - QColor mColor; QBrush mBrush; QPen mPen; diff --git a/src/barchart/barcategory.cpp b/src/barchart/barcategory.cpp new file mode 100644 index 0000000..f405dd9 --- /dev/null +++ b/src/barchart/barcategory.cpp @@ -0,0 +1,17 @@ +#include "barcategory_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +BarCategory::BarCategory(QString name, QObject *parent) : + QObject(parent) + ,mName(name) +{ +} + +void BarCategory::barRightClickEvent() +{ + // TODO: + emit rightClicked(mName); +} + +#include "moc_barcategory_p.cpp" +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barcategory_p.h b/src/barchart/barcategory_p.h new file mode 100644 index 0000000..96df531 --- /dev/null +++ b/src/barchart/barcategory_p.h @@ -0,0 +1,26 @@ +#ifndef BARCATEGORY_P_H +#define BARCATEGORY_P_H + +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +// Event handler for bar category +class BarCategory : public QObject +{ + Q_OBJECT +public: + explicit BarCategory(QString name, QObject *parent = 0); + +signals: + void rightClicked(QString name); // "We want something to happen that involves this category" + +public slots: + void barRightClickEvent(); + +private: + QString mName; +}; + +QTCOMMERCIALCHART_END_NAMESPACE +#endif // BARCATEGORY_P_H diff --git a/src/barchart/barchart.pri b/src/barchart/barchart.pri index 3ae84f5..926999c 100644 --- a/src/barchart/barchart.pri +++ b/src/barchart/barchart.pri @@ -14,7 +14,8 @@ SOURCES += \ $$PWD/qstackedbarseries.cpp \ $$PWD/separator.cpp \ $$PWD/stackedbarpresenter.cpp \ - $$PWD/barvalue.cpp + $$PWD/barvalue.cpp \ + $$PWD/barcategory.cpp PRIVATE_HEADERS += \ $$PWD/bar_p.h \ @@ -25,12 +26,12 @@ PRIVATE_HEADERS += \ $$PWD/percentbarpresenter_p.h \ $$PWD/separator_p.h \ $$PWD/stackedbarpresenter_p.h \ - $$PWD/barvalue_p.h + $$PWD/barvalue_p.h \ + $$PWD/barcategory_p.h PUBLIC_HEADERS += \ $$PWD/qbarseries.h \ $$PWD/qbarset.h \ $$PWD/qpercentbarseries.h \ $$PWD/qstackedbarseries.h - diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp index 1af7615..450408f 100644 --- a/src/barchart/barchartmodel.cpp +++ b/src/barchart/barchartmodel.cpp @@ -2,6 +2,7 @@ #include #include #include "barchartmodel_p.h" +#include "barcategory_p.h" #include "qbarset.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -10,6 +11,10 @@ BarChartModel::BarChartModel(QStringList categories, QObject *parent) : QObject(parent) ,mCategory(categories) { + for (int i=0; i mDataModel; QStringList mCategory; + QList mCategoryObjects; int mCurrentSet; diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barpresenterbase.cpp index 48247f9..c3974cb 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barpresenterbase.cpp @@ -3,6 +3,7 @@ #include "barvalue_p.h" #include "barlabel_p.h" #include "separator_p.h" +#include "barcategory_p.h" #include "qbarset.h" #include "qbarseries.h" #include @@ -56,7 +57,7 @@ void BarPresenterBase::setBarWidth( int w ) void BarPresenterBase::dataChanged() { // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? - qDebug() << "datachanged"; +// qDebug() << "datachanged"; // Delete old bars foreach (QGraphicsItem* item, childItems()) { delete item; @@ -69,14 +70,16 @@ void BarPresenterBase::dataChanged() // Create new graphic items for bars for (int c=0; ccategoryCount(); c++) { + BarCategory *category = mSeries->categoryObject(c); for (int s=0; sbarsetCount(); s++) { QBarSet *set = mSeries->barsetAt(s); Bar *bar = new Bar(this); childItems().append(bar); mBars.append(bar); - connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); - connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint))); - connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved())); + connect(bar,SIGNAL(clicked()),set,SLOT(barClickedEvent())); + connect(bar,SIGNAL(rightClicked()),category,SLOT(barRightClickEvent())); + connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); + connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); } } @@ -84,7 +87,7 @@ void BarPresenterBase::dataChanged() int count = mSeries->categoryCount(); for (int i=0; iset(mSeries->label(i)); + label->set(mSeries->categoryName(i)); childItems().append(label); mLabels.append(label); } diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index 11a0ffa..b67549f 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -2,7 +2,7 @@ #include "qbarseries.h" #include "qbarset.h" #include "barchartmodel_p.h" - +#include "barcategory_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -53,6 +53,10 @@ QBarSeries::QBarSeries(QStringList categories, QObject *parent) : QSeries(parent) ,mModel(new BarChartModel(categories, this)) { + for (int i=0; icategoryCount(); i++) { + BarCategory *categoryObject = mModel->categoryObject(i); + connect(categoryObject, SIGNAL(rightClicked(QString)), this, SIGNAL(categoryRightClicked(QString))); + } } /*! @@ -114,9 +118,9 @@ QList QBarSeries::legend() /*! \internal \a category */ -QString QBarSeries::label(int category) +QString QBarSeries::categoryName(int category) { - return mModel->label(category); + return mModel->categoryName(category); } /*! @@ -225,6 +229,12 @@ BarChartModel& QBarSeries::model() return *mModel; } +BarCategory* QBarSeries::categoryObject(int category) +{ + return mModel->categoryObject(category); +} + + #include "moc_qbarseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarseries.h b/src/barchart/qbarseries.h index b303b85..5e68d4c 100644 --- a/src/barchart/qbarseries.h +++ b/src/barchart/qbarseries.h @@ -8,6 +8,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QBarSet; class BarChartModel; +class BarCategory; // Container for series class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries @@ -29,8 +30,8 @@ public: // TODO: Functions below this are not part of api and will be moved // to private implementation, when we start using it // TODO: TO PIMPL ---> - QBarSet *barsetAt(int index); - QString label(int category); + QBarSet* barsetAt(int index); + QString categoryName(int category); qreal min(); qreal max(); qreal valueAt(int set, int category); @@ -38,10 +39,12 @@ public: qreal categorySum(int category); qreal maxCategorySum(); BarChartModel& model(); + BarCategory* categoryObject(int category); // <--- TO PIMPL signals: void changed(int index); + void categoryRightClicked(QString category); // TODO: internal signals, these to private implementation. // TODO: TO PIMPL ---> diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index caa04bf..4d15a4f 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -133,9 +133,8 @@ QBrush QBarSet::brush() /*! \internal */ -void QBarSet::barClicked() +void QBarSet::barClickedEvent() { -// qDebug() << "QBarset::barClicked" << this; // Some bar of this set has been clicked // TODO: What happens then? emit clicked(); // Notify that set has been clicked @@ -144,7 +143,7 @@ void QBarSet::barClicked() /*! \internal \a pos */ -void QBarSet::barHoverEntered(QPoint pos) +void QBarSet::barHoverEnterEvent(QPoint pos) { emit showToolTip(pos, mName); emit hoverEnter(pos); @@ -153,12 +152,8 @@ void QBarSet::barHoverEntered(QPoint pos) /*! \internal */ -void QBarSet::barHoverLeaved() +void QBarSet::barHoverLeaveEvent() { -// qDebug() << "QBarset::barHoverLeaved" << this; -// if (mToolTipEnabled) { - // TODO: do what? -// } // Emit signal to user of charts emit hoverLeave(); } diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 6d89792..a3d3e6b 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -41,9 +41,9 @@ Q_SIGNALS: public Q_SLOTS: // These are for internal communication // TODO: TO PIMPL ---> - void barClicked(); - void barHoverEntered(QPoint pos); - void barHoverLeaved(); + void barClickedEvent(); + void barHoverEnterEvent(QPoint pos); + void barHoverLeaveEvent(); // <--- TO PIMPL private: diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 93218b1..dcc3515 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -149,7 +149,7 @@ void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY) m_domainMap.replace(axisY,domain); m_seriesMap.insert(axisY,series); emit seriesAdded(series); - setDomain(m_domainIndex); + setDomain(m_domainIndex,axisY); } @@ -244,6 +244,27 @@ void ChartDataSet::setDomain(int index) emit axisRangeChanged(axisX(),labels); } +void ChartDataSet::setDomain(int index,QChartAxis* axis) +{ + int i = m_domainMap.count(axis) - index -1; + Q_ASSERT(i>=0); + Domain domain = m_domainMap.values(axis).at(i); + { + QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY); + QList seriesList = m_seriesMap.values(axis); + foreach(QSeries* series, seriesList) { + emit seriesDomainChanged(series,domain); + } + axis->updateRange(domain.m_minY,domain.m_maxY); + emit axisRangeChanged(axis,labels); + } + + QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX); + axisX()->updateRange(domain.m_minX,domain.m_maxY); + emit axisRangeChanged(axisX(),labels); +} + + void ChartDataSet::clearDomains(int toIndex) { Q_ASSERT(toIndex>=0); @@ -342,6 +363,8 @@ void ChartDataSet::handleRangeChanged(QChartAxis* axis) m_domainMap.replace(axis,domain); } + setDomain(m_domainIndex); + } else { @@ -356,9 +379,11 @@ void ChartDataSet::handleRangeChanged(QChartAxis* axis) for(int j=domains.size()-1; j>=0;j--) m_domainMap.insert(axis,domains.at(j)); + + setDomain(m_domainIndex,axis); } - setDomain(m_domainIndex); + } void ChartDataSet::handleTickChanged(QChartAxis* axis) diff --git a/src/chartdataset_p.h b/src/chartdataset_p.h index 8116e67..f8e449b 100644 --- a/src/chartdataset_p.h +++ b/src/chartdataset_p.h @@ -26,6 +26,7 @@ public: const Domain domain(QChartAxis *axisY) const; int domainIndex() const {return m_domainIndex;} void setDomain(int index); + void setDomain(int index,QChartAxis* axis); QChartAxis* axisX() const { return m_axisX;}; QChartAxis* axisY(QSeries* series = 0) const; diff --git a/src/piechart/piepresenter.cpp b/src/piechart/piepresenter.cpp index bd6cbd1..9be832e 100644 --- a/src/piechart/piepresenter.cpp +++ b/src/piechart/piepresenter.cpp @@ -123,12 +123,22 @@ void PiePresenter::updateGeometry() foreach (QPieSlice* s, m_series->m_slices) { // calculate the farthest point in the slice from the pie center + + // the arm qreal centerAngle = s->m_startAngle + (s->m_angleSpan / 2); - qreal len = pieRadius + s->labelArmLength() + s->explodeDistance(); + qreal len = pieRadius + PIESLICE_LABEL_GAP + s->labelArmLength() + s->explodeDistance(); QPointF dp(qSin(centerAngle*(PI/180)) * len, -qCos(centerAngle*(PI/180)) * len); QPointF p = pieRect.center() + dp; - // TODO: consider the label text + // the label text + QFontMetricsF fm(s->labelFont()); + QRectF labelRect = fm.boundingRect(s->label()); + if (centerAngle < 90 || centerAngle > 270) + p += QPointF(0, -labelRect.height()); + if (centerAngle < 180) + p += QPointF(labelRect.width(), 0); + else + p += QPointF(-labelRect.width(), 0); // calculate how much the radius must get smaller to fit that point in the base rectangle qreal dt = m_rect.top() - p.y(); diff --git a/src/piechart/pieslice.cpp b/src/piechart/pieslice.cpp index cdb07d3..756783e 100644 --- a/src/piechart/pieslice.cpp +++ b/src/piechart/pieslice.cpp @@ -100,7 +100,7 @@ void PieSlice::updateGeometry() // update label position qreal radius = rect.height() / 2; - QPointF edgeCenter = rect.center() + offset(centerAngle, radius + 5); + QPointF edgeCenter = rect.center() + offset(centerAngle, radius + PIESLICE_LABEL_GAP); m_slicelabel->setArmStartPoint(edgeCenter); m_slicelabel->setArmAngle(centerAngle); m_slicelabel->updateGeometry(); diff --git a/src/piechart/pieslice_p.h b/src/piechart/pieslice_p.h index fd0660e..f6623d6 100644 --- a/src/piechart/pieslice_p.h +++ b/src/piechart/pieslice_p.h @@ -9,6 +9,8 @@ #include #include +#define PIESLICE_LABEL_GAP 5 + QTCOMMERCIALCHART_BEGIN_NAMESPACE class PiePresenter; class PieSliceLabel; diff --git a/src/piechart/pieslicelabel.cpp b/src/piechart/pieslicelabel.cpp index 697dd93..495566f 100644 --- a/src/piechart/pieslicelabel.cpp +++ b/src/piechart/pieslicelabel.cpp @@ -45,9 +45,9 @@ void PieSliceLabel::updateGeometry() QPointF parm2 = parm1; textRect.moveBottomLeft(parm1); if (m_armAngle < 180) { // arm swings the other way on the left side - parm2 += QPointF(m_textRect.width(), 0); + parm2 += QPointF(textRect.width(), 0); } else { - parm2 += QPointF(-m_textRect.width(),0); + parm2 += QPointF(-textRect.width(),0); textRect.moveBottomLeft(parm2); } diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 7749348..5e0802d 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -188,6 +188,16 @@ void QPieSeries::add(QPieSlice* slice) add(QList() << slice); } +/*! + Adds a single \a slice to the series and returns a reference to the series. + Slice ownership is passed to the series. +*/ +QPieSeries& QPieSeries::operator << (QPieSlice* slice) +{ + add(slice); + return *this; +} + /*! Adds a single slice to the series with give \a value and \a name. @@ -348,10 +358,7 @@ void QPieSeries::setClickExplodes(bool enable) Convenience method for highlighting a slice when user hovers over the slice. It changes the slice color to be lighter and shows the label of the slice. Set \a enable to true to highlight a slice when user hovers on top of it. - - \sa QPieSlice::isExploded(), QPieSlice::setExploded() */ - void QPieSeries::setHoverHighlighting(bool enable) { if (enable) { @@ -364,6 +371,16 @@ void QPieSeries::setHoverHighlighting(bool enable) } /*! + Returns the sum of all slice values in this series. + + \sa QPieSlice::value(), QPieSlice::setValue() +*/ +qreal QPieSeries::total() const +{ + return m_total; +} + +/*! \fn void QPieSeries::changed(const QPieSeries::ChangeSet& changeSet) This signal emitted when something has changed in the series. @@ -456,7 +473,6 @@ void QPieSeries::highlightOn(QPieSlice* slice) Q_ASSERT(slice); QColor c = slice->brush().color().lighter(); slice->setBrush(c); - slice->setLabelVisible(true); } void QPieSeries::highlightOff(QPieSlice* slice) @@ -464,7 +480,6 @@ void QPieSeries::highlightOff(QPieSlice* slice) Q_ASSERT(slice); QColor c = slice->brush().color().darker(150); slice->setBrush(c); - slice->setLabelVisible(false); } void QPieSeries::updateDerivativeData() @@ -480,7 +495,10 @@ void QPieSeries::updateDerivativeData() m_total += s->value(); // we must have some values - Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? + if (m_total == 0) { + qDebug() << "QPieSeries::updateDerivativeData() total == 0"; + Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? + } // update slice attributes qreal sliceAngle = m_pieStartAngle; diff --git a/src/piechart/qpieseries.h b/src/piechart/qpieseries.h index b1fbbc3..96bd797 100644 --- a/src/piechart/qpieseries.h +++ b/src/piechart/qpieseries.h @@ -62,6 +62,7 @@ public: void add(QList slices); void add(QPieSlice* slice); QPieSlice* add(qreal value, QString name); + QPieSeries& operator << (QPieSlice* slice); void remove(QPieSlice* slice); void clear(); @@ -78,6 +79,8 @@ public: void setClickExplodes(bool enable = true); void setHoverHighlighting(bool enable = true); + qreal total() const; + // TODO: find slices? // QList findByValue(qreal value); // ... diff --git a/src/qseries.cpp b/src/qseries.cpp index 9e1d78d..57bb67f 100644 --- a/src/qseries.cpp +++ b/src/qseries.cpp @@ -52,6 +52,20 @@ \brief Returns the legend of the series. If series is empty, empty list is returned. */ +/*! + \fn void QSeries::setTitle(QString title) + \brief Sets a \a title for the series. + + This is not used directly by the chart itself. It is up to the user to use this as for example + chart title. + \sa QChart::setChartTitle() +*/ + +/*! + \fn QString QSeries::title() + \brief Returns the title of the series. +*/ + QTCOMMERCIALCHART_BEGIN_NAMESPACE #include "moc_qseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qseries.h b/src/qseries.h index 13116df..a8f5e6a 100644 --- a/src/qseries.h +++ b/src/qseries.h @@ -42,6 +42,12 @@ public: // TODO: should this be internal? virtual QList legend() { QList l; return l; } + + void setTitle(QString title) { m_title = title; } + QString title() { return m_title; } + +private: + QString m_title; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/test/auto/chartdataset/tst_chartdataset.cpp b/test/auto/chartdataset/tst_chartdataset.cpp index 92581e3..66543a7 100644 --- a/test/auto/chartdataset/tst_chartdataset.cpp +++ b/test/auto/chartdataset/tst_chartdataset.cpp @@ -135,69 +135,85 @@ void tst_ChartDataSet::addDomain() void tst_ChartDataSet::addSeries_data() { - QTest::addColumn("seriesCount"); - QTest::addColumn("axisYCount"); - QTest::newRow("2 series, default axis") << 2 << 0; - QTest::newRow("2 series, 2 new axis") << 2 << 2; - QTest::newRow("2 series, 1 new axis") << 2 << 2; - QTest::newRow("3 series, 3 new axis") << 3 << 3; - QTest::newRow("3 series, 2 new axis") << 3 << 2; - QTest::newRow("3 series, 1 new axis") << 3 << 1; + QTest::addColumn("defaultAxisCount"); + QTest::addColumn("firstAxisCount"); + QTest::addColumn("secoundAxisCount"); + QTest::newRow("2 ,1 ,0") << 2 << 1 << 0; + QTest::newRow("2 series, 2 new axis") << 2 << 0 << 2; + QTest::newRow("2 series, 1 new axis") << 2 << 0 << 2; + QTest::newRow("3 series, 3 new axis") << 3 << 0 << 3; + QTest::newRow("3 series, 2 new axis") << 3 << 0 << 2; + QTest::newRow("3 series, 1 new axis") << 3 << 0 << 1; + QTest::newRow("3 series, default + 1 new axis") << 3 << 0 << 1; } void tst_ChartDataSet::addSeries() { - QFETCH(int, seriesCount); - QFETCH(int, axisYCount); + QFETCH(int, defaultAxisCount); + QFETCH(int, firstAxisCount); + QFETCH(int, secoundAxisCount); - ChartDataSet dataSet; + ChartDataSet dataSet; - QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*))); - QSignalSpy spy1(&dataSet, - SIGNAL(axisRangeChanged(QChartAxis*, QStringList const&))); - QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*))); - QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*))); - QSignalSpy spy4(&dataSet, - SIGNAL(seriesDomainChanged(QSeries*, Domain const&))); - QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*))); + QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*))); + QSignalSpy spy1(&dataSet, SIGNAL(axisRangeChanged(QChartAxis*, QStringList const&))); + QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*))); + QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*))); + QSignalSpy spy4(&dataSet, SIGNAL(seriesDomainChanged(QSeries*, Domain const&))); + QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*))); - QList axisList; + for (int i = 0; i < defaultAxisCount; i++) { + QLineSeries* series = new QLineSeries(); + dataSet.addSeries(series); + } - for (int i = 0; i < axisYCount; i++) { - QChartAxis* axis = new QChartAxis(); - axisList << axis; - } + QChartAxis* firstAxis = new QChartAxis(); - QList::iterator iterator = axisList.begin(); + for (int i = 0; i < firstAxisCount; i++) { + QLineSeries* series = new QLineSeries(); + dataSet.addSeries(series, firstAxis); + } - for (int i = 0; i < seriesCount; i++) { - QChartAxis* axisY = 0; - QLineSeries* series = new QLineSeries(); - if (iterator != axisList.end()) { - axisY = *iterator; - iterator++; - } else if (axisList.count() > 0) { - iterator--; - axisY = *iterator; - iterator++; - } - dataSet.addSeries(series, axisY); - } + QChartAxis* secoundAxis = new QChartAxis(); + + for (int i = 0; i < secoundAxisCount; i++) { + QLineSeries* series = new QLineSeries(); + dataSet.addSeries(series, secoundAxis); + } - //default axis - if (axisYCount == 0) - axisYCount+=2; - else - axisYCount++; + int axisCount = 1; - QCOMPARE(spy0.count(), axisYCount); - QCOMPARE(spy1.count(), seriesCount*2); - QCOMPARE(spy2.count(), 0); - QCOMPARE(spy3.count(), seriesCount); - QCOMPARE(spy4.count(), seriesCount); - QCOMPARE(spy5.count(), 0); + if (defaultAxisCount > 0) + axisCount++; + if (firstAxisCount > 0) + axisCount++; + if (secoundAxisCount > 0) + axisCount++; - QCOMPARE(dataSet.domainIndex(), 0); + QCOMPARE(spy0.count(), axisCount); + QCOMPARE(spy1.count(), (defaultAxisCount + firstAxisCount + secoundAxisCount)*2); + QCOMPARE(spy2.count(), 0); + QCOMPARE(spy3.count(), defaultAxisCount + firstAxisCount + secoundAxisCount); + + int i = 0; + while (defaultAxisCount) { + i+=defaultAxisCount; + defaultAxisCount--; + } + int j = 0; + while (firstAxisCount>0) { + j += firstAxisCount; + firstAxisCount--; + } + int k = 0; + while (secoundAxisCount>0) { + k += secoundAxisCount; + secoundAxisCount--; + } + QCOMPARE(spy4.count(),i + j + k); + QCOMPARE(spy5.count(), 0); + + QCOMPARE(dataSet.domainIndex(), 0); } void tst_ChartDataSet::axisY_data() @@ -344,7 +360,7 @@ void tst_ChartDataSet::domain() QCOMPARE(spy1.count(), 6); QCOMPARE(spy2.count(), 0); QCOMPARE(spy3.count(), 3); - QCOMPARE(spy4.count(), 3); + QCOMPARE(spy4.count(), 6); QCOMPARE(spy5.count(), 0); }