diff --git a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp index daddff8..34459f3 100644 --- a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp +++ b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp @@ -50,7 +50,8 @@ QVector 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 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 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 2488b7b..caedbb4 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 9bb59a3..befc034 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 top) { + top = temp; + } + } + return top; +} + +qreal QAbstractBarSeriesPrivate::bottom() +{ + // Returns bottom of all categories + qreal bottom(0); + int count = categoryCount(); + for (int i=0; i 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 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 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; } }