From 023d2c8150a8e32d8b9af762d138af59d2150fdc 2012-02-10 15:19:45 From: sauimone Date: 2012-02-10 15:19:45 Subject: [PATCH] added labels to series, intergrated with test app. minor hack to test app --- diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp index bd1b6c5..8620df5 100644 --- a/src/barchart/barchartmodel.cpp +++ b/src/barchart/barchartmodel.cpp @@ -25,6 +25,7 @@ int BarChartModel::addData(QList data) DataContainer* c = new DataContainer(data,mRunningId); mDataModel.append(c); mRunningId++; + emit modelUpdated(); return mRunningId-1; } @@ -37,6 +38,7 @@ void BarChartModel::removeData(int id) delete c; } } + emit modelUpdated(); } int BarChartModel::countRows() @@ -66,18 +68,18 @@ int BarChartModel::countTotalItems() return total; } -int BarChartModel::min() +qreal BarChartModel::min() { // qDebug() << "BarChartModel::min"; Q_ASSERT(mDataModel.count() > 0); // TODO: make min and max members and update them when data changes. // This is slower since they are checked every time, even if data is same since previous call. - int min = INT_MAX; + qreal min = INT_MAX; for (int i=0; i countColumns(); for (int j=0; jvalueAt(j); + qreal temp = mDataModel.at(i)->valueAt(j); if (temp < min) { min = temp; } @@ -86,19 +88,19 @@ int BarChartModel::min() return min; } -int BarChartModel::max() +qreal BarChartModel::max() { // qDebug() << "BarChartModel::max"; Q_ASSERT(mDataModel.count() > 0); // TODO: make min and max members and update them when data changes. // This is slower since they are checked every time, even if data is same since previous call. - int max = INT_MIN; + qreal max = INT_MIN; for (int i=0; i countColumns(); for (int j=0; jvalueAt(j); + qreal temp = mDataModel.at(i)->valueAt(j); if (temp > max) { max = temp; } diff --git a/src/barchart/barchartmodel_p.h b/src/barchart/barchartmodel_p.h index 82b1527..2ab132f 100644 --- a/src/barchart/barchartmodel_p.h +++ b/src/barchart/barchartmodel_p.h @@ -24,9 +24,8 @@ public: int countColumns(); // Maximum number of items in series int countTotalItems(); // Total items in all series. Includes empty items. - // TODO: qreal these - int max(); // Maximum value of all series - int min(); // Minimum value of all series + qreal max(); // Maximum value of all series + qreal min(); // Minimum value of all series qreal valueAt(int series, int item); qreal columnSum(int column); diff --git a/src/barchart/barchartseriesbase.cpp b/src/barchart/barchartseriesbase.cpp index 61c246f..66f25d4 100644 --- a/src/barchart/barchartseriesbase.cpp +++ b/src/barchart/barchartseriesbase.cpp @@ -8,50 +8,64 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE BarChartSeriesBase::BarChartSeriesBase(QObject *parent) : QChartSeries(parent) - ,mModel(*(new BarChartModel(this))) // TODO: is this ok? + ,mModel(new BarChartModel(this)) { } int BarChartSeriesBase::addData(QList data) { - return mModel.addData(data); + return mModel->addData(data); } void BarChartSeriesBase::removeData(int id) { - mModel.removeData(id); + mModel->removeData(id); +} + +void BarChartSeriesBase::setLabels(QList labels) +{ + mLabels = labels; } qreal BarChartSeriesBase::min() { - return mModel.min(); + return mModel->min(); } qreal BarChartSeriesBase::max() { - return mModel.max(); + return mModel->max(); } int BarChartSeriesBase::countColumns() { - return mModel.countColumns(); + return mModel->countColumns(); } qreal BarChartSeriesBase::valueAt(int series, int item) { - qDebug() << "BarChartSeriesBase::valueAt" << series << item; - return mModel.valueAt(series,item); +// qDebug() << "BarChartSeriesBase::valueAt" << series << item; + return mModel->valueAt(series,item); } qreal BarChartSeriesBase::maxColumnSum() { - qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel.maxColumnSum(); - return mModel.maxColumnSum(); +// qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel->maxColumnSum(); + return mModel->maxColumnSum(); } BarChartModel& BarChartSeriesBase::model() { - return mModel; + return *mModel; +} + +QString BarChartSeriesBase::label(int item) +{ + if ((item>=0) && (item < mLabels.count())) { + return mLabels.at(item); + } + + return QString(""); } #include "moc_barchartseriesbase.cpp" diff --git a/src/barchart/barchartseriesbase.h b/src/barchart/barchartseriesbase.h index b0c60ae..bee6ac1 100644 --- a/src/barchart/barchartseriesbase.h +++ b/src/barchart/barchartseriesbase.h @@ -26,7 +26,9 @@ public: // Returns id for vector. int addData(QList data); void removeData(int id); + void setLabels(QList labels); + // These shouldn't be visible to chart series user. However, ChartDataSet needs to access them, and friends are evil. qreal min(); qreal max(); int countColumns(); // Count items in one series. @@ -34,6 +36,7 @@ public: qreal maxColumnSum(); BarChartModel& model(); + QString label(int item); signals: void changed(int index); @@ -42,9 +45,9 @@ public Q_SLOTS: private: - BarChartModel& mModel; - BarGroupBase* mBarGroup; + BarChartModel* mModel; + QList mLabels; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/bargroup.cpp b/src/barchart/bargroup.cpp index f0a0b61..f95a6ce 100644 --- a/src/barchart/bargroup.cpp +++ b/src/barchart/bargroup.cpp @@ -32,9 +32,8 @@ void BarGroup::layoutChanged() qreal tW = mWidth; qreal tH = mHeight; - qreal tM = mMax; + qreal tM = mModel.max(); qreal scale = (tH/tM); - qreal tC = itemCount+1; qreal xStepPerSeries = (tW/tC); diff --git a/src/barchart/bargroupbase.cpp b/src/barchart/bargroupbase.cpp index 09a2944..0f88b39 100644 --- a/src/barchart/bargroupbase.cpp +++ b/src/barchart/bargroupbase.cpp @@ -12,10 +12,11 @@ BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent) ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready ,mLayoutSet(false) ,mLayoutDirty(true) - ,mTheme(0) ,mSeparatorsVisible(true) ,mModel(series.model()) + ,mSeries(series) { + dataChanged(); } void BarGroupBase::setSeparatorsVisible(bool visible) @@ -82,8 +83,8 @@ void BarGroupBase::dataChanged() int count = mModel.countColumns(); // mSeries.countColumns(); for (int i=0; iset(text); +// QString text("Label " + QString::number(i)); + label->set(mSeries.label(i)); childItems().append(label); } @@ -102,14 +103,17 @@ void BarGroupBase::dataChanged() void BarGroupBase::handleModelChanged(int index) { -// qDebug() << "BarGroupBase::handleModelChanged" << index; + qDebug() << "BarGroupBase::handleModelChanged" << index; dataChanged(); } void BarGroupBase::handleDomainChanged(const Domain& domain) { // qDebug() << "BarGroupBase::handleDomainChanged"; - dataChanged(); + + // TODO: Figure out the use case for this. + // Affects the size of visible item, so layout is changed. +// layoutChanged(); } void BarGroupBase::handleGeometryChanged(const QRectF& rect) diff --git a/src/barchart/bargroupbase.h b/src/barchart/bargroupbase.h index aa508f5..2d3e3d1 100644 --- a/src/barchart/bargroupbase.h +++ b/src/barchart/bargroupbase.h @@ -1,10 +1,7 @@ #ifndef BARGROUPBASE_H #define BARGROUPBASE_H -#include "charttheme_p.h" #include "chartitem_p.h" -//#include "barlabel_p.h" -//#include "bar_p.h" #include "barchartseriesbase.h" #include "barchartmodel_p.h" #include @@ -20,21 +17,18 @@ public: void setSeparatorsVisible(bool visible = true); public: // From ChartItem - void setSize(const QSizeF &size){}; - void setPlotDomain(const PlotDomain& data){}; +// void setSize(const QSizeF &size){}; // From QGraphicsItem void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QRectF boundingRect() const; - // From ChartThemeObserver -// void themeChanged(ChartTheme *theme); - // TODO: these may change with layout awarness. void setBarWidth( int w ); int addColor( QColor color ); void resetColors(); + // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes @@ -43,12 +37,11 @@ protected slots: void handleDomainChanged(const Domain& domain); void handleGeometryChanged(const QRectF& size); - protected: // TODO: consider these. - int mMin; // Min and max values of data. (updated when data is changed, used when drawing) - int mMax; + //int mMin; // Min and max values of data. (updated when data is changed, used when drawing) + //int mMax; int mHeight; // Layout spesific int mWidth; @@ -58,11 +51,9 @@ protected: bool mLayoutDirty; QList mColors; // List of colors for series for now - - ChartTheme* mTheme; bool mSeparatorsVisible; - BarChartModel& mModel; + BarChartSeriesBase& mSeries; }; diff --git a/src/barchart/stackedbargroup.cpp b/src/barchart/stackedbargroup.cpp index 0b047ca..fc858af 100644 --- a/src/barchart/stackedbargroup.cpp +++ b/src/barchart/stackedbargroup.cpp @@ -51,9 +51,6 @@ void StackedBarGroup::layoutChanged() qreal barHeight = mModel.valueAt(row, column) * scale; Bar* bar = reinterpret_cast (childItems().at(itemIndex)); - // TODO: width settable per bar? - // TODO: theme stuff -// mTheme->themeForSeries(); bar->resize(mBarDefaultWidth, barHeight); bar->setColor(mColors.at(row)); bar->setPos(xPos, yPos-barHeight); diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 78469dc..f011b18 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -60,8 +60,8 @@ void ChartDataSet::addSeries(QChartSeries* series) domain.m_minY = qMin(domain.m_minY,y); domain.m_maxX = qMax(domain.m_maxX,x); domain.m_maxY = qMax(domain.m_maxY,y); + break; } - break; case QChartSeries::SeriesTypeStackedBar: { StackedBarChartSeries* stackedBarSeries = static_cast(series); @@ -71,8 +71,8 @@ void ChartDataSet::addSeries(QChartSeries* series) domain.m_minY = qMin(domain.m_minY,y); domain.m_maxX = qMax(domain.m_maxX,x); domain.m_maxY = qMax(domain.m_maxY,y); - } break; + } case QChartSeries::SeriesTypePercentBar: { PercentBarChartSeries* percentBarSeries = static_cast(series); @@ -81,8 +81,8 @@ void ChartDataSet::addSeries(QChartSeries* series) domain.m_minY = 0; domain.m_maxX = qMax(domain.m_maxX,x); domain.m_maxY = 100; - } break; + } case QChartSeries::SeriesTypePie: { QPieSeries *pieSeries = static_cast(series); diff --git a/src/charttheme.cpp b/src/charttheme.cpp index 00f7357..6e2df46 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -128,7 +128,8 @@ void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int coun void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count) { - for (int i=0; i< m_seriesColor.count(); i++) { + item->resetColors(); + for (int i=0; iaddColor(m_seriesColor.at(i)); } item->addColor(QColor(255,0,0,128)); @@ -140,6 +141,7 @@ void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count) void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count) { + item->resetColors(); for (int i=0; i< m_seriesColor.count(); i++) { item->addColor(m_seriesColor.at(i)); } @@ -152,6 +154,7 @@ void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,i void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count) { + item->resetColors(); for (int i=0; i< m_seriesColor.count(); i++) { item->addColor(m_seriesColor.at(i)); } diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index c5a0177..27890f4 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -251,6 +251,9 @@ void MainWidget::addSeries(QString series, QString data) series0->addData(data2); series0->addData(data3); series0->addData(data4); + QList labels; + labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec"; + series0->setLabels(labels); m_chartWidget->addSeries(series0); newSeries = series0; } else if (series == "StackedBar") { @@ -261,6 +264,9 @@ void MainWidget::addSeries(QString series, QString data) series0->addData(data2); series0->addData(data3); series0->addData(data4); + QList labels; + labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec"; + series0->setLabels(labels); m_chartWidget->addSeries(series0); newSeries = series0; } else if (series == "PercentBar") { @@ -271,6 +277,9 @@ void MainWidget::addSeries(QString series, QString data) series0->addData(data2); series0->addData(data3); series0->addData(data4); + QList labels; + labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec"; + series0->setLabels(labels); m_chartWidget->addSeries(series0); newSeries = series0; } else { @@ -384,6 +393,10 @@ void MainWidget::changeChartTheme(int themeIndex) { qDebug() << "changeChartTheme: " << themeIndex; m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex); + //TODO: remove this hack. This is just to make it so that theme change is seen immediately. + QSize s = size(); + s.setWidth(s.width()+1); + resize(s); } void MainWidget::setPieSizeFactor(double size)