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/chartbarcategoryaxisy_p.h>
31#include <private/chartpresenter_p.h>
32#include <private/qbarcategoryaxis_p.h>
33#include <private/abstractchartlayout_p.h>
34#include <QtCore/QtMath>
35#include <QtCore/QDebug>
36
37QT_CHARTS_BEGIN_NAMESPACE
38
39ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, QGraphicsItem* item)
40 : VerticalAxis(axis, item, true),
41 m_categoriesAxis(axis)
42{
43 QObject::connect( sender: m_categoriesAxis,SIGNAL(categoriesChanged()),receiver: this, SLOT(handleCategoriesChanged()));
44 handleCategoriesChanged();
45}
46
47ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
48{
49}
50
51QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
52{
53 QVector<qreal> points;
54 const QRectF& gridRect = gridGeometry();
55 qreal range = max() - min();
56 const qreal delta = gridRect.height() / range;
57
58 if (delta < 2)
59 return points;
60
61 qreal adjustedMin = min() + 0.5;
62 qreal offset = (qRound(d: adjustedMin) - adjustedMin) * delta;
63
64 int count = qFloor(v: range);
65 if (count < 1)
66 return points;
67
68 points.resize(size: count + 2);
69
70 for (int i = 0; i < count + 2; ++i)
71 points[i] = gridRect.bottom() - (qreal(i) * delta) - offset;
72
73 return points;
74}
75
76QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
77{
78 QStringList result;
79 const QRectF &gridRect = gridGeometry();
80 qreal d = (max() - min()) / gridRect.height();
81
82 for (int i = 0; i < layout.count() - 1; ++i) {
83 int x = qFloor(v: ((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5));
84 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
85 result << m_categoriesAxis->categories().at(i: x);
86 } else {
87 // No label for x coordinate
88 result << QString();
89 }
90 }
91 result << QString();
92 return result;
93}
94
95void ChartBarCategoryAxisY::updateGeometry()
96{
97 const QVector<qreal>& layout = ChartAxisElement::layout();
98 if (layout.isEmpty())
99 return;
100 setLabels(createCategoryLabels(layout));
101 VerticalAxis::updateGeometry();
102}
103
104void ChartBarCategoryAxisY::handleCategoriesChanged()
105{
106 QGraphicsLayoutItem::updateGeometry();
107 if(presenter()) presenter()->layout()->invalidate();
108}
109
110QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
111{
112 Q_UNUSED(constraint)
113
114 QSizeF sh;
115 QSizeF base = VerticalAxis::sizeHint(which, constraint);
116 QStringList ticksList = m_categoriesAxis->categories();
117 qreal width = 0;
118 qreal height = 0; // Height is irrelevant for Y axes with interval labels
119
120 switch (which) {
121 case Qt::MinimumSize: {
122 QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(),
123 QStringLiteral("..."),
124 angle: axis()->labelsAngle());
125 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
126 if (base.width() > 0.0)
127 width += labelPadding();
128 sh = QSizeF(width, height);
129 break;
130 }
131 case Qt::PreferredSize:{
132 qreal labelWidth = 0.0;
133 foreach (const QString& s, ticksList) {
134 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle());
135 labelWidth = qMax(a: rect.width(), b: labelWidth);
136 }
137 width = labelWidth + labelPadding() + base.width() + 1.0;
138 if (base.width() > 0.0)
139 width += labelPadding();
140 sh = QSizeF(width, height);
141 break;
142 }
143 default:
144 break;
145 }
146 return sh;
147}
148
149QT_CHARTS_END_NAMESPACE
150
151#include "moc_chartbarcategoryaxisy_p.cpp"
152

source code of qtcharts/src/charts/axis/barcategoryaxis/chartbarcategoryaxisy.cpp