1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <private/chartcoloraxisy_p.h>
5#include <QtCharts/QColorAxis>
6#include <private/chartpresenter_p.h>
7#include <private/abstractchartlayout_p.h>
8#include <QtWidgets/QGraphicsLayout>
9
10QT_BEGIN_NAMESPACE
11
12ChartColorAxisY::ChartColorAxisY(QColorAxis *axis, QGraphicsItem *item)
13 : VerticalAxis(axis, item, true)
14 , m_axis(axis)
15{
16}
17
18ChartColorAxisY::~ChartColorAxisY()
19{
20}
21
22QList<qreal> ChartColorAxisY::calculateLayout() const
23{
24 int tickCount = m_axis->tickCount();
25
26 Q_ASSERT(tickCount >= 2);
27
28 QList<qreal> points;
29 points.resize(size: tickCount);
30
31 const QRectF &gridRect = gridGeometry();
32
33 const qreal deltaY = gridRect.height() / (qreal(tickCount) - 1.0);
34 for (int i = 0; i < tickCount; ++i)
35 points[i] = qreal(i) * -deltaY + gridRect.bottom();
36
37 return points;
38}
39
40void ChartColorAxisY::updateGeometry()
41{
42 setLabels(createColorLabels(min: min(), max: max(), ticks: m_axis->tickCount()));
43
44 VerticalAxis::updateGeometry();
45}
46
47QSizeF ChartColorAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
48{
49 Q_UNUSED(constraint);
50
51 QSizeF sh;
52 QSizeF base = VerticalAxis::sizeHint(which, constraint);
53 const QStringList &ticksList = createColorLabels(min: min(), max: max(), ticks: m_axis->tickCount());
54 qreal width = 0;
55 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
56 // first and last ticks. Base height is irrelevant.
57 qreal height = 0;
58
59 switch (which) {
60 case Qt::MinimumSize: {
61 if (labelsVisible()) {
62 QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(),
63 QStringLiteral("..."),
64 angle: axis()->labelsAngle());
65 width = boundingRect.width() + labelPadding() + base.width() + m_axis->size()
66 + colorScalePadding() + 1.0;
67 height = boundingRect.height() / 2.0;
68 } else {
69 width = base.width() + m_axis->size() + colorScalePadding() + 1.0;
70 height = 0;
71 }
72 sh = QSizeF(width, height);
73 break;
74 }
75 case Qt::PreferredSize: {
76 if (labelsVisible()) {
77 qreal labelWidth = 0.0;
78 qreal firstHeight = -1.0;
79 for (const QString &s : ticksList) {
80 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s,
81 angle: axis()->labelsAngle());
82 labelWidth = qMax(a: rect.width(), b: labelWidth);
83 height = rect.height();
84 if (firstHeight < 0.0)
85 firstHeight = height;
86 }
87 width = labelWidth + labelPadding() + base.width() + m_axis->size() + colorScalePadding()
88 + 2.0; // two pixels of tolerance
89 height = qMax(a: height, b: firstHeight) / 2.0;
90 } else {
91 width = base.width() + m_axis->size() + colorScalePadding()
92 + 2.0; // two pixels of tolerance
93 height = 0;
94 }
95 sh = QSizeF(width, height);
96 break;
97 }
98 default:
99 break;
100 }
101 return sh;
102}
103
104QT_END_NAMESPACE
105
106#include "moc_chartcoloraxisy_p.cpp"
107

source code of qtcharts/src/charts/axis/coloraxis/chartcoloraxisy.cpp