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