##// END OF EJS Templates
Draw the tick and label on logaxis when tick is exactly at high edge...
Miikka Heikkinen -
r2837:e6f5cbaa5aca
parent child
Show More
@@ -1,136 +1,147
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <private/chartlogvalueaxisx_p.h>
20 20 #include <private/chartpresenter_p.h>
21 21 #include <QtCharts/QLogValueAxis>
22 22 #include <private/abstractchartlayout_p.h>
23 23 #include <QtWidgets/QGraphicsLayout>
24 24 #include <QtCore/QtMath>
25 25 #include <QtCore/QDebug>
26 26 #include <cmath>
27 27
28 28 QT_CHARTS_BEGIN_NAMESPACE
29 29
30 30 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem *item)
31 31 : HorizontalAxis(axis, item),
32 32 m_axis(axis)
33 33 {
34 34 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)), this, SLOT(handleBaseChanged(qreal)));
35 35 QObject::connect(m_axis, SIGNAL(labelFormatChanged(QString)), this, SLOT(handleLabelFormatChanged(QString)));
36 36 }
37 37
38 38 ChartLogValueAxisX::~ChartLogValueAxisX()
39 39 {
40 40 }
41 41
42 42 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
43 43 {
44 44 QVector<qreal> points;
45 45
46 46 qreal logMax = std::log10(m_axis->max()) / std::log10(m_axis->base());
47 47 qreal logMin = std::log10(m_axis->min()) / std::log10(m_axis->base());
48 48 qreal leftEdge = logMin < logMax ? logMin : logMax;
49 49 qreal ceilEdge = qCeil(leftEdge);
50 50 int tickCount = qAbs(qCeil(logMax) - qCeil(logMin));
51 51
52 // If the high edge sits exactly on the tick value, add a tick
53 qreal highValue = logMin < logMax ? logMax : logMin;
54 if (qFuzzyCompare(highValue, qreal(qCeil(highValue))))
55 tickCount++;
56
52 57 points.resize(tickCount);
53 58 const QRectF &gridRect = gridGeometry();
54 59 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
55 60 for (int i = 0; i < tickCount; ++i)
56 61 points[i] = (ceilEdge + qreal(i)) * deltaX - leftEdge * deltaX + gridRect.left();
57 62
58 63 return points;
59 64 }
60 65
61 66 void ChartLogValueAxisX::updateGeometry()
62 67 {
63 68 const QVector<qreal>& layout = ChartAxisElement::layout();
64 69 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
65 70 HorizontalAxis::updateGeometry();
66 71 }
67 72
68 73 void ChartLogValueAxisX::handleBaseChanged(qreal base)
69 74 {
70 75 Q_UNUSED(base);
71 76 QGraphicsLayoutItem::updateGeometry();
72 77 if(presenter()) presenter()->layout()->invalidate();
73 78 }
74 79
75 80 void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format)
76 81 {
77 82 Q_UNUSED(format);
78 83 QGraphicsLayoutItem::updateGeometry();
79 84 if(presenter()) presenter()->layout()->invalidate();
80 85 }
81 86
82 87 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
83 88 {
84 89 Q_UNUSED(constraint)
85 90
86 91 QSizeF sh;
87 92
88 93 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
89 94 QStringList ticksList;
90 95 qreal logMax = std::log10(m_axis->max()) / std::log10(m_axis->base());
91 96 qreal logMin = std::log10(m_axis->min()) / std::log10(m_axis->base());
92 97 int tickCount = qAbs(qCeil(logMax) - qCeil(logMin));
98
99 // If the high edge sits exactly on the tick value, add a tick
100 qreal highValue = logMin < logMax ? logMax : logMin;
101 if (qFuzzyCompare(highValue, qreal(qCeil(highValue))))
102 tickCount++;
103
93 104 if (m_axis->max() > m_axis->min() && tickCount > 0)
94 105 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
95 106 else
96 107 ticksList.append(QStringLiteral(" "));
97 108 // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past
98 109 // first and last ticks. Base width is irrelevant.
99 110 qreal width = 0;
100 111 qreal height = 0;
101 112
102 113 switch (which) {
103 114 case Qt::MinimumSize:{
104 115 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(),
105 116 QStringLiteral("..."),
106 117 axis()->labelsAngle());
107 118 width = boundingRect.width() / 2.0;
108 119 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
109 120 sh = QSizeF(width, height);
110 121 break;
111 122 }
112 123 case Qt::PreferredSize: {
113 124 qreal labelHeight = 0.0;
114 125 qreal firstWidth = -1.0;
115 126 foreach (const QString& s, ticksList) {
116 127 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
117 128 labelHeight = qMax(rect.height(), labelHeight);
118 129 width = rect.width();
119 130 if (firstWidth < 0.0)
120 131 firstWidth = width;
121 132 }
122 133 height = labelHeight + labelPadding() + base.height() + 1.0;
123 134 width = qMax(width, firstWidth) / 2.0;
124 135 sh = QSizeF(width, height);
125 136 break;
126 137 }
127 138 default:
128 139 break;
129 140 }
130 141
131 142 return sh;
132 143 }
133 144
134 145 #include "moc_chartlogvalueaxisx_p.cpp"
135 146
136 147 QT_CHARTS_END_NAMESPACE
@@ -1,136 +1,147
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <private/chartlogvalueaxisy_p.h>
20 20 #include <private/chartpresenter_p.h>
21 21 #include <QtCharts/QLogValueAxis>
22 22 #include <private/abstractchartlayout_p.h>
23 23 #include <QtWidgets/QGraphicsLayout>
24 24 #include <QtCore/QtMath>
25 25 #include <QtCore/QDebug>
26 26 #include <cmath>
27 27
28 28 QT_CHARTS_BEGIN_NAMESPACE
29 29
30 30 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem *item)
31 31 : VerticalAxis(axis, item),
32 32 m_axis(axis)
33 33 {
34 34 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)), this, SLOT(handleBaseChanged(qreal)));
35 35 QObject::connect(m_axis, SIGNAL(labelFormatChanged(QString)), this, SLOT(handleLabelFormatChanged(QString)));
36 36 }
37 37
38 38 ChartLogValueAxisY::~ChartLogValueAxisY()
39 39 {
40 40 }
41 41
42 42 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
43 43 {
44 44 QVector<qreal> points;
45 45 qreal logMax = std::log10(m_axis->max()) / std::log10(m_axis->base());
46 46 qreal logMin = std::log10(m_axis->min()) / std::log10(m_axis->base());
47 47 qreal leftEdge = logMin < logMax ? logMin : logMax;
48 48 qreal ceilEdge = qCeil(leftEdge);
49 49 int tickCount = qAbs(qCeil(logMax) - qCeil(logMin));
50 50
51 // If the high edge sits exactly on the tick value, add a tick
52 qreal highValue = logMin < logMax ? logMax : logMin;
53 if (qFuzzyCompare(highValue, qreal(qCeil(highValue))))
54 tickCount++;
55
51 56 points.resize(tickCount);
52 57 const QRectF &gridRect = gridGeometry();
53 58 const qreal deltaY = gridRect.height() / qAbs(logMax - logMin);
54 59 for (int i = 0; i < tickCount; ++i)
55 60 points[i] = (ceilEdge + qreal(i)) * -deltaY - leftEdge * -deltaY + gridRect.bottom();
56 61
57 62 return points;
58 63 }
59 64
60 65
61 66 void ChartLogValueAxisY::updateGeometry()
62 67 {
63 68 const QVector<qreal> &layout = ChartAxisElement::layout();
64 69 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
65 70 VerticalAxis::updateGeometry();
66 71 }
67 72
68 73 void ChartLogValueAxisY::handleBaseChanged(qreal base)
69 74 {
70 75 Q_UNUSED(base);
71 76 QGraphicsLayoutItem::updateGeometry();
72 77 if(presenter()) presenter()->layout()->invalidate();
73 78 }
74 79
75 80 void ChartLogValueAxisY::handleLabelFormatChanged(const QString &format)
76 81 {
77 82 Q_UNUSED(format);
78 83 QGraphicsLayoutItem::updateGeometry();
79 84 if(presenter()) presenter()->layout()->invalidate();
80 85 }
81 86
82 87 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
83 88 {
84 89 Q_UNUSED(constraint)
85 90
86 91 QSizeF sh;
87 92
88 93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
89 94 QStringList ticksList;
90 95 qreal logMax = std::log10(m_axis->max()) / std::log10(m_axis->base());
91 96 qreal logMin = std::log10(m_axis->min()) / std::log10(m_axis->base());
92 97 int tickCount = qAbs(qCeil(logMax) - qCeil(logMin));
98
99 // If the high edge sits exactly on the tick value, add a tick
100 qreal highValue = logMin < logMax ? logMax : logMin;
101 if (qFuzzyCompare(highValue, qreal(qCeil(highValue))))
102 tickCount++;
103
93 104 if (m_axis->max() > m_axis->min() && tickCount > 0)
94 105 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
95 106 else
96 107 ticksList.append(QStringLiteral(" "));
97 108 qreal width = 0;
98 109 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
99 110 // first and last ticks. Base height is irrelevant.
100 111 qreal height = 0;
101 112
102 113 switch (which) {
103 114 case Qt::MinimumSize: {
104 115 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(),
105 116 QStringLiteral("..."),
106 117 axis()->labelsAngle());
107 118 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
108 119 height = boundingRect.height() / 2.0;
109 120 sh = QSizeF(width, height);
110 121 break;
111 122 }
112 123 case Qt::PreferredSize: {
113 124 qreal labelWidth = 0.0;
114 125 qreal firstHeight = -1.0;
115 126 foreach (const QString& s, ticksList) {
116 127 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
117 128 labelWidth = qMax(rect.width(), labelWidth);
118 129 height = rect.height();
119 130 if (firstHeight < 0.0)
120 131 firstHeight = height;
121 132 }
122 133 width = labelWidth + labelPadding() + base.width() + 2.0; //two pixels of tolerance
123 134 height = qMax(height, firstHeight) / 2.0;
124 135 sh = QSizeF(width, height);
125 136 break;
126 137 }
127 138 default:
128 139 break;
129 140 }
130 141
131 142 return sh;
132 143 }
133 144
134 145 #include "moc_chartlogvalueaxisy_p.cpp"
135 146
136 147 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now