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