1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <private/chartbarcategoryaxisy_p.h>
5#include <private/chartpresenter_p.h>
6#include <private/qbarcategoryaxis_p.h>
7#include <private/abstractchartlayout_p.h>
8#include <QtCore/QtMath>
9#include <QtCore/QDebug>
10
11QT_BEGIN_NAMESPACE
12
13ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, QGraphicsItem* item)
14 : VerticalAxis(axis, item, true),
15 m_categoriesAxis(axis)
16{
17 QObject::connect(sender: m_categoriesAxis,SIGNAL(categoriesChanged()),receiver: this, SLOT(handleCategoriesChanged()));
18 handleCategoriesChanged();
19}
20
21ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
22{
23}
24
25QList<qreal> ChartBarCategoryAxisY::calculateLayout() const
26{
27 QList<qreal> points;
28 const QRectF& gridRect = gridGeometry();
29 const qreal range = max() - min();
30 const qreal delta = gridRect.height() / range;
31
32 if (delta < 2)
33 return points;
34
35 const qreal adjustedMin = min() + 0.5;
36 const qreal offset = (qRound(d: adjustedMin) - adjustedMin) * delta;
37
38 int count = qFloor(v: range);
39 if (count < 1)
40 return points;
41
42 points.resize(size: count + 2);
43
44 for (int i = 0; i < count + 2; ++i)
45 points[i] = gridRect.bottom() - (qreal(i) * delta) - offset;
46
47 return points;
48}
49
50QStringList ChartBarCategoryAxisY::createCategoryLabels(const QList<qreal> &layout) const
51{
52 QStringList result;
53 const QRectF &gridRect = gridGeometry();
54 const qreal d = (max() - min()) / gridRect.height();
55
56 for (int i = 0; i < layout.size() - 1; ++i) {
57 int x = qFloor(v: ((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5));
58 if ((x < m_categoriesAxis->categories().size()) && (x >= 0)) {
59 result << m_categoriesAxis->categories().at(i: x);
60 } else {
61 // No label for x coordinate
62 result << QString();
63 }
64 }
65 result << QString();
66 return result;
67}
68
69void ChartBarCategoryAxisY::updateGeometry()
70{
71 const QList<qreal> &layout = ChartAxisElement::layout();
72 if (layout.isEmpty())
73 return;
74 setLabels(createCategoryLabels(layout));
75 VerticalAxis::updateGeometry();
76}
77
78void ChartBarCategoryAxisY::handleCategoriesChanged()
79{
80 QGraphicsLayoutItem::updateGeometry();
81 if(presenter()) presenter()->layout()->invalidate();
82}
83
84QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
85{
86 Q_UNUSED(constraint);
87
88 QSizeF sh;
89 QSizeF base = VerticalAxis::sizeHint(which, constraint);
90 QStringList ticksList = m_categoriesAxis->categories();
91 qreal width = 0;
92 qreal height = 0; // Height is irrelevant for Y axes with interval labels
93
94 switch (which) {
95 case Qt::MinimumSize: {
96 if (labelsVisible()) {
97 QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(),
98 QStringLiteral("..."),
99 angle: axis()->labelsAngle());
100 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
101 if (base.width() > 0.0)
102 width += labelPadding();
103 } else {
104 width = base.width() + 1.0;
105 }
106 sh = QSizeF(width, height);
107 break;
108 }
109 case Qt::PreferredSize:{
110 if (labelsVisible()) {
111 qreal labelWidth = 0.0;
112 foreach (const QString& s, ticksList) {
113 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle());
114 labelWidth = qMax(a: rect.width(), b: labelWidth);
115 }
116 width = labelWidth + labelPadding() + base.width() + 1.0;
117 if (base.width() > 0.0)
118 width += labelPadding();
119 } else {
120 width = base.width() + 1.0;
121 }
122 sh = QSizeF(width, height);
123 break;
124 }
125 default:
126 break;
127 }
128 return sh;
129}
130
131QT_END_NAMESPACE
132
133#include "moc_chartbarcategoryaxisy_p.cpp"
134

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