##// END OF EJS Templates
Renamed barcategory grephics item class which has been previously ommited
Marek Rosa -
r1918:a404d0b7b27f
parent child
Show More
@@ -1,161 +1,161
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 "chartbarcategoryaxisx_p.h"
21 #include "chartbarcategoryaxisx_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qbarcategoryaxis_p.h"
23 #include "qbarcategoryaxis_p.h"
24 #include <QDebug>
24 #include <QDebug>
25 #include <qmath.h>
25 #include <qmath.h>
26
26
27 static int label_padding = 5;
27 static int label_padding = 5;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 ChartCategoriesAxisX::ChartCategoriesAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
31 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
32 m_categoriesAxis(axis)
32 m_categoriesAxis(axis)
33 {
33 {
34
34
35 }
35 }
36
36
37 ChartCategoriesAxisX::~ChartCategoriesAxisX()
37 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
38 {
38 {
39 }
39 }
40
40
41 QVector<qreal> ChartCategoriesAxisX::calculateLayout() const
41 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
42 {
42 {
43 int count = m_categoriesAxis->d_ptr->count();
43 int count = m_categoriesAxis->d_ptr->count();
44
44
45 Q_ASSERT(count>=1);
45 Q_ASSERT(count>=1);
46
46
47 QVector<qreal> points;
47 QVector<qreal> points;
48 points.resize(count+2);
48 points.resize(count+2);
49
49
50 const qreal delta = m_rect.width()/(count);
50 const qreal delta = m_rect.width()/(count);
51 qreal offset =-m_min-0.5;
51 qreal offset =-m_min-0.5;
52
52
53 if(delta<1) return points;
53 if(delta<1) return points;
54
54
55 if(offset<=0) {
55 if(offset<=0) {
56 offset = int(offset * m_rect.width()/(m_max - m_min))%int(delta) + delta;
56 offset = int(offset * m_rect.width()/(m_max - m_min))%int(delta) + delta;
57 }
57 }
58 else {
58 else {
59 offset = int(offset * m_rect.width()/(m_max - m_min))%int(delta);
59 offset = int(offset * m_rect.width()/(m_max - m_min))%int(delta);
60 }
60 }
61
61
62 points[0] = m_rect.left();
62 points[0] = m_rect.left();
63 points[count+1] = m_rect.right();
63 points[count+1] = m_rect.right();
64
64
65 for (int i = 0; i < count; ++i) {
65 for (int i = 0; i < count; ++i) {
66 qreal x = offset + i * delta + m_rect.left();
66 qreal x = offset + i * delta + m_rect.left();
67 points[i+1] = x;
67 points[i+1] = x;
68 }
68 }
69 return points;
69 return points;
70 }
70 }
71
71
72 QStringList ChartCategoriesAxisX::createCategoryLabels(const QVector<qreal>& layout) const
72 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
73 {
73 {
74 QStringList result;
74 QStringList result;
75 qreal d = (m_max - m_min)/m_rect.width();
75 qreal d = (m_max - m_min)/m_rect.width();
76 for (int i = 0;i < layout.count()-1; ++i) {
76 for (int i = 0;i < layout.count()-1; ++i) {
77 qreal x = qFloor((((layout[i+1] + layout[i])/2-m_rect.left())*d + m_min+0.5));
77 qreal x = qFloor((((layout[i+1] + layout[i])/2-m_rect.left())*d + m_min+0.5));
78 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
78 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
79 result << m_categoriesAxis->categories().at(x);
79 result << m_categoriesAxis->categories().at(x);
80 }
80 }
81 else {
81 else {
82 // No label for x coordinate
82 // No label for x coordinate
83 result << "";
83 result << "";
84 }
84 }
85 }
85 }
86 result << "";
86 result << "";
87 return result;
87 return result;
88 }
88 }
89
89
90
90
91 void ChartCategoriesAxisX::updateGeometry()
91 void ChartBarCategoryAxisX::updateGeometry()
92 {
92 {
93 const QVector<qreal>& layout = ChartAxis::layout();
93 const QVector<qreal>& layout = ChartAxis::layout();
94
94
95 m_minWidth = 0;
95 m_minWidth = 0;
96 m_minHeight = 0;
96 m_minHeight = 0;
97
97
98 if(layout.isEmpty()) return;
98 if(layout.isEmpty()) return;
99
99
100 QStringList ticksList = createCategoryLabels(layout);
100 QStringList ticksList = createCategoryLabels(layout);
101
101
102 QList<QGraphicsItem *> lines = m_grid->childItems();
102 QList<QGraphicsItem *> lines = m_grid->childItems();
103 QList<QGraphicsItem *> labels = m_labels->childItems();
103 QList<QGraphicsItem *> labels = m_labels->childItems();
104 QList<QGraphicsItem *> shades = m_shades->childItems();
104 QList<QGraphicsItem *> shades = m_shades->childItems();
105 QList<QGraphicsItem *> axis = m_arrow->childItems();
105 QList<QGraphicsItem *> axis = m_arrow->childItems();
106
106
107 Q_ASSERT(labels.size() == ticksList.size());
107 Q_ASSERT(labels.size() == ticksList.size());
108 Q_ASSERT(layout.size() == ticksList.size());
108 Q_ASSERT(layout.size() == ticksList.size());
109
109
110 const qreal delta = m_rect.width()/(m_categoriesAxis->d_ptr->count());
110 const qreal delta = m_rect.width()/(m_categoriesAxis->d_ptr->count());
111
111
112 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
112 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
113 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
113 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
114
114
115 qreal width = m_rect.left();
115 qreal width = m_rect.left();
116 for (int i = 0; i < layout.size(); ++i) {
116 for (int i = 0; i < layout.size(); ++i) {
117 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
117 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
118 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
118 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
119 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
119 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
120 labelItem->setText(ticksList.at(i));
120 labelItem->setText(ticksList.at(i));
121 const QRectF& rect = labelItem->boundingRect();
121 const QRectF& rect = labelItem->boundingRect();
122 QPointF center = rect.center();
122 QPointF center = rect.center();
123 labelItem->setTransformOriginPoint(center.x(), center.y());
123 labelItem->setTransformOriginPoint(center.x(), center.y());
124
124
125 if(i==0){
125 if(i==0){
126 labelItem->setPos(layout[i+1] - (delta)/2 - center.x(), m_rect.bottom() + label_padding);
126 labelItem->setPos(layout[i+1] - (delta)/2 - center.x(), m_rect.bottom() + label_padding);
127 }else{
127 }else{
128 labelItem->setPos(layout[i] + (delta)/2 - center.x(), m_rect.bottom() + label_padding);
128 labelItem->setPos(layout[i] + (delta)/2 - center.x(), m_rect.bottom() + label_padding);
129 }
129 }
130
130
131 if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()>m_rect.right()) {
131 if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()>m_rect.right()) {
132 labelItem->setVisible(false);
132 labelItem->setVisible(false);
133 }
133 }
134 else {
134 else {
135 labelItem->setVisible(true);
135 labelItem->setVisible(true);
136 width=rect.width()+labelItem->pos().x();
136 width=rect.width()+labelItem->pos().x();
137 }
137 }
138
138
139 m_minWidth+=rect.width();
139 m_minWidth+=rect.width();
140 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
140 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
141
141
142 if ((i+1)%2 && i>1) {
142 if ((i+1)%2 && i>1) {
143 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
143 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
144 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
144 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
145 }
145 }
146 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
146 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
147 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
147 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
148 }
148 }
149 }
149 }
150
150
151 void ChartCategoriesAxisX::handleAxisUpdated()
151 void ChartBarCategoryAxisX::handleAxisUpdated()
152 {
152 {
153 if(m_categoriesAxis->categories()!=m_categories)
153 if(m_categoriesAxis->categories()!=m_categories)
154 {
154 {
155 m_categories=m_categoriesAxis->categories();
155 m_categories=m_categoriesAxis->categories();
156 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) updateGeometry();
156 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) updateGeometry();
157 }
157 }
158 ChartAxis::handleAxisUpdated();
158 ChartAxis::handleAxisUpdated();
159 }
159 }
160
160
161 QTCOMMERCIALCHART_END_NAMESPACE
161 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +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 QBarCategoryAxis;
39 class QBarCategoryAxis;
40
40
41 class ChartCategoriesAxisX : public ChartAxis
41 class ChartBarCategoryAxisX : public ChartAxis
42 {
42 {
43 public:
43 public:
44 ChartCategoriesAxisX(QBarCategoryAxis *axis, ChartPresenter *presenter);
44 ChartBarCategoryAxisX(QBarCategoryAxis *axis, ChartPresenter *presenter);
45 ~ChartCategoriesAxisX();
45 ~ChartBarCategoryAxisX();
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:
52 private:
53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
54 Q_SLOTS
54 Q_SLOTS
55 void handleAxisUpdated();
55 void handleAxisUpdated();
56
56
57 private:
57 private:
58 QStringList m_categories;
58 QStringList m_categories;
59 QBarCategoryAxis *m_categoriesAxis;
59 QBarCategoryAxis *m_categoriesAxis;
60 };
60 };
61
61
62 QTCOMMERCIALCHART_END_NAMESPACE
62 QTCOMMERCIALCHART_END_NAMESPACE
63
63
64 #endif /* CHARTCATEGORIESAXISX_H */
64 #endif /* CHARTCATEGORIESAXISX_H */
@@ -1,163 +1,163
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 <QDebug>
25 #include <QDebug>
26
26
27 static int label_padding = 5;
27 static int label_padding = 5;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 ChartCategoriesAxisY::ChartCategoriesAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
31 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
32 m_categoriesAxis(axis)
32 m_categoriesAxis(axis)
33 {
33 {
34 }
34 }
35
35
36 ChartCategoriesAxisY::~ChartCategoriesAxisY()
36 ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
37 {
37 {
38 }
38 }
39
39
40 QVector<qreal> ChartCategoriesAxisY::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 qreal delta = m_rect.height()/(count);
49 const qreal delta = m_rect.height()/(count);
50 qreal offset = - m_min - 0.5;
50 qreal offset = - m_min - 0.5;
51
51
52 if(delta<1) return points;
52 if(delta<1) return points;
53
53
54 if(offset<=0) {
54 if(offset<=0) {
55 offset = int(offset * m_rect.height()/(m_max - m_min))%int(delta) + delta;
55 offset = int(offset * m_rect.height()/(m_max - m_min))%int(delta) + delta;
56 }
56 }
57 else {
57 else {
58 offset = int(offset * m_rect.height()/(m_max - m_min))%int(delta);
58 offset = int(offset * m_rect.height()/(m_max - m_min))%int(delta);
59 }
59 }
60
60
61 points[0] = m_rect.bottom();
61 points[0] = m_rect.bottom();
62 points[count+1] = m_rect.top();
62 points[count+1] = m_rect.top();
63
63
64 for (int i = 0; i < count; ++i) {
64 for (int i = 0; i < count; ++i) {
65 int y = m_rect.bottom() - i * delta - offset;
65 int y = m_rect.bottom() - i * delta - offset;
66 points[i+1] = y;
66 points[i+1] = y;
67 }
67 }
68 return points;
68 return points;
69 }
69 }
70
70
71 QStringList ChartCategoriesAxisY::createCategoryLabels(const QVector<qreal>& layout) const
71 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
72 {
72 {
73 QStringList result;
73 QStringList result;
74 qreal d = (m_max - m_min)/m_rect.height();
74 qreal d = (m_max - m_min)/m_rect.height();
75 for (int i = 0;i < layout.count()-1; ++i) {
75 for (int i = 0;i < layout.count()-1; ++i) {
76 qreal x = qFloor(((m_rect.height()- (layout[i+1] + layout[i])/2 + m_rect.top())*d + m_min+0.5));
76 qreal x = qFloor(((m_rect.height()- (layout[i+1] + layout[i])/2 + m_rect.top())*d + m_min+0.5));
77 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
77 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
78 result << m_categoriesAxis->categories().at(x);
78 result << m_categoriesAxis->categories().at(x);
79 }
79 }
80 else {
80 else {
81 // No label for x coordinate
81 // No label for x coordinate
82 result << "";
82 result << "";
83 }
83 }
84 }
84 }
85 result << "";
85 result << "";
86 return result;
86 return result;
87 }
87 }
88
88
89 void ChartCategoriesAxisY::updateGeometry()
89 void ChartBarCategoryAxisY::updateGeometry()
90 {
90 {
91 const QVector<qreal>& layout = ChartAxis::layout();
91 const QVector<qreal>& layout = ChartAxis::layout();
92
92
93 m_minWidth = 0;
93 m_minWidth = 0;
94 m_minHeight = 0;
94 m_minHeight = 0;
95
95
96 if(layout.isEmpty()) return;
96 if(layout.isEmpty()) return;
97
97
98 QStringList ticksList = createCategoryLabels(layout);
98 QStringList ticksList = createCategoryLabels(layout);
99
99
100 QList<QGraphicsItem *> lines = m_grid->childItems();
100 QList<QGraphicsItem *> lines = m_grid->childItems();
101 QList<QGraphicsItem *> labels = m_labels->childItems();
101 QList<QGraphicsItem *> labels = m_labels->childItems();
102 QList<QGraphicsItem *> shades = m_shades->childItems();
102 QList<QGraphicsItem *> shades = m_shades->childItems();
103 QList<QGraphicsItem *> axis = m_arrow->childItems();
103 QList<QGraphicsItem *> axis = m_arrow->childItems();
104
104
105 Q_ASSERT(labels.size() == ticksList.size());
105 Q_ASSERT(labels.size() == ticksList.size());
106 Q_ASSERT(layout.size() == ticksList.size());
106 Q_ASSERT(layout.size() == ticksList.size());
107
107
108 const qreal delta = m_rect.height()/(m_categoriesAxis->d_ptr->count());
108 const qreal delta = m_rect.height()/(m_categoriesAxis->d_ptr->count());
109
109
110 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
110 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
111 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
111 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
112
112
113 qreal height = m_rect.bottom();
113 qreal height = m_rect.bottom();
114 for (int i = 0; i < layout.size(); ++i) {
114 for (int i = 0; i < layout.size(); ++i) {
115 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
115 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
116 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
116 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
117 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
117 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
118 labelItem->setText(ticksList.at(i));
118 labelItem->setText(ticksList.at(i));
119 const QRectF& rect = labelItem->boundingRect();
119 const QRectF& rect = labelItem->boundingRect();
120 QPointF center = rect.center();
120 QPointF center = rect.center();
121 labelItem->setTransformOriginPoint(center.x(), center.y());
121 labelItem->setTransformOriginPoint(center.x(), center.y());
122
122
123 if(i==0) {
123 if(i==0) {
124 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y());
124 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y());
125 }
125 }
126 else {
126 else {
127 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i] - (delta)/2 - center.y());
127 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i] - (delta)/2 - center.y());
128 }
128 }
129
129
130 if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < m_rect.top()) {
130 if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < m_rect.top()) {
131 labelItem->setVisible(false);
131 labelItem->setVisible(false);
132 }
132 }
133 else {
133 else {
134 labelItem->setVisible(true);
134 labelItem->setVisible(true);
135 height=labelItem->pos().y();
135 height=labelItem->pos().y();
136 }
136 }
137
137
138 m_minWidth=qMax(rect.width()+label_padding,m_minWidth);
138 m_minWidth=qMax(rect.width()+label_padding,m_minWidth);
139 m_minHeight+=rect.height();
139 m_minHeight+=rect.height();
140
140
141 if ((i+1)%2 && i>1) {
141 if ((i+1)%2 && i>1) {
142 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
142 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
143 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
143 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
144 }
144 }
145 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
145 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
146 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
146 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
147 }
147 }
148 }
148 }
149
149
150 void ChartCategoriesAxisY::handleAxisUpdated()
150 void ChartBarCategoryAxisY::handleAxisUpdated()
151 {
151 {
152
152
153 if(m_categoriesAxis->categories()!=m_categories)
153 if(m_categoriesAxis->categories()!=m_categories)
154 {
154 {
155 m_categories=m_categoriesAxis->categories();
155 m_categories=m_categoriesAxis->categories();
156 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) {
156 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) {
157 updateGeometry();
157 updateGeometry();
158 }
158 }
159 }
159 }
160 ChartAxis::handleAxisUpdated();
160 ChartAxis::handleAxisUpdated();
161 }
161 }
162
162
163 QTCOMMERCIALCHART_END_NAMESPACE
163 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,63 +1,63
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 QBarCategoryAxis;
38 class QBarCategoryAxis;
39 class ChartPresenter;
39 class ChartPresenter;
40
40
41 class ChartCategoriesAxisY : public ChartAxis
41 class ChartBarCategoryAxisY : public ChartAxis
42 {
42 {
43 public:
43 public:
44 ChartCategoriesAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter);
44 ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter);
45 ~ChartCategoriesAxisY();
45 ~ChartBarCategoryAxisY();
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:
52 private:
53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
54 Q_SLOTS
54 Q_SLOTS
55 void handleAxisUpdated();
55 void handleAxisUpdated();
56 private:
56 private:
57 QStringList m_categories;
57 QStringList m_categories;
58 QBarCategoryAxis *m_categoriesAxis;
58 QBarCategoryAxis *m_categoriesAxis;
59 };
59 };
60
60
61 QTCOMMERCIALCHART_END_NAMESPACE
61 QTCOMMERCIALCHART_END_NAMESPACE
62
62
63 #endif /* CHARTCATEGORIESAXISY_H */
63 #endif /* CHARTCATEGORIESAXISY_H */
@@ -1,487 +1,487
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 "qbarcategoryaxis.h"
21 #include "qbarcategoryaxis.h"
22 #include "qbarcategoryaxis_p.h"
22 #include "qbarcategoryaxis_p.h"
23 #include "chartbarcategoryaxisx_p.h"
23 #include "chartbarcategoryaxisx_p.h"
24 #include "chartbarcategoryaxisy_p.h"
24 #include "chartbarcategoryaxisy_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "chartdataset_p.h"
26 #include "chartdataset_p.h"
27 #include <qmath.h>
27 #include <qmath.h>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 /*!
30 /*!
31 \class QBarCategoryAxis
31 \class QBarCategoryAxis
32 \brief The QBarCategoryAxis class is used for manipulating chart's axis.
32 \brief The QBarCategoryAxis class is used for manipulating chart's axis.
33 \mainclass
33 \mainclass
34
34
35 BarCategoryAxis can be setup to show axis line with tick marks, grid lines and shades.
35 BarCategoryAxis can be setup to show axis line with tick marks, grid lines and shades.
36 Categories are drawn between ticks. Note that you can use this also with lineseries too.
36 Categories are drawn between ticks. Note that you can use this also with lineseries too.
37 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
37 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
38
38
39 Example code on how to use QBarCategoryAxis.
39 Example code on how to use QBarCategoryAxis.
40 \code
40 \code
41 QChartView *chartView = new QChartView;
41 QChartView *chartView = new QChartView;
42 QBarSeries *series = new QBarSeries;
42 QBarSeries *series = new QBarSeries;
43 // ...
43 // ...
44 chartView->chart()->addSeries(series);
44 chartView->chart()->addSeries(series);
45 chartView->chart()->createDefaultAxes();
45 chartView->chart()->createDefaultAxes();
46
46
47 QBarCategoryAxis *axisX = new QBarCategoryAxis;
47 QBarCategoryAxis *axisX = new QBarCategoryAxis;
48 QStringList categories;
48 QStringList categories;
49 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
49 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
50 axisX->append(categories);
50 axisX->append(categories);
51 chartView->chart()->setAxisX(series, axisX);
51 chartView->chart()->setAxisX(series, axisX);
52 \endcode
52 \endcode
53 */
53 */
54
54
55 /*!
55 /*!
56 \qmlclass BarCategoryAxis QBarCategoryAxis
56 \qmlclass BarCategoryAxis QBarCategoryAxis
57 \inherits AbstractAxis
57 \inherits AbstractAxis
58 \brief The Axis element is used for manipulating chart's axes.
58 \brief The Axis element is used for manipulating chart's axes.
59
59
60 Axis can be setup to show axis line with tick marks, grid lines and shades.
60 Axis can be setup to show axis line with tick marks, grid lines and shades.
61 Categories are drawn between ticks. Note that you can use this also with lineseries too.
61 Categories are drawn between ticks. Note that you can use this also with lineseries too.
62
62
63 To access BarCategoryAxis you can use ChartView API. For example:
63 To access BarCategoryAxis you can use ChartView API. For example:
64 \code
64 \code
65 ChartView {
65 ChartView {
66 BarCategoryAxis {
66 BarCategoryAxis {
67 id: categoryAxis
67 id: categoryAxis
68 categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ]
68 categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ]
69 }
69 }
70 // Add a few series...
70 // Add a few series...
71 }
71 }
72 \endcode
72 \endcode
73 */
73 */
74
74
75 /*!
75 /*!
76 \property QBarCategoryAxis::categories
76 \property QBarCategoryAxis::categories
77 Defines the categories of axis
77 Defines the categories of axis
78 */
78 */
79 /*!
79 /*!
80 \qmlproperty QStringList BarCategoryAxis::categories
80 \qmlproperty QStringList BarCategoryAxis::categories
81 Defines the categories of axis
81 Defines the categories of axis
82 */
82 */
83
83
84 /*!
84 /*!
85 \property QBarCategoryAxis::min
85 \property QBarCategoryAxis::min
86 Defines the minimum value on the axis.
86 Defines the minimum value on the axis.
87 */
87 */
88 /*!
88 /*!
89 \qmlproperty QString BarCategoryAxis::min
89 \qmlproperty QString BarCategoryAxis::min
90 Defines the minimum value on the axis.
90 Defines the minimum value on the axis.
91 */
91 */
92
92
93 /*!
93 /*!
94 \property QBarCategoryAxis::max
94 \property QBarCategoryAxis::max
95 Defines the maximum value on the axis.
95 Defines the maximum value on the axis.
96 */
96 */
97 /*!
97 /*!
98 \qmlproperty QString BarCategoryAxis::max
98 \qmlproperty QString BarCategoryAxis::max
99 Defines the maximum value on the axis.
99 Defines the maximum value on the axis.
100 */
100 */
101
101
102
102
103 /*!
103 /*!
104 \fn void QBarCategoryAxis::categoriesChanged()
104 \fn void QBarCategoryAxis::categoriesChanged()
105 Axis emits signal when the categories of the axis has changed.
105 Axis emits signal when the categories of the axis has changed.
106 */
106 */
107 /*!
107 /*!
108 \fn void QBarCategoryAxis::minChanged(const QString &min)
108 \fn void QBarCategoryAxis::minChanged(const QString &min)
109 Axis emits signal when \a min of axis has changed.
109 Axis emits signal when \a min of axis has changed.
110 */
110 */
111 /*!
111 /*!
112 \qmlsignal BarCategoryAxis::onMinChanged(const QString &min)
112 \qmlsignal BarCategoryAxis::onMinChanged(const QString &min)
113 Axis emits signal when \a min of axis has changed.
113 Axis emits signal when \a min of axis has changed.
114 */
114 */
115
115
116 /*!
116 /*!
117 \fn void QBarCategoryAxis::maxChanged(const QString &max)
117 \fn void QBarCategoryAxis::maxChanged(const QString &max)
118 Axis emits signal when \a max of axis has changed.
118 Axis emits signal when \a max of axis has changed.
119 */
119 */
120 /*!
120 /*!
121 \qmlsignal BarCategoryAxis::onMaxChanged(const QString &max)
121 \qmlsignal BarCategoryAxis::onMaxChanged(const QString &max)
122 Axis emits signal when \a max of axis has changed.
122 Axis emits signal when \a max of axis has changed.
123 */
123 */
124
124
125 /*!
125 /*!
126 \fn void QBarCategoryAxis::rangeChanged(const QString &min, const QString &max)
126 \fn void QBarCategoryAxis::rangeChanged(const QString &min, const QString &max)
127 Axis emits signal when \a min or \a max of axis has changed.
127 Axis emits signal when \a min or \a max of axis has changed.
128 */
128 */
129
129
130 /*!
130 /*!
131 Constructs an axis object which is a child of \a parent.
131 Constructs an axis object which is a child of \a parent.
132 */
132 */
133 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
133 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
134 QAbstractAxis(*new QBarCategoryAxisPrivate(this),parent)
134 QAbstractAxis(*new QBarCategoryAxisPrivate(this),parent)
135 {
135 {
136 }
136 }
137
137
138 /*!
138 /*!
139 Destroys the object
139 Destroys the object
140 */
140 */
141 QBarCategoryAxis::~QBarCategoryAxis()
141 QBarCategoryAxis::~QBarCategoryAxis()
142 {
142 {
143 Q_D(QBarCategoryAxis);
143 Q_D(QBarCategoryAxis);
144 if(d->m_dataset){
144 if(d->m_dataset){
145 d->m_dataset->removeAxis(this);
145 d->m_dataset->removeAxis(this);
146 }
146 }
147 }
147 }
148
148
149 /*!
149 /*!
150 \internal
150 \internal
151 */
151 */
152 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
152 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
153 {
153 {
154
154
155 }
155 }
156
156
157 /*!
157 /*!
158 Appends \a categories to axis
158 Appends \a categories to axis
159 */
159 */
160 void QBarCategoryAxis::append(const QStringList &categories)
160 void QBarCategoryAxis::append(const QStringList &categories)
161 {
161 {
162 if(categories.isEmpty()) return;
162 if(categories.isEmpty()) return;
163
163
164 Q_D(QBarCategoryAxis);
164 Q_D(QBarCategoryAxis);
165 if (d->m_categories.isEmpty()) {
165 if (d->m_categories.isEmpty()) {
166 d->m_categories.append(categories);
166 d->m_categories.append(categories);
167 setRange(categories.first(),categories.last());
167 setRange(categories.first(),categories.last());
168 }else{
168 }else{
169 d->m_categories.append(categories);
169 d->m_categories.append(categories);
170 d->emitUpdated();
170 d->emitUpdated();
171 }
171 }
172 emit categoriesChanged();
172 emit categoriesChanged();
173 }
173 }
174
174
175 /*!
175 /*!
176 Appends \a category to axis
176 Appends \a category to axis
177 */
177 */
178 void QBarCategoryAxis::append(const QString &category)
178 void QBarCategoryAxis::append(const QString &category)
179 {
179 {
180 Q_D(QBarCategoryAxis);
180 Q_D(QBarCategoryAxis);
181 if (d->m_categories.isEmpty()) {
181 if (d->m_categories.isEmpty()) {
182 d->m_categories.append(category);
182 d->m_categories.append(category);
183 setRange(category,category);
183 setRange(category,category);
184 }else{
184 }else{
185 d->m_categories.append(category);
185 d->m_categories.append(category);
186 d->emitUpdated();
186 d->emitUpdated();
187 }
187 }
188 emit categoriesChanged();
188 emit categoriesChanged();
189 }
189 }
190
190
191 /*!
191 /*!
192 Removes \a category from axis
192 Removes \a category from axis
193 */
193 */
194 void QBarCategoryAxis::remove(const QString &category)
194 void QBarCategoryAxis::remove(const QString &category)
195 {
195 {
196 Q_D(QBarCategoryAxis);
196 Q_D(QBarCategoryAxis);
197 if (d->m_categories.contains(category)) {
197 if (d->m_categories.contains(category)) {
198 d->m_categories.removeAt(d->m_categories.indexOf(category));
198 d->m_categories.removeAt(d->m_categories.indexOf(category));
199 if(!d->m_categories.isEmpty())
199 if(!d->m_categories.isEmpty())
200 setRange(d->m_categories.first(),d->m_categories.last());
200 setRange(d->m_categories.first(),d->m_categories.last());
201 else
201 else
202 setRange(QString::null,QString::null);
202 setRange(QString::null,QString::null);
203 emit categoriesChanged();
203 emit categoriesChanged();
204 }
204 }
205 }
205 }
206
206
207 /*!
207 /*!
208 Inserts \a category to axis at \a index
208 Inserts \a category to axis at \a index
209 */
209 */
210 void QBarCategoryAxis::insert(int index, const QString &category)
210 void QBarCategoryAxis::insert(int index, const QString &category)
211 {
211 {
212 Q_D(QBarCategoryAxis);
212 Q_D(QBarCategoryAxis);
213 if (d->m_categories.isEmpty()) {
213 if (d->m_categories.isEmpty()) {
214 d->m_categories.insert(index,category);
214 d->m_categories.insert(index,category);
215 setRange(category,category);
215 setRange(category,category);
216 }else{
216 }else{
217 d->m_categories.insert(index,category);
217 d->m_categories.insert(index,category);
218 d->emitUpdated();
218 d->emitUpdated();
219 }
219 }
220 emit categoriesChanged();
220 emit categoriesChanged();
221 }
221 }
222
222
223 /*!
223 /*!
224 Replaces \a oldCategory with \a newCategory.
224 Replaces \a oldCategory with \a newCategory.
225 If \a oldCategory does not exits on the axis nothing is done.
225 If \a oldCategory does not exits on the axis nothing is done.
226 */
226 */
227 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
227 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
228 {
228 {
229 Q_D(QBarCategoryAxis);
229 Q_D(QBarCategoryAxis);
230 int pos = d->m_categories.indexOf(oldCategory);
230 int pos = d->m_categories.indexOf(oldCategory);
231 if (pos != -1) {
231 if (pos != -1) {
232 d->m_categories.replace(pos, newCategory);
232 d->m_categories.replace(pos, newCategory);
233 d->emitUpdated();
233 d->emitUpdated();
234 emit categoriesChanged();
234 emit categoriesChanged();
235 }
235 }
236 }
236 }
237
237
238 /*!
238 /*!
239 Removes all categories.
239 Removes all categories.
240 */
240 */
241 void QBarCategoryAxis::clear()
241 void QBarCategoryAxis::clear()
242 {
242 {
243 Q_D(QBarCategoryAxis);
243 Q_D(QBarCategoryAxis);
244 d->m_categories.clear();
244 d->m_categories.clear();
245 setRange(QString::null,QString::null);
245 setRange(QString::null,QString::null);
246 emit categoriesChanged();
246 emit categoriesChanged();
247 }
247 }
248
248
249 void QBarCategoryAxis::setCategories(const QStringList &categories)
249 void QBarCategoryAxis::setCategories(const QStringList &categories)
250 {
250 {
251 Q_D(QBarCategoryAxis);
251 Q_D(QBarCategoryAxis);
252 if(d->m_categories!=categories) {
252 if(d->m_categories!=categories) {
253 d->m_categories = categories;
253 d->m_categories = categories;
254 setRange(categories.first(),categories.last());
254 setRange(categories.first(),categories.last());
255 emit categoriesChanged();
255 emit categoriesChanged();
256 }
256 }
257 }
257 }
258
258
259 QStringList QBarCategoryAxis::categories()
259 QStringList QBarCategoryAxis::categories()
260 {
260 {
261 Q_D(QBarCategoryAxis);
261 Q_D(QBarCategoryAxis);
262 return d->m_categories;
262 return d->m_categories;
263 }
263 }
264
264
265 /*!
265 /*!
266 Returns number of categories.
266 Returns number of categories.
267 */
267 */
268 int QBarCategoryAxis::count() const
268 int QBarCategoryAxis::count() const
269 {
269 {
270 Q_D(const QBarCategoryAxis);
270 Q_D(const QBarCategoryAxis);
271 return d->m_categories.count();
271 return d->m_categories.count();
272 }
272 }
273
273
274 /*!
274 /*!
275 Returns category at \a index. Index must be valid.
275 Returns category at \a index. Index must be valid.
276 */
276 */
277 QString QBarCategoryAxis::at(int index) const
277 QString QBarCategoryAxis::at(int index) const
278 {
278 {
279 Q_D(const QBarCategoryAxis);
279 Q_D(const QBarCategoryAxis);
280 return d->m_categories.at(index);
280 return d->m_categories.at(index);
281 }
281 }
282
282
283 /*!
283 /*!
284 Sets minimum category to \a min.
284 Sets minimum category to \a min.
285 */
285 */
286 void QBarCategoryAxis::setMin(const QString& min)
286 void QBarCategoryAxis::setMin(const QString& min)
287 {
287 {
288 Q_D(QBarCategoryAxis);
288 Q_D(QBarCategoryAxis);
289 setRange(min,d->m_maxCategory);
289 setRange(min,d->m_maxCategory);
290 }
290 }
291
291
292 /*!
292 /*!
293 Returns minimum category.
293 Returns minimum category.
294 */
294 */
295 QString QBarCategoryAxis::min() const
295 QString QBarCategoryAxis::min() const
296 {
296 {
297 Q_D(const QBarCategoryAxis);
297 Q_D(const QBarCategoryAxis);
298 return d->m_minCategory;
298 return d->m_minCategory;
299 }
299 }
300
300
301 /*!
301 /*!
302 Sets maximum category to \a max.
302 Sets maximum category to \a max.
303 */
303 */
304 void QBarCategoryAxis::setMax(const QString& max)
304 void QBarCategoryAxis::setMax(const QString& max)
305 {
305 {
306 Q_D(QBarCategoryAxis);
306 Q_D(QBarCategoryAxis);
307 setRange(d->m_minCategory,max);
307 setRange(d->m_minCategory,max);
308 }
308 }
309
309
310 /*!
310 /*!
311 Returns maximum category
311 Returns maximum category
312 */
312 */
313 QString QBarCategoryAxis::max() const
313 QString QBarCategoryAxis::max() const
314 {
314 {
315 Q_D(const QBarCategoryAxis);
315 Q_D(const QBarCategoryAxis);
316 return d->m_maxCategory;
316 return d->m_maxCategory;
317 }
317 }
318
318
319 /*!
319 /*!
320 Sets range from \a minCategory to \a maxCategory
320 Sets range from \a minCategory to \a maxCategory
321 */
321 */
322 void QBarCategoryAxis::setRange(const QString& minCategory, const QString& maxCategory)
322 void QBarCategoryAxis::setRange(const QString& minCategory, const QString& maxCategory)
323 {
323 {
324 Q_D(QBarCategoryAxis);
324 Q_D(QBarCategoryAxis);
325
325
326 bool changed = false;
326 bool changed = false;
327
327
328 //special case
328 //special case
329 if(minCategory.isNull() && maxCategory.isNull()){
329 if(minCategory.isNull() && maxCategory.isNull()){
330 d->m_minCategory = minCategory;
330 d->m_minCategory = minCategory;
331 d->m_maxCategory = maxCategory;
331 d->m_maxCategory = maxCategory;
332 d->m_min = 0;
332 d->m_min = 0;
333 d->m_max = 0;
333 d->m_max = 0;
334 emit minChanged(minCategory);
334 emit minChanged(minCategory);
335 emit maxChanged(maxCategory);
335 emit maxChanged(maxCategory);
336 d->m_count=0;
336 d->m_count=0;
337 emit rangeChanged(d->m_minCategory,d->m_maxCategory);
337 emit rangeChanged(d->m_minCategory,d->m_maxCategory);
338 d->emitUpdated();
338 d->emitUpdated();
339 }
339 }
340
340
341 if(d->m_categories.indexOf(d->m_maxCategory)<d->m_categories.indexOf(d->m_minCategory)) return;
341 if(d->m_categories.indexOf(d->m_maxCategory)<d->m_categories.indexOf(d->m_minCategory)) return;
342
342
343 if (!minCategory.isEmpty() && d->m_minCategory!=minCategory && d->m_categories.contains(minCategory)) {
343 if (!minCategory.isEmpty() && d->m_minCategory!=minCategory && d->m_categories.contains(minCategory)) {
344 d->m_minCategory = minCategory;
344 d->m_minCategory = minCategory;
345 d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5;
345 d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5;
346 changed = true;
346 changed = true;
347 emit minChanged(minCategory);
347 emit minChanged(minCategory);
348 }
348 }
349
349
350 if (!maxCategory.isEmpty() && d->m_maxCategory!=maxCategory && d->m_categories.contains(maxCategory)) {
350 if (!maxCategory.isEmpty() && d->m_maxCategory!=maxCategory && d->m_categories.contains(maxCategory)) {
351 d->m_maxCategory = maxCategory;
351 d->m_maxCategory = maxCategory;
352 d->m_max = d->m_categories.indexOf(d->m_maxCategory) + 0.5;
352 d->m_max = d->m_categories.indexOf(d->m_maxCategory) + 0.5;
353 changed = true;
353 changed = true;
354 emit maxChanged(maxCategory);
354 emit maxChanged(maxCategory);
355 }
355 }
356
356
357 if (changed) {
357 if (changed) {
358 d->m_count=d->m_max - d->m_min;
358 d->m_count=d->m_max - d->m_min;
359 emit rangeChanged(d->m_minCategory,d->m_maxCategory);
359 emit rangeChanged(d->m_minCategory,d->m_maxCategory);
360 d->emitUpdated();
360 d->emitUpdated();
361 }
361 }
362 }
362 }
363
363
364 /*!
364 /*!
365 Returns the type of the axis
365 Returns the type of the axis
366 */
366 */
367 QAbstractAxis::AxisType QBarCategoryAxis::type() const
367 QAbstractAxis::AxisType QBarCategoryAxis::type() const
368 {
368 {
369 return AxisTypeBarCategory;
369 return AxisTypeBarCategory;
370 }
370 }
371
371
372 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
372 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
373
373
374 QBarCategoryAxisPrivate::QBarCategoryAxisPrivate(QBarCategoryAxis* q):
374 QBarCategoryAxisPrivate::QBarCategoryAxisPrivate(QBarCategoryAxis* q):
375 QAbstractAxisPrivate(q),
375 QAbstractAxisPrivate(q),
376 m_min(0.0),
376 m_min(0.0),
377 m_max(0.0),
377 m_max(0.0),
378 m_count(0)
378 m_count(0)
379 {
379 {
380
380
381 }
381 }
382
382
383 QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate()
383 QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate()
384 {
384 {
385
385
386 }
386 }
387
387
388 void QBarCategoryAxisPrivate::setMin(const QVariant &min)
388 void QBarCategoryAxisPrivate::setMin(const QVariant &min)
389 {
389 {
390 setRange(min,m_maxCategory);
390 setRange(min,m_maxCategory);
391 }
391 }
392
392
393 void QBarCategoryAxisPrivate::setMax(const QVariant &max)
393 void QBarCategoryAxisPrivate::setMax(const QVariant &max)
394 {
394 {
395 setRange(m_minCategory,max);
395 setRange(m_minCategory,max);
396 }
396 }
397
397
398 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
398 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
399 {
399 {
400 Q_Q(QBarCategoryAxis);
400 Q_Q(QBarCategoryAxis);
401 QString value1 = min.toString();
401 QString value1 = min.toString();
402 QString value2 = max.toString();
402 QString value2 = max.toString();
403 q->setRange(value1,value2);
403 q->setRange(value1,value2);
404 }
404 }
405
405
406 void QBarCategoryAxisPrivate::handleDomainUpdated()
406 void QBarCategoryAxisPrivate::handleDomainUpdated()
407 {
407 {
408 Q_Q(QBarCategoryAxis);
408 Q_Q(QBarCategoryAxis);
409 Domain* domain = qobject_cast<Domain*>(sender());
409 Domain* domain = qobject_cast<Domain*>(sender());
410
410
411 if(m_orientation==Qt::Horizontal) {
411 if(m_orientation==Qt::Horizontal) {
412 m_min = domain->minX();
412 m_min = domain->minX();
413 m_max = domain->maxX();
413 m_max = domain->maxX();
414 }
414 }
415 else if(m_orientation==Qt::Vertical) {
415 else if(m_orientation==Qt::Vertical) {
416 m_min = domain->minY();
416 m_min = domain->minY();
417 m_max = domain->maxY();
417 m_max = domain->maxY();
418 }
418 }
419
419
420 bool changed = false;
420 bool changed = false;
421
421
422 int min = m_min + 0.5;
422 int min = m_min + 0.5;
423 if(min>=0 && min<m_categories.count()) {
423 if(min>=0 && min<m_categories.count()) {
424 QString minCategory = m_categories.at(min);
424 QString minCategory = m_categories.at(min);
425 if(m_minCategory!=minCategory && !minCategory.isEmpty()) {
425 if(m_minCategory!=minCategory && !minCategory.isEmpty()) {
426 m_minCategory=minCategory;
426 m_minCategory=minCategory;
427 changed=true;
427 changed=true;
428 emit q->minChanged(minCategory);
428 emit q->minChanged(minCategory);
429 }
429 }
430 }
430 }
431 int max = m_max - 0.5;
431 int max = m_max - 0.5;
432 if(max>=0 && max<m_categories.count()) {
432 if(max>=0 && max<m_categories.count()) {
433 QString maxCategory = m_categories.at(max);
433 QString maxCategory = m_categories.at(max);
434 if(m_maxCategory!=maxCategory && !maxCategory.isEmpty()) {
434 if(m_maxCategory!=maxCategory && !maxCategory.isEmpty()) {
435 m_maxCategory=maxCategory;
435 m_maxCategory=maxCategory;
436 emit q->maxChanged(maxCategory);
436 emit q->maxChanged(maxCategory);
437 }
437 }
438 }
438 }
439
439
440 if (changed) {
440 if (changed) {
441 emit q->rangeChanged(m_minCategory,m_maxCategory);
441 emit q->rangeChanged(m_minCategory,m_maxCategory);
442 }
442 }
443 }
443 }
444
444
445 ChartAxis* QBarCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
445 ChartAxis* QBarCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
446 {
446 {
447 Q_Q(QBarCategoryAxis);
447 Q_Q(QBarCategoryAxis);
448 if(m_orientation == Qt::Vertical){
448 if(m_orientation == Qt::Vertical){
449 return new ChartCategoriesAxisY(q,presenter);
449 return new ChartBarCategoryAxisY(q,presenter);
450 }else{
450 }else{
451 return new ChartCategoriesAxisX(q,presenter);
451 return new ChartBarCategoryAxisX(q,presenter);
452 }
452 }
453 }
453 }
454
454
455 void QBarCategoryAxisPrivate::intializeDomain(Domain* domain)
455 void QBarCategoryAxisPrivate::intializeDomain(Domain* domain)
456 {
456 {
457
457
458 Q_Q(QBarCategoryAxis);
458 Q_Q(QBarCategoryAxis);
459 if(m_max==m_min) {
459 if(m_max==m_min) {
460 int min;
460 int min;
461 int max;
461 int max;
462 if(m_orientation==Qt::Vertical) {
462 if(m_orientation==Qt::Vertical) {
463 min = domain->minY() + 0.5;
463 min = domain->minY() + 0.5;
464 max = domain->maxY() - 0.5;
464 max = domain->maxY() - 0.5;
465 }
465 }
466 else {
466 else {
467 min = domain->minX() + 0.5;
467 min = domain->minX() + 0.5;
468 max = domain->maxX() - 0.5;
468 max = domain->maxX() - 0.5;
469 }
469 }
470
470
471 if(min>0 && min<m_categories.count() && max>0 && max<m_categories.count())
471 if(min>0 && min<m_categories.count() && max>0 && max<m_categories.count())
472 q->setRange(m_categories.at(min),m_categories.at(max));
472 q->setRange(m_categories.at(min),m_categories.at(max));
473 }
473 }
474 else {
474 else {
475 if(m_orientation==Qt::Vertical) {
475 if(m_orientation==Qt::Vertical) {
476 domain->setRangeY(m_min, m_max);
476 domain->setRangeY(m_min, m_max);
477 }
477 }
478 else {
478 else {
479 domain->setRangeX(m_min, m_max);
479 domain->setRangeX(m_min, m_max);
480 }
480 }
481 }
481 }
482 }
482 }
483
483
484 #include "moc_qbarcategoryaxis.cpp"
484 #include "moc_qbarcategoryaxis.cpp"
485 #include "moc_qbarcategoryaxis_p.cpp"
485 #include "moc_qbarcategoryaxis_p.cpp"
486
486
487 QTCOMMERCIALCHART_END_NAMESPACE
487 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,79 +1,79
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 #ifndef QBARCATEGORIESAXIS_H
21 #ifndef QBARCATEGORIESAXIS_H
22 #define QBARCATEGORIESAXIS_H
22 #define QBARCATEGORIESAXIS_H
23
23
24 #include "qabstractaxis.h"
24 #include "qabstractaxis.h"
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 class QBarCategoryAxisPrivate;
28 class QBarCategoryAxisPrivate;
29
29
30 class QTCOMMERCIALCHART_EXPORT QBarCategoryAxis : public QAbstractAxis
30 class QTCOMMERCIALCHART_EXPORT QBarCategoryAxis : public QAbstractAxis
31 {
31 {
32 Q_OBJECT
32 Q_OBJECT
33 Q_PROPERTY(QStringList categories READ categories WRITE setCategories NOTIFY categoriesChanged)
33 Q_PROPERTY(QStringList categories READ categories WRITE setCategories NOTIFY categoriesChanged)
34 Q_PROPERTY(QString min READ min WRITE setMin NOTIFY minChanged)
34 Q_PROPERTY(QString min READ min WRITE setMin NOTIFY minChanged)
35 Q_PROPERTY(QString max READ max WRITE setMax NOTIFY maxChanged)
35 Q_PROPERTY(QString max READ max WRITE setMax NOTIFY maxChanged)
36
36
37 public:
37 public:
38 explicit QBarCategoryAxis(QObject *parent = 0);
38 explicit QBarCategoryAxis(QObject *parent = 0);
39 ~QBarCategoryAxis();
39 ~QBarCategoryAxis();
40
40
41 protected:
41 protected:
42 QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent = 0);
42 QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent = 0);
43
43
44 public:
44 public:
45 AxisType type() const;
45 AxisType type() const;
46 void append(const QStringList &categories);
46 void append(const QStringList &categories);
47 void append(const QString &category);
47 void append(const QString &category);
48 void remove(const QString &category);
48 void remove(const QString &category);
49 void insert(int index, const QString &category);
49 void insert(int index, const QString &category);
50 void replace(const QString &oldCategory, const QString &newCategory);
50 void replace(const QString &oldCategory, const QString &newCategory);
51 void clear();
51 void clear();
52 void setCategories(const QStringList &categories);
52 void setCategories(const QStringList &categories);
53 QStringList categories();
53 QStringList categories();
54 int count() const;
54 int count() const;
55 QString at(int index) const;
55 QString at(int index) const;
56
56
57 //range handling
57 //range handling
58 void setMin(const QString& minCategory);
58 void setMin(const QString& minCategory);
59 QString min() const;
59 QString min() const;
60 void setMax(const QString& maxCategory);
60 void setMax(const QString& maxCategory);
61 QString max() const;
61 QString max() const;
62 void setRange(const QString& minCategory, const QString& maxCategory);
62 void setRange(const QString& minCategory, const QString& maxCategory);
63
63
64 Q_SIGNALS:
64 Q_SIGNALS:
65 void categoriesChanged();
65 void categoriesChanged();
66 void minChanged(const QString &min);
66 void minChanged(const QString &min);
67 void maxChanged(const QString &max);
67 void maxChanged(const QString &max);
68 void rangeChanged(const QString &min, const QString &max);
68 void rangeChanged(const QString &min, const QString &max);
69
69
70 private:
70 private:
71 Q_DECLARE_PRIVATE(QBarCategoryAxis)
71 Q_DECLARE_PRIVATE(QBarCategoryAxis)
72 Q_DISABLE_COPY(QBarCategoryAxis)
72 Q_DISABLE_COPY(QBarCategoryAxis)
73 friend class ChartCategoriesAxisX;
73 friend class ChartBarCategoryAxisX;
74 friend class ChartCategoriesAxisY;
74 friend class ChartBarCategoryAxisY;
75 };
75 };
76
76
77 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
78
78
79 #endif // QCATEGORIESAXIS_H
79 #endif // QCATEGORIESAXIS_H
General Comments 0
You need to be logged in to leave comments. Login now