From 144fd3c1acc40f9b1e2732a13d4907b6763194cd 2012-01-26 12:43:41 From: sauimone Date: 2012-01-26 12:43:41 Subject: [PATCH] correct drawing for barchart --- diff --git a/example/barchart/main.cpp b/example/barchart/main.cpp index c5f529c..fe2556f 100644 --- a/example/barchart/main.cpp +++ b/example/barchart/main.cpp @@ -14,18 +14,23 @@ int main(int argc, char *argv[]) BarChartSeries* series0 = new BarChartSeries(); // Create some test data to chart - QStandardItemModel dataModel(2,10); + QStandardItemModel dataModel(5,10); QModelIndex index; index = dataModel.index(0,0); - // Series 1, items 6 to 9 missing. + + // Series 0 dataModel.setData(dataModel.index(0,0),1); - dataModel.setData(dataModel.index(0,1),12); - dataModel.setData(dataModel.index(0,2),5); - dataModel.setData(dataModel.index(0,3),8); - dataModel.setData(dataModel.index(0,4),17); - dataModel.setData(dataModel.index(0,5),9); + dataModel.setData(dataModel.index(0,1),2); + dataModel.setData(dataModel.index(0,2),3); + dataModel.setData(dataModel.index(0,3),4); + dataModel.setData(dataModel.index(0,4),5); + dataModel.setData(dataModel.index(0,5),6); + dataModel.setData(dataModel.index(0,6),7); + dataModel.setData(dataModel.index(0,7),8); + dataModel.setData(dataModel.index(0,8),9); + dataModel.setData(dataModel.index(0,9),10); - // Series 2, some other items missing + // Series 1, some other items missing dataModel.setData(dataModel.index(1,0),5); dataModel.setData(dataModel.index(1,3),4); dataModel.setData(dataModel.index(1,5),7); @@ -33,6 +38,42 @@ int main(int argc, char *argv[]) dataModel.setData(dataModel.index(1,8),9); dataModel.setData(dataModel.index(1,9),9); + // Series 2 + dataModel.setData(dataModel.index(2,0),3); + dataModel.setData(dataModel.index(2,1),5); + dataModel.setData(dataModel.index(2,2),8); + dataModel.setData(dataModel.index(2,3),13); + dataModel.setData(dataModel.index(2,4),8); + dataModel.setData(dataModel.index(2,5),5); + dataModel.setData(dataModel.index(2,6),3); + dataModel.setData(dataModel.index(2,7),2); + dataModel.setData(dataModel.index(2,8),1); + dataModel.setData(dataModel.index(2,9),1); + + // Series 3 + dataModel.setData(dataModel.index(3,0),5); + dataModel.setData(dataModel.index(3,1),6); + dataModel.setData(dataModel.index(3,2),7); + dataModel.setData(dataModel.index(3,3),3); + dataModel.setData(dataModel.index(3,4),4); + dataModel.setData(dataModel.index(3,5),5); + dataModel.setData(dataModel.index(3,6),8); + dataModel.setData(dataModel.index(3,7),9); + dataModel.setData(dataModel.index(3,8),10); + dataModel.setData(dataModel.index(3,9),5); + + // Series 4 + dataModel.setData(dataModel.index(4,0),9); + dataModel.setData(dataModel.index(4,1),7); + dataModel.setData(dataModel.index(4,2),5); + dataModel.setData(dataModel.index(4,3),3); + dataModel.setData(dataModel.index(4,4),1); + dataModel.setData(dataModel.index(4,5),2); + dataModel.setData(dataModel.index(4,6),4); + dataModel.setData(dataModel.index(4,7),6); + dataModel.setData(dataModel.index(4,8),8); + dataModel.setData(dataModel.index(4,9),10); + series0->setData(&dataModel); ChartWidget* chartWidget = new ChartWidget(&window); diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index 5f22824..a57356a 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -41,6 +41,9 @@ void Bar::setPos(qreal x, qreal y) void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { + if (0 == mHeight) { + return; + } // Set color for bar. TODO: gradients, textures etc QPen pen = painter->pen(); pen.setColor( mColor ); @@ -49,14 +52,13 @@ void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidg // Draw bar // TODO: Pen width affects bar height for now. This should be rect - painter->drawLine(scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mHeight - mWidth, - scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mWidth); +// painter->drawRect(mXpos,mYpos,mWidth,mHeight); + painter->drawLine(mXpos, mYpos + mHeight - mWidth, + mXpos, mYpos + mWidth); } QRectF Bar::boundingRect() const { - // TODO: check validity of this (I suppose there is easier way, and currently this bit incorrect :) -// QRectF r(scenePos().x()+mXpos, scenePos().y()+mYpos, scenePos().x() + mWidth, scenePos().y() + mHeight ); QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); return r; } diff --git a/src/barchart/barchartseries.cpp b/src/barchart/barchartseries.cpp index 87913a7..97b0ce5 100644 --- a/src/barchart/barchartseries.cpp +++ b/src/barchart/barchartseries.cpp @@ -79,12 +79,13 @@ int BarChartSeries::valueAt(int series, int item) return mModel->data(index).toInt(); } +/* void BarChartSeries::chartSizeChanged(QRectF rect) { qDebug() << "barchart size changed:" << rect; // mBarGroup->resize(rect.toRect().width(), rect.toRect().height()); } - +*/ #include "moc_barchartseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barchartseries.h b/src/barchart/barchartseries.h index af012c3..42a97e4 100644 --- a/src/barchart/barchartseries.h +++ b/src/barchart/barchartseries.h @@ -22,7 +22,7 @@ public: // from QChartSeries virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } - // TODO: This as dataModel instead of n different setters. (data model itself can accept lists and whatnot) + // TODO: Better data model? virtual bool setData(QAbstractItemModel* model); // Methods to find out minimum and maximum values of data @@ -35,7 +35,8 @@ public: public Q_SLOTS: - void chartSizeChanged(QRectF rect); + // TODO: wrong place for this... series don't know anything about layout +// void chartSizeChanged(QRectF rect); private: diff --git a/src/barchart/bargroup.cpp b/src/barchart/bargroup.cpp index cdce441..d1e5182 100644 --- a/src/barchart/bargroup.cpp +++ b/src/barchart/bargroup.cpp @@ -31,6 +31,7 @@ void BarGroup::setSize(const QSize& size) void BarGroup::setPlotDomain(const PlotDomain& data) { qDebug() << "BarGroup::setPlotDomain"; + // TODO: } @@ -48,9 +49,11 @@ void BarGroup::setBarWidth( int w ) mBarDefaultWidth = w; } -void BarGroup::setColor( QColor color ) +int BarGroup::addColor( QColor color ) { - mColor = color; + int colorIndex = mColors.count(); + mColors.append(color); + return colorIndex; } void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) @@ -69,7 +72,6 @@ void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QRectF BarGroup::boundingRect() const { - // TODO: correct this (currently ignores position) return QRectF(0,0,mWidth,mHeight); } @@ -107,13 +109,13 @@ void BarGroup::layoutChanged() return; } - // Align center + // TODO: better way to auto-layout int count = mSeries.countItemsInSeries(); - int posStep = (mWidth / (count)); - int startPos = (mWidth / count+1); + int posStep = (mWidth / (count+1)); + int startPos = (mWidth / (count+1)) - mSeries.countSeries() * mBarDefaultWidth /2; qDebug() << "startpos" << startPos; - // Scaling. TODO: better one. + // Scaling. int itemIndex(0); for (int series = 0; series < mSeries.countSeries(); series++) { for (int item=0; item < mSeries.countItemsInSeries(); item++) { @@ -121,14 +123,12 @@ void BarGroup::layoutChanged() int barHeight = mSeries.valueAt(series, item) * mHeight / mMax; Bar* bar = reinterpret_cast (childItems().at(itemIndex)); - bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar - //TODO: test hack - if (0 == series) { - bar->setColor(QColor(255,0,0,128)); - } else { - bar->setColor(QColor(255,255,0,128)); - } - bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, 0); + // TODO: width settable per bar? + bar->resize(mBarDefaultWidth, barHeight); + bar->setColor(mColors.at(series)); + + // TODO: bar width shouldn't affect height. Currently it does because pen width also affects height. (QPainter thingy...) + bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, mHeight - barHeight + mBarDefaultWidth); itemIndex++; } } diff --git a/src/barchart/bargroup.h b/src/barchart/bargroup.h index 6d69d5d..af8d53d 100644 --- a/src/barchart/bargroup.h +++ b/src/barchart/bargroup.h @@ -37,7 +37,9 @@ public: void resize( int w, int h ); // Size for whole series. Single bars are drawn inside this area void setPos(qreal x, qreal y); void setBarWidth( int w ); // Default width for each bar - void setColor( QColor color ); // Default color for each bar + + // TODO: set color theme instead? or use some external color theme call this + int addColor( QColor color ); // From QGraphicsItem void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); @@ -59,11 +61,10 @@ private: int mWidth; int mBarDefaultWidth; - QColor mColor; - bool mLayoutSet; // True, if component has been laid out. bool mLayoutDirty; + QList mColors; // List of colors for series for now }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchart.cpp b/src/qchart.cpp index 2a5a63c..66e827b 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -84,6 +84,14 @@ void QChart::addSeries(QChartSeries* series) qDebug() << "barSeries added"; BarChartSeries* barSeries = static_cast(series); BarGroup* barGroup = new BarGroup(*barSeries,this); + + // Add some fugly colors for 5 fist series... + barGroup->addColor(QColor(255,0,0,128)); + barGroup->addColor(QColor(255,255,0,128)); + barGroup->addColor(QColor(0,255,0,128)); + barGroup->addColor(QColor(0,0,255,128)); + barGroup->addColor(QColor(255,128,0,128)); + m_chartItems<