##// END OF EJS Templates
Fix bar category axis labels...
Titta Heikkala -
r2720:d65072475355
parent child
Show More
@@ -1,140 +1,140
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2014 Digia Plc
3 ** Copyright (C) 2014 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 Enterprise Charts Add-on.
7 ** This file is part of the Qt Enterprise Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 ** accordance with the Qt Enterprise License Agreement provided with the
11 ** accordance with the Qt Enterprise 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 <private/chartbarcategoryaxisx_p.h>
21 #include <private/chartbarcategoryaxisx_p.h>
22 #include <private/chartpresenter_p.h>
22 #include <private/chartpresenter_p.h>
23 #include <private/qbarcategoryaxis_p.h>
23 #include <private/qbarcategoryaxis_p.h>
24 #include <private/abstractchartlayout_p.h>
24 #include <private/abstractchartlayout_p.h>
25 #include <QtCore/QDebug>
25 #include <QtCore/QDebug>
26 #include <QtCore/QtMath>
26 #include <QtCore/QtMath>
27
27
28 QT_CHARTS_BEGIN_NAMESPACE
28 QT_CHARTS_BEGIN_NAMESPACE
29
29
30 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, QGraphicsItem* item)
30 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, QGraphicsItem* item)
31 : HorizontalAxis(axis, item, true),
31 : HorizontalAxis(axis, item, true),
32 m_categoriesAxis(axis)
32 m_categoriesAxis(axis)
33 {
33 {
34 QObject::connect(m_categoriesAxis,SIGNAL(categoriesChanged()),this, SLOT(handleCategoriesChanged()));
34 QObject::connect(m_categoriesAxis,SIGNAL(categoriesChanged()),this, SLOT(handleCategoriesChanged()));
35 handleCategoriesChanged();
35 handleCategoriesChanged();
36 }
36 }
37
37
38 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
38 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
39 {
39 {
40 }
40 }
41
41
42 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
42 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
43 {
43 {
44 QVector<qreal> points;
44 QVector<qreal> points;
45 const QRectF& gridRect = gridGeometry();
45 const QRectF& gridRect = gridGeometry();
46 qreal range = max() - min();
46 qreal range = max() - min();
47 const qreal delta = gridRect.width() / range;
47 const qreal delta = gridRect.width() / range;
48
48
49 if (delta < 2)
49 if (delta < 2)
50 return points;
50 return points;
51
51
52 qreal adjustedMin = min() + 0.5;
52 qreal adjustedMin = min() + 0.5;
53 qreal offset = (ceil(adjustedMin) - adjustedMin) * delta;
53 qreal offset = (ceil(adjustedMin) - adjustedMin) * delta;
54
54
55 int count = qFloor(range);
55 int count = qFloor(range);
56 if (count < 1)
56 if (count < 1)
57 return points;
57 return points;
58
58
59 points.resize(count + 2);
59 points.resize(count + 2);
60
60
61 for (int i = 0; i < count + 2; ++i)
61 for (int i = 0; i < count + 2; ++i)
62 points[i] = offset + (qreal(i) * delta) + gridRect.left();
62 points[i] = offset + (qreal(i) * delta) + gridRect.left();
63
63
64 return points;
64 return points;
65 }
65 }
66
66
67 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
67 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
68 {
68 {
69 QStringList result ;
69 QStringList result ;
70 const QRectF &gridRect = gridGeometry();
70 const QRectF &gridRect = gridGeometry();
71 qreal d = (max() - min()) / gridRect.width();
71 qreal d = (max() - min()) / gridRect.width();
72
72
73 for (int i = 0; i < layout.count() - 1; ++i) {
73 for (int i = 0; i < layout.count() - 1; ++i) {
74 qreal x = qFloor((((layout[i] + layout[i + 1]) / 2 - gridRect.left()) * d + min() + 0.5));
74 qreal x = qFloor((((layout[i] + layout[i + 1]) / 2 - gridRect.left()) * d + min() + 0.5));
75 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
75 if (x < max() && (x >= 0)) {
76 result << m_categoriesAxis->categories().at(x);
76 result << m_categoriesAxis->categories().at(x);
77 } else {
77 } else {
78 // No label for x coordinate
78 // No label for x coordinate
79 result << QString();
79 result << QString();
80 }
80 }
81 }
81 }
82 result << QString();
82 result << QString();
83 return result;
83 return result;
84 }
84 }
85
85
86
86
87 void ChartBarCategoryAxisX::updateGeometry()
87 void ChartBarCategoryAxisX::updateGeometry()
88 {
88 {
89 const QVector<qreal>& layout = ChartAxisElement::layout();
89 const QVector<qreal>& layout = ChartAxisElement::layout();
90 if (layout.isEmpty())
90 if (layout.isEmpty())
91 return;
91 return;
92 setLabels(createCategoryLabels(layout));
92 setLabels(createCategoryLabels(layout));
93 HorizontalAxis::updateGeometry();
93 HorizontalAxis::updateGeometry();
94 }
94 }
95
95
96 void ChartBarCategoryAxisX::handleCategoriesChanged()
96 void ChartBarCategoryAxisX::handleCategoriesChanged()
97 {
97 {
98 QGraphicsLayoutItem::updateGeometry();
98 QGraphicsLayoutItem::updateGeometry();
99 if(presenter()) presenter()->layout()->invalidate();
99 if(presenter()) presenter()->layout()->invalidate();
100 }
100 }
101
101
102 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
102 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
103 {
103 {
104 Q_UNUSED(constraint)
104 Q_UNUSED(constraint)
105
105
106 QSizeF sh;
106 QSizeF sh;
107 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
107 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
108 QStringList ticksList = m_categoriesAxis->categories();
108 QStringList ticksList = m_categoriesAxis->categories();
109
109
110 qreal width = 0; // Width is irrelevant for X axes with interval labels
110 qreal width = 0; // Width is irrelevant for X axes with interval labels
111 qreal height = 0;
111 qreal height = 0;
112
112
113 switch (which) {
113 switch (which) {
114 case Qt::MinimumSize: {
114 case Qt::MinimumSize: {
115 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(),
115 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(),
116 QStringLiteral("..."),
116 QStringLiteral("..."),
117 axis()->labelsAngle());
117 axis()->labelsAngle());
118 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
118 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
119 sh = QSizeF(width, height);
119 sh = QSizeF(width, height);
120 break;
120 break;
121 }
121 }
122 case Qt::PreferredSize:{
122 case Qt::PreferredSize:{
123 qreal labelHeight = 0.0;
123 qreal labelHeight = 0.0;
124 foreach (const QString& s, ticksList) {
124 foreach (const QString& s, ticksList) {
125 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
125 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
126 labelHeight = qMax(rect.height(), labelHeight);
126 labelHeight = qMax(rect.height(), labelHeight);
127 }
127 }
128 height = labelHeight + labelPadding() + base.height() + 1.0;
128 height = labelHeight + labelPadding() + base.height() + 1.0;
129 sh = QSizeF(width, height);
129 sh = QSizeF(width, height);
130 break;
130 break;
131 }
131 }
132 default:
132 default:
133 break;
133 break;
134 }
134 }
135 return sh;
135 return sh;
136 }
136 }
137
137
138 #include "moc_chartbarcategoryaxisx_p.cpp"
138 #include "moc_chartbarcategoryaxisx_p.cpp"
139
139
140 QT_CHARTS_END_NAMESPACE
140 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now