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/percentbarchartitem_p.h> |
31 | #include <private/bar_p.h> |
32 | #include <private/qabstractbarseries_p.h> |
33 | #include <QtCharts/QBarSet> |
34 | #include <private/qbarset_p.h> |
35 | |
36 | QT_CHARTS_BEGIN_NAMESPACE |
37 | |
38 | PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) : |
39 | AbstractBarChartItem(series, item) |
40 | { |
41 | m_orientation = Qt::Vertical; |
42 | connect(sender: series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)), |
43 | receiver: this, SLOT(handleLabelsPositionChanged())); |
44 | connect(sender: series, SIGNAL(labelsFormatChanged(QString)), receiver: this, SLOT(positionLabels())); |
45 | } |
46 | |
47 | QString PercentBarChartItem::generateLabelText(int set, int category, qreal value) |
48 | { |
49 | Q_UNUSED(value) |
50 | |
51 | static const QString valueTag(QLatin1String("@value" )); |
52 | qreal p = m_series->d_func()->percentageAt(set, category) * 100.0; |
53 | QString vString(presenter()->numberToString(value: p, f: 'f', prec: 0)); |
54 | QString valueLabel; |
55 | if (m_series->labelsFormat().isEmpty()) { |
56 | vString.append(QStringLiteral("%" )); |
57 | valueLabel = vString; |
58 | } else { |
59 | valueLabel = m_series->labelsFormat(); |
60 | valueLabel.replace(before: valueTag, after: vString); |
61 | } |
62 | |
63 | return valueLabel; |
64 | } |
65 | |
66 | void PercentBarChartItem::initializeLayout(int set, int category, |
67 | int layoutIndex, bool resetAnimation) |
68 | { |
69 | Q_UNUSED(set) |
70 | Q_UNUSED(resetAnimation) |
71 | |
72 | QRectF rect; |
73 | |
74 | if (set > 0) { |
75 | QBarSet *barSet = m_series->barSets().at(i: set - 1); |
76 | Bar *bar = m_indexForBarMap.value(akey: barSet).value(akey: category); |
77 | rect = m_layout.at(i: bar->layoutIndex()); |
78 | rect.setBottom(rect.top()); |
79 | } else { |
80 | QPointF topLeft; |
81 | QPointF bottomRight; |
82 | const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth; |
83 | if (domain()->type() == AbstractDomain::XLogYDomain |
84 | || domain()->type() == AbstractDomain::LogXLogYDomain) { |
85 | topLeft = topLeftPoint(category, barWidth, value: domain()->minY()); |
86 | bottomRight = bottomRightPoint(category, barWidth, value: domain()->minY()); |
87 | } else { |
88 | topLeft = topLeftPoint(category, barWidth, value: 0.0); |
89 | bottomRight = bottomRightPoint(category, barWidth, value: 0.0); |
90 | } |
91 | if (m_validData) { |
92 | rect.setTopLeft(topLeft); |
93 | rect.setBottomRight(bottomRight); |
94 | } |
95 | } |
96 | |
97 | m_layout[layoutIndex] = rect.normalized(); |
98 | } |
99 | |
100 | void PercentBarChartItem::markLabelsDirty(QBarSet *barset, int index, int count) |
101 | { |
102 | Q_UNUSED(barset) |
103 | // Percent series need to dirty all labels of the stack |
104 | QList<QBarSet *> sets = m_barMap.keys(); |
105 | for (int set = 0; set < sets.size(); set++) |
106 | AbstractBarChartItem::markLabelsDirty(barset: sets.at(i: set), index, count); |
107 | } |
108 | |
109 | QPointF PercentBarChartItem::topLeftPoint(int category, qreal barWidth, qreal value) |
110 | { |
111 | return domain()->calculateGeometryPoint( |
112 | point: QPointF(m_seriesPosAdjustment + category - (barWidth / 2.0), value), ok&: m_validData); |
113 | } |
114 | |
115 | QPointF PercentBarChartItem::bottomRightPoint(int category, qreal barWidth, qreal value) |
116 | { |
117 | return domain()->calculateGeometryPoint( |
118 | point: QPointF(m_seriesPosAdjustment + category + (barWidth / 2.0), value), ok&: m_validData); |
119 | } |
120 | |
121 | QVector<QRectF> PercentBarChartItem::calculateLayout() |
122 | { |
123 | QVector<QRectF> layout; |
124 | layout.resize(asize: m_layout.size()); |
125 | |
126 | const int setCount = m_series->count(); |
127 | const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth; |
128 | |
129 | QVector<qreal> categorySums(m_categoryCount); |
130 | QVector<qreal> tempSums(m_categoryCount, 0.0); |
131 | |
132 | for (int category = 0; category < m_categoryCount; category++) |
133 | categorySums[category] = m_series->d_func()->categorySum(category: category + m_firstCategory); |
134 | |
135 | for (int set = 0; set < setCount; set++) { |
136 | QBarSet *barSet = m_series->barSets().at(i: set); |
137 | const QList<Bar *> bars = m_barMap.value(akey: barSet); |
138 | for (int i = 0; i < m_categoryCount; i++) { |
139 | Bar *bar = bars.at(i); |
140 | const int category = bar->index(); |
141 | qreal &sum = tempSums[category - m_firstCategory]; |
142 | const qreal &categorySum = categorySums.at(i: category - m_firstCategory); |
143 | qreal value = barSet->at(index: category); |
144 | QRectF rect; |
145 | qreal topY = 0.0; |
146 | qreal newSum = value + sum; |
147 | qreal bottomY = 0.0; |
148 | if (categorySum != 0.0) { |
149 | if (newSum > 0.0) |
150 | topY = 100.0 * newSum / categorySum; |
151 | if (sum > 0.0) |
152 | bottomY = 100.0 * sum / categorySum; |
153 | } |
154 | QPointF topLeft = topLeftPoint(category, barWidth, value: topY); |
155 | QPointF bottomRight; |
156 | if (domain()->type() == AbstractDomain::XLogYDomain |
157 | || domain()->type() == AbstractDomain::LogXLogYDomain) { |
158 | bottomRight = bottomRightPoint(category, barWidth, |
159 | value: set ? bottomY : domain()->minY()); |
160 | } else { |
161 | bottomRight = bottomRightPoint(category, barWidth, value: bottomY); |
162 | } |
163 | |
164 | rect.setTopLeft(topLeft); |
165 | rect.setBottomRight(bottomRight); |
166 | layout[bar->layoutIndex()] = rect.normalized(); |
167 | sum = newSum; |
168 | } |
169 | } |
170 | return layout; |
171 | } |
172 | |
173 | void PercentBarChartItem::handleLabelsPositionChanged() |
174 | { |
175 | positionLabels(); |
176 | } |
177 | |
178 | void PercentBarChartItem::positionLabels() |
179 | { |
180 | positionLabelsVertical(); |
181 | } |
182 | |
183 | QT_CHARTS_END_NAMESPACE |
184 | |
185 | #include "moc_percentbarchartitem_p.cpp" |
186 | |