1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <private/horizontalpercentbarchartitem_p.h> |
31 | #include <private/qabstractbarseries_p.h> |
32 | #include <private/qbarset_p.h> |
33 | #include <private/bar_p.h> |
34 | |
35 | QT_CHARTS_BEGIN_NAMESPACE |
36 | |
37 | HorizontalPercentBarChartItem::HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) |
38 | : AbstractBarChartItem(series, item) |
39 | { |
40 | } |
41 | |
42 | QString HorizontalPercentBarChartItem::generateLabelText(int set, int category, qreal value) |
43 | { |
44 | Q_UNUSED(value) |
45 | |
46 | static const QString valueTag(QLatin1String("@value" )); |
47 | qreal p = m_series->d_func()->percentageAt(set, category) * 100.0; |
48 | QString vString(presenter()->numberToString(value: p, f: 'f', prec: 0)); |
49 | QString valueLabel; |
50 | if (m_series->labelsFormat().isEmpty()) { |
51 | vString.append(QStringLiteral("%" )); |
52 | valueLabel = vString; |
53 | } else { |
54 | valueLabel = m_series->labelsFormat(); |
55 | valueLabel.replace(before: valueTag, after: vString); |
56 | } |
57 | |
58 | return valueLabel; |
59 | } |
60 | |
61 | void HorizontalPercentBarChartItem::initializeLayout(int set, int category, |
62 | int layoutIndex, bool resetAnimation) |
63 | { |
64 | Q_UNUSED(set) |
65 | Q_UNUSED(resetAnimation) |
66 | |
67 | QRectF rect; |
68 | |
69 | if (set > 0) { |
70 | QBarSet *barSet = m_series->barSets().at(i: set - 1); |
71 | Bar *bar = m_indexForBarMap.value(akey: barSet).value(akey: category); |
72 | rect = m_layout.at(i: bar->layoutIndex()); |
73 | rect.setLeft(rect.right()); |
74 | } else { |
75 | QPointF topLeft; |
76 | QPointF bottomRight; |
77 | const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth; |
78 | if (domain()->type() == AbstractDomain::LogXYDomain |
79 | || domain()->type() == AbstractDomain::LogXLogYDomain) { |
80 | topLeft = topLeftPoint(category, barWidth, value: domain()->minX()); |
81 | bottomRight = bottomRightPoint(category, barWidth, value: domain()->minX()); |
82 | } else { |
83 | topLeft = topLeftPoint(category, barWidth, value: 0.0); |
84 | bottomRight = bottomRightPoint(category, barWidth, value: 0.0); |
85 | } |
86 | if (m_validData) { |
87 | rect.setTopLeft(topLeft); |
88 | rect.setBottomRight(bottomRight); |
89 | } |
90 | } |
91 | m_layout[layoutIndex] = rect.normalized(); |
92 | } |
93 | |
94 | void HorizontalPercentBarChartItem::markLabelsDirty(QBarSet *barset, int index, int count) |
95 | { |
96 | Q_UNUSED(barset) |
97 | // Percent series need to dirty all labels of the stack |
98 | QList<QBarSet *> sets = m_barMap.keys(); |
99 | for (int set = 0; set < sets.size(); set++) |
100 | AbstractBarChartItem::markLabelsDirty(barset: sets.at(i: set), index, count); |
101 | } |
102 | |
103 | QPointF HorizontalPercentBarChartItem::topLeftPoint(int category, qreal barWidth, qreal value) |
104 | { |
105 | return domain()->calculateGeometryPoint( |
106 | point: QPointF(value, m_seriesPosAdjustment + category - (barWidth / 2.0)), ok&: m_validData); |
107 | } |
108 | |
109 | QPointF HorizontalPercentBarChartItem::bottomRightPoint(int category, qreal barWidth, qreal value) |
110 | { |
111 | return domain()->calculateGeometryPoint( |
112 | point: QPointF(value, m_seriesPosAdjustment + category + (barWidth / 2.0)), ok&: m_validData); |
113 | } |
114 | |
115 | QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout() |
116 | { |
117 | QVector<QRectF> layout; |
118 | layout.resize(asize: m_layout.size()); |
119 | |
120 | const int setCount = m_series->count(); |
121 | const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth; |
122 | |
123 | QVector<qreal> categorySums(m_categoryCount); |
124 | QVector<qreal> tempSums(m_categoryCount, 0.0); |
125 | |
126 | for (int category = 0; category < m_categoryCount; category++) |
127 | categorySums[category] = m_series->d_func()->categorySum(category: category + m_firstCategory); |
128 | |
129 | for (int set = 0; set < setCount; set++) { |
130 | QBarSet *barSet = m_series->barSets().at(i: set); |
131 | const QList<Bar *> bars = m_barMap.value(akey: barSet); |
132 | for (int i = 0; i < m_categoryCount; i++) { |
133 | Bar *bar = bars.at(i); |
134 | const int category = bar->index(); |
135 | qreal &sum = tempSums[category - m_firstCategory]; |
136 | const qreal &categorySum = categorySums.at(i: category - m_firstCategory); |
137 | qreal value = barSet->at(index: category); |
138 | QRectF rect; |
139 | qreal topX = 0.0; |
140 | qreal bottomX = 0.0; |
141 | qreal newSum = value + sum; |
142 | if (categorySum != 0.0) { |
143 | if (sum > 0.0) |
144 | topX = 100.0 * sum / categorySum; |
145 | if (newSum > 0.0) |
146 | bottomX = 100.0 * newSum / categorySum; |
147 | } |
148 | QPointF topLeft; |
149 | if (domain()->type() == AbstractDomain::LogXYDomain |
150 | || domain()->type() == AbstractDomain::LogXLogYDomain) { |
151 | topLeft = topLeftPoint(category, barWidth, value: set ? topX : domain()->minX()); |
152 | } else { |
153 | topLeft = topLeftPoint(category, barWidth, value: set ? topX : 0.0); |
154 | } |
155 | QPointF bottomRight = bottomRightPoint(category, barWidth, value: bottomX); |
156 | |
157 | rect.setTopLeft(topLeft); |
158 | rect.setBottomRight(bottomRight); |
159 | layout[bar->layoutIndex()] = rect.normalized(); |
160 | sum = newSum; |
161 | } |
162 | } |
163 | return layout; |
164 | } |
165 | |
166 | QT_CHARTS_END_NAMESPACE |
167 | |
168 | #include "moc_horizontalpercentbarchartitem_p.cpp" |
169 | |