##// END OF EJS Templates
QLogValueAxis number of labels calculation fixed
Marek Rosa -
r2370:6a9743cf4397
parent child
Show More
@@ -1,137 +1,135
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartlogvalueaxisx_p.h"
21 #include "chartlogvalueaxisx_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include "chartlayout_p.h"
24 #include "chartlayout_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <qmath.h>
27 #include <qmath.h>
28 #include <QDebug>
28 #include <QDebug>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item)
32 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item)
33 : HorizontalAxis(axis, item),
33 : HorizontalAxis(axis, item),
34 m_axis(axis)
34 m_axis(axis)
35 {
35 {
36 QObject::connect(m_axis,SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
36 QObject::connect(m_axis,SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
37 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
37 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
38 }
38 }
39
39
40 ChartLogValueAxisX::~ChartLogValueAxisX()
40 ChartLogValueAxisX::~ChartLogValueAxisX()
41 {
41 {
42 }
42 }
43
43
44 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
44 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
45 {
45 {
46 QVector<qreal> points;
46 QVector<qreal> points;
47
47
48 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
48 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
49 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
49 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
50 qreal leftEdge = logMin < logMax ? logMin : logMax;
50 qreal leftEdge = logMin < logMax ? logMin : logMax;
51 qreal ceilEdge = ceil(leftEdge);
51 qreal ceilEdge = ceil(leftEdge);
52 int tickCount = qAbs(qRound(logMax - logMin));
52 int tickCount = qAbs(qRound(logMax - logMin));
53 tickCount++;
53 tickCount++;
54
54
55 points.resize(tickCount);
55 points.resize(tickCount);
56 const QRectF &gridRect = gridGeometry();
56 const QRectF &gridRect = gridGeometry();
57 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
57 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
58 for (int i = 0; i < tickCount; ++i)
58 for (int i = 0; i < tickCount; ++i)
59 points[i] = (ceilEdge + i) * deltaX - leftEdge * deltaX + gridRect.left();
59 points[i] = (ceilEdge + i) * deltaX - leftEdge * deltaX + gridRect.left();
60
60
61 return points;
61 return points;
62 }
62 }
63
63
64 void ChartLogValueAxisX::updateGeometry()
64 void ChartLogValueAxisX::updateGeometry()
65 {
65 {
66 const QVector<qreal>& layout = ChartAxis::layout();
66 const QVector<qreal>& layout = ChartAxis::layout();
67 if (layout.isEmpty())
67 if (layout.isEmpty())
68 return;
68 return;
69 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()));
70 HorizontalAxis::updateGeometry();
70 HorizontalAxis::updateGeometry();
71 }
71 }
72
72
73 void ChartLogValueAxisX::handleBaseChanged(qreal base)
73 void ChartLogValueAxisX::handleBaseChanged(qreal base)
74 {
74 {
75 Q_UNUSED(base);
75 Q_UNUSED(base);
76 QGraphicsLayoutItem::updateGeometry();
76 QGraphicsLayoutItem::updateGeometry();
77 if(presenter()) presenter()->layout()->invalidate();
77 if(presenter()) presenter()->layout()->invalidate();
78 }
78 }
79
79
80 void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format)
80 void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format)
81 {
81 {
82 Q_UNUSED(format);
82 Q_UNUSED(format);
83 QGraphicsLayoutItem::updateGeometry();
83 QGraphicsLayoutItem::updateGeometry();
84 if(presenter()) presenter()->layout()->invalidate();
84 if(presenter()) presenter()->layout()->invalidate();
85 }
85 }
86
86
87 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
87 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
88 {
88 {
89 Q_UNUSED(constraint)
89 Q_UNUSED(constraint)
90
90
91 QFontMetrics fn(font());
91 QFontMetrics fn(font());
92 QSizeF sh;
92 QSizeF sh;
93
93
94 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
94 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
95 QStringList ticksList;
95 QStringList ticksList;
96 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
96 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
97 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
97 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
98 int tickCount = qAbs(qRound(logMax - logMin));
98 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
99 tickCount++;
100
101 if (m_axis->max() > m_axis->min() && tickCount > 1)
99 if (m_axis->max() > m_axis->min() && tickCount > 1)
102 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
100 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
103 else
101 else
104 ticksList.append(QString(" "));
102 ticksList.append(QString(" "));
105 qreal width = 0;
103 qreal width = 0;
106 qreal height = 0;
104 qreal height = 0;
107
105
108
106
109 switch (which) {
107 switch (which) {
110 case Qt::MinimumSize:{
108 case Qt::MinimumSize:{
111 int count = qMax(ticksList.last().count(),ticksList.first().count());
109 int count = qMax(ticksList.last().count(),ticksList.first().count());
112 width = fn.averageCharWidth() * count;
110 width = fn.averageCharWidth() * count;
113 height = fn.height() + labelPadding();
111 height = fn.height() + labelPadding();
114 width = qMax(width,base.width());
112 width = qMax(width,base.width());
115 height += base.height();
113 height += base.height();
116 sh = QSizeF(width,height);
114 sh = QSizeF(width,height);
117 break;
115 break;
118 }
116 }
119 case Qt::PreferredSize: {
117 case Qt::PreferredSize: {
120 int count = qMax(ticksList.last().count(),ticksList.first().count());
118 int count = qMax(ticksList.last().count(),ticksList.first().count());
121 width=fn.averageCharWidth() * count;
119 width=fn.averageCharWidth() * count;
122 height=fn.height()+labelPadding();
120 height=fn.height()+labelPadding();
123 width=qMax(width,base.width());
121 width=qMax(width,base.width());
124 height+=base.height();
122 height+=base.height();
125 sh = QSizeF(width,height);
123 sh = QSizeF(width,height);
126 break;
124 break;
127 }
125 }
128 default:
126 default:
129 break;
127 break;
130 }
128 }
131
129
132 return sh;
130 return sh;
133 }
131 }
134
132
135 #include "moc_chartlogvalueaxisx_p.cpp"
133 #include "moc_chartlogvalueaxisx_p.cpp"
136
134
137 QTCOMMERCIALCHART_END_NAMESPACE
135 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,133 +1,135
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartlogvalueaxisy_p.h"
21 #include "chartlogvalueaxisy_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include "chartlayout_p.h"
24 #include "chartlayout_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <qmath.h>
27 #include <qmath.h>
28 #include <QDebug>
28
29
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
31
31 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item)
32 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item)
32 : VerticalAxis(axis, item),
33 : VerticalAxis(axis, item),
33 m_axis(axis)
34 m_axis(axis)
34 {
35 {
35 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
36 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
36 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
37 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
37 }
38 }
38
39
39 ChartLogValueAxisY::~ChartLogValueAxisY()
40 ChartLogValueAxisY::~ChartLogValueAxisY()
40 {
41 {
41 }
42 }
42
43
43 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
44 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
44 {
45 {
45 QVector<qreal> points;
46 QVector<qreal> points;
46 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
47 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
47 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
48 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
48 qreal leftEdge = logMin < logMax ? logMin : logMax;
49 qreal leftEdge = logMin < logMax ? logMin : logMax;
49 qreal ceilEdge = ceil(leftEdge);
50 qreal ceilEdge = ceil(leftEdge);
50 int tickCount = qAbs(qRound(logMax - logMin));
51 int tickCount = qAbs(qRound(logMax - logMin));
51 tickCount++;
52 tickCount++;
52
53
53 points.resize(tickCount);
54 points.resize(tickCount);
54 const QRectF &gridRect = gridGeometry();
55 const QRectF &gridRect = gridGeometry();
55 const qreal deltaY = gridRect.height() / qAbs(logMax - logMin);
56 const qreal deltaY = gridRect.height() / qAbs(logMax - logMin);
56 for (int i = 0; i < tickCount; ++i)
57 for (int i = 0; i < tickCount; ++i)
57 points[i] = (ceilEdge + i) * -deltaY - leftEdge * -deltaY + gridRect.bottom();
58 points[i] = (ceilEdge + i) * -deltaY - leftEdge * -deltaY + gridRect.bottom();
58
59
59 return points;
60 return points;
60 }
61 }
61
62
62
63
63 void ChartLogValueAxisY::updateGeometry()
64 void ChartLogValueAxisY::updateGeometry()
64 {
65 {
65 const QVector<qreal> &layout = ChartAxis::layout();
66 const QVector<qreal> &layout = ChartAxis::layout();
66 if (layout.isEmpty())
67 if (layout.isEmpty())
67 return;
68 return;
68 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()));
69 VerticalAxis::updateGeometry();
70 VerticalAxis::updateGeometry();
70 }
71 }
71
72
72 void ChartLogValueAxisY::handleBaseChanged(qreal base)
73 void ChartLogValueAxisY::handleBaseChanged(qreal base)
73 {
74 {
74 Q_UNUSED(base);
75 Q_UNUSED(base);
75 QGraphicsLayoutItem::updateGeometry();
76 QGraphicsLayoutItem::updateGeometry();
76 if(presenter()) presenter()->layout()->invalidate();
77 if(presenter()) presenter()->layout()->invalidate();
77 }
78 }
78
79
79 void ChartLogValueAxisY::handleLabelFormatChanged(const QString &format)
80 void ChartLogValueAxisY::handleLabelFormatChanged(const QString &format)
80 {
81 {
81 Q_UNUSED(format);
82 Q_UNUSED(format);
82 QGraphicsLayoutItem::updateGeometry();
83 QGraphicsLayoutItem::updateGeometry();
83 if(presenter()) presenter()->layout()->invalidate();
84 if(presenter()) presenter()->layout()->invalidate();
84 }
85 }
85
86
86 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
87 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
87 {
88 {
88 Q_UNUSED(constraint)
89 Q_UNUSED(constraint)
89
90
90 QFontMetrics fn(font());
91 QFontMetrics fn(font());
91 QSizeF sh;
92 QSizeF sh;
92
93
93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
94 QSizeF base = VerticalAxis::sizeHint(which, constraint);
94 QStringList ticksList;
95 QStringList ticksList;
95 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
96 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
96 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
97 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
97 int tickCount = qAbs(qRound(logMax - logMin));
98 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
98 tickCount++;
99 if (m_axis->max() > m_axis->min() && tickCount > 1)
99 if (m_axis->max() > m_axis->min() && tickCount > 1)
100 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
100 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
101 else
101 else
102 ticksList.append(QString(" "));
102 ticksList.append(QString(" "));
103 qDebug() << ticksList;
104 qDebug() << tickCount;
103 qreal width = 0;
105 qreal width = 0;
104 qreal height = 0;
106 qreal height = 0;
105
107
106 switch (which) {
108 switch (which) {
107 case Qt::MinimumSize: {
109 case Qt::MinimumSize: {
108 width = fn.boundingRect("...").width() + labelPadding();
110 width = fn.boundingRect("...").width() + labelPadding();
109 width += base.width();
111 width += base.width();
110 height = fn.height();
112 height = fn.height();
111 height = qMax(height,base.height());
113 height = qMax(height,base.height());
112 sh = QSizeF(width,height);
114 sh = QSizeF(width,height);
113 break;
115 break;
114 }
116 }
115 case Qt::PreferredSize: {
117 case Qt::PreferredSize: {
116 int count = qMax(ticksList.first().count() , ticksList.last().count());
118 int count = qMax(ticksList.first().count(), ticksList.last().count());
117 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
119 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
118 width += base.width();
120 width += base.width();
119 height = fn.height() * ticksList.count();
121 height = fn.height() * ticksList.count();
120 height = qMax(height,base.height());
122 height = qMax(height,base.height());
121 sh = QSizeF(width,height);
123 sh = QSizeF(width,height);
122 break;
124 break;
123 }
125 }
124 default:
126 default:
125 break;
127 break;
126 }
128 }
127
129
128 return sh;
130 return sh;
129 }
131 }
130
132
131 #include "moc_chartlogvalueaxisy_p.cpp"
133 #include "moc_chartlogvalueaxisy_p.cpp"
132
134
133 QTCOMMERCIALCHART_END_NAMESPACE
135 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now