From 29e33b79f138910b5c63989f2417cceca154b033 2012-11-27 15:39:48 From: Marek Rosa Date: 2012-11-27 15:39:48 Subject: [PATCH] BarChartItems code cleanup --- diff --git a/src/barchart/abstractbarchartitem.cpp b/src/barchart/abstractbarchartitem.cpp index 2ca2e15..b26d393 100644 --- a/src/barchart/abstractbarchartitem.cpp +++ b/src/barchart/abstractbarchartitem.cpp @@ -46,8 +46,10 @@ AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphics connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged())); connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged())); connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged())); - setZValue(ChartPresenter::BarSeriesZValue); + setZValue(ChartPresenter::BarSeriesZValue); handleDataStructureChanged(); + handleVisibleChanged(); + handleUpdatedBars(); } AbstractBarChartItem::~AbstractBarChartItem() @@ -89,8 +91,12 @@ void AbstractBarChartItem::setLayout(const QVector &layout) m_layout = layout; - for (int i = 0; i < m_bars.count(); i++) + for (int i = 0; i < m_bars.count(); i++) { m_bars.at(i)->setRect(layout.at(i)); + QGraphicsSimpleTextItem *label = m_labels.at(i); + label->setPos(layout.at(i).center() - label->boundingRect().center()); + + } } //handlers @@ -192,6 +198,7 @@ void AbstractBarChartItem::handleUpdatedBars() bar->update(); QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); + label->setText(QString("%1").arg(barSet->value(category))); label->setFont(barSet->m_labelFont); label->setBrush(barSet->m_labelBrush); label->update(); diff --git a/src/barchart/abstractbarchartitem_p.h b/src/barchart/abstractbarchartitem_p.h index a128513..6ae78d9 100644 --- a/src/barchart/abstractbarchartitem_p.h +++ b/src/barchart/abstractbarchartitem_p.h @@ -68,7 +68,7 @@ public Q_SLOTS: void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items void handleVisibleChanged(); void handleOpacityChanged(); - void handleUpdatedBars(); + virtual void handleUpdatedBars(); protected: diff --git a/src/barchart/horizontal/bar/horizontalbarchartitem.cpp b/src/barchart/horizontal/bar/horizontalbarchartitem.cpp index c2222ee..f51343d 100644 --- a/src/barchart/horizontal/bar/horizontalbarchartitem.cpp +++ b/src/barchart/horizontal/bar/horizontalbarchartitem.cpp @@ -37,7 +37,6 @@ QVector HorizontalBarChartItem::calculateLayout() // Use temporary qreals for accuracy qreal categoryCount = m_series->d_func()->categoryCount(); qreal setCount = m_series->count(); -// bool barsVisible = m_series->isVisible(); qreal barWidth = m_series->d_func()->barWidth(); int itemIndex(0); @@ -46,8 +45,7 @@ QVector HorizontalBarChartItem::calculateLayout() qreal value = m_series->barSets().at(set)->at(category); QRectF rect; QPointF topLeft; - if (domain()->type() == AbstractDomain::LogXYDomain - || domain()->type() == AbstractDomain::LogXLogYDomain) + if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2 + (set + 1)/(setCount) * barWidth)); else topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2 + (set + 1)/(setCount) * barWidth)); @@ -58,71 +56,12 @@ QVector HorizontalBarChartItem::calculateLayout() layout.append(rect); QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); + label->setPos(rect.center() - label->boundingRect().center()); label->setZValue(200); itemIndex++; - if (!qFuzzyCompare(value, 0)) - label->setText(QString::number(value)); - else - label->setText(QString("")); - -// label->setFont(m_series->barSets().at(set)->labelFont()); -// label->setBrush(m_series->barSets().at(set)->labelBrush()); label->setBrush(Qt::black); - - label->setPos(rect.center() - label->boundingRect().center()); - } } - - -// // AbstractDomain: -// qreal width = geometry().width(); -// qreal height = geometry().height(); -// qreal rangeY = m_domainMaxY - m_domainMinY; -// qreal rangeX = m_domainMaxX - m_domainMinX; -// qreal scaleY = (height / rangeY); -// qreal scaleX = (width / rangeX); - -// // On horizontal chart barWidth of the barseries means height of the rect. -// qreal rectHeight = (scaleY / setCount) * m_series->d_func()->barWidth(); - -// int itemIndex(0); -// for (int category = 0; category < categoryCount; category++) { -// qreal xPos = -scaleX * m_domainMinX + geometry().left(); -// for (int set = 0; set < setCount; set++) { -// QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - -// qreal yPos = geometry().bottom() + (m_domainMinY - barSet->pos(category)) * scaleY; -// yPos += setCount * rectHeight / 2; -// yPos -= set * rectHeight; - -// qreal rectWidth = barSet->value(category) * scaleX; -// Bar *bar = m_bars.at(itemIndex); - -// QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight); -// layout.append(rect); -// bar->setPen(barSet->m_pen); -// bar->setBrush(barSet->m_brush); -// if (qFuzzyCompare(rectHeight, 0)) -// bar->setVisible(false); -// else -// bar->setVisible(barsVisible); - -// QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); - -// if (!qFuzzyCompare(barSet->value(category), 0)) -// label->setText(QString::number(barSet->value(category))); -// else -// label->setText(QString("")); - -// label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), -// yPos - rectHeight / 2 - label->boundingRect().height() / 2); -// label->setFont(barSet->m_labelFont); -// label->setBrush(barSet->m_labelBrush); - -// itemIndex++; -// } -// } return layout; } diff --git a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp index 3de2084..b36d5d6 100644 --- a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp +++ b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp @@ -37,8 +37,6 @@ QVector HorizontalPercentBarChartItem::calculateLayout() // Use temporary qreals for accuracy qreal categoryCount = m_series->d_func()->categoryCount(); qreal setCount = m_series->count(); -// bool barsVisible = m_series->isVisible(); - qreal barWidth = m_series->d_func()->barWidth(); for(int category = 0; category < categoryCount; category++) { @@ -48,8 +46,7 @@ QVector HorizontalPercentBarChartItem::calculateLayout() qreal value = m_series->barSets().at(set)->at(category); QRectF rect; QPointF topLeft; - if (domain()->type() == AbstractDomain::LogXYDomain - || domain()->type() == AbstractDomain::LogXLogYDomain) + if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : domain()->minX(), category + barWidth/2)); else topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : 0, category + barWidth/2)); @@ -60,60 +57,36 @@ QVector HorizontalPercentBarChartItem::calculateLayout() sum +=value; } } + return layout; +} -// // AbstractDomain: -// qreal width = geometry().width(); -// qreal height = geometry().height(); -// qreal rangeY = m_domainMaxY - m_domainMinY; -// qreal rangeX = m_domainMaxX - m_domainMinX; -// qreal scaleY = (height / rangeY); -// qreal scaleX = (width / rangeX); -// qreal rectHeight = scaleY * m_series->d_func()->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect. - -// int itemIndex(0); -// for (int category = 0; category < categoryCount; category++) { -// qreal colSum = m_series->d_func()->categorySum(category); -// qreal percentage = (100 / colSum); -// qreal xPos = -scaleX * m_domainMinX + geometry().left(); -// for (int set = 0; set < setCount; set++) { -// QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - -// qreal yPos = (m_domainMinY + 0.5 - barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight / 2; - -// qreal rectWidth = barSet->value(category) * percentage * scaleX; -// Bar *bar = m_bars.at(itemIndex); - -// QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight); -// layout.append(rect); -// bar->setPen(barSet->m_pen); -// bar->setBrush(barSet->m_brush); -// if (qFuzzyCompare(rectHeight, 0)) -// bar->setVisible(false); -// else -// bar->setVisible(barsVisible); - -// QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); - -// if (!qFuzzyCompare(m_series->d_func()->valueAt(set, category), 0)) { -// int p = m_series->d_func()->percentageAt(set, category) * 100; -// QString vString(QString::number(p)); -// vString.truncate(3); -// vString.append("%"); -// label->setText(vString); -// } else { -// label->setText(QString("")); -// } - -// label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), -// yPos - rectHeight / 2 - label->boundingRect().height() / 2); -// label->setFont(barSet->m_labelFont); -// label->setBrush(barSet->m_labelBrush); +void HorizontalPercentBarChartItem::handleUpdatedBars() +{ + // Handle changes in pen, brush, labels etc. + int categoryCount = m_series->d_func()->categoryCount(); + int setCount = m_series->count(); + int itemIndex(0); -// itemIndex++; -// xPos += rectWidth; -// } -// } - return layout; + for (int category = 0; category < categoryCount; category++) { + for (int set = 0; set < setCount; set++) { + QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); + Bar *bar = m_bars.at(itemIndex); + bar->setPen(barSet->m_pen); + bar->setBrush(barSet->m_brush); + bar->update(); + + QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); + int p = m_series->d_func()->percentageAt(set, category) * 100; + QString vString(QString::number(p)); + vString.truncate(3); + vString.append("%"); + label->setText(vString); + label->setFont(barSet->m_labelFont); + label->setBrush(barSet->m_labelBrush); + label->update(); + itemIndex++; + } + } } #include "moc_horizontalpercentbarchartitem_p.cpp" diff --git a/src/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h b/src/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h index 364af93..ead2f42 100644 --- a/src/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h +++ b/src/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h @@ -40,6 +40,7 @@ class HorizontalPercentBarChartItem : public AbstractBarChartItem Q_OBJECT public: HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0); + void handleUpdatedBars(); private: virtual QVector calculateLayout(); diff --git a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp index 952f2a5..eb614f4 100644 --- a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp +++ b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp @@ -37,8 +37,6 @@ QVector HorizontalStackedBarChartItem::calculateLayout() // Use temporary qreals for accuracy qreal categoryCount = m_series->d_func()->categoryCount(); qreal setCount = m_series->count(); -// bool barsVisible = m_series->isVisible(); - qreal barWidth = m_series->d_func()->barWidth(); for(int category = 0; category < categoryCount; category++) { @@ -51,16 +49,14 @@ QVector HorizontalStackedBarChartItem::calculateLayout() QPointF bottomRight; if (value < 0) { bottomRight = domain()->calculateGeometryPoint(QPointF(value + negativeSum, category - barWidth / 2)); - if (domain()->type() == AbstractDomain::LogXYDomain - || domain()->type() == AbstractDomain::LogXLogYDomain) + if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : domain()->minX(), category + barWidth / 2)); else topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : 0, category + barWidth / 2)); negativeSum += value; } else { bottomRight = domain()->calculateGeometryPoint(QPointF(value + positiveSum, category - barWidth / 2)); - if (domain()->type() == AbstractDomain::LogXYDomain - || domain()->type() == AbstractDomain::LogXLogYDomain) + if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : domain()->minX(), category + barWidth / 2)); else topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : 0, category + barWidth / 2)); @@ -71,60 +67,6 @@ QVector HorizontalStackedBarChartItem::calculateLayout() layout.append(rect); } } - - // // AbstractDomain: - // qreal width = geometry().width(); - // qreal height = geometry().height(); - // qreal rangeY = m_domainMaxY - m_domainMinY; - // qreal rangeX = m_domainMaxX - m_domainMinX; - // qreal scaleY = (height / rangeY); - // qreal scaleX = (width / rangeX); - // qreal rectHeight = scaleY * m_series->d_func()->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect. - - // int itemIndex(0); - // for (int category = 0; category < categoryCount; category++) { - // 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(); - - // qreal yPos = (m_domainMinY + 0.5 - barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight / 2; - - // qreal rectWidth = barSet->value(category) * scaleX; - // Bar *bar = m_bars.at(itemIndex); - - // bar->setPen(barSet->m_pen); - // bar->setBrush(barSet->m_brush); - // if (qFuzzyCompare(rectHeight, 0)) - // bar->setVisible(false); - // else - // bar->setVisible(barsVisible); - - // QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); - - // if (!qFuzzyCompare(barSet->value(category), 0)) - // label->setText(QString::number(barSet->value(category))); - // else - // label->setText(QString("")); - // label->setFont(barSet->m_labelFont); - // label->setBrush(barSet->m_labelBrush); - - // if (rectWidth > 0) { - // QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight); - // layout.append(rect); - // label->setPos(xMax + (rect.width() / 2 - label->boundingRect().width() / 2), - // yPos - rectHeight / 2 - label->boundingRect().height() / 2); - // xMax += rectWidth; - // } else { - // QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight); - // layout.append(rect); - // label->setPos(xMin + (rect.width() / 2 - label->boundingRect().width() / 2), - // yPos - rectHeight / 2 - label->boundingRect().height() / 2); - // xMin += rectWidth; - // } - // itemIndex++; - // } - // } return layout; } diff --git a/src/barchart/vertical/bar/barchartitem.cpp b/src/barchart/vertical/bar/barchartitem.cpp index e4d15e8..0df2e79 100644 --- a/src/barchart/vertical/bar/barchartitem.cpp +++ b/src/barchart/vertical/bar/barchartitem.cpp @@ -38,89 +38,23 @@ QVector BarChartItem::calculateLayout() // Use temporary qreals for accuracy qreal categoryCount = m_series->d_func()->categoryCount(); qreal setCount = m_series->count(); -// bool barsVisible = m_series->isVisible(); qreal barWidth = m_series->d_func()->barWidth(); - // barWidth = 1.8; - int itemIndex(0); for(int category = 0; category < categoryCount; category++) { for (int set = 0; set < setCount; set++) { qreal value = m_series->barSets().at(set)->at(category); QRectF rect; QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set)/(setCount) * barWidth, value)); QPointF bottomRight; - if (domain()->type() == AbstractDomain::XLogYDomain - || domain()->type() == AbstractDomain::LogXLogYDomain) + if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, domain()->minY())); else bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, 0)); rect.setTopLeft(topLeft); rect.setBottomRight(bottomRight); layout.append(rect); - - QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); - label->setZValue(200); - itemIndex++; - if (!qFuzzyCompare(value, 0)) - label->setText(QString::number(value)); - else - label->setText(QString("")); - -// label->setFont(m_series->barSets().at(set)->labelFont()); -// label->setBrush(m_series->barSets().at(set)->labelBrush()); - label->setBrush(Qt::black); - - label->setPos(rect.center() - label->boundingRect().center()); - } } - - // AbstractDomain: - // qreal width = geometry().width(); - // qreal height = geometry().height(); - // qreal rangeY = m_domainMaxY - m_domainMinY; - // qreal rangeX = m_domainMaxX - m_domainMinX; - // qreal scaleY = (height / rangeY); - // qreal scaleX = (width / rangeX); - // qreal rectWidth = (scaleX / setCount) * m_series->d_func()->barWidth(); - - // int itemIndex(0); - // for (int category = 0; category < categoryCount; category++) { - // qreal yPos = height + scaleY * m_domainMinY + geometry().top(); - // for (int set = 0; set < setCount; set++) { - // QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - - // qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left(); - // xPos -= setCount * rectWidth / 2; - // xPos += set * rectWidth;barWidth / 2 - - // qreal rectHeight = barSet->value(category) * scaleY; - // Bar *bar = m_bars.at(itemIndex); - - // QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight); - // layout.append(rect); - // bar->setPen(barSet->m_pen); - // bar->setBrush(barSet->m_brush); - // if (qFuzzyIsNull(rectHeight)) - // bar->setVisible(false); - // else - // bar->setVisible(barsVisible); - - // QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); - - // if (!qFuzzyIsNull(barSet->value(category))) - // label->setText(QString::number(barSet->value(category))); - // else - // label->setText(QString("")); - - // label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), - // yPos - rectHeight / 2 - label->boundingRect().height() / 2); - // label->setFont(barSet->m_labelFont); - // label->setBrush(barSet->m_labelBrush); - - // itemIndex++; - // } - // } return layout; } diff --git a/src/barchart/vertical/percent/percentbarchartitem.cpp b/src/barchart/vertical/percent/percentbarchartitem.cpp index a2ba65d..1c12044 100644 --- a/src/barchart/vertical/percent/percentbarchartitem.cpp +++ b/src/barchart/vertical/percent/percentbarchartitem.cpp @@ -38,8 +38,6 @@ QVector PercentBarChartItem::calculateLayout() // Use temporary qreals for accuracy qreal categoryCount = m_series->d_func()->categoryCount(); qreal setCount = m_series->count(); -// bool barsVisible = m_series->isVisible(); - qreal barWidth = m_series->d_func()->barWidth(); for(int category = 0; category < categoryCount; category++) { @@ -47,11 +45,10 @@ QVector PercentBarChartItem::calculateLayout() qreal categorySum = m_series->d_func()->categorySum(category); for (int set = 0; set < setCount; set++) { qreal value = m_series->barSets().at(set)->at(category); - QRectF rect; + QRectF rect; QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, 100 * (value + sum)/categorySum)); QPointF bottomRight; - if (domain()->type() == AbstractDomain::XLogYDomain - || domain()->type() == AbstractDomain::LogXLogYDomain) + if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : domain()->minY())); else bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : 0)); @@ -61,60 +58,36 @@ QVector PercentBarChartItem::calculateLayout() sum +=value; } } + return layout; +} - // AbstractDomain: -// qreal width = geometry().width(); -// qreal height = geometry().height(); -// qreal rangeY = m_domainMaxY - m_domainMinY; -// qreal rangeX = m_domainMaxX - m_domainMinX; -// qreal scaleY = (height / rangeY); -// qreal scaleX = (width / rangeX); -// qreal rectWidth = scaleX * m_series->d_func()->barWidth(); - -// int itemIndex(0); -// for (int category = 0; category < categoryCount; category++) { -// qreal colSum = m_series->d_func()->categorySum(category); -// qreal percentage = (100 / colSum); -// qreal yPos = height + scaleY * m_domainMinY + geometry().top(); -// for (int set = 0; set < setCount; set++) { -// QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - -// qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2; - -// qreal rectHeight = barSet->value(category) * percentage * scaleY; -// Bar *bar = m_bars.at(itemIndex); -// bar->setPen(barSet->m_pen); -// bar->setBrush(barSet->m_brush); -// if (qFuzzyIsNull(rectHeight)) -// bar->setVisible(false); -// else -// bar->setVisible(barsVisible); - -// QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight); -// layout.append(rect); - -// QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); - -// if (!qFuzzyIsNull(m_series->d_func()->valueAt(set, category))) { -// int p = m_series->d_func()->percentageAt(set, category) * 100; -// QString vString(QString::number(p)); -// vString.truncate(3); -// vString.append("%"); -// label->setText(vString); -// } else { -// label->setText(QString("")); -// } - -// label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), -// yPos - rectHeight / 2 - label->boundingRect().height() / 2); -// label->setFont(barSet->m_labelFont); -// label->setBrush(barSet->m_labelBrush); +void PercentBarChartItem::handleUpdatedBars() +{ + // Handle changes in pen, brush, labels etc. + int categoryCount = m_series->d_func()->categoryCount(); + int setCount = m_series->count(); + int itemIndex(0); -// itemIndex++; -// yPos -= rectHeight; -// } -// } - return layout; + for (int category = 0; category < categoryCount; category++) { + for (int set = 0; set < setCount; set++) { + QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); + Bar *bar = m_bars.at(itemIndex); + bar->setPen(barSet->m_pen); + bar->setBrush(barSet->m_brush); + bar->update(); + + QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); + int p = m_series->d_func()->percentageAt(set, category) * 100; + QString vString(QString::number(p)); + vString.truncate(3); + vString.append("%"); + label->setText(vString); + label->setFont(barSet->m_labelFont); + label->setBrush(barSet->m_labelBrush); + label->update(); + itemIndex++; + } + } } #include "moc_percentbarchartitem_p.cpp" diff --git a/src/barchart/vertical/percent/percentbarchartitem_p.h b/src/barchart/vertical/percent/percentbarchartitem_p.h index f5b610c..d0ceb64 100644 --- a/src/barchart/vertical/percent/percentbarchartitem_p.h +++ b/src/barchart/vertical/percent/percentbarchartitem_p.h @@ -43,9 +43,10 @@ class PercentBarChartItem : public AbstractBarChartItem Q_OBJECT public: PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0); + void handleUpdatedBars(); private: - virtual QVector calculateLayout(); + virtual QVector calculateLayout(); }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/vertical/stacked/stackedbarchartitem.cpp b/src/barchart/vertical/stacked/stackedbarchartitem.cpp index 2b81111..732c220 100644 --- a/src/barchart/vertical/stacked/stackedbarchartitem.cpp +++ b/src/barchart/vertical/stacked/stackedbarchartitem.cpp @@ -37,7 +37,6 @@ QVector StackedBarChartItem::calculateLayout() // Use temporary qreals for accuracy qreal categoryCount = m_series->d_func()->categoryCount(); qreal setCount = m_series->count(); -// bool barsVisible = m_series->isVisible(); qreal barWidth = m_series->d_func()->barWidth(); for(int category = 0; category < categoryCount; category++) { @@ -50,16 +49,14 @@ QVector StackedBarChartItem::calculateLayout() QPointF bottomRight; if (value < 0) { bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum)); - if (domain()->type() == AbstractDomain::XLogYDomain - || domain()->type() == AbstractDomain::LogXLogYDomain) + if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : domain()->minY())); else topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : 0)); negativeSum += value; } else { bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum)); - if (domain()->type() == AbstractDomain::XLogYDomain - || domain()->type() == AbstractDomain::LogXLogYDomain) + if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : domain()->minY())); else topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : 0)); @@ -70,64 +67,7 @@ QVector StackedBarChartItem::calculateLayout() layout.append(rect); } } - - -// AbstractDomain: -// qreal width = geometry().width(); -// qreal height = geometry().height(); -// qreal rangeY = m_domainMaxY - m_domainMinY; -// qreal rangeX = m_domainMaxX - m_domainMinX; -// qreal scaleY = (height / rangeY); -// qreal scaleX = (width / rangeX); -// qreal rectWidth = scaleX * m_series->d_func()->barWidth(); - -// int itemIndex(0); -// for (int category = 0; category < categoryCount; category++) { -// qreal yMax = height + scaleY * m_domainMinY + geometry().top(); -// qreal yMin = height + scaleY * m_domainMinY + geometry().top(); -// for (int set = 0; set < setCount; set++) { -// QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - -// qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2; - -// qreal rectHeight = barSet->value(category) * scaleY; -// Bar *bar = m_bars.at(itemIndex); -// bar->setPen(barSet->m_pen); -// bar->setBrush(barSet->m_brush); -// if (qFuzzyIsNull(rectHeight)) -// bar->setVisible(false); -// else -// bar->setVisible(barsVisible); - -// QGraphicsSimpleTextItem *label = m_labels.at(itemIndex); - -// if (!qFuzzyIsNull(barSet->value(category))) -// label->setText(QString::number(barSet->value(category))); -// else -// label->setText(QString("")); - -// label->setFont(barSet->m_labelFont); -// label->setBrush(barSet->m_labelBrush); - -// if (rectHeight < 0) { -// QRectF rect(xPos, yMax - rectHeight, rectWidth, rectHeight); -// layout.append(rect); -// label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), -// yMax - rectHeight / 2 - label->boundingRect().height() / 2); -// yMax -= rectHeight; -// } else { -// QRectF rect(xPos, yMin - rectHeight, rectWidth, rectHeight); -// layout.append(rect); -// label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2), -// yMin - rectHeight / 2 - label->boundingRect().height() / 2); -// yMin -= rectHeight; -// } - -// itemIndex++; -// } -// } - -return layout; + return layout; } #include "moc_stackedbarchartitem_p.cpp"