diff options
author | sauimone <samu.uimonen@digia.com> | 2012-08-29 13:16:51 +0300 |
---|---|---|
committer | sauimone <samu.uimonen@digia.com> | 2012-08-29 13:17:20 +0300 |
commit | c0fc53c8ab57052239a4f2060d341d56c0c673c4 (patch) | |
tree | 79835fdef36f78ba797350d347f97672eaa29bb9 /src | |
parent | 232ea264e05ed4268cdb97c13a08681605624fc3 (diff) |
Better support for negative values in stacked barcharts. Negative values are stacked from zero to negative direction. Positive values are stacked from zero to positive direction.
Diffstat (limited to 'src')
-rw-r--r-- | src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp | 22 | ||||
-rw-r--r-- | src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp | 5 | ||||
-rw-r--r-- | src/barchart/qabstractbarseries.cpp | 71 | ||||
-rw-r--r-- | src/barchart/qabstractbarseries.h | 4 | ||||
-rw-r--r-- | src/barchart/qabstractbarseries_p.h | 4 | ||||
-rw-r--r-- | src/barchart/vertical/stacked/qstackedbarseries.cpp | 6 | ||||
-rw-r--r-- | src/barchart/vertical/stacked/stackedbarchartitem.cpp | 25 |
7 files changed, 102 insertions, 35 deletions
diff --git a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp index daddff8a..34459f3c 100644 --- a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp +++ b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp @@ -50,7 +50,8 @@ QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout() int itemIndex(0); for (int category = 0; category < categoryCount; category++) { - qreal xPos = -scaleX * m_domainMinX + geometry().left(); + qreal xMax = -scaleX * m_domainMinX + geometry().left(); + qreal xMin = -scaleX * m_domainMinX + geometry().left(); for (int set = 0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); @@ -59,8 +60,6 @@ QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout() qreal barWidth = barSet->value(category) * scaleX; Bar* bar = m_bars.at(itemIndex); - QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); - layout.append(rect); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); if (qFuzzyIsNull(barHeight)) { @@ -76,14 +75,23 @@ QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout() } else { label->setText(QString("")); } - - label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) - ,yPos - barHeight/2 - label->boundingRect().height()/2); label->setFont(barSet->m_labelFont); label->setBrush(barSet->m_labelBrush); + if (barWidth > 0) { + QRectF rect(xMax, yPos - barHeight, barWidth, barHeight); + layout.append(rect); + label->setPos(xMax + (rect.width()/2 - label->boundingRect().width()/2) + ,yPos - barHeight/2 - label->boundingRect().height()/2); + xMax += barWidth; + } else { + QRectF rect(xMin, yPos - barHeight, barWidth, barHeight); + layout.append(rect); + label->setPos(xMin + (rect.width()/2 - label->boundingRect().width()/2) + ,yPos - barHeight/2 - label->boundingRect().height()/2); + xMin += barWidth; + } itemIndex++; - xPos += barWidth; } } return layout; diff --git a/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp b/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp index 2488b7b9..caedbb4c 100644 --- a/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp +++ b/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp @@ -66,10 +66,9 @@ void QHorizontalStackedBarSeriesPrivate::scaleDomain(Domain& domain) qreal maxY(domain.maxY()); qreal y = categoryCount(); - qreal x = maxCategorySum(); - minX = qMin(minX, x); + minX = qMin(minX, bottom()); minY = qMin(minY, - (qreal)0.5); - maxX = qMax(maxX, x); + maxX = qMax(maxX, top()); maxY = qMax(maxY, y - (qreal)0.5); domain.setRange(minX,maxX,minY,maxY); diff --git a/src/barchart/qabstractbarseries.cpp b/src/barchart/qabstractbarseries.cpp index 9bb59a35..befc0344 100644 --- a/src/barchart/qabstractbarseries.cpp +++ b/src/barchart/qabstractbarseries.cpp @@ -188,15 +188,6 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! - This is depreciated constructor. - \a parent -*/ -QAbstractBarSeries::QAbstractBarSeries(QObject *parent) : - QAbstractSeries(*(QAbstractBarSeriesPrivate*)(0),parent) -{ -} - -/*! Destructs abstractbarseries and owned barsets. */ QAbstractBarSeries::~QAbstractBarSeries() @@ -602,6 +593,68 @@ qreal QAbstractBarSeriesPrivate::maxX() return max; } +qreal QAbstractBarSeriesPrivate::categoryTop(int category) +{ + // Returns top (sum of all positive values) of category. + // Returns 0, if all values are negative + qreal top(0); + int count = m_barSets.count(); + for (int set = 0; set < count; set++) { + if (category < m_barSets.at(set)->count()) { + qreal temp = m_barSets.at(set)->at(category); + if (temp > 0) { + top += temp; + } + } + } + return top; +} + +qreal QAbstractBarSeriesPrivate::categoryBottom(int category) +{ + // Returns bottom (sum of all negative values) of category + // Returns 0, if all values are positive + qreal bottom(0); + int count = m_barSets.count(); + for (int set = 0; set < count; set++) { + if (category < m_barSets.at(set)->count()) { + qreal temp = m_barSets.at(set)->at(category); + if (temp < 0) { + bottom += temp; + } + } + } + return bottom; +} + +qreal QAbstractBarSeriesPrivate::top() +{ + // Returns top of all categories + qreal top(0); + int count = categoryCount(); + for (int i=0; i<count; i++) { + qreal temp = categoryTop(i); + if (temp > top) { + top = temp; + } + } + return top; +} + +qreal QAbstractBarSeriesPrivate::bottom() +{ + // Returns bottom of all categories + qreal bottom(0); + int count = categoryCount(); + for (int i=0; i<count; i++) { + qreal temp = categoryBottom(i); + if (temp < bottom) { + bottom = temp; + } + } + return bottom; +} + void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain) { diff --git a/src/barchart/qabstractbarseries.h b/src/barchart/qabstractbarseries.h index 601ebfc5..c3a788a1 100644 --- a/src/barchart/qabstractbarseries.h +++ b/src/barchart/qabstractbarseries.h @@ -37,10 +37,6 @@ class QTCOMMERCIALCHART_EXPORT QAbstractBarSeries : public QAbstractSeries Q_PROPERTY(int count READ count NOTIFY countChanged) Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged) -protected: - //TODO DEPRECIATED - explicit QAbstractBarSeries(QObject *parent = 0); - public: virtual ~QAbstractBarSeries(); diff --git a/src/barchart/qabstractbarseries_p.h b/src/barchart/qabstractbarseries_p.h index 0835f430..63c09837 100644 --- a/src/barchart/qabstractbarseries_p.h +++ b/src/barchart/qabstractbarseries_p.h @@ -77,6 +77,10 @@ public: qreal maxCategorySum(); qreal minX(); qreal maxX(); + qreal categoryTop(int category); + qreal categoryBottom(int category); + qreal top(); + qreal bottom(); Q_SIGNALS: void clicked(int index, QBarSet *barset); diff --git a/src/barchart/vertical/stacked/qstackedbarseries.cpp b/src/barchart/vertical/stacked/qstackedbarseries.cpp index 65c8cdd9..b2034d23 100644 --- a/src/barchart/vertical/stacked/qstackedbarseries.cpp +++ b/src/barchart/vertical/stacked/qstackedbarseries.cpp @@ -97,16 +97,14 @@ void QStackedBarSeriesPrivate::scaleDomain(Domain& domain) qreal maxY(domain.maxY()); qreal x = categoryCount(); - qreal y = maxCategorySum(); minX = qMin(minX, - (qreal)0.5); - minY = qMin(minY, y); + minY = qMin(minY, bottom()); maxX = qMax(maxX, x - (qreal)0.5); - maxY = qMax(maxY, y); + maxY = qMax(maxY, top()); domain.setRange(minX,maxX,minY,maxY); } - ChartElement* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter) { Q_Q(QStackedBarSeries); diff --git a/src/barchart/vertical/stacked/stackedbarchartitem.cpp b/src/barchart/vertical/stacked/stackedbarchartitem.cpp index 111b6685..ce33f2fc 100644 --- a/src/barchart/vertical/stacked/stackedbarchartitem.cpp +++ b/src/barchart/vertical/stacked/stackedbarchartitem.cpp @@ -50,7 +50,8 @@ QVector<QRectF> StackedBarChartItem::calculateLayout() int itemIndex(0); for (int category = 0; category < categoryCount; category++) { - qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y(); + qreal yMax = height + scaleY * m_domainMinY + geometry().topLeft().y(); + qreal yMin = height + scaleY * m_domainMinY + geometry().topLeft().y(); for (int set=0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); @@ -66,9 +67,6 @@ QVector<QRectF> StackedBarChartItem::calculateLayout() bar->setVisible(barsVisible); } - QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); - layout.append(rect); - QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); if (!qFuzzyIsNull(barSet->value(category))) { @@ -76,13 +74,24 @@ QVector<QRectF> StackedBarChartItem::calculateLayout() } else { label->setText(QString("")); } - - label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) - ,yPos - barHeight/2 - label->boundingRect().height()/2); label->setFont(barSet->m_labelFont); label->setBrush(barSet->m_labelBrush); + + if (barHeight < 0) { + QRectF rect(xPos, yMax-barHeight, barWidth, barHeight); + layout.append(rect); + label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) + ,yMax - barHeight/2 - label->boundingRect().height()/2); + yMax -= barHeight; + } else { + QRectF rect(xPos, yMin-barHeight, barWidth, barHeight); + layout.append(rect); + label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) + ,yMin - barHeight/2 - label->boundingRect().height()/2); + yMin -= barHeight; + } + itemIndex++; - yPos -= barHeight; } } |