diff --git a/src/barchart/horizontal/bar/horizontalbarchartitem.cpp b/src/barchart/horizontal/bar/horizontalbarchartitem.cpp index 4b9579a..c2222ee 100644 --- a/src/barchart/horizontal/bar/horizontalbarchartitem.cpp +++ b/src/barchart/horizontal/bar/horizontalbarchartitem.cpp @@ -37,56 +37,92 @@ 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(); - - // 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(); +// bool barsVisible = m_series->isVisible(); + qreal barWidth = m_series->d_func()->barWidth(); int itemIndex(0); - for (int category = 0; category < categoryCount; category++) { - qreal xPos = -scaleX * m_domainMinX + geometry().left(); + 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(); - - 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); + qreal value = m_series->barSets().at(set)->at(category); + QRectF rect; + QPointF topLeft; + 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)); - QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight); + QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(value, category - barWidth / 2 + (set)/(setCount) * barWidth)); + rect.setTopLeft(topLeft); + rect.setBottomRight(bottomRight); 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))); + label->setZValue(200); + itemIndex++; + if (!qFuzzyCompare(value, 0)) + label->setText(QString::number(value)); 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); +// 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()); - itemIndex++; } } + + +// // 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 09595f3..3de2084 100644 --- a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp +++ b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp @@ -37,60 +37,82 @@ 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(); - - // 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; +// bool barsVisible = m_series->isVisible(); - qreal rectWidth = barSet->value(category) * percentage * scaleX; - Bar *bar = m_bars.at(itemIndex); + qreal barWidth = m_series->d_func()->barWidth(); - 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); + for(int category = 0; category < categoryCount; category++) { + qreal sum = 0; + 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; + QPointF topLeft; + if (domain()->type() == AbstractDomain::LogXYDomain + || domain()->type() == AbstractDomain::LogXLogYDomain) + topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : domain()->minX(), category + barWidth/2)); 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); - - itemIndex++; - xPos += rectWidth; + topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : 0, category + barWidth/2)); + QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(100 * (value + sum)/categorySum, category - barWidth/2)); + rect.setTopLeft(topLeft); + rect.setBottomRight(bottomRight); + layout.append(rect); + sum +=value; } } + +// // 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); + +// itemIndex++; +// xPos += rectWidth; +// } +// } return layout; } diff --git a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp index c1c3245..952f2a5 100644 --- a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp +++ b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp @@ -37,61 +37,94 @@ 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(); - - // 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(); +// bool barsVisible = m_series->isVisible(); + + qreal barWidth = m_series->d_func()->barWidth(); + + for(int category = 0; category < categoryCount; category++) { + qreal positiveSum = 0; + qreal negativeSum = 0; 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; + qreal value = m_series->barSets().at(set)->at(category); + QRectF rect; + QPointF topLeft; + QPointF bottomRight; + if (value < 0) { + bottomRight = domain()->calculateGeometryPoint(QPointF(value + negativeSum, category - barWidth / 2)); + 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 { - 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; + bottomRight = domain()->calculateGeometryPoint(QPointF(value + positiveSum, category - barWidth / 2)); + 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)); + positiveSum += value; } - itemIndex++; + rect.setTopLeft(topLeft); + rect.setBottomRight(bottomRight); + 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 1ef0259..e4d15e8 100644 --- a/src/barchart/vertical/bar/barchartitem.cpp +++ b/src/barchart/vertical/bar/barchartitem.cpp @@ -38,54 +38,89 @@ 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(); - - // Domain: - 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(); +// 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++) { - qreal yPos = height + scaleY * m_domainMinY + geometry().top(); + 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(); - - qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left(); - xPos -= setCount * rectWidth / 2; - xPos += set * rectWidth; - - 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); + 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) + bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, domain()->minY())); else - bar->setVisible(barsVisible); + 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); - - if (!qFuzzyIsNull(barSet->value(category))) - label->setText(QString::number(barSet->value(category))); + label->setZValue(200); + itemIndex++; + if (!qFuzzyCompare(value, 0)) + label->setText(QString::number(value)); 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); +// 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()); - itemIndex++; } } + + // 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 5690928..a2ba65d 100644 --- a/src/barchart/vertical/percent/percentbarchartitem.cpp +++ b/src/barchart/vertical/percent/percentbarchartitem.cpp @@ -38,60 +38,82 @@ 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(); - - // Domain: - 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(); +// bool barsVisible = m_series->isVisible(); - qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2; + qreal barWidth = m_series->d_func()->barWidth(); - 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); + for(int category = 0; category < categoryCount; category++) { + qreal sum = 0; + 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; + QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, 100 * (value + sum)/categorySum)); + QPointF bottomRight; + if (domain()->type() == AbstractDomain::XLogYDomain + || domain()->type() == AbstractDomain::LogXLogYDomain) + bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : domain()->minY())); else - bar->setVisible(barsVisible); - - QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight); + bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : 0)); + rect.setTopLeft(topLeft); + rect.setBottomRight(bottomRight); 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); - - itemIndex++; - yPos -= rectHeight; + sum +=value; } } + + // 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); + +// itemIndex++; +// yPos -= rectHeight; +// } +// } return layout; } diff --git a/src/barchart/vertical/stacked/stackedbarchartitem.cpp b/src/barchart/vertical/stacked/stackedbarchartitem.cpp index ccc6f6f..2b81111 100644 --- a/src/barchart/vertical/stacked/stackedbarchartitem.cpp +++ b/src/barchart/vertical/stacked/stackedbarchartitem.cpp @@ -37,64 +37,97 @@ 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(); - - // Domain: - 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(); +// bool barsVisible = m_series->isVisible(); + qreal barWidth = m_series->d_func()->barWidth(); + + for(int category = 0; category < categoryCount; category++) { + qreal positiveSum = 0; + qreal negativeSum = 0; 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; + qreal value = m_series->barSets().at(set)->at(category); + QRectF rect; + QPointF topLeft; + QPointF bottomRight; + if (value < 0) { + bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum)); + 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 { - 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; + bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum)); + 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)); + positiveSum += value; } - - itemIndex++; + rect.setTopLeft(topLeft); + rect.setBottomRight(bottomRight); + layout.append(rect); } } - 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 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; } #include "moc_stackedbarchartitem_p.cpp"