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