| 1 | // Copyright (C) 2016 The Qt Company Ltd. | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only | 
| 3 |  | 
| 4 | #include <private/horizontalpercentbarchartitem_p.h> | 
| 5 | #include <private/qabstractbarseries_p.h> | 
| 6 | #include <private/qbarset_p.h> | 
| 7 | #include <private/bar_p.h> | 
| 8 |  | 
| 9 | QT_BEGIN_NAMESPACE | 
| 10 |  | 
| 11 | HorizontalPercentBarChartItem::HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) | 
| 12 |     : AbstractBarChartItem(series, item) | 
| 13 | { | 
| 14 | } | 
| 15 |  | 
| 16 | QString HorizontalPercentBarChartItem::generateLabelText(int set, int category, qreal value) | 
| 17 | { | 
| 18 |     Q_UNUSED(value); | 
| 19 |  | 
| 20 |     static const QString valueTag(QLatin1String("@value" )); | 
| 21 |     qreal p = m_series->d_func()->percentageAt(set, category) * 100.0; | 
| 22 |     QString vString(presenter()->numberToString(value: p, f: 'f', prec: 0)); | 
| 23 |     QString valueLabel; | 
| 24 |     if (m_series->labelsFormat().isEmpty()) { | 
| 25 |         vString.append(QStringLiteral("%" )); | 
| 26 |         valueLabel = vString; | 
| 27 |     } else { | 
| 28 |         valueLabel = m_series->labelsFormat(); | 
| 29 |         valueLabel.replace(before: valueTag, after: vString); | 
| 30 |     } | 
| 31 |  | 
| 32 |     return valueLabel; | 
| 33 | } | 
| 34 |  | 
| 35 | void HorizontalPercentBarChartItem::initializeLayout(int set, int category, | 
| 36 |                                                      int layoutIndex, bool resetAnimation) | 
| 37 | { | 
| 38 |     Q_UNUSED(set); | 
| 39 |     Q_UNUSED(resetAnimation); | 
| 40 |  | 
| 41 |     QRectF rect; | 
| 42 |  | 
| 43 |     if (set > 0) { | 
| 44 |         QBarSet *barSet = m_series->barSets().at(i: set - 1); | 
| 45 |         Bar *bar = m_indexForBarMap.value(key: barSet).value(key: category); | 
| 46 |         rect = m_layout.at(i: bar->layoutIndex()); | 
| 47 |         rect.setLeft(rect.right()); | 
| 48 |     } else { | 
| 49 |         QPointF topLeft; | 
| 50 |         QPointF bottomRight; | 
| 51 |         const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth; | 
| 52 |         if (domain()->type() == AbstractDomain::LogXYDomain | 
| 53 |                 || domain()->type() == AbstractDomain::LogXLogYDomain) { | 
| 54 |             topLeft = topLeftPoint(category, barWidth, value: domain()->minX()); | 
| 55 |             bottomRight = bottomRightPoint(category, barWidth, value: domain()->minX()); | 
| 56 |         } else { | 
| 57 |             topLeft = topLeftPoint(category, barWidth, value: 0.0); | 
| 58 |             bottomRight = bottomRightPoint(category, barWidth, value: 0.0); | 
| 59 |         } | 
| 60 |         if (m_validData) { | 
| 61 |             rect.setTopLeft(topLeft); | 
| 62 |             rect.setBottomRight(bottomRight); | 
| 63 |         } | 
| 64 |     } | 
| 65 |     m_layout[layoutIndex] = rect.normalized(); | 
| 66 | } | 
| 67 |  | 
| 68 | void HorizontalPercentBarChartItem::markLabelsDirty(QBarSet *barset, int index, int count) | 
| 69 | { | 
| 70 |     Q_UNUSED(barset); | 
| 71 |     // Percent series need to dirty all labels of the stack | 
| 72 |     QList<QBarSet *> sets = m_barMap.keys(); | 
| 73 |     for (int set = 0; set < sets.size(); set++) | 
| 74 |         AbstractBarChartItem::markLabelsDirty(barset: sets.at(i: set), index, count); | 
| 75 | } | 
| 76 |  | 
| 77 | QPointF HorizontalPercentBarChartItem::topLeftPoint(int category, qreal barWidth, qreal value) | 
| 78 | { | 
| 79 |     return domain()->calculateGeometryPoint( | 
| 80 |                 point: QPointF(value, m_seriesPosAdjustment + category - (barWidth / 2.0)), ok&: m_validData); | 
| 81 | } | 
| 82 |  | 
| 83 | QPointF HorizontalPercentBarChartItem::bottomRightPoint(int category, qreal barWidth, qreal value) | 
| 84 | { | 
| 85 |     return domain()->calculateGeometryPoint( | 
| 86 |                 point: QPointF(value, m_seriesPosAdjustment + category + (barWidth / 2.0)), ok&: m_validData); | 
| 87 | } | 
| 88 |  | 
| 89 | QList<QRectF> HorizontalPercentBarChartItem::calculateLayout() | 
| 90 | { | 
| 91 |     QList<QRectF> layout; | 
| 92 |     layout.resize(size: m_layout.size()); | 
| 93 |  | 
| 94 |     const int setCount = m_series->count(); | 
| 95 |     const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth; | 
| 96 |  | 
| 97 |     QList<qreal> categorySums(m_categoryCount); | 
| 98 |     QList<qreal> tempSums(m_categoryCount, 0.0); | 
| 99 |  | 
| 100 |     for (int category = 0; category < m_categoryCount; category++) | 
| 101 |         categorySums[category] = m_series->d_func()->categorySum(category: category + m_firstCategory); | 
| 102 |  | 
| 103 |     for (int set = 0; set < setCount; set++) { | 
| 104 |         QBarSet *barSet = m_series->barSets().at(i: set); | 
| 105 |         const QList<Bar *> bars = m_barMap.value(key: barSet); | 
| 106 |         for (int i = 0; i < m_categoryCount; i++) { | 
| 107 |             Bar *bar = bars.at(i); | 
| 108 |             const int category = bar->index(); | 
| 109 |             qreal &sum = tempSums[category - m_firstCategory]; | 
| 110 |             const qreal &categorySum = categorySums.at(i: category - m_firstCategory); | 
| 111 |             qreal value = barSet->at(index: category); | 
| 112 |             QRectF rect; | 
| 113 |             qreal topX = 0.0; | 
| 114 |             qreal bottomX = 0.0; | 
| 115 |             qreal newSum = value + sum; | 
| 116 |             if (categorySum != 0.0) { | 
| 117 |                 if (sum > 0.0) | 
| 118 |                     topX = 100.0 * sum / categorySum; | 
| 119 |                 if (newSum > 0.0) | 
| 120 |                     bottomX = 100.0 * newSum / categorySum; | 
| 121 |             } | 
| 122 |             QPointF topLeft; | 
| 123 |             if (domain()->type() == AbstractDomain::LogXYDomain | 
| 124 |                     || domain()->type() == AbstractDomain::LogXLogYDomain) { | 
| 125 |                 topLeft = topLeftPoint(category, barWidth, value: set ? topX : domain()->minX()); | 
| 126 |             } else { | 
| 127 |                 topLeft = topLeftPoint(category, barWidth, value: set ? topX : 0.0); | 
| 128 |             } | 
| 129 |             QPointF bottomRight = bottomRightPoint(category, barWidth, value: bottomX); | 
| 130 |  | 
| 131 |             rect.setTopLeft(topLeft); | 
| 132 |             rect.setBottomRight(bottomRight); | 
| 133 |             layout[bar->layoutIndex()] = rect.normalized(); | 
| 134 |             sum = newSum; | 
| 135 |         } | 
| 136 |     } | 
| 137 |     return layout; | 
| 138 | } | 
| 139 |  | 
| 140 | QT_END_NAMESPACE | 
| 141 |  | 
| 142 | #include "moc_horizontalpercentbarchartitem_p.cpp" | 
| 143 |  |