##// END OF EJS Templates
Fix label visibility for category axis...
Titta Heikkala -
r2651:99ad874661ac
parent child
Show More
@@ -1,224 +1,223
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 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 "horizontalaxis_p.h"
21 #include "horizontalaxis_p.h"
22 #include "qabstractaxis_p.h"
22 #include "qabstractaxis_p.h"
23 #include "chartpresenter_p.h"
23 #include "chartpresenter_p.h"
24 #include <qmath.h>
24 #include <qmath.h>
25 #include <QDebug>
25 #include <QDebug>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
29 HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
30 : CartesianChartAxis(axis, item, intervalAxis)
30 : CartesianChartAxis(axis, item, intervalAxis)
31 {
31 {
32 }
32 }
33
33
34 HorizontalAxis::~HorizontalAxis()
34 HorizontalAxis::~HorizontalAxis()
35 {
35 {
36 }
36 }
37
37
38 void HorizontalAxis::updateGeometry()
38 void HorizontalAxis::updateGeometry()
39 {
39 {
40 const QVector<qreal> &layout = ChartAxisElement::layout();
40 const QVector<qreal> &layout = ChartAxisElement::layout();
41
41
42 if (layout.isEmpty())
42 if (layout.isEmpty())
43 return;
43 return;
44
44
45 QStringList labelList = labels();
45 QStringList labelList = labels();
46
46
47 QList<QGraphicsItem *> lines = gridItems();
47 QList<QGraphicsItem *> lines = gridItems();
48 QList<QGraphicsItem *> labels = labelItems();
48 QList<QGraphicsItem *> labels = labelItems();
49 QList<QGraphicsItem *> shades = shadeItems();
49 QList<QGraphicsItem *> shades = shadeItems();
50 QList<QGraphicsItem *> arrow = arrowItems();
50 QList<QGraphicsItem *> arrow = arrowItems();
51 QGraphicsTextItem *title = titleItem();
51 QGraphicsTextItem *title = titleItem();
52
52
53 Q_ASSERT(labels.size() == labelList.size());
53 Q_ASSERT(labels.size() == labelList.size());
54 Q_ASSERT(layout.size() == labelList.size());
54 Q_ASSERT(layout.size() == labelList.size());
55
55
56 const QRectF &axisRect = axisGeometry();
56 const QRectF &axisRect = axisGeometry();
57 const QRectF &gridRect = gridGeometry();
57 const QRectF &gridRect = gridGeometry();
58
58
59 //arrow
59 //arrow
60 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem *>(arrow.at(0));
60 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem *>(arrow.at(0));
61
61
62 if (axis()->alignment() == Qt::AlignTop)
62 if (axis()->alignment() == Qt::AlignTop)
63 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
63 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
64 else if (axis()->alignment() == Qt::AlignBottom)
64 else if (axis()->alignment() == Qt::AlignBottom)
65 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
65 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
66
66
67 qreal width = 0;
67 qreal width = 0;
68
68
69 //title
69 //title
70 QRectF titleBoundingRect;
70 QRectF titleBoundingRect;
71 QString titleText = axis()->titleText();
71 QString titleText = axis()->titleText();
72 qreal availableSpace = axisRect.height() - labelPadding();
72 qreal availableSpace = axisRect.height() - labelPadding();
73 if (!titleText.isEmpty() && titleItem()->isVisible()) {
73 if (!titleText.isEmpty() && titleItem()->isVisible()) {
74 availableSpace -= titlePadding() * 2.0;
74 availableSpace -= titlePadding() * 2.0;
75 qreal minimumLabelHeight = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...").height();
75 qreal minimumLabelHeight = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...").height();
76 qreal titleSpace = availableSpace - minimumLabelHeight;
76 qreal titleSpace = availableSpace - minimumLabelHeight;
77 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(0.0),
77 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(0.0),
78 gridRect.width(), titleSpace,
78 gridRect.width(), titleSpace,
79 titleBoundingRect));
79 titleBoundingRect));
80 title->setTextWidth(titleBoundingRect.width());
80 title->setTextWidth(titleBoundingRect.width());
81
81
82 titleBoundingRect = title->boundingRect();
82 titleBoundingRect = title->boundingRect();
83
83
84 QPointF center = gridRect.center() - titleBoundingRect.center();
84 QPointF center = gridRect.center() - titleBoundingRect.center();
85 if (axis()->alignment() == Qt::AlignTop)
85 if (axis()->alignment() == Qt::AlignTop)
86 title->setPos(center.x(), axisRect.top() + titlePadding());
86 title->setPos(center.x(), axisRect.top() + titlePadding());
87 else if (axis()->alignment() == Qt::AlignBottom)
87 else if (axis()->alignment() == Qt::AlignBottom)
88 title->setPos(center.x(), axisRect.bottom() - titleBoundingRect.height() - titlePadding());
88 title->setPos(center.x(), axisRect.bottom() - titleBoundingRect.height() - titlePadding());
89
89
90 availableSpace -= titleBoundingRect.height();
90 availableSpace -= titleBoundingRect.height();
91 }
91 }
92
92
93 for (int i = 0; i < layout.size(); ++i) {
93 for (int i = 0; i < layout.size(); ++i) {
94 //items
94 //items
95 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
95 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
96 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(arrow.at(i + 1));
96 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(arrow.at(i + 1));
97 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i));
97 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i));
98
98
99 //grid line
99 //grid line
100 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
100 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
101
101
102 //label text wrapping
102 //label text wrapping
103 QString text = labelList.at(i);
103 QString text = labelList.at(i);
104 QRectF boundingRect;
104 QRectF boundingRect;
105 // don't truncate empty labels
105 // don't truncate empty labels
106 if (text.isEmpty()) {
106 if (text.isEmpty()) {
107 labelItem->setHtml(text);
107 labelItem->setHtml(text);
108 } else {
108 } else {
109 qreal labelWidth = axisRect.width() / layout.count() - (2 * labelPadding());
109 qreal labelWidth = axisRect.width() / layout.count() - (2 * labelPadding());
110 QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text,
110 QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text,
111 axis()->labelsAngle(),
111 axis()->labelsAngle(),
112 labelWidth,
112 labelWidth,
113 availableSpace, boundingRect);
113 availableSpace, boundingRect);
114 labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(),
114 labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(),
115 truncatedText).width());
115 truncatedText).width());
116 labelItem->setHtml(truncatedText);
116 labelItem->setHtml(truncatedText);
117 }
117 }
118
118
119 //label transformation origin point
119 //label transformation origin point
120 const QRectF& rect = labelItem->boundingRect();
120 const QRectF& rect = labelItem->boundingRect();
121 QPointF center = rect.center();
121 QPointF center = rect.center();
122 labelItem->setTransformOriginPoint(center.x(), center.y());
122 labelItem->setTransformOriginPoint(center.x(), center.y());
123 qreal heightDiff = rect.height() - boundingRect.height();
123 qreal heightDiff = rect.height() - boundingRect.height();
124 qreal widthDiff = rect.width() - boundingRect.width();
124 qreal widthDiff = rect.width() - boundingRect.width();
125
125
126 //ticks and label position
126 //ticks and label position
127 if (axis()->alignment() == Qt::AlignTop) {
127 if (axis()->alignment() == Qt::AlignTop) {
128 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() + (heightDiff / 2.0) - labelPadding());
128 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() + (heightDiff / 2.0) - labelPadding());
129 tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding());
129 tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding());
130 } else if (axis()->alignment() == Qt::AlignBottom) {
130 } else if (axis()->alignment() == Qt::AlignBottom) {
131 labelItem->setPos(layout[i] - center.x(), axisRect.top() - (heightDiff / 2.0) + labelPadding());
131 labelItem->setPos(layout[i] - center.x(), axisRect.top() - (heightDiff / 2.0) + labelPadding());
132 tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding());
132 tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding());
133 }
133 }
134
134
135 //label in between
135 //label in between
136 bool forceHide = false;
136 bool forceHide = false;
137 if (intervalAxis() && (i + 1) != layout.size()) {
137 if (intervalAxis() && (i + 1) != layout.size()) {
138 qreal leftBound = qMax(layout[i], gridRect.left());
138 qreal leftBound = qMax(layout[i], gridRect.left());
139 qreal rightBound = qMin(layout[i + 1], gridRect.right());
139 qreal rightBound = qMin(layout[i + 1], gridRect.right());
140 const qreal delta = rightBound - leftBound;
140 const qreal delta = rightBound - leftBound;
141 // Hide label in case visible part of the category at the grid edge is too narrow
141 // Hide label in case visible part of the category at the grid edge is too narrow
142 if (delta < boundingRect.width()
142 if (delta < boundingRect.width()
143 && (leftBound == gridRect.left() || rightBound == gridRect.right())
143 && (leftBound == gridRect.left() || rightBound == gridRect.right())
144 && !intervalAxis()) {
144 && !intervalAxis()) {
145 forceHide = true;
145 forceHide = true;
146 } else {
146 } else {
147 labelItem->setPos(leftBound + (delta / 2.0) - center.x(), labelItem->pos().y());
147 labelItem->setPos(leftBound + (delta / 2.0) - center.x(), labelItem->pos().y());
148 }
148 }
149 }
149 }
150 //label overlap detection - compensate one pixel for rounding errors
150 //label overlap detection - compensate one pixel for rounding errors
151 if ((labelItem->pos().x() < width || forceHide ||
151 if (labelItem->pos().x() < width || forceHide ||
152 (labelItem->pos().x() + (widthDiff / 2.0)) < (axisRect.left() - 1.0) ||
152 (labelItem->pos().x() + (widthDiff / 2.0)) < (axisRect.left() - 1.0) ||
153 (labelItem->pos().x() + (widthDiff / 2.0) - 1.0) > axisRect.right())
153 (labelItem->pos().x() + (widthDiff / 2.0) - 1.0) > axisRect.right()) {
154 && !intervalAxis()) {
155 labelItem->setVisible(false);
154 labelItem->setVisible(false);
156 } else {
155 } else {
157 labelItem->setVisible(true);
156 labelItem->setVisible(true);
158 width = boundingRect.width() + labelItem->pos().x();
157 width = boundingRect.width() + labelItem->pos().x();
159 }
158 }
160
159
161 //shades
160 //shades
162 if ((i + 1) % 2 && i > 1) {
161 if ((i + 1) % 2 && i > 1) {
163 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
162 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
164 qreal leftBound = qMax(layout[i - 1], gridRect.left());
163 qreal leftBound = qMax(layout[i - 1], gridRect.left());
165 qreal rightBound = qMin(layout[i], gridRect.right());
164 qreal rightBound = qMin(layout[i], gridRect.right());
166 rectItem->setRect(leftBound, gridRect.top(), rightBound - leftBound, gridRect.height());
165 rectItem->setRect(leftBound, gridRect.top(), rightBound - leftBound, gridRect.height());
167 if (rectItem->rect().width() <= 0.0)
166 if (rectItem->rect().width() <= 0.0)
168 rectItem->setVisible(false);
167 rectItem->setVisible(false);
169 else
168 else
170 rectItem->setVisible(true);
169 rectItem->setVisible(true);
171 }
170 }
172
171
173 // check if the grid line and the axis tick should be shown
172 // check if the grid line and the axis tick should be shown
174 qreal x = gridItem->line().p1().x();
173 qreal x = gridItem->line().p1().x();
175 if (x < gridRect.left() || x > gridRect.right()) {
174 if (x < gridRect.left() || x > gridRect.right()) {
176 gridItem->setVisible(false);
175 gridItem->setVisible(false);
177 tickItem->setVisible(false);
176 tickItem->setVisible(false);
178 } else {
177 } else {
179 gridItem->setVisible(true);
178 gridItem->setVisible(true);
180 tickItem->setVisible(true);
179 tickItem->setVisible(true);
181 }
180 }
182
181
183 }
182 }
184
183
185 //begin/end grid line in case labels between
184 //begin/end grid line in case labels between
186 if (intervalAxis()) {
185 if (intervalAxis()) {
187 QGraphicsLineItem *gridLine;
186 QGraphicsLineItem *gridLine;
188 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
187 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
189 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
188 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
190 gridLine->setVisible(true);
189 gridLine->setVisible(true);
191 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
190 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
192 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
191 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
193 gridLine->setVisible(true);
192 gridLine->setVisible(true);
194 }
193 }
195 }
194 }
196
195
197 QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
196 QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
198 {
197 {
199 Q_UNUSED(constraint);
198 Q_UNUSED(constraint);
200 QSizeF sh(0,0);
199 QSizeF sh(0,0);
201
200
202 if (axis()->titleText().isEmpty() || !titleItem()->isVisible())
201 if (axis()->titleText().isEmpty() || !titleItem()->isVisible())
203 return sh;
202 return sh;
204
203
205 switch (which) {
204 switch (which) {
206 case Qt::MinimumSize: {
205 case Qt::MinimumSize: {
207 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), "...");
206 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), "...");
208 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2.0));
207 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2.0));
209 break;
208 break;
210 }
209 }
211 case Qt::MaximumSize:
210 case Qt::MaximumSize:
212 case Qt::PreferredSize: {
211 case Qt::PreferredSize: {
213 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText());
212 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText());
214 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2.0));
213 sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2.0));
215 break;
214 break;
216 }
215 }
217 default:
216 default:
218 break;
217 break;
219 }
218 }
220
219
221 return sh;
220 return sh;
222 }
221 }
223
222
224 QTCOMMERCIALCHART_END_NAMESPACE
223 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,230 +1,229
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 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 "verticalaxis_p.h"
21 #include "verticalaxis_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include "chartpresenter_p.h"
23 #include "chartpresenter_p.h"
24 #include <QDebug>
24 #include <QDebug>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 VerticalAxis::VerticalAxis(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
28 VerticalAxis::VerticalAxis(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis)
29 : CartesianChartAxis(axis, item, intervalAxis)
29 : CartesianChartAxis(axis, item, intervalAxis)
30 {
30 {
31 }
31 }
32
32
33 VerticalAxis::~VerticalAxis()
33 VerticalAxis::~VerticalAxis()
34 {
34 {
35 }
35 }
36
36
37 void VerticalAxis::updateGeometry()
37 void VerticalAxis::updateGeometry()
38 {
38 {
39 const QVector<qreal> &layout = ChartAxisElement::layout();
39 const QVector<qreal> &layout = ChartAxisElement::layout();
40
40
41 if (layout.isEmpty())
41 if (layout.isEmpty())
42 return;
42 return;
43
43
44 QStringList labelList = labels();
44 QStringList labelList = labels();
45
45
46 QList<QGraphicsItem *> lines = gridItems();
46 QList<QGraphicsItem *> lines = gridItems();
47 QList<QGraphicsItem *> labels = labelItems();
47 QList<QGraphicsItem *> labels = labelItems();
48 QList<QGraphicsItem *> shades = shadeItems();
48 QList<QGraphicsItem *> shades = shadeItems();
49 QList<QGraphicsItem *> arrow = arrowItems();
49 QList<QGraphicsItem *> arrow = arrowItems();
50 QGraphicsTextItem *title = titleItem();
50 QGraphicsTextItem *title = titleItem();
51
51
52 Q_ASSERT(labels.size() == labelList.size());
52 Q_ASSERT(labels.size() == labelList.size());
53 Q_ASSERT(layout.size() == labelList.size());
53 Q_ASSERT(layout.size() == labelList.size());
54
54
55 const QRectF &axisRect = axisGeometry();
55 const QRectF &axisRect = axisGeometry();
56 const QRectF &gridRect = gridGeometry();
56 const QRectF &gridRect = gridGeometry();
57
57
58 qreal height = axisRect.bottom();
58 qreal height = axisRect.bottom();
59
59
60
60
61 //arrow
61 //arrow
62 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(arrow.at(0));
62 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(arrow.at(0));
63
63
64 //arrow position
64 //arrow position
65 if (axis()->alignment() == Qt::AlignLeft)
65 if (axis()->alignment() == Qt::AlignLeft)
66 arrowItem->setLine(axisRect.right(), gridRect.top(), axisRect.right(), gridRect.bottom());
66 arrowItem->setLine(axisRect.right(), gridRect.top(), axisRect.right(), gridRect.bottom());
67 else if (axis()->alignment() == Qt::AlignRight)
67 else if (axis()->alignment() == Qt::AlignRight)
68 arrowItem->setLine(axisRect.left(), gridRect.top(), axisRect.left(), gridRect.bottom());
68 arrowItem->setLine(axisRect.left(), gridRect.top(), axisRect.left(), gridRect.bottom());
69
69
70 //title
70 //title
71 QRectF titleBoundingRect;
71 QRectF titleBoundingRect;
72 QString titleText = axis()->titleText();
72 QString titleText = axis()->titleText();
73 qreal availableSpace = axisRect.width() - labelPadding();
73 qreal availableSpace = axisRect.width() - labelPadding();
74 if (!titleText.isEmpty() && titleItem()->isVisible()) {
74 if (!titleText.isEmpty() && titleItem()->isVisible()) {
75 availableSpace -= titlePadding() * 2.0;
75 availableSpace -= titlePadding() * 2.0;
76 qreal minimumLabelWidth = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...").width();
76 qreal minimumLabelWidth = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...").width();
77 qreal titleSpace = availableSpace - minimumLabelWidth;
77 qreal titleSpace = availableSpace - minimumLabelWidth;
78 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(90.0),
78 title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(90.0),
79 titleSpace, gridRect.height(),
79 titleSpace, gridRect.height(),
80 titleBoundingRect));
80 titleBoundingRect));
81 title->setTextWidth(titleBoundingRect.height());
81 title->setTextWidth(titleBoundingRect.height());
82
82
83 titleBoundingRect = title->boundingRect();
83 titleBoundingRect = title->boundingRect();
84
84
85 QPointF center = gridRect.center() - titleBoundingRect.center();
85 QPointF center = gridRect.center() - titleBoundingRect.center();
86 if (axis()->alignment() == Qt::AlignLeft)
86 if (axis()->alignment() == Qt::AlignLeft)
87 title->setPos(axisRect.left() - titleBoundingRect.width() / 2.0 + titleBoundingRect.height() / 2.0 + titlePadding(), center.y());
87 title->setPos(axisRect.left() - titleBoundingRect.width() / 2.0 + titleBoundingRect.height() / 2.0 + titlePadding(), center.y());
88 else if (axis()->alignment() == Qt::AlignRight)
88 else if (axis()->alignment() == Qt::AlignRight)
89 title->setPos(axisRect.right() - titleBoundingRect.width() / 2.0 - titleBoundingRect.height() / 2.0 - titlePadding(), center.y());
89 title->setPos(axisRect.right() - titleBoundingRect.width() / 2.0 - titleBoundingRect.height() / 2.0 - titlePadding(), center.y());
90
90
91 title->setTransformOriginPoint(titleBoundingRect.center());
91 title->setTransformOriginPoint(titleBoundingRect.center());
92 title->setRotation(270);
92 title->setRotation(270);
93
93
94 availableSpace -= titleBoundingRect.height();
94 availableSpace -= titleBoundingRect.height();
95 }
95 }
96
96
97 for (int i = 0; i < layout.size(); ++i) {
97 for (int i = 0; i < layout.size(); ++i) {
98 //items
98 //items
99 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
99 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
100 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(arrow.at(i + 1));
100 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(arrow.at(i + 1));
101 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i));
101 QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i));
102
102
103 //grid line
103 //grid line
104 gridItem->setLine(gridRect.left(), layout[i], gridRect.right(), layout[i]);
104 gridItem->setLine(gridRect.left(), layout[i], gridRect.right(), layout[i]);
105
105
106 //label text wrapping
106 //label text wrapping
107 QString text = labelList.at(i);
107 QString text = labelList.at(i);
108 QRectF boundingRect;
108 QRectF boundingRect;
109 // don't truncate empty labels
109 // don't truncate empty labels
110 if (text.isEmpty()) {
110 if (text.isEmpty()) {
111 labelItem->setHtml(text);
111 labelItem->setHtml(text);
112 } else {
112 } else {
113 qreal labelHeight = (axisRect.height() / layout.count()) - (2 * labelPadding());
113 qreal labelHeight = (axisRect.height() / layout.count()) - (2 * labelPadding());
114 QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text,
114 QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text,
115 axis()->labelsAngle(),
115 axis()->labelsAngle(),
116 availableSpace,
116 availableSpace,
117 labelHeight, boundingRect);
117 labelHeight, boundingRect);
118 labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(),
118 labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(),
119 truncatedText).width());
119 truncatedText).width());
120 labelItem->setHtml(truncatedText);
120 labelItem->setHtml(truncatedText);
121 }
121 }
122
122
123 //label transformation origin point
123 //label transformation origin point
124 const QRectF &rect = labelItem->boundingRect();
124 const QRectF &rect = labelItem->boundingRect();
125 QPointF center = rect.center();
125 QPointF center = rect.center();
126 labelItem->setTransformOriginPoint(center.x(), center.y());
126 labelItem->setTransformOriginPoint(center.x(), center.y());
127 qreal widthDiff = rect.width() - boundingRect.width();
127 qreal widthDiff = rect.width() - boundingRect.width();
128 qreal heightDiff = rect.height() - boundingRect.height();
128 qreal heightDiff = rect.height() - boundingRect.height();
129
129
130 //ticks and label position
130 //ticks and label position
131 if (axis()->alignment() == Qt::AlignLeft) {
131 if (axis()->alignment() == Qt::AlignLeft) {
132 labelItem->setPos(axisRect.right() - rect.width() + (widthDiff / 2.0) - labelPadding(), layout[i] - center.y());
132 labelItem->setPos(axisRect.right() - rect.width() + (widthDiff / 2.0) - labelPadding(), layout[i] - center.y());
133 tickItem->setLine(axisRect.right() - labelPadding(), layout[i], axisRect.right(), layout[i]);
133 tickItem->setLine(axisRect.right() - labelPadding(), layout[i], axisRect.right(), layout[i]);
134 } else if (axis()->alignment() == Qt::AlignRight) {
134 } else if (axis()->alignment() == Qt::AlignRight) {
135 labelItem->setPos(axisRect.left() + labelPadding() - (widthDiff / 2.0), layout[i] - center.y());
135 labelItem->setPos(axisRect.left() + labelPadding() - (widthDiff / 2.0), layout[i] - center.y());
136 tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]);
136 tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]);
137 }
137 }
138
138
139 //label in between
139 //label in between
140 bool forceHide = false;
140 bool forceHide = false;
141 if (intervalAxis() && (i + 1) != layout.size()) {
141 if (intervalAxis() && (i + 1) != layout.size()) {
142 qreal lowerBound = qMin(layout[i], gridRect.bottom());
142 qreal lowerBound = qMin(layout[i], gridRect.bottom());
143 qreal upperBound = qMax(layout[i + 1], gridRect.top());
143 qreal upperBound = qMax(layout[i + 1], gridRect.top());
144 const qreal delta = lowerBound - upperBound;
144 const qreal delta = lowerBound - upperBound;
145 // Hide label in case visible part of the category at the grid edge is too narrow
145 // Hide label in case visible part of the category at the grid edge is too narrow
146 if (delta < boundingRect.height()
146 if (delta < boundingRect.height()
147 && (lowerBound == gridRect.bottom() || upperBound == gridRect.top())
147 && (lowerBound == gridRect.bottom() || upperBound == gridRect.top())
148 && !intervalAxis()) {
148 && !intervalAxis()) {
149 forceHide = true;
149 forceHide = true;
150 } else {
150 } else {
151 labelItem->setPos(labelItem->pos().x() , lowerBound - (delta / 2.0) - center.y());
151 labelItem->setPos(labelItem->pos().x() , lowerBound - (delta / 2.0) - center.y());
152 }
152 }
153 }
153 }
154
154
155 //label overlap detection - compensate one pixel for rounding errors
155 //label overlap detection - compensate one pixel for rounding errors
156 if ((labelItem->pos().y() + boundingRect.height() > height || forceHide ||
156 if (labelItem->pos().y() + boundingRect.height() > height || forceHide ||
157 (labelItem->pos().y() + (heightDiff / 2.0) - 1.0) > axisRect.bottom() ||
157 (labelItem->pos().y() + (heightDiff / 2.0) - 1.0) > axisRect.bottom() ||
158 labelItem->pos().y() + (heightDiff / 2.0) < (axisRect.top() - 1.0))
158 labelItem->pos().y() + (heightDiff / 2.0) < (axisRect.top() - 1.0)) {
159 && !intervalAxis()) {
160 labelItem->setVisible(false);
159 labelItem->setVisible(false);
161 }
160 }
162 else {
161 else {
163 labelItem->setVisible(true);
162 labelItem->setVisible(true);
164 height=labelItem->pos().y();
163 height=labelItem->pos().y();
165 }
164 }
166
165
167 //shades
166 //shades
168 if ((i + 1) % 2 && i > 1) {
167 if ((i + 1) % 2 && i > 1) {
169 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
168 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
170 qreal lowerBound = qMin(layout[i - 1], gridRect.bottom());
169 qreal lowerBound = qMin(layout[i - 1], gridRect.bottom());
171 qreal upperBound = qMax(layout[i], gridRect.top());
170 qreal upperBound = qMax(layout[i], gridRect.top());
172 rectItem->setRect(gridRect.left(), upperBound, gridRect.width(), lowerBound - upperBound);
171 rectItem->setRect(gridRect.left(), upperBound, gridRect.width(), lowerBound - upperBound);
173 if (rectItem->rect().height() <= 0.0)
172 if (rectItem->rect().height() <= 0.0)
174 rectItem->setVisible(false);
173 rectItem->setVisible(false);
175 else
174 else
176 rectItem->setVisible(true);
175 rectItem->setVisible(true);
177 }
176 }
178
177
179 // check if the grid line and the axis tick should be shown
178 // check if the grid line and the axis tick should be shown
180 qreal y = gridItem->line().p1().y();
179 qreal y = gridItem->line().p1().y();
181 if ((y < gridRect.top() || y > gridRect.bottom()))
180 if ((y < gridRect.top() || y > gridRect.bottom()))
182 {
181 {
183 gridItem->setVisible(false);
182 gridItem->setVisible(false);
184 tickItem->setVisible(false);
183 tickItem->setVisible(false);
185 }else{
184 }else{
186 gridItem->setVisible(true);
185 gridItem->setVisible(true);
187 tickItem->setVisible(true);
186 tickItem->setVisible(true);
188 }
187 }
189
188
190 }
189 }
191 //begin/end grid line in case labels between
190 //begin/end grid line in case labels between
192 if (intervalAxis()) {
191 if (intervalAxis()) {
193 QGraphicsLineItem *gridLine;
192 QGraphicsLineItem *gridLine;
194 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
193 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
195 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
194 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
196 gridLine->setVisible(true);
195 gridLine->setVisible(true);
197 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size() + 1));
196 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size() + 1));
198 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
197 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
199 gridLine->setVisible(true);
198 gridLine->setVisible(true);
200 }
199 }
201 }
200 }
202
201
203 QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
202 QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
204 {
203 {
205 Q_UNUSED(constraint);
204 Q_UNUSED(constraint);
206 QSizeF sh(0, 0);
205 QSizeF sh(0, 0);
207
206
208 if (axis()->titleText().isEmpty() || !titleItem()->isVisible())
207 if (axis()->titleText().isEmpty() || !titleItem()->isVisible())
209 return sh;
208 return sh;
210
209
211 switch (which) {
210 switch (which) {
212 case Qt::MinimumSize: {
211 case Qt::MinimumSize: {
213 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), "...");
212 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), "...");
214 sh = QSizeF(titleRect.height() + (titlePadding() * 2.0), titleRect.width());
213 sh = QSizeF(titleRect.height() + (titlePadding() * 2.0), titleRect.width());
215 break;
214 break;
216 }
215 }
217 case Qt::MaximumSize:
216 case Qt::MaximumSize:
218 case Qt::PreferredSize: {
217 case Qt::PreferredSize: {
219 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText());
218 QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText());
220 sh = QSizeF(titleRect.height() + (titlePadding() * 2.0), titleRect.width());
219 sh = QSizeF(titleRect.height() + (titlePadding() * 2.0), titleRect.width());
221 break;
220 break;
222 }
221 }
223 default:
222 default:
224 break;
223 break;
225 }
224 }
226
225
227 return sh;
226 return sh;
228 }
227 }
229
228
230 QTCOMMERCIALCHART_END_NAMESPACE
229 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now