@@ -1,146 +1,146 | |||||
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 "chartbarcategoryaxisy_p.h" |
|
21 | #include "chartbarcategoryaxisy_p.h" | |
22 | #include "chartpresenter_p.h" |
|
22 | #include "chartpresenter_p.h" | |
23 | #include "qbarcategoryaxis_p.h" |
|
23 | #include "qbarcategoryaxis_p.h" | |
24 | #include <qmath.h> |
|
24 | #include <qmath.h> | |
25 | #include <QFontMetrics> |
|
25 | #include <QFontMetrics> | |
26 | #include <QDebug> |
|
26 | #include <QDebug> | |
27 |
|
27 | |||
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
29 |
|
29 | |||
30 | ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter) |
|
30 | ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter) | |
31 | : VerticalAxis(axis, presenter, true), |
|
31 | : VerticalAxis(axis, presenter, true), | |
32 | m_categoriesAxis(axis) |
|
32 | m_categoriesAxis(axis) | |
33 | { |
|
33 | { | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 | ChartBarCategoryAxisY::~ChartBarCategoryAxisY() |
|
36 | ChartBarCategoryAxisY::~ChartBarCategoryAxisY() | |
37 | { |
|
37 | { | |
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const |
|
40 | QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const | |
41 | { |
|
41 | { | |
42 | int count = m_categoriesAxis->d_ptr->count(); |
|
42 | int count = m_categoriesAxis->d_ptr->count(); | |
43 |
|
43 | |||
44 | Q_ASSERT(count >= 1); |
|
44 | Q_ASSERT(count >= 1); | |
45 |
|
45 | |||
46 | QVector<qreal> points; |
|
46 | QVector<qreal> points; | |
47 | points.resize(count + 2); |
|
47 | points.resize(count + 2); | |
48 |
|
48 | |||
49 | const QRectF &gridRect = gridGeometry(); |
|
49 | const QRectF &gridRect = gridGeometry(); | |
50 |
|
50 | |||
51 | const qreal delta = gridRect.height() / (count); |
|
51 | const qreal delta = gridRect.height() / (count); | |
52 | qreal offset = - min() - 0.5; |
|
52 | qreal offset = - min() - 0.5; | |
53 |
|
53 | |||
54 | if (delta < 1) |
|
54 | if (delta < 1) | |
55 | return points; |
|
55 | return points; | |
56 |
|
56 | |||
57 | if (offset < 0) |
|
57 | if (offset < 0) | |
58 | offset = int(offset * gridRect.height() / (max() - min())) % int(delta) + delta; |
|
58 | offset = int(offset * gridRect.height() / (max() - min())) % int(delta) + delta; | |
59 | else |
|
59 | else | |
60 | offset = int(offset * gridRect.height() / (max() - min())) % int(delta); |
|
60 | offset = int(offset * gridRect.height() / (max() - min())) % int(delta); | |
61 |
|
61 | |||
62 | for (int i = -1; i < count + 1; ++i) { |
|
62 | for (int i = -1; i < count + 1; ++i) { | |
63 | int y = gridRect.bottom() - i * delta - offset; |
|
63 | int y = gridRect.bottom() - i * delta - offset; | |
64 | points[i + 1] = y; |
|
64 | points[i + 1] = y; | |
65 | } |
|
65 | } | |
66 | return points; |
|
66 | return points; | |
67 | } |
|
67 | } | |
68 |
|
68 | |||
69 | QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const |
|
69 | QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const | |
70 | { |
|
70 | { | |
71 | QStringList result; |
|
71 | QStringList result; | |
72 | const QRectF &gridRect = gridGeometry(); |
|
72 | const QRectF &gridRect = gridGeometry(); | |
73 | qreal d = (max() - min()) / gridRect.height(); |
|
73 | qreal d = (max() - min()) / gridRect.height(); | |
74 | for (int i = 0; i < layout.count() - 1; ++i) { |
|
74 | for (int i = 0; i < layout.count() - 1; ++i) { | |
75 | qreal x = qFloor(((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5)); |
|
75 | qreal x = qFloor(((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5)); | |
76 | if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { |
|
76 | if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { | |
77 | result << m_categoriesAxis->categories().at(x); |
|
77 | result << m_categoriesAxis->categories().at(x); | |
78 | } else { |
|
78 | } else { | |
79 | // No label for x coordinate |
|
79 | // No label for x coordinate | |
80 | result << ""; |
|
80 | result << ""; | |
81 | } |
|
81 | } | |
82 | } |
|
82 | } | |
83 | result << ""; |
|
83 | result << ""; | |
84 | return result; |
|
84 | return result; | |
85 | } |
|
85 | } | |
86 |
|
86 | |||
87 | void ChartBarCategoryAxisY::updateGeometry() |
|
87 | void ChartBarCategoryAxisY::updateGeometry() | |
88 | { |
|
88 | { | |
89 | const QVector<qreal>& layout = ChartAxis::layout(); |
|
89 | const QVector<qreal>& layout = ChartAxis::layout(); | |
90 | if (layout.isEmpty()) |
|
90 | if (layout.isEmpty()) | |
91 | return; |
|
91 | return; | |
92 | setLabels(createCategoryLabels(layout)); |
|
92 | setLabels(createCategoryLabels(layout)); | |
93 | VerticalAxis::updateGeometry(); |
|
93 | VerticalAxis::updateGeometry(); | |
94 | } |
|
94 | } | |
95 |
|
95 | |||
96 | void ChartBarCategoryAxisY::handleAxisUpdated() |
|
96 | void ChartBarCategoryAxisY::handleAxisUpdated() | |
97 | { |
|
97 | { | |
98 |
|
98 | |||
99 | if (m_categoriesAxis->categories() != m_categories) { |
|
99 | if (m_categoriesAxis->categories() != m_categories) { | |
100 | m_categories = m_categoriesAxis->categories(); |
|
100 | m_categories = m_categoriesAxis->categories(); | |
101 | if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2) |
|
101 | if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2) | |
102 | updateGeometry(); |
|
102 | updateGeometry(); | |
103 | } |
|
103 | } | |
104 | ChartAxis::handleAxisUpdated(); |
|
104 | ChartAxis::handleAxisUpdated(); | |
105 | } |
|
105 | } | |
106 |
|
106 | |||
107 | QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
107 | QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | |
108 | { |
|
108 | { | |
109 | Q_UNUSED(constraint) |
|
109 | Q_UNUSED(constraint) | |
110 |
|
110 | |||
111 | QFontMetrics fn(font()); |
|
111 | QFontMetrics fn(font()); | |
112 | QSizeF sh; |
|
112 | QSizeF sh; | |
113 | QSizeF base = ChartAxis::sizeHint(which, constraint); |
|
113 | QSizeF base = ChartAxis::sizeHint(which, constraint); | |
114 | QStringList ticksList = createCategoryLabels(ChartAxis::layout()); |
|
114 | QStringList ticksList = createCategoryLabels(ChartAxis::layout()); | |
115 |
|
115 | |||
116 | qreal width=0; |
|
116 | qreal width=0; | |
117 | qreal height=0; |
|
117 | qreal height=0; | |
118 |
|
118 | |||
119 | switch (which) { |
|
119 | switch (which) { | |
120 | case Qt::MinimumSize: |
|
120 | case Qt::MinimumSize: | |
121 | width = fn.boundingRect("...").width() + labelPadding(); |
|
121 | width = fn.boundingRect("...").width() + labelPadding(); | |
122 | height = fn.height(); |
|
122 | height = fn.height(); | |
123 | width+=base.width(); |
|
123 | width+=base.width(); | |
124 | height=qMax(height,base.height()); |
|
124 | height=qMax(height,base.height()); | |
125 | sh = QSizeF(width,height); |
|
125 | sh = QSizeF(width,height); | |
126 | break; |
|
126 | break; | |
127 | case Qt::PreferredSize:{ |
|
127 | case Qt::PreferredSize:{ | |
128 |
|
128 | |||
129 | for (int i = 0; i < ticksList.size(); ++i) |
|
129 | for (int i = 0; i < ticksList.size(); ++i) | |
130 | { |
|
130 | { | |
131 | QRectF rect = fn.boundingRect(ticksList.at(i)); |
|
131 | QRectF rect = fn.boundingRect(ticksList.at(i)); | |
132 | height+=rect.height(); |
|
132 | height+=rect.height(); | |
133 | width=qMax(rect.width()+labelPadding() +1 ,width); //one pixel torelance |
|
133 | width=qMax(rect.width()+labelPadding() + 1 ,width); //one pixel torelance | |
134 | } |
|
134 | } | |
135 | height=qMax(height,base.height()); |
|
135 | height=qMax(height,base.height()); | |
136 | width+=base.width(); |
|
136 | width+=base.width(); | |
137 | sh = QSizeF(width,height); |
|
137 | sh = QSizeF(width,height); | |
138 | break; |
|
138 | break; | |
139 | } |
|
139 | } | |
140 | default: |
|
140 | default: | |
141 | break; |
|
141 | break; | |
142 | } |
|
142 | } | |
143 | return sh; |
|
143 | return sh; | |
144 | } |
|
144 | } | |
145 |
|
145 | |||
146 | QTCOMMERCIALCHART_END_NAMESPACE |
|
146 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,118 +1,119 | |||||
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 "chartcategoryaxisx_p.h" |
|
21 | #include "chartcategoryaxisx_p.h" | |
22 | #include "qcategoryaxis.h" |
|
22 | #include "qcategoryaxis.h" | |
23 | #include "qabstractaxis.h" |
|
23 | #include "qabstractaxis.h" | |
24 | #include "chartpresenter_p.h" |
|
24 | #include "chartpresenter_p.h" | |
25 | #include <QGraphicsLayout> |
|
25 | #include <QGraphicsLayout> | |
26 | #include <QFontMetrics> |
|
26 | #include <QFontMetrics> | |
27 | #include <qmath.h> |
|
27 | #include <qmath.h> | |
28 |
|
28 | |||
29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
30 |
|
30 | |||
31 | ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis, ChartPresenter *presenter) |
|
31 | ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis, ChartPresenter *presenter) | |
32 | : HorizontalAxis(axis, presenter, true), |
|
32 | : HorizontalAxis(axis, presenter, true), | |
33 | m_axis(axis) |
|
33 | m_axis(axis) | |
34 | { |
|
34 | { | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | ChartCategoryAxisX::~ChartCategoryAxisX() |
|
37 | ChartCategoryAxisX::~ChartCategoryAxisX() | |
38 | { |
|
38 | { | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | QVector<qreal> ChartCategoryAxisX::calculateLayout() const |
|
41 | QVector<qreal> ChartCategoryAxisX::calculateLayout() const | |
42 | { |
|
42 | { | |
43 | int tickCount = m_axis->categoriesLabels().count() + 1; |
|
43 | int tickCount = m_axis->categoriesLabels().count() + 1; | |
44 | QVector<qreal> points; |
|
44 | QVector<qreal> points; | |
45 |
|
45 | |||
46 | if (tickCount < 2) |
|
46 | if (tickCount < 2) | |
47 | return points; |
|
47 | return points; | |
48 |
|
48 | |||
49 | const QRectF &gridRect = gridGeometry(); |
|
49 | const QRectF &gridRect = gridGeometry(); | |
50 |
qreal range = |
|
50 | qreal range = max() - min(); | |
51 | if (range > 0) { |
|
51 | if (range > 0) { | |
52 | points.resize(tickCount); |
|
52 | points.resize(tickCount); | |
53 | qreal scale = gridRect.width() / range; |
|
53 | qreal scale = gridRect.width() / range; | |
54 | for (int i = 0; i < tickCount; ++i) { |
|
54 | for (int i = 0; i < tickCount; ++i) { | |
55 | if (i < tickCount - 1) { |
|
55 | if (i < tickCount - 1) { | |
56 |
int x = (m_axis->startValue(m_axis->categoriesLabels().at(i)) - |
|
56 | int x = (m_axis->startValue(m_axis->categoriesLabels().at(i)) - min()) * scale + gridRect.left(); | |
57 | points[i] = x; |
|
57 | points[i] = x; | |
58 | } else { |
|
58 | } else { | |
59 |
int x = (m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - |
|
59 | int x = (m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - min()) * scale + gridRect.left(); | |
60 | points[i] = x; |
|
60 | points[i] = x; | |
61 | } |
|
61 | } | |
62 | } |
|
62 | } | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | return points; |
|
65 | return points; | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | void ChartCategoryAxisX::updateGeometry() |
|
68 | void ChartCategoryAxisX::updateGeometry() | |
69 | { |
|
69 | { | |
|
70 | //TODO: this is not optimal when many categories :( , create only visible lables | |||
70 | setLabels(m_axis->categoriesLabels() << ""); |
|
71 | setLabels(m_axis->categoriesLabels() << ""); | |
71 | HorizontalAxis::updateGeometry(); |
|
72 | HorizontalAxis::updateGeometry(); | |
72 | } |
|
73 | } | |
73 |
|
74 | |||
74 | void ChartCategoryAxisX::handleAxisUpdated() |
|
75 | void ChartCategoryAxisX::handleAxisUpdated() | |
75 | { |
|
76 | { | |
76 | updateGeometry(); |
|
77 | updateGeometry(); | |
77 | ChartAxis::handleAxisUpdated(); |
|
78 | ChartAxis::handleAxisUpdated(); | |
78 | } |
|
79 | } | |
79 |
|
80 | |||
80 | QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
81 | QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | |
81 | { |
|
82 | { | |
82 | Q_UNUSED(constraint) |
|
83 | Q_UNUSED(constraint) | |
83 |
|
84 | |||
84 | QFontMetrics fn(font()); |
|
85 | QFontMetrics fn(font()); | |
85 | QSizeF sh; |
|
86 | QSizeF sh; | |
86 | QSizeF base = ChartAxis::sizeHint(which, constraint); |
|
87 | QSizeF base = ChartAxis::sizeHint(which, constraint); | |
87 |
QStringList ticksList ; |
|
88 | QStringList ticksList = m_axis->categoriesLabels(); | |
88 | qreal width = 0; |
|
89 | qreal width = 0; | |
89 | qreal height = 0; |
|
90 | qreal height = 0; | |
90 |
|
91 | |||
91 | switch (which) { |
|
92 | switch (which) { | |
92 | case Qt::MinimumSize: |
|
93 | case Qt::MinimumSize: | |
93 | width = fn.boundingRect("...").width(); |
|
94 | width = fn.boundingRect("...").width(); | |
94 | height = fn.height() + labelPadding(); |
|
95 | height = fn.height() + labelPadding(); | |
95 | width = qMax(width, base.width()); |
|
96 | width = qMax(width, base.width()); | |
96 | height += base.height(); |
|
97 | height += base.height(); | |
97 | sh = QSizeF(width, height); |
|
98 | sh = QSizeF(width, height); | |
98 | break; |
|
99 | break; | |
99 | case Qt::PreferredSize: { |
|
100 | case Qt::PreferredSize: { | |
100 |
|
101 | |||
101 | for (int i = 0; i < ticksList.size(); ++i) { |
|
102 | for (int i = 0; i < ticksList.size(); ++i) { | |
102 | QRectF rect = fn.boundingRect(ticksList.at(i)); |
|
103 | QRectF rect = fn.boundingRect(ticksList.at(i)); | |
103 | width += rect.width(); |
|
104 | width += rect.width(); | |
104 | height = qMax(rect.height() + labelPadding(), height); |
|
105 | height = qMax(rect.height() + labelPadding(), height); | |
105 | } |
|
106 | } | |
106 | width = qMax(width, base.width()); |
|
107 | width = qMax(width, base.width()); | |
107 | height += base.height(); |
|
108 | height += base.height(); | |
108 | sh = QSizeF(width, height); |
|
109 | sh = QSizeF(width, height); | |
109 | break; |
|
110 | break; | |
110 | } |
|
111 | } | |
111 | default: |
|
112 | default: | |
112 | break; |
|
113 | break; | |
113 | } |
|
114 | } | |
114 |
|
115 | |||
115 | return sh; |
|
116 | return sh; | |
116 | } |
|
117 | } | |
117 |
|
118 | |||
118 | QTCOMMERCIALCHART_END_NAMESPACE |
|
119 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,118 +1,118 | |||||
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 "chartcategoryaxisy_p.h" |
|
21 | #include "chartcategoryaxisy_p.h" | |
22 | #include "qcategoryaxis.h" |
|
22 | #include "qcategoryaxis.h" | |
23 | #include "qabstractaxis.h" |
|
23 | #include "qabstractaxis.h" | |
24 | #include "chartpresenter_p.h" |
|
24 | #include "chartpresenter_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 | ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, ChartPresenter *presenter) |
|
32 | ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, ChartPresenter *presenter) | |
32 | : VerticalAxis(axis, presenter, true), |
|
33 | : VerticalAxis(axis, presenter, true), | |
33 | m_axis(axis) |
|
34 | m_axis(axis) | |
34 | { |
|
35 | { | |
35 | } |
|
36 | } | |
36 |
|
37 | |||
37 | ChartCategoryAxisY::~ChartCategoryAxisY() |
|
38 | ChartCategoryAxisY::~ChartCategoryAxisY() | |
38 | { |
|
39 | { | |
39 | } |
|
40 | } | |
40 |
|
41 | |||
41 | QVector<qreal> ChartCategoryAxisY::calculateLayout() const |
|
42 | QVector<qreal> ChartCategoryAxisY::calculateLayout() const | |
42 | { |
|
43 | { | |
43 | int tickCount = m_axis->categoriesLabels().count() + 1; |
|
44 | int tickCount = m_axis->categoriesLabels().count() + 1; | |
44 | QVector<qreal> points; |
|
45 | QVector<qreal> points; | |
45 |
|
46 | |||
46 | if (tickCount < 2) |
|
47 | if (tickCount < 2) | |
47 | return points; |
|
48 | return points; | |
48 |
|
49 | |||
49 | const QRectF &gridRect = gridGeometry(); |
|
50 | const QRectF &gridRect = gridGeometry(); | |
50 |
qreal range = |
|
51 | qreal range = max() - min(); | |
51 | if (range > 0) { |
|
52 | if (range > 0) { | |
52 | points.resize(tickCount); |
|
53 | points.resize(tickCount); | |
53 | qreal scale = gridRect.height() / range; |
|
54 | qreal scale = gridRect.height() / range; | |
54 | for (int i = 0; i < tickCount; ++i) { |
|
55 | for (int i = 0; i < tickCount; ++i) { | |
55 | if (i < tickCount - 1) { |
|
56 | if (i < tickCount - 1) { | |
56 |
int y = -(m_axis->startValue(m_axis->categoriesLabels().at(i)) - |
|
57 | int y = -(m_axis->startValue(m_axis->categoriesLabels().at(i)) - min()) * scale + gridRect.bottom(); | |
57 | points[i] = y; |
|
58 | points[i] = y; | |
58 | } else { |
|
59 | } else { | |
59 |
int y = -(m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - |
|
60 | int y = -(m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - min()) * scale + gridRect.bottom(); | |
60 | points[i] = y; |
|
61 | points[i] = y; | |
61 | } |
|
62 | } | |
62 | } |
|
63 | } | |
63 | } |
|
64 | } | |
64 |
|
65 | |||
65 | return points; |
|
66 | return points; | |
66 | } |
|
67 | } | |
67 |
|
68 | |||
68 | void ChartCategoryAxisY::updateGeometry() |
|
69 | void ChartCategoryAxisY::updateGeometry() | |
69 | { |
|
70 | { | |
70 | setLabels(m_axis->categoriesLabels() << ""); |
|
71 | setLabels(m_axis->categoriesLabels() << ""); | |
71 | VerticalAxis::updateGeometry(); |
|
72 | VerticalAxis::updateGeometry(); | |
72 | } |
|
73 | } | |
73 |
|
74 | |||
74 | void ChartCategoryAxisY::handleAxisUpdated() |
|
75 | void ChartCategoryAxisY::handleAxisUpdated() | |
75 | { |
|
76 | { | |
76 | updateGeometry(); |
|
77 | updateGeometry(); | |
77 | ChartAxis::handleAxisUpdated(); |
|
78 | ChartAxis::handleAxisUpdated(); | |
78 | } |
|
79 | } | |
79 |
|
80 | |||
80 | QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
81 | QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | |
81 | { |
|
82 | { | |
82 | Q_UNUSED(constraint) |
|
83 | Q_UNUSED(constraint) | |
83 |
|
84 | |||
84 | QFontMetrics fn(font()); |
|
85 | QFontMetrics fn(font()); | |
85 | QSizeF sh; |
|
86 | QSizeF sh; | |
86 | QSizeF base = ChartAxis::sizeHint(which, constraint); |
|
87 | QSizeF base = ChartAxis::sizeHint(which, constraint); | |
87 | QStringList ticksList; //TODO:: |
|
88 | QStringList ticksList = m_axis->categoriesLabels(); | |
88 | qreal width = 0; |
|
89 | qreal width = 0; | |
89 | qreal height = 0; |
|
90 | qreal height = 0; | |
90 |
|
91 | |||
91 | switch (which) { |
|
92 | switch (which) { | |
92 | case Qt::MinimumSize: |
|
93 | case Qt::MinimumSize: | |
93 | width = fn.boundingRect("...").width() + labelPadding(); |
|
94 | width = fn.boundingRect("...").width() + labelPadding(); | |
94 | height = fn.height(); |
|
95 | height = fn.height(); | |
95 |
width = |
|
96 | width += base.width(); | |
96 |
height |
|
97 | height = qMax(height, base.height());; | |
97 | sh = QSizeF(width, height); |
|
98 | sh = QSizeF(width, height); | |
98 | break; |
|
99 | break; | |
99 | case Qt::PreferredSize: { |
|
100 | case Qt::PreferredSize: { | |
100 |
|
101 | |||
101 | for (int i = 0; i < ticksList.size(); ++i) { |
|
102 | for (int i = 0; i < ticksList.size(); ++i) { | |
102 | QRectF rect = fn.boundingRect(ticksList.at(i)); |
|
103 | QRectF rect = fn.boundingRect(ticksList.at(i)); | |
|
104 | width = qMax(rect.width() + labelPadding() + 1, width); | |||
103 | height += rect.height(); |
|
105 | height += rect.height(); | |
104 | width = qMax(rect.width() + labelPadding(), width); |
|
|||
105 | } |
|
106 | } | |
106 | height = qMax(height, base.height()); |
|
107 | height = qMax(height, base.height()); | |
107 | width += base.width(); |
|
108 | width += base.width(); | |
108 | sh = QSizeF(width, height); |
|
109 | sh = QSizeF(width, height); | |
109 | break; |
|
110 | break; | |
110 | } |
|
111 | } | |
111 | default: |
|
112 | default: | |
112 | break; |
|
113 | break; | |
113 | } |
|
114 | } | |
114 |
|
||||
115 | return sh; |
|
115 | return sh; | |
116 | } |
|
116 | } | |
117 |
|
117 | |||
118 | QTCOMMERCIALCHART_END_NAMESPACE |
|
118 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now