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

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