##// END OF EJS Templates
Fixes rounding issue with labels numbering
Michal Klocek -
r1720:79d9101394e5
parent child
Show More
@@ -1,140 +1,157
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 "chartcategoriesaxisx_p.h"
21 #include "chartcategoriesaxisx_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qbarcategoriesaxis_p.h"
23 #include "qbarcategoriesaxis_p.h"
24 #include <cmath>
24 #include <QDebug>
25 #include <qmath.h>
25
26
26 static int label_padding = 5;
27 static int label_padding = 5;
27
28
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30
30 ChartCategoriesAxisX::ChartCategoriesAxisX(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
31 ChartCategoriesAxisX::ChartCategoriesAxisX(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
31 m_categoriesAxis(axis)
32 m_categoriesAxis(axis)
32 {
33 {
33
34
34 }
35 }
35
36
36 ChartCategoriesAxisX::~ChartCategoriesAxisX()
37 ChartCategoriesAxisX::~ChartCategoriesAxisX()
37 {
38 {
38 }
39 }
39
40
40 QVector<qreal> ChartCategoriesAxisX::calculateLayout() const
41 QVector<qreal> ChartCategoriesAxisX::calculateLayout() const
41 {
42 {
42 Q_ASSERT(m_categoriesAxis->categories().count()>=1);
43 Q_ASSERT(m_categoriesAxis->categories().count()>=1);
43
44
44 QVector<qreal> points;
45 QVector<qreal> points;
45 points.resize(m_categoriesAxis->categories().count()+1);
46 points.resize(m_categoriesAxis->categories().count()+2);
46
47
47 const qreal delta = m_rect.width()/(m_categoriesAxis->categories().count());
48 const qreal delta = m_rect.width()/(m_categoriesAxis->categories().count());
48 const qreal min = m_categoriesAxis->d_ptr->min();
49 qreal offset =-m_min-0.5;
49 const qreal max = m_categoriesAxis->d_ptr->max();
50
50 qreal start =-min-0.5;
51 if(offset<=0) {
51 if(start<=0) {
52 offset = int(offset * m_rect.width()/(m_max - m_min))%int(delta) + delta;
52 start = fmod(start * m_rect.width()/(max - min),delta) + delta;
53 }
53 }
54 else {
54 else {
55 start = fmod(start * m_rect.width()/(max - min),delta);
55 offset = int(offset * m_rect.width()/(m_max - m_min))%int(delta);
56 }
56 }
57
57
58 points[m_categoriesAxis->categories().count()] = m_rect.left() + m_rect.width();
58 points[0] = m_rect.left();
59 points[m_categoriesAxis->categories().count()+1] = m_rect.right();
59
60
60 for (int i = 0; i < m_categoriesAxis->categories().count(); ++i) {
61 for (int i = 0; i < m_categoriesAxis->categories().count(); ++i) {
61 qreal x = start + i * delta + m_rect.left();
62 qreal x = offset + i * delta + m_rect.left();
62 points[i] = x;
63 points[i+1] = x;
63 }
64 }
64
65 return points;
65 return points;
66 }
66 }
67
67
68 QStringList ChartCategoriesAxisX::createCategoryLabels(const QVector<qreal>& layout) const
69 {
70 QStringList result;
71 qreal d = (m_max - m_min)/m_rect.width();
72 for (int i = 0;i < layout.count()-1; ++i) {
73 qreal x = qFloor((((layout[i+1] + layout[i])/2-m_rect.left())*d + m_min+0.5));
74 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
75 result << m_categoriesAxis->categories().at(x);
76 }
77 else {
78 // No label for x coordinate
79 result << "";
80 }
81 }
82 result << "";
83 return result;
84 }
85
86
68 void ChartCategoriesAxisX::updateGeometry()
87 void ChartCategoriesAxisX::updateGeometry()
69 {
88 {
70 const QVector<qreal>& layout = ChartAxis::layout();
89 const QVector<qreal>& layout = ChartAxis::layout();
71
90
72 m_minWidth = 0;
91 m_minWidth = 0;
73 m_minHeight = 0;
92 m_minHeight = 0;
74
93
75 if(layout.isEmpty()) return;
94 if(layout.isEmpty()) return;
76
95
77 QStringList ticksList;
96 QStringList ticksList = createCategoryLabels(layout);
78
79 createCategoryLabels(ticksList,m_min,m_max,m_categoriesAxis->categories());
80
97
81 QList<QGraphicsItem *> lines = m_grid->childItems();
98 QList<QGraphicsItem *> lines = m_grid->childItems();
82 QList<QGraphicsItem *> labels = m_labels->childItems();
99 QList<QGraphicsItem *> labels = m_labels->childItems();
83 QList<QGraphicsItem *> shades = m_shades->childItems();
100 QList<QGraphicsItem *> shades = m_shades->childItems();
84 QList<QGraphicsItem *> axis = m_axis->childItems();
101 QList<QGraphicsItem *> axis = m_axis->childItems();
85
102
86 Q_ASSERT(labels.size() == ticksList.size());
103 Q_ASSERT(labels.size() == ticksList.size());
87 Q_ASSERT(layout.size() == ticksList.size());
104 Q_ASSERT(layout.size() == ticksList.size());
88
105
89 const qreal delta = m_rect.width()/(m_categoriesAxis->categories().count());
106 const qreal delta = m_rect.width()/(m_categoriesAxis->categories().count());
90
107
91 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
108 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
92 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
109 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
93
110
94 qreal width = m_rect.left();
111 qreal width = m_rect.left();
95 for (int i = 0; i < layout.size(); ++i) {
112 for (int i = 0; i < layout.size(); ++i) {
96 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
113 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
97 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
114 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
98 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
115 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
99 labelItem->setText(ticksList.at(i));
116 labelItem->setText(ticksList.at(i));
100 const QRectF& rect = labelItem->boundingRect();
117 const QRectF& rect = labelItem->boundingRect();
101 QPointF center = rect.center();
118 QPointF center = rect.center();
102 labelItem->setTransformOriginPoint(center.x(), center.y());
119 labelItem->setTransformOriginPoint(center.x(), center.y());
103
120
104 if(i==layout.size()-1){
121 if(i==0){
105 labelItem->setPos(layout[i-1] + (delta)/2 - center.x(), m_rect.bottom() + label_padding);
122 labelItem->setPos(layout[i+1] - (delta)/2 - center.x(), m_rect.bottom() + label_padding);
106 }else{
123 }else{
107 labelItem->setPos(layout[i] - (delta)/2 - center.x(), m_rect.bottom() + label_padding);
124 labelItem->setPos(layout[i] + (delta)/2 - center.x(), m_rect.bottom() + label_padding);
108 }
125 }
109
126
110 if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()>m_rect.right()) {
127 if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()>m_rect.right()) {
111 labelItem->setVisible(false);
128 labelItem->setVisible(false);
112 }
129 }
113 else {
130 else {
114 labelItem->setVisible(true);
131 labelItem->setVisible(true);
115 width=rect.width()+labelItem->pos().x();
132 width=rect.width()+labelItem->pos().x();
116 }
133 }
117
134
118 m_minWidth+=rect.width();
135 m_minWidth+=rect.width();
119 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
136 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
120
137
121 if ((i+1)%2 && i>1) {
138 if ((i+1)%2 && i>1) {
122 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
139 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
123 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
140 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
124 }
141 }
125 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
142 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
126 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
143 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
127 }
144 }
128 }
145 }
129
146
130 void ChartCategoriesAxisX::handleAxisUpdated()
147 void ChartCategoriesAxisX::handleAxisUpdated()
131 {
148 {
132 if(m_categoriesAxis->categories()!=m_categories)
149 if(m_categoriesAxis->categories()!=m_categories)
133 {
150 {
134 m_categories=m_categoriesAxis->categories();
151 m_categories=m_categoriesAxis->categories();
135 if(ChartAxis::layout().count()==m_categories.size()+1) updateGeometry();
152 if(ChartAxis::layout().count()==m_categories.size()+1) updateGeometry();
136 }
153 }
137 ChartAxis::handleAxisUpdated();
154 ChartAxis::handleAxisUpdated();
138 }
155 }
139
156
140 QTCOMMERCIALCHART_END_NAMESPACE
157 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,62 +1,64
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTCATEGORIESAXISX_H
30 #ifndef CHARTCATEGORIESAXISX_H
31 #define CHARTCATEGORIESAXISX_H
31 #define CHARTCATEGORIESAXISX_H
32
32
33 #include "chartaxis_p.h"
33 #include "chartaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAbstractAxis;
37 class QAbstractAxis;
38 class ChartPresenter;
38 class ChartPresenter;
39 class QBarCategoriesAxis;
39 class QBarCategoriesAxis;
40
40
41 class ChartCategoriesAxisX : public ChartAxis
41 class ChartCategoriesAxisX : public ChartAxis
42 {
42 {
43 public:
43 public:
44 ChartCategoriesAxisX(QBarCategoriesAxis *axis, ChartPresenter *presenter);
44 ChartCategoriesAxisX(QBarCategoriesAxis *axis, ChartPresenter *presenter);
45 ~ChartCategoriesAxisX();
45 ~ChartCategoriesAxisX();
46
46
47 AxisType axisType() const { return X_AXIS;}
47 AxisType axisType() const { return X_AXIS;}
48
48
49 protected:
49 protected:
50 QVector<qreal> calculateLayout() const;
50 QVector<qreal> calculateLayout() const;
51 void updateGeometry();
51 void updateGeometry();
52 private:
53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
52 Q_SLOTS
54 Q_SLOTS
53 void handleAxisUpdated();
55 void handleAxisUpdated();
54
56
55 private:
57 private:
56 QStringList m_categories;
58 QStringList m_categories;
57 QBarCategoriesAxis *m_categoriesAxis;
59 QBarCategoriesAxis *m_categoriesAxis;
58 };
60 };
59
61
60 QTCOMMERCIALCHART_END_NAMESPACE
62 QTCOMMERCIALCHART_END_NAMESPACE
61
63
62 #endif /* CHARTCATEGORIESAXISX_H */
64 #endif /* CHARTCATEGORIESAXISX_H */
@@ -1,145 +1,159
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 "chartcategoriesaxisy_p.h"
21 #include "chartcategoriesaxisy_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qbarcategoriesaxis_p.h"
23 #include "qbarcategoriesaxis_p.h"
24 #include <QDebug>
24 #include <qmath.h>
25 #include <cmath>
26
25
27 static int label_padding = 5;
26 static int label_padding = 5;
28
27
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
29
31 ChartCategoriesAxisY::ChartCategoriesAxisY(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
30 ChartCategoriesAxisY::ChartCategoriesAxisY(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
32 m_categoriesAxis(axis)
31 m_categoriesAxis(axis)
33 {
32 {
34 }
33 }
35
34
36 ChartCategoriesAxisY::~ChartCategoriesAxisY()
35 ChartCategoriesAxisY::~ChartCategoriesAxisY()
37 {
36 {
38 }
37 }
39
38
40 QVector<qreal> ChartCategoriesAxisY::calculateLayout() const
39 QVector<qreal> ChartCategoriesAxisY::calculateLayout() const
41 {
40 {
42 Q_ASSERT(m_categoriesAxis->categories().count()>=1);
41 Q_ASSERT(m_categoriesAxis->categories().count()>=1);
43
42
44 QVector<qreal> points;
43 QVector<qreal> points;
45 points.resize(m_categoriesAxis->categories().count()+1);
44 points.resize(m_categoriesAxis->categories().count()+2);
46
45
47 qreal delta = m_rect.height()/(m_categoriesAxis->categories().count());
46 const qreal delta = m_rect.height()/(m_categoriesAxis->categories().count());
48
47 qreal offset = - m_min - 0.5;
49 const qreal min = m_categoriesAxis->d_ptr->min();
50 const qreal max = m_categoriesAxis->d_ptr->max();
51
52 qreal start =-min-0.5;
53
48
54 if(start<=0) {
49 if(offset<=0) {
55 start = fmod(start * m_rect.height()/(max - min),delta) + delta;
50 offset = int(offset * m_rect.height()/(m_max - m_min))%int(delta) + delta;
56 }
51 }
57 else {
52 else {
58 start = fmod(start * m_rect.height()/(max - min),delta);
53 offset = int(offset * m_rect.height()/(m_max - m_min))%int(delta);
59 }
54 }
60
55
61 points[m_categoriesAxis->categories().count()] = m_rect.top();
56 points[0] = m_rect.bottom();
57 points[m_categoriesAxis->categories().count()+1] = m_rect.top();
62
58
63 for (int i = 0; i < m_categoriesAxis->categories().count(); ++i) {
59 for (int i = 0; i < m_categoriesAxis->categories().count(); ++i) {
64 int y = m_rect.bottom() - i * delta - start;
60 int y = m_rect.bottom() - i * delta - offset;
65 points[i] = y;
61 points[i+1] = y;
66 }
62 }
67
68 return points;
63 return points;
69 }
64 }
70
65
66 QStringList ChartCategoriesAxisY::createCategoryLabels(const QVector<qreal>& layout) const
67 {
68 QStringList result;
69 qreal d = (m_max - m_min)/m_rect.height();
70 for (int i = 0;i < layout.count()-1; ++i) {
71 qreal x = qFloor(((m_rect.height()- (layout[i+1] + layout[i])/2 + m_rect.top())*d + m_min+0.5));
72 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
73 result << m_categoriesAxis->categories().at(x);
74 }
75 else {
76 // No label for x coordinate
77 result << "";
78 }
79 }
80 result << "";
81 return result;
82 }
83
71 void ChartCategoriesAxisY::updateGeometry()
84 void ChartCategoriesAxisY::updateGeometry()
72 {
85 {
73 const QVector<qreal>& layout = ChartAxis::layout();
86 const QVector<qreal>& layout = ChartAxis::layout();
74
87
75 m_minWidth = 0;
88 m_minWidth = 0;
76 m_minHeight = 0;
89 m_minHeight = 0;
77
90
78 if(layout.isEmpty()) return;
91 if(layout.isEmpty()) return;
79
92
80 QStringList ticksList;
93 QStringList ticksList = createCategoryLabels(layout);
81
82 createCategoryLabels(ticksList,m_min,m_max,m_categoriesAxis->categories());
83
94
84 QList<QGraphicsItem *> lines = m_grid->childItems();
95 QList<QGraphicsItem *> lines = m_grid->childItems();
85 QList<QGraphicsItem *> labels = m_labels->childItems();
96 QList<QGraphicsItem *> labels = m_labels->childItems();
86 QList<QGraphicsItem *> shades = m_shades->childItems();
97 QList<QGraphicsItem *> shades = m_shades->childItems();
87 QList<QGraphicsItem *> axis = m_axis->childItems();
98 QList<QGraphicsItem *> axis = m_axis->childItems();
88
99
89 Q_ASSERT(labels.size() == ticksList.size());
100 Q_ASSERT(labels.size() == ticksList.size());
90 Q_ASSERT(layout.size() == ticksList.size());
101 Q_ASSERT(layout.size() == ticksList.size());
91
102
92 const qreal delta = m_rect.height()/(m_categoriesAxis->categories().count());
103 const qreal delta = m_rect.height()/(m_categoriesAxis->categories().count());
93
104
94 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
105 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
95 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
106 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
96
107
97 qreal height = m_rect.bottom();
108 qreal height = m_rect.bottom();
98 for (int i = 0; i < layout.size(); ++i) {
109 for (int i = 0; i < layout.size(); ++i) {
99 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
110 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
100 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
111 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
101 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
112 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
102 labelItem->setText(ticksList.at(i));
113 labelItem->setText(ticksList.at(i));
103 const QRectF& rect = labelItem->boundingRect();
114 const QRectF& rect = labelItem->boundingRect();
104 QPointF center = rect.center();
115 QPointF center = rect.center();
105 labelItem->setTransformOriginPoint(center.x(), center.y());
116 labelItem->setTransformOriginPoint(center.x(), center.y());
106
117
107 if(i==layout.size()-1) {
118 if(i==0) {
108 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i-1] - (delta)/2 - center.y());
119 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y());
109 }
120 }
110 else {
121 else {
111 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i] + (delta)/2 - center.y());
122 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i] - (delta)/2 - center.y());
112 }
123 }
113
124
114 if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < m_rect.top()) {
125 if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < m_rect.top()) {
115 labelItem->setVisible(false);
126 labelItem->setVisible(false);
116 }
127 }
117 else {
128 else {
118 labelItem->setVisible(true);
129 labelItem->setVisible(true);
119 height=labelItem->pos().y();
130 height=labelItem->pos().y();
120 }
131 }
121
132
122 m_minWidth+=rect.width();
133 m_minWidth+=rect.width();
123 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
134 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
124
135
125 if ((i+1)%2 && i>1) {
136 if ((i+1)%2 && i>1) {
126 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
137 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
127 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
138 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
128 }
139 }
129 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
140 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
130 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
141 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
131 }
142 }
132 }
143 }
133
144
134 void ChartCategoriesAxisY::handleAxisUpdated()
145 void ChartCategoriesAxisY::handleAxisUpdated()
135 {
146 {
136
147
137 if(m_categoriesAxis->categories()!=m_categories)
148 if(m_categoriesAxis->categories()!=m_categories)
138 {
149 {
139 m_categories=m_categoriesAxis->categories();
150 m_categories=m_categoriesAxis->categories();
140 if(ChartAxis::layout().count()==m_categories.size()+1) updateGeometry();
151 if(ChartAxis::layout().count()==m_categories.size()+1) {
152
153 updateGeometry();
154 }
141 }
155 }
142 ChartAxis::handleAxisUpdated();
156 ChartAxis::handleAxisUpdated();
143 }
157 }
144
158
145 QTCOMMERCIALCHART_END_NAMESPACE
159 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,61 +1,64
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTCATEGORIESAXISY_H
30 #ifndef CHARTCATEGORIESAXISY_H
31 #define CHARTCATEGORIESAXISY_H
31 #define CHARTCATEGORIESAXISY_H
32
32
33 #include "chartaxis_p.h"
33 #include "chartaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAbstractAxis;
37 class QAbstractAxis;
38 class QBarCategoriesAxis;
38 class QBarCategoriesAxis;
39 class ChartPresenter;
39 class ChartPresenter;
40
40
41 class ChartCategoriesAxisY : public ChartAxis
41 class ChartCategoriesAxisY : public ChartAxis
42 {
42 {
43 public:
43 public:
44 ChartCategoriesAxisY(QBarCategoriesAxis *axis, ChartPresenter *presenter);
44 ChartCategoriesAxisY(QBarCategoriesAxis *axis, ChartPresenter *presenter);
45 ~ChartCategoriesAxisY();
45 ~ChartCategoriesAxisY();
46
46
47 AxisType axisType() const { return Y_AXIS;}
47 AxisType axisType() const { return Y_AXIS;}
48
48
49 protected:
49 protected:
50 QVector<qreal> calculateLayout() const;
50 QVector<qreal> calculateLayout() const;
51 void updateGeometry();
51 void updateGeometry();
52 private:
53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
52 Q_SLOTS
54 Q_SLOTS
53 void handleAxisUpdated();
55 void handleAxisUpdated();
54 private:
56 private:
55 QStringList m_categories;
57 QStringList m_categories;
58 QVector<int> m_labelIndex;
56 QBarCategoriesAxis *m_categoriesAxis;
59 QBarCategoriesAxis *m_categoriesAxis;
57 };
60 };
58
61
59 QTCOMMERCIALCHART_END_NAMESPACE
62 QTCOMMERCIALCHART_END_NAMESPACE
60
63
61 #endif /* CHARTCATEGORIESAXISY_H */
64 #endif /* CHARTCATEGORIESAXISY_H */
@@ -1,396 +1,373
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 "chartaxis_p.h"
21 #include "chartaxis_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include "qabstractaxis_p.h"
23 #include "qabstractaxis_p.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "chartanimator_p.h"
25 #include "chartanimator_p.h"
26 #include "domain_p.h"
26 #include "domain_p.h"
27 #include <QPainter>
28 #include <QDebug>
29 #include <qmath.h>
27 #include <qmath.h>
30 #include <QDateTime>
28 #include <QDateTime>
31
29
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
31
34 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : Chart(presenter),
32 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : Chart(presenter),
35 m_chartAxis(axis),
33 m_chartAxis(axis),
36 m_labelsAngle(0),
34 m_labelsAngle(0),
37 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
35 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
38 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
36 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
39 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
37 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
40 m_axis(new QGraphicsItemGroup(presenter->rootItem())),
38 m_axis(new QGraphicsItemGroup(presenter->rootItem())),
41 m_min(0),
39 m_min(0),
42 m_max(0),
40 m_max(0),
43 m_animation(0),
41 m_animation(0),
44 m_minWidth(0),
42 m_minWidth(0),
45 m_minHeight(0)
43 m_minHeight(0)
46 {
44 {
47 //initial initialization
45 //initial initialization
48 m_axis->setZValue(ChartPresenter::AxisZValue);
46 m_axis->setZValue(ChartPresenter::AxisZValue);
49 m_axis->setHandlesChildEvents(false);
47 m_axis->setHandlesChildEvents(false);
50
48
51 m_shades->setZValue(ChartPresenter::ShadesZValue);
49 m_shades->setZValue(ChartPresenter::ShadesZValue);
52 m_grid->setZValue(ChartPresenter::GridZValue);
50 m_grid->setZValue(ChartPresenter::GridZValue);
53
51
54 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
52 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
55
53
56 QGraphicsSimpleTextItem item;
54 QGraphicsSimpleTextItem item;
57 m_font = item.font();
55 m_font = item.font();
58 }
56 }
59
57
60 ChartAxis::~ChartAxis()
58 ChartAxis::~ChartAxis()
61 {
59 {
62 }
60 }
63
61
64 void ChartAxis::setAnimation(AxisAnimation* animation)
62 void ChartAxis::setAnimation(AxisAnimation* animation)
65 {
63 {
66 m_animation=animation;
64 m_animation=animation;
67 }
65 }
68
66
69 void ChartAxis::setLayout(QVector<qreal> &layout)
67 void ChartAxis::setLayout(QVector<qreal> &layout)
70 {
68 {
71 m_layoutVector=layout;
69 m_layoutVector=layout;
72 }
70 }
73
71
74 void ChartAxis::createItems(int count)
72 void ChartAxis::createItems(int count)
75 {
73 {
76 if (m_axis->children().size() == 0)
74 if (m_axis->children().size() == 0)
77 m_axis->addToGroup(new AxisItem(this));
75 m_axis->addToGroup(new AxisItem(this));
78 for (int i = 0; i < count; ++i) {
76 for (int i = 0; i < count; ++i) {
79 m_grid->addToGroup(new QGraphicsLineItem());
77 m_grid->addToGroup(new QGraphicsLineItem());
80 m_labels->addToGroup(new QGraphicsSimpleTextItem());
78 m_labels->addToGroup(new QGraphicsSimpleTextItem());
81 m_axis->addToGroup(new QGraphicsLineItem());
79 m_axis->addToGroup(new QGraphicsLineItem());
82 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem());
80 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem());
83 }
81 }
84 }
82 }
85
83
86 void ChartAxis::deleteItems(int count)
84 void ChartAxis::deleteItems(int count)
87 {
85 {
88 QList<QGraphicsItem *> lines = m_grid->childItems();
86 QList<QGraphicsItem *> lines = m_grid->childItems();
89 QList<QGraphicsItem *> labels = m_labels->childItems();
87 QList<QGraphicsItem *> labels = m_labels->childItems();
90 QList<QGraphicsItem *> shades = m_shades->childItems();
88 QList<QGraphicsItem *> shades = m_shades->childItems();
91 QList<QGraphicsItem *> axis = m_axis->childItems();
89 QList<QGraphicsItem *> axis = m_axis->childItems();
92
90
93 for (int i = 0; i < count; ++i) {
91 for (int i = 0; i < count; ++i) {
94 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
92 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
95 delete(lines.takeLast());
93 delete(lines.takeLast());
96 delete(labels.takeLast());
94 delete(labels.takeLast());
97 delete(axis.takeLast());
95 delete(axis.takeLast());
98 }
96 }
99 }
97 }
100
98
101 void ChartAxis::updateLayout(QVector<qreal> &layout)
99 void ChartAxis::updateLayout(QVector<qreal> &layout)
102 {
100 {
103 int diff = m_layoutVector.size() - layout.size();
101 int diff = m_layoutVector.size() - layout.size();
104
102
105 if (diff>0) {
103 if (diff>0) {
106 deleteItems(diff);
104 deleteItems(diff);
107 }
105 }
108 else if (diff<0) {
106 else if (diff<0) {
109 createItems(-diff);
107 createItems(-diff);
110 }
108 }
111
109
112 if(diff<0) handleAxisUpdated();
110 if(diff<0) handleAxisUpdated();
113
111
114 if (m_animation) {
112 if (m_animation) {
115 switch(presenter()->state()){
113 switch(presenter()->state()){
116 case ChartPresenter::ZoomInState:
114 case ChartPresenter::ZoomInState:
117 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
115 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
118 m_animation->setAnimationPoint(presenter()->statePoint());
116 m_animation->setAnimationPoint(presenter()->statePoint());
119 break;
117 break;
120 case ChartPresenter::ZoomOutState:
118 case ChartPresenter::ZoomOutState:
121 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
119 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
122 m_animation->setAnimationPoint(presenter()->statePoint());
120 m_animation->setAnimationPoint(presenter()->statePoint());
123 break;
121 break;
124 case ChartPresenter::ScrollUpState:
122 case ChartPresenter::ScrollUpState:
125 case ChartPresenter::ScrollLeftState:
123 case ChartPresenter::ScrollLeftState:
126 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
124 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
127 break;
125 break;
128 case ChartPresenter::ScrollDownState:
126 case ChartPresenter::ScrollDownState:
129 case ChartPresenter::ScrollRightState:
127 case ChartPresenter::ScrollRightState:
130 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
128 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
131 break;
129 break;
132 case ChartPresenter::ShowState:
130 case ChartPresenter::ShowState:
133 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
131 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
134 break;
132 break;
135 }
133 }
136 m_animation->setValues(m_layoutVector,layout);
134 m_animation->setValues(m_layoutVector,layout);
137 presenter()->startAnimation(m_animation);
135 presenter()->startAnimation(m_animation);
138 }
136 }
139 else {
137 else {
140 setLayout(layout);
138 setLayout(layout);
141 updateGeometry();
139 updateGeometry();
142 }
140 }
143 }
141 }
144
142
145 void ChartAxis::setAxisOpacity(qreal opacity)
143 void ChartAxis::setAxisOpacity(qreal opacity)
146 {
144 {
147 m_axis->setOpacity(opacity);
145 m_axis->setOpacity(opacity);
148 }
146 }
149
147
150 qreal ChartAxis::axisOpacity() const
148 qreal ChartAxis::axisOpacity() const
151 {
149 {
152 return m_axis->opacity();
150 return m_axis->opacity();
153 }
151 }
154
152
155 void ChartAxis::setGridOpacity(qreal opacity)
153 void ChartAxis::setGridOpacity(qreal opacity)
156 {
154 {
157 m_grid->setOpacity(opacity);
155 m_grid->setOpacity(opacity);
158 }
156 }
159
157
160 qreal ChartAxis::gridOpacity() const
158 qreal ChartAxis::gridOpacity() const
161 {
159 {
162 return m_grid->opacity();
160 return m_grid->opacity();
163 }
161 }
164
162
165 void ChartAxis::setLabelsOpacity(qreal opacity)
163 void ChartAxis::setLabelsOpacity(qreal opacity)
166 {
164 {
167 m_labels->setOpacity(opacity);
165 m_labels->setOpacity(opacity);
168 }
166 }
169
167
170 qreal ChartAxis::labelsOpacity() const
168 qreal ChartAxis::labelsOpacity() const
171 {
169 {
172 return m_labels->opacity();
170 return m_labels->opacity();
173 }
171 }
174
172
175 void ChartAxis::setShadesOpacity(qreal opacity)
173 void ChartAxis::setShadesOpacity(qreal opacity)
176 {
174 {
177 m_shades->setOpacity(opacity);
175 m_shades->setOpacity(opacity);
178 }
176 }
179
177
180 qreal ChartAxis::shadesOpacity() const
178 qreal ChartAxis::shadesOpacity() const
181 {
179 {
182 return m_shades->opacity();
180 return m_shades->opacity();
183 }
181 }
184
182
185 void ChartAxis::setLabelsAngle(int angle)
183 void ChartAxis::setLabelsAngle(int angle)
186 {
184 {
187 foreach(QGraphicsItem* item , m_labels->childItems()) {
185 foreach(QGraphicsItem* item , m_labels->childItems()) {
188 item->setRotation(angle);
186 item->setRotation(angle);
189 }
187 }
190
188
191 m_labelsAngle=angle;
189 m_labelsAngle=angle;
192 }
190 }
193
191
194 void ChartAxis::setLabelsPen(const QPen &pen)
192 void ChartAxis::setLabelsPen(const QPen &pen)
195 {
193 {
196 foreach(QGraphicsItem* item , m_labels->childItems()) {
194 foreach(QGraphicsItem* item , m_labels->childItems()) {
197 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
195 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
198 }
196 }
199 }
197 }
200
198
201 void ChartAxis::setLabelsBrush(const QBrush &brush)
199 void ChartAxis::setLabelsBrush(const QBrush &brush)
202 {
200 {
203 foreach(QGraphicsItem* item , m_labels->childItems()) {
201 foreach(QGraphicsItem* item , m_labels->childItems()) {
204 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
202 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
205 }
203 }
206 }
204 }
207
205
208 void ChartAxis::setLabelsFont(const QFont &font)
206 void ChartAxis::setLabelsFont(const QFont &font)
209 {
207 {
210 foreach(QGraphicsItem* item , m_labels->childItems()) {
208 foreach(QGraphicsItem* item , m_labels->childItems()) {
211 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
209 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
212 }
210 }
213 m_font = font;
211 m_font = font;
214 }
212 }
215
213
216 void ChartAxis::setShadesBrush(const QBrush &brush)
214 void ChartAxis::setShadesBrush(const QBrush &brush)
217 {
215 {
218 foreach(QGraphicsItem* item , m_shades->childItems()) {
216 foreach(QGraphicsItem* item , m_shades->childItems()) {
219 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
217 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
220 }
218 }
221 }
219 }
222
220
223 void ChartAxis::setShadesPen(const QPen &pen)
221 void ChartAxis::setShadesPen(const QPen &pen)
224 {
222 {
225 foreach(QGraphicsItem* item , m_shades->childItems()) {
223 foreach(QGraphicsItem* item , m_shades->childItems()) {
226 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
224 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
227 }
225 }
228 }
226 }
229
227
230 void ChartAxis::setAxisPen(const QPen &pen)
228 void ChartAxis::setAxisPen(const QPen &pen)
231 {
229 {
232 foreach(QGraphicsItem* item , m_axis->childItems()) {
230 foreach(QGraphicsItem* item , m_axis->childItems()) {
233 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
231 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
234 }
232 }
235 }
233 }
236
234
237 void ChartAxis::setGridPen(const QPen &pen)
235 void ChartAxis::setGridPen(const QPen &pen)
238 {
236 {
239 foreach(QGraphicsItem* item , m_grid->childItems()) {
237 foreach(QGraphicsItem* item , m_grid->childItems()) {
240 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
238 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
241 }
239 }
242 }
240 }
243
241
244 bool ChartAxis::isEmpty()
242 bool ChartAxis::isEmpty()
245 {
243 {
246 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max);
244 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max);
247 }
245 }
248
246
249 void ChartAxis::handleDomainUpdated()
247 void ChartAxis::handleDomainUpdated()
250 {
248 {
251 Domain* domain = qobject_cast<Domain*>(sender());
249 Domain* domain = qobject_cast<Domain*>(sender());
252 qreal min(0);
250 qreal min(0);
253 qreal max(0);
251 qreal max(0);
254
252
255 if(m_chartAxis->orientation()==Qt::Horizontal) {
253 if(m_chartAxis->orientation()==Qt::Horizontal) {
256 min = domain->minX();
254 min = domain->minX();
257 max = domain->maxX();
255 max = domain->maxX();
258 }
256 }
259 else if (m_chartAxis->orientation()==Qt::Vertical)
257 else if (m_chartAxis->orientation()==Qt::Vertical)
260 {
258 {
261 min = domain->minY();
259 min = domain->minY();
262 max = domain->maxY();
260 max = domain->maxY();
263 }
261 }
264
262
265 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max))
263 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max))
266 {
264 {
267 m_min = min;
265 m_min = min;
268 m_max = max;
266 m_max = max;
269
267
270 if (!isEmpty()) {
268 if (!isEmpty()) {
271 QVector<qreal> layout = calculateLayout();
269 QVector<qreal> layout = calculateLayout();
272 updateLayout(layout);
270 updateLayout(layout);
273 }
271 }
274 }
272 }
275 }
273 }
276
274
277 void ChartAxis::handleAxisUpdated()
275 void ChartAxis::handleAxisUpdated()
278 {
276 {
279 if(isEmpty()) return;
277 if(isEmpty()) return;
280
278
281 if (!m_chartAxis->isVisible()) {
279 if (!m_chartAxis->isVisible()) {
282 setAxisOpacity(0);
280 setAxisOpacity(0);
283 setGridOpacity(0);
281 setGridOpacity(0);
284 setLabelsOpacity(0);
282 setLabelsOpacity(0);
285 setShadesOpacity(0);
283 setShadesOpacity(0);
286 }
284 }
287 else {
285 else {
288
286
289 if (m_chartAxis->isArrowVisible()) {
287 if (m_chartAxis->isArrowVisible()) {
290 setAxisOpacity(100);
288 setAxisOpacity(100);
291 }
289 }
292 else {
290 else {
293 setAxisOpacity(0);
291 setAxisOpacity(0);
294 }
292 }
295
293
296 if (m_chartAxis->isGridLineVisible()) {
294 if (m_chartAxis->isGridLineVisible()) {
297 setGridOpacity(100);
295 setGridOpacity(100);
298 }
296 }
299 else {
297 else {
300 setGridOpacity(0);
298 setGridOpacity(0);
301 }
299 }
302
300
303 if (m_chartAxis->labelsVisible()) {
301 if (m_chartAxis->labelsVisible()) {
304 setLabelsOpacity(100);
302 setLabelsOpacity(100);
305 }
303 }
306 else {
304 else {
307 setLabelsOpacity(0);
305 setLabelsOpacity(0);
308 }
306 }
309
307
310 if (m_chartAxis->shadesVisible()) {
308 if (m_chartAxis->shadesVisible()) {
311 setShadesOpacity(100);
309 setShadesOpacity(100);
312 }
310 }
313 else {
311 else {
314 setShadesOpacity(0);
312 setShadesOpacity(0);
315 }
313 }
316 }
314 }
317
315
318 setLabelsAngle(m_chartAxis->labelsAngle());
316 setLabelsAngle(m_chartAxis->labelsAngle());
319 setAxisPen(m_chartAxis->axisPen());
317 setAxisPen(m_chartAxis->axisPen());
320 setLabelsPen(m_chartAxis->labelsPen());
318 setLabelsPen(m_chartAxis->labelsPen());
321 setLabelsBrush(m_chartAxis->labelsBrush());
319 setLabelsBrush(m_chartAxis->labelsBrush());
322 setLabelsFont(m_chartAxis->labelsFont());
320 setLabelsFont(m_chartAxis->labelsFont());
323 setGridPen(m_chartAxis->gridLinePen());
321 setGridPen(m_chartAxis->gridLinePen());
324 setShadesPen(m_chartAxis->shadesPen());
322 setShadesPen(m_chartAxis->shadesPen());
325 setShadesBrush(m_chartAxis->shadesBrush());
323 setShadesBrush(m_chartAxis->shadesBrush());
326
324
327 }
325 }
328
326
329 void ChartAxis::handleGeometryChanged(const QRectF &rect)
327 void ChartAxis::handleGeometryChanged(const QRectF &rect)
330 {
328 {
331 if(m_rect != rect)
329 if(m_rect != rect)
332 {
330 {
333 m_rect = rect;
331 m_rect = rect;
334 if (isEmpty()) return;
332 if (isEmpty()) return;
335 QVector<qreal> layout = calculateLayout();
333 QVector<qreal> layout = calculateLayout();
336 updateLayout(layout);
334 updateLayout(layout);
337 }
335 }
338 }
336 }
339
337
340
338
341 qreal ChartAxis::minimumWidth()
339 qreal ChartAxis::minimumWidth()
342 {
340 {
343 if(m_minWidth == 0) updateGeometry();
341 if(m_minWidth == 0) updateGeometry();
344 return m_minWidth;
342 return m_minWidth;
345 }
343 }
346
344
347 qreal ChartAxis::minimumHeight()
345 qreal ChartAxis::minimumHeight()
348 {
346 {
349 if(m_minHeight == 0) updateGeometry();
347 if(m_minHeight == 0) updateGeometry();
350 return m_minHeight;
348 return m_minHeight;
351 }
349 }
352
350
353
351
354 void ChartAxis::axisSelected()
352 void ChartAxis::axisSelected()
355 {
353 {
356 qDebug()<<"TODO: axis clicked";
354 qDebug()<<"TODO: axis clicked";
357 }
355 }
358
356
359
357
360 void ChartAxis::createNumberLabels(QStringList &labels,qreal min, qreal max, int ticks) const
358 void ChartAxis::createNumberLabels(QStringList &labels,qreal min, qreal max, int ticks) const
361 {
359 {
362 Q_ASSERT(max>min);
360 Q_ASSERT(max>min);
363 Q_ASSERT(ticks>1);
361 Q_ASSERT(ticks>1);
364
362
365 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
363 int n = qMax(int(-qFloor(log10((max-min)/(ticks-1)))),0);
366 n++;
364 n++;
367 for (int i=0; i< ticks; i++) {
365 for (int i=0; i< ticks; i++) {
368 qreal value = min + (i * (max - min)/ (ticks-1));
366 qreal value = min + (i * (max - min)/ (ticks-1));
369 labels << QString::number(value,'f',n);
367 labels << QString::number(value,'f',n);
370 }
368 }
371 }
369 }
372
370
373 void ChartAxis::createCategoryLabels(QStringList &labels,qreal min, qreal max,const QStringList &categories) const
374 {
375 Q_ASSERT(max>min);
376 Q_UNUSED(max);
377
378 int x = qFloor(min+0.5);
379 int count = 0;
380
381 // Try to find category for x coordinate
382 while (count < categories.count()+1) {
383 if ((x < categories.count()) && (x >= 0)) {
384 labels << categories.at(x);
385 } else {
386 // No label for x coordinate
387 labels << "";
388 }
389 x++;
390 count++;
391 }
392 }
393
394 #include "moc_chartaxis_p.cpp"
371 #include "moc_chartaxis_p.cpp"
395
372
396 QTCOMMERCIALCHART_END_NAMESPACE
373 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,163 +1,162
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTAXIS_H
30 #ifndef CHARTAXIS_H
31 #define CHARTAXIS_H
31 #define CHARTAXIS_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include "chart_p.h"
34 #include "chart_p.h"
35 #include "axisanimation_p.h"
35 #include "axisanimation_p.h"
36 #include <QGraphicsItem>
36 #include <QGraphicsItem>
37 #include <QFont>
37 #include <QFont>
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 class QAbstractAxis;
41 class QAbstractAxis;
42 class ChartPresenter;
42 class ChartPresenter;
43
43
44 class ChartAxis : public Chart
44 class ChartAxis : public Chart
45 {
45 {
46 Q_OBJECT
46 Q_OBJECT
47 public:
47 public:
48 enum AxisType{ X_AXIS,Y_AXIS };
48 enum AxisType{ X_AXIS,Y_AXIS };
49
49
50 ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter);
50 ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter);
51 ~ChartAxis();
51 ~ChartAxis();
52
52
53 virtual AxisType axisType() const = 0;
53 virtual AxisType axisType() const = 0;
54
54
55 void setAxisOpacity(qreal opacity);
55 void setAxisOpacity(qreal opacity);
56 qreal axisOpacity() const;
56 qreal axisOpacity() const;
57
57
58 void setGridOpacity(qreal opacity);
58 void setGridOpacity(qreal opacity);
59 qreal gridOpacity() const;
59 qreal gridOpacity() const;
60
60
61 void setLabelsOpacity(qreal opacity);
61 void setLabelsOpacity(qreal opacity);
62 qreal labelsOpacity() const;
62 qreal labelsOpacity() const;
63
63
64 void setShadesOpacity(qreal opacity);
64 void setShadesOpacity(qreal opacity);
65 qreal shadesOpacity() const;
65 qreal shadesOpacity() const;
66
66
67 void setLabelsAngle(int angle);
67 void setLabelsAngle(int angle);
68 int labelsAngle()const { return m_labelsAngle; }
68 int labelsAngle()const { return m_labelsAngle; }
69
69
70 void setShadesBrush(const QBrush &brush);
70 void setShadesBrush(const QBrush &brush);
71 void setShadesPen(const QPen &pen);
71 void setShadesPen(const QPen &pen);
72
72
73 void setAxisPen(const QPen &pen);
73 void setAxisPen(const QPen &pen);
74 void setGridPen(const QPen &pen);
74 void setGridPen(const QPen &pen);
75
75
76 void setLabelsPen(const QPen &pen);
76 void setLabelsPen(const QPen &pen);
77 void setLabelsBrush(const QBrush &brush);
77 void setLabelsBrush(const QBrush &brush);
78 void setLabelsFont(const QFont &font);
78 void setLabelsFont(const QFont &font);
79
79
80 void setLayout(QVector<qreal> &layout);
80 void setLayout(QVector<qreal> &layout);
81 QVector<qreal> layout() const { return m_layoutVector; }
81 QVector<qreal> layout() const { return m_layoutVector; }
82
82
83 void setAnimation(AxisAnimation* animation);
83 void setAnimation(AxisAnimation* animation);
84 ChartAnimation* animation() const { return m_animation; };
84 ChartAnimation* animation() const { return m_animation; };
85
85
86 QRectF geometry() const { return m_rect; }
86 QRectF geometry() const { return m_rect; }
87
87
88 qreal minimumWidth();
88 qreal minimumWidth();
89 qreal minimumHeight();
89 qreal minimumHeight();
90
90
91 protected:
91 protected:
92 virtual void updateGeometry() = 0;
92 virtual void updateGeometry() = 0;
93 virtual QVector<qreal> calculateLayout() const = 0;
93 virtual QVector<qreal> calculateLayout() const = 0;
94 void createNumberLabels(QStringList &labels,qreal min, qreal max,int ticks) const;
94 void createNumberLabels(QStringList &labels,qreal min, qreal max,int ticks) const;
95 void createCategoryLabels(QStringList &labels,qreal min, qreal max,const QStringList &categories) const;
96
95
97 public Q_SLOTS:
96 public Q_SLOTS:
98 virtual void handleAxisUpdated();
97 virtual void handleAxisUpdated();
99 virtual void handleDomainUpdated();
98 virtual void handleDomainUpdated();
100 void handleGeometryChanged(const QRectF &size);
99 void handleGeometryChanged(const QRectF &size);
101
100
102 private:
101 private:
103 inline bool isEmpty();
102 inline bool isEmpty();
104 void createItems(int count);
103 void createItems(int count);
105 void deleteItems(int count);
104 void deleteItems(int count);
106 void updateLayout(QVector<qreal> &layout);
105 void updateLayout(QVector<qreal> &layout);
107 void axisSelected();
106 void axisSelected();
108
107
109 protected:
108 protected:
110 QAbstractAxis* m_chartAxis;
109 QAbstractAxis* m_chartAxis;
111 QRectF m_rect;
110 QRectF m_rect;
112 int m_labelsAngle;
111 int m_labelsAngle;
113 QScopedPointer<QGraphicsItemGroup> m_grid;
112 QScopedPointer<QGraphicsItemGroup> m_grid;
114 QScopedPointer<QGraphicsItemGroup> m_shades;
113 QScopedPointer<QGraphicsItemGroup> m_shades;
115 QScopedPointer<QGraphicsItemGroup> m_labels;
114 QScopedPointer<QGraphicsItemGroup> m_labels;
116 QScopedPointer<QGraphicsItemGroup> m_axis;
115 QScopedPointer<QGraphicsItemGroup> m_axis;
117 QVector<qreal> m_layoutVector;
116 QVector<qreal> m_layoutVector;
118 qreal m_min;
117 qreal m_min;
119 qreal m_max;
118 qreal m_max;
120 AxisAnimation *m_animation;
119 AxisAnimation *m_animation;
121 qreal m_minWidth;
120 qreal m_minWidth;
122 qreal m_minHeight;
121 qreal m_minHeight;
123 QFont m_font;
122 QFont m_font;
124
123
125 friend class AxisAnimation;
124 friend class AxisAnimation;
126 friend class AxisItem;
125 friend class AxisItem;
127
126
128 };
127 };
129
128
130 class AxisItem: public QGraphicsLineItem
129 class AxisItem: public QGraphicsLineItem
131 {
130 {
132
131
133 public:
132 public:
134 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
133 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
135
134
136 protected:
135 protected:
137 void mousePressEvent(QGraphicsSceneMouseEvent *event)
136 void mousePressEvent(QGraphicsSceneMouseEvent *event)
138 {
137 {
139 Q_UNUSED(event)
138 Q_UNUSED(event)
140 m_axis->axisSelected();
139 m_axis->axisSelected();
141 }
140 }
142
141
143 QRectF boundingRect() const
142 QRectF boundingRect() const
144 {
143 {
145 return shape().boundingRect();
144 return shape().boundingRect();
146 }
145 }
147
146
148 QPainterPath shape() const
147 QPainterPath shape() const
149 {
148 {
150 QPainterPath path = QGraphicsLineItem::shape();
149 QPainterPath path = QGraphicsLineItem::shape();
151 QRectF rect = path.boundingRect();
150 QRectF rect = path.boundingRect();
152 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0));
151 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0));
153 return path;
152 return path;
154 }
153 }
155
154
156 private:
155 private:
157 ChartAxis* m_axis;
156 ChartAxis* m_axis;
158
157
159 };
158 };
160
159
161 QTCOMMERCIALCHART_END_NAMESPACE
160 QTCOMMERCIALCHART_END_NAMESPACE
162
161
163 #endif /* AXISITEM_H_ */
162 #endif /* AXISITEM_H_ */
General Comments 0
You need to be logged in to leave comments. Login now