diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index 70644e8..d860381 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -93,7 +93,7 @@ void BarChartItem::dataChanged() } } - // Create floating values + // Create value items for (int category = 0; category < m_series->categoryCount(); category++) { for (int s = 0; s < m_series->barsetCount(); s++) { QBarSet *set = m_series->barsetAt(s); @@ -132,43 +132,32 @@ QVector BarChartItem::calculateLayout() qreal xPos = categoryWidth * category + barWidth / 2; qreal yPos = height; for (int set = 0; set < setCount; set++) { - qreal barHeight = m_series->valueAt(set, category) * scale; + QBarSet* barSet = m_series->barsetAt(set); + + qreal barHeight = barSet->valueAt(category) * scale; Bar* bar = m_bars.at(itemIndex); QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); layout.append(rect); - bar->setPen(m_series->barsetAt(set)->pen()); - bar->setBrush(m_series->barsetAt(set)->brush()); - itemIndex++; - xPos += barWidth; - } - } + bar->setPen(barSet->pen()); + bar->setBrush(barSet->brush()); - // Position floating values - itemIndex = 0; - for (int category = 0; category < m_series->categoryCount(); category++) { - qreal xPos = categoryWidth * category + barWidth; - qreal yPos = height; - for (int set=0; set < m_series->barsetCount(); set++) { - qreal barHeight = m_series->valueAt(set, category) * scale; BarValue* value = m_values.at(itemIndex); - QBarSet* barSet = m_series->barsetAt(set); - - if (!qFuzzyIsNull(m_series->valueAt(set,category))) { - value->setText(QString::number(m_series->valueAt(set, category))); + if (!qFuzzyIsNull(barSet->valueAt(category))) { + value->setText(QString::number(barSet->valueAt(category))); } else { value->setText(QString("")); } - value->setPos(xPos, yPos-barHeight / 2); - value->setPen(barSet->floatingValuePen()); + value->setPos(xPos + (rect.width()/2 - value->boundingRect().width()/2) + ,yPos - barHeight/2 - value->boundingRect().height()/2); + value->setPen(barSet->valuePen()); itemIndex++; xPos += barWidth; } } - return layout; } diff --git a/src/barchart/barchartitem_p.h b/src/barchart/barchartitem_p.h index 8b9ed1c..39a1283 100644 --- a/src/barchart/barchartitem_p.h +++ b/src/barchart/barchartitem_p.h @@ -42,9 +42,6 @@ public: BarChartItem(QBarSeries *series, ChartPresenter *presenter); virtual ~BarChartItem(); -// Common implemantation of different presenters. Not to be instantiated. -// TODO: combine this with BarPresenter and derive other presenters from it? - public: // From QGraphicsItem void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); diff --git a/src/barchart/barvalue.cpp b/src/barchart/barvalue.cpp index c0c8512..302270e 100644 --- a/src/barchart/barvalue.cpp +++ b/src/barchart/barvalue.cpp @@ -82,6 +82,7 @@ QRectF BarValue::boundingRect() const void BarValue::valuesVisibleChanged(bool visible) { + qDebug() << "BarValue visiblle changed:" < PercentBarChartItem::calculateLayout() qreal scale = (height / colSum); qreal yPos = height; for (int set=0; set < m_series->barsetCount(); set++) { - qreal barHeight = m_series->valueAt(set, category) * scale; + QBarSet* barSet = m_series->barsetAt(set); + qreal barHeight = barSet->valueAt(category) * scale; Bar* bar = m_bars.at(itemIndex); - bar->setPen(m_series->barsetAt(set)->pen()); - bar->setBrush(m_series->barsetAt(set)->brush()); + bar->setPen(barSet->pen()); + bar->setBrush(barSet->brush()); QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); layout.append(rect); - itemIndex++; - yPos -= barHeight; - } - xPos += xStep; - } - // Position floating values - itemIndex = 0; - xPos = (width/categoryCount); - for (int category=0; category < m_series->categoryCount(); category++) { - qreal yPos = height; - qreal colSum = m_series->categorySum(category); - qreal scale = (height / colSum); - for (int set=0; set < m_series->barsetCount(); set++) { - qreal barHeight = m_series->valueAt(set,category) * scale; BarValue* value = m_values.at(itemIndex); - QBarSet* barSet = m_series->barsetAt(set); - if (!qFuzzyIsNull(m_series->valueAt(set,category))) { int p = m_series->percentageAt(set,category) * 100; QString vString(QString::number(p)); @@ -85,9 +70,9 @@ QVector PercentBarChartItem::calculateLayout() value->setText(QString("")); } - value->setPos(xPos, yPos-barHeight / 2); - value->setPen(barSet->floatingValuePen()); - + value->setPos(xPos + (rect.width()/2 - value->boundingRect().width()/2) + ,yPos - barHeight/2 - value->boundingRect().height()/2); + value->setPen(barSet->valuePen()); itemIndex++; yPos -= barHeight; } diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index 96f8c04..6f76cd9 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -56,8 +56,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! - \fn void QBarSet::toggleFloatingValues() - \brief \internal + \fn void QBarSet::setValuesVisible(bool visible = true) + \brief Sets visibility of bar values. Values are visible, if parameter \a visible is true */ /*! @@ -183,19 +183,19 @@ QBrush QBarSet::brush() const } /*! - Sets the pen for floating values that are drawn on top of this set + Sets the pen for values that are drawn on top of this set */ -void QBarSet::setFloatingValuePen(const QPen &pen) +void QBarSet::setValuePen(const QPen &pen) { - m_floatingValuePen = pen; + m_valuePen = pen; } /*! - Returns the pen for floating values that are drawn on top of this set + Returns the pen for values that are drawn on top of this set */ -QPen QBarSet::floatingValuePen() const +QPen QBarSet::valuePen() const { - return m_floatingValuePen; + return m_valuePen; } /*! diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 96607f4..7e2b996 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -55,8 +55,8 @@ public: void setBrush(const QBrush &brush); QBrush brush() const; - void setFloatingValuePen(const QPen &pen); - QPen floatingValuePen() const; + void setValuePen(const QPen &pen); + QPen valuePen() const; void setValuesVisible(bool visible = true); @@ -86,7 +86,7 @@ private: QMap m_mappedValues; QPen m_pen; QBrush m_brush; - QPen m_floatingValuePen; + QPen m_valuePen; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index d58b2d2..5025dab 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -58,43 +58,33 @@ QVector StackedBarChartItem::calculateLayout() for (int category = 0; category < categotyCount; category++) { qreal yPos = height; for (int set=0; set < m_series->barsetCount(); set++) { - qreal barHeight = m_series->valueAt(set, category) * scale; + QBarSet* barSet = m_series->barsetAt(set); + + qreal barHeight = barSet->valueAt(category) * scale; //m_series->valueAt(set, category) * scale; Bar* bar = m_bars.at(itemIndex); - bar->setPen(m_series->barsetAt(set)->pen()); - bar->setBrush(m_series->barsetAt(set)->brush()); + bar->setPen(barSet->pen()); + bar->setBrush(barSet->brush()); QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); layout.append(rect); - itemIndex++; - yPos -= barHeight; - } - xPos += xStep; - } - // Position floating values - itemIndex = 0; - xPos = (width/categotyCount); - for (int category=0; category < m_series->categoryCount(); category++) { - qreal yPos = height; - for (int set=0; set < m_series->barsetCount(); set++) { - qreal barHeight = m_series->valueAt(set, category) * scale; BarValue* value = m_values.at(itemIndex); - QBarSet* barSet = m_series->barsetAt(set); - - if (!qFuzzyIsNull(m_series->valueAt(set, category))) { - value->setText(QString::number(m_series->valueAt(set,category))); + if (!qFuzzyIsNull(barSet->valueAt(category))) { + value->setText(QString::number(barSet->valueAt(category))); } else { value->setText(QString("")); } - value->setPos(xPos, yPos-barHeight / 2); - value->setPen(barSet->floatingValuePen()); + value->setPos(xPos + (rect.width()/2 - value->boundingRect().width()/2) + ,yPos - barHeight/2 - value->boundingRect().height()/2); + value->setPen(barSet->valuePen()); itemIndex++; yPos -= barHeight; } xPos += xStep; } + return layout; } diff --git a/src/charttheme.cpp b/src/charttheme.cpp index 6068557..ed4be54 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -183,9 +183,9 @@ void ChartTheme::decorate(QBarSeries* series, int index, bool force) // Pick label color from the opposite end of the gradient. // 0.3 as a boundary seems to work well. if (takeAtPos < 0.3) - sets.at(i)->setFloatingValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1)); + sets.at(i)->setValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1)); else - sets.at(i)->setFloatingValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0)); + sets.at(i)->setValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0)); if (pen == sets.at(i)->pen() || force) { QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);